1. ## Avg. Number Proggie

I'm kinda stuck on what to do, basically the user can input as many inputs as he/she wants to (within reasonable amount) and the program is supposed to calculate the avg, simple enuff. But I can't get it working.. heres wat I tried so far..

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
float num[30];
int n = 0;
float total = 0, avg = 0;

printf("Input numbers separated by a space: ");

// Input numbers
for (n = 0; (num[n] != '\n'); n++)
{
scanf("%d ",&num[n]);
}

// Total numbers
for (n = 0; (num[n] != '\n'); n++)
{
total += num[n];
}

avg = total / ((float)n);

printf("The average number was: %f",total);

return 0;
}```
The problem lies in me receiving input from the scanf() function into an array.. any suggestions?

2. >basically the user can input as many inputs as he/she wants to (within reasonable amount)
ie. 30.

>for (n = 0; (num[n] != '\n'); n++)
Just out of curiosity, how were you planning on representing '\n' as a floating-point value in an array initialized by a function that ignores whitespace? Perhaps using 0 as a stopping condition would be better suited because adding 0 to the sum does nothing and thus has no purpose in the input:
Code:
```for (n = 0; n < 30; n++)
{
scanf("%f",&num[n]);
if ( num[n] == 0 )
break;
}```
Your call to scanf is also wrong in two ways. First, you tell it to read an integer but give it a pointer to float. This will rarely work as you want. Second, the trailing space in the format string causes scanf to search for and discard any amount of whitespace after the call. Your program will probably hang for no obvious reason because of this unless you add extra junk input for scanf to stop on.

3. Oo okies, I fixed up some of the code. I kinda mis-typed the scanf() to read an integer instead of a float..

Code:
```int main()
{
float num[30];
int n = 0;
int m = 0;
float total = 0, avg = 0;

printf("Input numbers separated by a space: ");

// Input numbers
for (n = 0; n < 30; n++)
{
scanf("%f",&num[n]);
if (num[n] == 0)
break;
}
m = n;

// Total numbers
for (n = 0; n < m; n++)
{
total += num[n];
}

avg = total / ((float)(n));

printf("The average number was: %f\n",&total);

return 0;
}```
Ok, it terminates well when I input 0, the problem is that I can't see why the output is alwayz equiv to 0.. Since I incremented n when I tally'ed the numbers, shouldn't it work? Unless my eyes are missing somethiung again

4. Dun owrri I fink I got it.. need more sleep I ain't thinking straight

5. Originally Posted by 0rion
Dun owrri I fink I got it.. need more sleep I ain't thinking straight
What are you printing?

Code:
`printf("The average number was: %f\n",&total);`
Not right, for a couple of reasons.

Dave

6. I hope this code is correct. I'm currently at work and on the phone with clients but at least the logic should be correct. Someone correct me if i'm wrong.
Code:
```/* avg program */

#define MAX 30
#include <stdio.h>
#include <stdlib.h>

int main(void){

int count;
float num, avg;

for(count=0; count<=MAX; count++){
printf("Enter in num %d\n", count);
while( (scanf("%f", &num)) ==1 && num>0){
num+=num;
}Printf("Number must be 1 or greater\n");
continue;

}
float(avg) = num/MAX;
printf("The average is %f", avg);
system("pause");
return 0;
}```
I'll check it when i get home. should work though.

7. Originally Posted by caroundw5h
I hope this code is correct. I'm currently at work and on the phone with clients but at least the logic should be correct. Someone correct me if i'm wrong.
Code:
```/* avg program */

#define MAX 30
#include <stdio.h>
#include <stdlib.h>

int main(void){

int count;
float num, avg;

for(count=0; count<=MAX; count++){
printf("Enter in num %d\n", count);
while( (scanf("%f", &num)) ==1 && num>0){
num+=num;
}Printf("Number must be 1 or greater\n");
continue;

}
float(avg) = num/MAX;
printf("The average is %f", avg);
system("pause");
return 0;
}```
I'll check it when i get home. should work though.

What variable are you 'scanf'ing into?
What variable is holding your total?
Is there a conflict?

8. Originally Posted by WaltP
What variable are you 'scanf'ing into?
What variable is holding your total?
Is there a conflict?
expalain that a bit more to me on where the problem lay walt. I coded it on the fly so i was expecting it to work. Here is a corrected version,Define MAX to be whatever u want
Code:
```#include <stdio.h>
#include <stdlib.h>
#define MAX 4

