Like Tree5Likes

Using arrays in C

This is a discussion on Using arrays in C within the C Programming forums, part of the General Programming Boards category; Write a program that prompts for and reads in test scores. You may assume that valid test scores will be ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    8

    Post Using arrays in C

    Write a program that prompts for and reads in test scores. You may assume that valid test scores will be integer values between 0 and 100. You may also assume that the user will not enter more than 35 test scores. Use a preprocessor directive to define this value. User input will be complete when the user enters a -1 for the test score. When all of the test scores have been entered, the program will print out the scores. Use a while or do-while loop to read in the values. Use a for loop to print out the values. Sample output:

    Enter test score 1: 88 Enter test score 2: 67 Enter test score 3: 74 Enter test score 4: 94 Enter test score 5: 79 Enter test score 6: 56 Enter test score 7: -1 Number of scores entered: 6 Test scores entered : 88 67 74 94 79 56

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Announcements - C Programming
    You need to make some kind of effort so we know exactly what is causing you problems.

    You can't just dump your assignment and wait for completed code.
    Crossfire likes this.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Krendo01!

    Now get to work.
    Crossfire likes this.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Do your own homework!
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Quote Originally Posted by krendo01 View Post
    Write a program that prompts for

    The user knows what they're doing; that's why they're running the program. Do they really need to be told "ENTER TEST SCORE NUMBER #" every single time they enter their next value? Also, if you're reading from a file, it will contaminate your output with spam.

    You may also assume that the user will not enter more than 35 test scores.
    Why hard-code that limit?

    User input will be complete when the user enters a -1 for the test score.
    scanf's return value indicates when there is no more input. Why don't you use that instead of making up your own silly rules for what constitutes the end of input?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,288
    On the journey of cross-posting, it seems the OP has been given somewhat of a solution (albeit in the wrong language) here. However, they didn't get a very good answer here.

    The OP should issue a formal apology for wasting everyone's time.
    Crossfire likes this.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    8

    Post Arrays and Loops in C language

    I'm supposed to write a program that prompts for and reads in test scores with integer values between 0 and 100 and enter no more than 35 test scores. Use a preprocessor directive to define this value. User input will be complete when the user enters a -1 for the test score. When all of the test scores have been entered, the program will print out the scores. Use a while or do-while loop to read in the values. Use a for loop to print out the values. It compiles but when I run it the following message appears "segmentation fault." Is there something wrong with my code? HELP?!
    Code:
    #include <stdio.h>
    #define SIZE 35
    int main (void)
    {
     int nums[SIZE];
     int count=0;
     int i;
     int tests;
     
     while (nums[i] >= 0)
     {
      printf("Enter test score %d: ", nums[i]);
      scanf("%d", &nums[i]);
     }
     
     for (i=0; i<SIZE; i++)
     {
      printf("Number of test scores entered: %d", tests);
      printf("Tests scores entered: %d", count);
     }
     return 0;
    }
    The output is supposed to look like this:

    Code:
    Enter test score 1: 88                       Enter test score 2: 67                         Enter test score 3: 74                         Enter test score 4: 94                         Enter test score 5: -1                         Number of scores entered: 4                                   Test scores entered : 88 67 74 94 

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,288
    In the future, don't start a new post when you have another one on the same question.

    In regard to your code - what value does 'i' contain when you start your "while()" loop?

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    @Barney, that entire post is so bad!!!

    "The user knows what they're doing; that's why they're running the program."

    with all the programmers on here lets ask the question, Does the USER EVER know what there doing???

    My opinion, if they did, we would NEVER need input validation!
    Not to mention never need to man a help desk!
    TheBigH and stahta01 like this.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    i see a prob with
    Code:
    for (i=0; i<SIZE; i++)
    if program is hard coded for 35, but the user only enters 5 scores, the printf is going to still print the 6-35, causing a jumbled mess of randomness!!!

    needs to make size change to the number entered before the for loop!!!

  11. #11
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Even if they knew what they were doing, input validation would still be necessary since it's common for humans to make mistakes when entering data. If they need to be constantly reminded of what they're doing for some reason, I find it's better to wrap a fancy interface with pretty buttons around a program that doesn't write useless information to output streams. The design suggested by the criteria supplied by OP makes a huge mess when stdin is connected to a file or a pipe, and it restricts you to reading exactly one number per line which is rarely done correctly by people who are taught to scanf everything.

    edit: It really just requires more work for the programmer and it makes the program more specific to a certain way of use. Consider the following program that reads non-negative numbers from stdin and prints the sum of them:

    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    static int eaterror(void)
    {
        int c;
    
        fputs("invalid token: \"", stderr);
        while ((c = getchar()) != EOF && !isspace(c))
            putc(c, stderr);
        fputs("\"\n", stderr);
        return c;
    }
    
    int main(void)
    {
        unsigned int u, sum = 0;
        int r;
    
        while ((r = scanf("%u", &u)) != EOF)
            if (r)
                sum += u;
            else if (eaterror() == EOF)
                break;
        printf("%u\n", sum);
        return 0;
    }
    If you were to add line-based prompts that keep track of the number you're up to, you'd have to write more code to get it working correctly, and you'd basically make it horrible to use with different methods of input, where this one works well for files and pipes as well as direct input from a terminal.
    Last edited by Barney McGrew; 02-11-2013 at 09:43 PM.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Quote Originally Posted by Matticus View Post
    In the future, don't start a new post when you have another one on the same question.

    In regard to your code - what value does 'i' contain when you start your "while()" loop?
    Threads merged.
    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.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Quote Originally Posted by Barney McGrew View Post
    The user knows what they're doing; that's why they're running the program. Do they really need to be told "ENTER TEST SCORE NUMBER #" every single time they enter their next value? Also, if you're reading from a file, it will contaminate your output with spam.


    Why hard-code that limit?


    scanf's return value indicates when there is no more input. Why don't you use that instead of making up your own silly rules for what constitutes the end of input?
    Funnily enough these questions have real answers.

    1) Not all console programs are batch programs.

    2) You shouldn't, but school assignments have nerfed requirements.

    3) Well, they're both sentinel controlled loops in the end, so I guess it doesn't matter.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem View Post
    Announcements - C Programming
    You need to make some kind of effort so we know exactly what is causing you problems.

    You can't just dump your assignment and wait for completed code.
    You can't???

    Well, I am SHOCKED, *SHOCKED* I say!! <ROFL!>

  15. #15
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    I'm sorry, size change to what?

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

Similar Threads

  1. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM

Tags for this Thread


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