# Thread: Need some help with coding

1. ## Need some help with coding

This is the assignment im doing.

Write a program that reads integer data from the standard input unit and prints a list of the numbers followed by the minimum integer read, maximum integer read, and the average of that list. Test your program with the data shown below.
{24 7 31 -5 64 0 57 -23 7 63 31 15 7 -3 2 4 6}

This is c programming btw.
Now i think i know basically what to do, the program needs an input device and then the program loops all the data input until theres nothing left to input. You would use an address like int x; and as you cycle through the numbers if the number your looking at is greater (or less than depending on which function you doing) it gets put in the int x slot, then you print out int x at the end.

My big problem is i dont know an input device that lets you put in any amount of integers. I could use scanf(); and scan a predetermined amount of integers but im not sure if theres some way to tweak scanf or if theres some command i missed when i didnt attend the lecture. Also, im not sure how i would know how to make my program quit looping after its evaluated all the numbers, like i said before if i knew that there where 5 integers being evaluated i could set a counter to decrease by one so that when the counter hits 0 the loop ends. Is there some way i can do this where the counter changes based on the number of integers input?

I hate programming so coming here makes it as painless as possible for me.

Looking through the book this is what ive pieced together so far, im pretty sure it doesnt work, its not close to done anyways.

Code:
```#include <stdio.h>
#include <stdlib.h>
int main()
{
int avg;
int max;
int min;
int x;
int newent;
printf("\nEnter your integers: <EOF> to stop\n");
newent = scanf("&#37;d", &x);
if (newent > max)
max = newent;
if (newent < min)
min = newent;
printf("Your average is: %d \n Your maximum is: %d \n Your minumum is: %d \n", avg, max, min);
system("PAUSE");
return 0;
}```
Ill be looking here from time to time most of the night, thanks in advance for any help.

2. 1. max and min are not initialized
2. scanf puts value entered by the user in the x and the number of values scanfed in the newent , in your case it can be 1 if success, 0 if input was wrong or EOF if stream is empty
3. If you want to get several items from the user you need some loop

3. 1 Why do they have to be initialized?
2) Even if i enter multiple integers it will still give me a 1 if the scanf works?
3) Yeah im not sure how that would work, im assuming since eof is used to end it that you would enter and integer then press return and it would inqure for another until u used eof.

So i could so something like

scanf("%d", x);
if (x != ^d)
scanf("%d", x);
printf("Your average is blah blah blah");

I think i could use a better loop command so ill look into that. Im really not good remembering commands and whatnot tho. Im guessing ill have to have a nested loop inside the scan loop that evaluates whether then next stored number is greater or less than the last one and is so to store it in the min and max addresses. How would i do average tho? I can use avg += x; but i still need to divide it by the number of integers, could i put a counter in the loop so that the counter increases by one each time a new number is input? Then use that counter as the divisor?

4. 1 Why do they have to be initialized?
Code:
```max = garbage
if(newent > garbage)
newent = garbage;```
Garbage as input - garbage as output

Even if i enter multiple integers it will still give me a 1 if the scanf works?
No. If you scanf several values with one scanf call - you get number of items parsed.

5. ## Try this

Hi
U need to use loop to enter data

u can use something like below

Code:
```main()
{

char Contd ;

do{

/* Write code for calculating max, min and average*/

printf("Do you want to continue?\n Press Y to enter next data.....");
scanf("%c",&Contd);
}while(('y' == Contd)||('Y' == Contd));
}```
Try this

abhijit banerjee

6. > newent = scanf("&#37;d", &x);
This scanf can return 3 values
1 - you typed in an integer "123" or something which began with an integer "123abc". The "abc" is left for something else.
0 - you typed in something not an integer, say "abc". The "abc" is left for something else.
EOF - you pressed CTRL-D / CTRL-Z (depending on your platform) to signal end of file.

If you're trying to get a min, max, average, then you need some kind of loop to read multiple values.

7. Okay i think ive got it!

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

int main()
{
int x;
int min = 1000000000;
int max = -1000000000;
float sum;
float avg;
float counter = 0;
printf("\nEnter your integers: <EOF> to stop\n");
do
{
scanf("&#37;d", &x);
if ( x > max)
max = x;
if (x < min)
min = x;
if (x != EOF)
sum += x;
counter++;
}while (x != EOF);
avg = sum / counter;
printf("Your average is: %f \n Your maximum is: %d \n Your minumum is: %d \n", avg, max, min);
system("PAUSE");
return 0;
}```
I tried running it but theres problems with using eof, im not sure how to get it to work on my comp, im using xp if that helps : S

Could i possibly do something like "type "gibblets" to stop and set the while loop to ( x != gibblets) and make x a char type? Will a char type still take integers?

8. int result = scanf("&#37;d", &myint );

result has the 3 values 0, 1 or EOF

You only look at myint if result == 1

9. You can't get x to be EOF that way. But scanf returns a status, so you can use that instead:
Code:
```while ( 1 ) {
if ( scanf("%d", &x) != 1 ) {
if ( x > max)
max = x;
if (x < min)
min = x;
sum += x;
counter++;
} else {
break;
}
}```
Could i possibly do something like "type "gibblets" to stop and set the while loop to ( x != gibblets) and make x a char type?
Yeah, but it's harder because you have to use a string.

10. Unfortunately part of the assignment is using eof to stop the input process.

11. If you do eof then scanf fails with an eof status. What's the problem?

12. Stupid assignment. Well then just use Noir's idea:
Code:
```while(scanf("&#37;d", &x) != EOF) {

}```
Code:
`while(scanf("%d", &x) == 1) {}`
which is better code, but if you must use EOF . . . .  Noir beat me to it. [/edit]

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

int main()
{
int x;
int min = 1000000000;
int max = -1000000000;
float sum;
float avg;
float counter = 0;
printf("\nEnter your integers: <EOF> to stop\n");
do
{
scanf("&#37;d", &x);
if ( x > max)
max = x;
if (x < min)
min = x;
if (x != EOF)
sum += x;
counter++;
}while (scanf("%d", &x) != EOF); // basically what got changed
avg = sum / counter;
printf("Your average is: %f \n Your maximum is: %d \n Your minumum is: %d \n", avg, max, min);
system("PAUSE");
return 0;
}```
I tried running it and it accepts integers but it doesnt do anything after eof.

Is that what you guys where talking about? Im not a whizz so im not familiar with everything.

edit: I think i get what your saying, it needs to be a while loop instead of a for ... while loop because if i hit eof it runs through all those if loops before realizing that it needs to end the do ... while loop because the while condition is at the end.

14. x won't be EOF unless you enter EOF, probably -1. Check the return value of scanf() as suggested; if it's EOF you've reached the end of the file.
Code:
```      do
{
scanf("&#37;d", &x);
if ( x > max)
max = x;
if (x < min)
min = x;
if (x != EOF)
sum += x;
counter++;
}while (scanf("%d", &x) != EOF); // basically what got changed```
You're reading in two numbers. Two scanf statements. You don't want that. Compare the return value of your first statement again EOF. Or use the while() loop but change it into a while loop from a do-while loop.

15. Yeah i just edited my post cuz i realied what you where saying about do while opposed to a while loop.