Need some help with coding

This is a discussion on Need some help with coding within the C Programming forums, part of the General Programming Boards category; This is the assignment im doing. Write a program that reads integer data from the standard input unit and prints ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    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. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    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. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    Thumbs up 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. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    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. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    int result = scanf("&#37;d", &myint );

    result has the 3 values 0, 1 or EOF

    You only look at myint if result == 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    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. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Unfortunately part of the assignment is using eof to stop the input process.

  11. #11
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    If you do eof then scanf fails with an eof status. What's the problem?

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Stupid assignment. Well then just use Noir's idea:
    Code:
    while(scanf("&#37;d", &x) != EOF) {
    
    }
    instead of
    Code:
    while(scanf("%d", &x) == 1) {}
    which is better code, but if you must use EOF . . . . [edit] Noir beat me to it. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    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.
    Last edited by Alphawaves; 03-28-2007 at 02:02 PM.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Yeah i just edited my post cuz i realied what you where saying about do while opposed to a while loop.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 01:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21