int main(void){

int count;
float num, avg;

for(count=1; count<=MAX; count++){
printf("Enter in num %d\n", count);
scanf("%f", &num);
num+=num;
avg = num/MAX;
}
printf("The average is %.2f\n", avg);
system("pause");
return 0;
}```

9. Essentially, when you loop and run the scanf to num, you clear out anything that was originally stored in the num variable... that includes the sum of previous data(not that it actually sums it up...)

so what will happen is that let's say the last value entered by the user is 300.0, it hits the num +=num line. expanding and substituting the values in get me num = 300.0 + 300.0;
meaning num == 600.0...
hit the divide part, assuming MAX == 30, avg = 600.0/30 == 20.0... Hardly the average of all values entered.

There's also the fact that it was supposed to be a user defined amount of data entered, not a fixed amount...

10. Originally Posted by caroundw5h
expalain that a bit more to me on where the problem lay walt. I coded it on the fly so i was expecting it to work.
You are expecting "on-the-fly" code to work? If you spend time on it do you expect it to not work? The problem with "on-the-fly" code is usually it does not work

Originally Posted by caroundw5h
Code:
```for(count=1; count<=MAX; count++){
printf("Enter in num %d\n", count);
scanf("%f", &num);
num+=num;
avg = num/MAX;
}```
Walk thru this code with pencil & paper, keeping track of all the variables as they change. You will see exactly where the problem is.

11. Originally Posted by WaltP
You are expecting "on-the-fly" code to work? If you spend time on it do you expect it to not work? The problem with "on-the-fly" code is usually it does not work
More likely not to work. On the fly, mean i'm doing things mecanically, not really thinking about it.

Walk thru this code with pencil & paper, keeping track of all the variables as they change. You will see exactly where the problem is.
Yeah, I give up already. The code seems to work fine for me when i run?
Tell me what the problem is.

Also that user defined amount is easily taken care of.
I think the person understand that.
Code:
`for(count = 1;count<=USER_NUM;count++)`

12. They mean it doesn't work:
Code:
```for( x = 1; x <=4; x++ )
{
/* first time through loop */
scanf into number /* say they input 5 */
number = number + number /* number now is 10 */

/* second time through loop */
scanf into number /* say the input is 6 */
number = number + number /* number is now 12 */

/* third time through loop */
scanf into number /* say the input is 7 */
number = number + number /* number is now 14 */
}```
See the problem yet? Had you actually walked through the code like they suggested, you'd see the problem.

Quzah.

13. Originally Posted by tzuchan
Essentially, when you loop and run the scanf to num, you clear out anything that was originally stored in the num variable... that includes the sum of previous data(not that it actually sums it up...)

so what will happen is that let's say the last value entered by the user is 300.0, it hits the num +=num line. expanding and substituting the values in get me num = 300.0 + 300.0;
meaning num == 600.0...
hit the divide part, assuming MAX == 30, avg = 600.0/30 == 20.0... Hardly the average of all values entered.

There's also the fact that it was supposed to be a user defined amount of data entered, not a fixed amount...
Thank you for breaking it down for me tzuchan - without the god complex like some members on this board. [cough]quzah[/cough] I'm still getting used to the intricacies of C so I appreaciate your knowledge you and walt share without the rancor.

Maybe quzah you should concentrate on augmenting the amount of ppl you can help out and not your posts count. You obvioulsy need some time out from the message boards and since you are so impatient with ppl who don't spend ALL their time around the keyboard && your intelligence is so high. write some code to augment your posts. I have full faith in you
Code:
`action = newbie ? sarcasm() : augment_posts();`

14. Originally Posted by caroundw5h
Thank you for breaking it down for me tzuchan - without the god complex like some members on this board. [cough]quzah[/cough]
Ok, I'll bite. Let's look at the order of the posts:

Tzuchan posts. You post the following reply:
Originally Posted by caroundw5h
Yeah, I give up already. The code seems to work fine for me when i run? Tell me what the problem is.
Originally Posted by Quzah
Code:
```for( x = 1; x <=4; x++ )
{
/* first time through loop */
scanf into number /* say they input 5 */
number = number + number /* number now is 10 */

/* second time through loop */
scanf into number /* say the input is 6 */
number = number + number /* number is now 12 */

/* third time through loop */
scanf into number /* say the input is 7 */
number = number + number /* number is now 14 */
}```
See the problem yet? Had you actually walked through the code like they suggested, you'd see the problem.
`action = newbie ? sarcasm() : augment_posts();`