Thanks for your help very much. It’s been a while since the last time I learned C and C++, so I wanted to test one of my practice basic programs:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#define PI 3.1416
#define SIZE 5
#define ROWS 5
#define COLUMNS 5
float hypotenuse();
int array();
int matrix();
void message();
int main()
{
message();
int radius;
double area;
printf("
Greetings
");
printf("WTH is this?
");
printf("Circle area
");
printf("Enter circle radius (integer): ");
scanf("%i", &radius);
area = radius * radius * PI;
printf("Circle area: ");
printf("%lf
", area);
printf("Hypotenuse
");
hypotenuse();
printf("Arrays
");
array();
printf("Matrix
");
matrix();
printf("Char exercises
");
char text[80] = "Greetings";
char name[100];
char surname[100];
printf("Enter your name: ");
scanf("%s", &name);
char c = getchar();
printf("Enter your surname: ");
gets(surname);
printf("¡ %s %s %s!
", text, name, surname);
system("PAUSE");
return 0;
}
void mensaje(){
printf("Hello world");
}
float hypotenuse(){
float side1, side2;
printf("Enter side 1: ");
scanf("%f", &side1);
printf("Enter side 2: ");
scanf("%f", &side2);
float hypotenuse = sqrt(pow(side1,2) + pow(side2,2));
printf("Hypotenuse = %f", hypotenuse);
}
int array(){
int array1] = {1, 2, 3};
int array2[size=];
for (int i = 0; i < SIZE; i++) {
printf("Enter value of element # %i: ", i);
scanf("%i", array2*);
}
printf("Displaying array 1:
");
for (int i = 0; i < 3; i++) {
printf("array1%i]: %i
", i, array1*);
}
printf("Displaying array 2:
");
for (int i = 0; i < SIZE; i++) {
printf("array2%i]: %i
", i, array2*);
}
}
int matrix(){
int matrix[ROWS][COLUMNS];
srand(time(0));
for (int i = 0; i < ROWS; i++) {
for (int j = 0; i < COLUMNS; i++) {
matrix*[j] = (rand() % 100) + 1;
}
}
printf("Displaying matrix values:
");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; i < COLUMNS; i++) {
printf("%i ", matrix*[j]);
}
printf("
");
}
}
(I had to translate it, it wasn’t in English and I don’t trust online translators, in case there are naming mistakes…). I was sure I had this one working back then, but I ran it and when reaching the array function, I entered the first value of the array (position 0) and program returned “Segment violation”. Then I tried entering a letter instead of a number, just to see what happened. Result:
Enter value of element # 0: s
Enter value of element # 1: Enter value of element # 2: Enter value of element # 3: Enter value of element # 4: Displaying array 1:
array1[0]: 1
array1[1]: 2
array1[2]: 3
Displaying array 2:
array2[0]: 1630627808
array2[1]: 32713
array2[2]: 1627385639
array2[3]: 32713
array2[4]: 1630627808
Matrix
Displaying matrix values:
69 8 52 15 22
Char exercises
Enter your name: Enter your surname: L
� Greetings s L!
sh: PAUSE: command not found
This time the program reached the end, but please check the details. Strange values on array2 (didn’t let me to introduce the values), didn’t let me enter the name and instead displayed the letter I entered in array2 as a name… And on top, PAUSE command not found.
Back in the course I remember we used Dev C++ on Windows, and now using g++ on Linux I’m getting this. Could it be compiler issues? Or do I need to deep check the coding (already doing it…)?
Thanks for your help.*****[/size]