Click to See Complete Forum and Search --> : Help with C program (Arrays, yippie!)


thephreak6
10-24-2002, 12:51 AM
Hey everyone,
I know this is going to end up being a really stupid mistake, but im taking a C class and Im stuck on my program, im trying to get this program to look like this:

Please enter the number of results per experiment: 3
Please enter the number of experiments: 2

Please enter result 1 for experiment 1: 1.0
Please enter result 2 for experiment 1: 2.0
Please enter result 3 for experiment 1: 3.0

Please enter result 1 for experiment 2: 4.0
Please enter result 2 for experiment 2: 5.0
Please enter result 3 for experiment 2: 6.0

Result 1 Result 2 Result 3 Exper. Average
--------------------------------- ------------
Exper. 1: 1.0000 2.0000 3.0000 2.0000


but my output at the bottom is coming out with numbers different than what they should be...(I.E. 1.85 for 1, 2.124 for 3).Any ideas what im doing wrong?




thanks :D
you all rock

(heres the code)

#include <stdio.h>



#define MAX_RESULTS 6
#define MAX_EXPERS 10


int
main(void)
{
int count = 0;
int results;
int experiments = 0;
int excount = 0;
int rescount = 0;


float fresults[MAX_RESULTS][MAX_EXPERS];




count = 0;


results = 1;

while(results != 0)
{

printf("Please enter the number of results per experiment (No More Than 6): ");
printf("\t\t\t\t\t\t\t\tOr '0' to quit ");
scanf("%d", &results);

if(results > 0)
{
printf("Please enter the number of experiments(No More than 10): ");
scanf("%d", &experiments);


if(results > MAX_RESULTS || experiments > MAX_EXPERS)
{
printf("Please check to make sure your values aren't greater than the defined limits!\n\n\n");
}

else
{

for (excount = 0; excount < experiments; excount++)
{

printf("\n");

for (rescount = 0; rescount < results; rescount++)
{

printf("Please enter result %d for experiment %d: ", (rescount + 1), (excount +1));

scanf("%lf", &fresults[rescount][excount]);



}

}
printf("\n\n");
}


for(rescount = 0; rescount < results; rescount++)
{
printf("Result %d\t", (rescount + 1));

}
printf("Exper. Average");
printf("\n");
for(rescount = 0; rescount < results; rescount++)

{
printf("--------- \t", (rescount + 1));

}
printf("---------");
printf("\n");


}
/* note to self: Add loop to output more than 1 row once you get this to work */
for(rescount = 0; rescount < results; rescount++)
{
printf("%f\t", fresults[][0]);

}
printf("\n");
}
return(0);
}

truls
10-24-2002, 02:17 AM
Shouldn't you be using "%f" instead of "%lf" in the input loop?
The array is defined as float, but you are telling scanf that you're getting doubles ( lf = double, f = float ). I haven't a compiler here, so I can't check this, but that's my best guess.

thephreak6
10-24-2002, 02:41 AM
thanks, I missed that, but now im getting ever stranger numbers, instead of 1 I get -107374176.00000000 and so on...

truls
10-24-2002, 09:48 AM
Well, the line:
scanf("%lf", &fresults[rescount][excount]);
Should be changed to
scanf("%f", &fresult[excount][rescount]);

Also, I don't see you calculating an average anywhere in your code. All you do is output the first field of each experiment as the average. Which explains the values in your first example (the average values are equal to the first input value for each experiment). I changed the above line, and I get the first input value as average. I'm on a windows machine now though, will check with gcc later.