Hi ive got an assignment that asks the user to input sets of data (x,y) and then stores them in an array and does various things with them, the program asks for the number of data sets to be entered (so that i know how big the array will be), the number of data sets must be a positive integer , so i wanted to put and 'if' statement that printed an error message if the user inputted zero or a negative number, it works fine for zero, -1 , -2 and -3 , but anything below -3 and the program shuts down, cant work out why its doing that, would appreciate any tips in figuring it out
Heres my code:
Thank youCode:#include<Stdio.h> int x,y,N; int main() { printf("How many pairs of sets of data are there?(note:must be a positive integer): "); scanf("%d",&N); if (N<=0) printf("Error Restart program"); float array [N][2]; for (x=0;x<N;x++) { printf("Enter the two values of the data set (leave a space inbetween) : "); /* does work if you put minus numbers in*/ scanf("%f%f",&array[x][0],&array[x][1]); printf("\n"); /*printf("(%f,%f)\n",array[x][0],array[x][1]); omit this line in final program*/ } printf(" x y"); printf("\n\n"); for (x=0;x<N;x++) { printf("%6.2f %6.2f\n",array [x][y],array [x][y+1]); printf("\n"); } printf("You entered the above data sets"); printf("\n");
Tegan



LinkBack URL
About LinkBacks



Ive got a lot to work on there, if i allocate lots of memory to the array and then run a loop asking whether or not the user wants to input a set of data would that be better than asked for the set amount of data values? Just thought that might make the code less clumsy by asking for a value and running the loop that number of times 