Thread: 2-dimensional array

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    2-dimensional array

    The C program should read a 5 x 5 array of integers, it's 2-dimensional. What the program should do? There are 5 quiz grades for each of the 5 students (so there are 5 quizes). After the user has entered the 5 quiz grades for each student, the program should compute the total score and the average score for each student. The program should also compute the average score, highest score and lowest score for each quiz. It's should be done using arrays...The average score should be a float ("%2f")

    This is how I started:

    Code:
    #include <stdio.h>
    #define NR_STUDENTS 5
    #define NR_QUIZES 5
    
    main()
    {
        int score[NR_STUDENTS] [NR_QUIZES];
        float average_score;
    Please note that I'm new to C. I know that I can assign a value or name to each value in the array. A score can be 1 to 10. This is the only way to do the maths I think. What is annoying about this exercise is that there are so many values, so it's hard to look what you exactly need...especially for someone who's new to C and programming...Please give me some tips and guidance, push me in the right direction.

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Use for loops like this
    Code:
    int i,j;
    
    for(i=0; i<NR_STUDENTS; i++)
       for(j=0; j<NR_QUIZES; j++)
       {
          scanf("%d",&score[i][j]);
       }

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    Thanks, I already know that I need For loops combined with arrays, because they go hand in hand. However, I'm really a "newbie" and unexperienced in C and programming in whole, does the user really need to enter 25 grades (values)? What format is the best? Five times printf with 5 values?

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    You said that the user has to enter 5 * 5 values:
    There are 5 quiz grades for each of the 5 students (so there are 5 quizes). After the user has entered the 5 quiz grades for each student,...
    5 * 5 = 25.
    So why are you asking me if you have to enter 25 values?

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    Originally posted by DrZoidberg
    You said that the user has to enter 5 * 5 values:


    5 * 5 = 25.
    So why are you asking me if you have to enter 25 values?
    I'm terribly sorry, dumb question.

    I've searched Google and I know what arrays are and how For loops work, but because everything is going so fast I don't know how to implement and combine all these things in a working program. I've made calendars, reversing three-digit numbers, calculating tax....however, this is the most difficult and annoying exercise I have come across, because there are so many values. Perhaps I'm making this exercise more difficult than it is.

    I made 5 printf, that askes the user to enter the grades of student 1, 2...5. Each printf I followed up with a scanf, however, I do not know if this is the way to go. Having this in my mind, I need to do maths with these 25 grades, such as taking the sum, taking the lowest score, highest scare, (taking the average seems simple)....with this in my mind, I don't have a clear strategy on how to proceed and I'm "freaking out".

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    No reason to worry. Just read your C book, try to understand the basics (for/while loops, array, functions) , look at the examples, etc. and I'm sure you'll be able to solve this problem ..

    Basicly, with this exercise you'll need a series of for loops stepping through the array, and computing the average, highest and lowest scores.

    Good luck.

    BTW, is Idolater your handle on arstechnica forums?
    $ENV: FreeBSD, gcc, emacs

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    Originally posted by cc0d3r
    No reason to worry. Just read your C book, try to understand the basics (for/while loops, array, functions) , look at the examples, etc. and I'm sure you'll be able to solve this problem ..

    Basicly, with this exercise you'll need a series of for loops stepping through the array, and computing the average, highest and lowest scores.

    Good luck.

    BTW, is Idolater your handle on arstechnica forums?
    Yes it is, I found this site while I was doing Google on arrays. I have read the tutorial of this site on arrays and other C tutorials, which I already know. Taking that knowlegde into practise is the most difficult part. Especially trying to translate "real world" problems in C and finding a solution to each problem. I have read the chapters over and over in my book, there is not an example code that resembles this exercise. Are you familiar with C Programming, a Modern Approach by K.N. King? That is the book I have and this exercise is from, chapter 8 page 153.

    I really don't want to give up on C and I really want to learn it.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Originally posted by sjalesho
    Are you familiar with C Programming, a Modern Approach by K.N. King? That is the book I have and this exercise is from, chapter 8 page 153.
    Yes, that's the book I initially started learning C from as well as K&R2. This chapter has enough example code on initializing and using mult. arrays. Once you've collected the data, it's trivial to compute the results.

    Post the code you've got so far.
    $ENV: FreeBSD, gcc, emacs

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    Originally posted by cc0d3r
    Yes, that's the book I initially started learning C from as well as K&R2. This chapter has enough example code on initializing and using mult. arrays. Once you've collected the data, it's trivial to compute the results.

    Post the code you've got so far.
    I don't have much now yet, because I do not know how to "simply spit the grades out".

    Code:
    #include <stdio.h>
    #define NR_STUDENTS 5
    #define NR_QUIZES 5
    
    main()
    {
        int score[NR_STUDENTS] [NR_QUIZES], col, row, lowest_score, highest_score, total_score;
        float average_score;
        
        printf("Please enter the scores of student 1 for test 1 to 5: ");
        printf("Please enter the scores of student 2 for test 1 to 5: ");
        printf("Please enter the scores of student 3 for test 1 to 5: ");
        printf("Please enter the scores of student 4 for test 1 to 5: ");
        printf("Please enter the scores of student 5 for test 1 to 5: ");
        
        for(row=0; row<NR_STUDENTS; row++)
        for(col=0; col<NR_QUIZES; col++)
        {
            scanf("%d",&score[col][row]);
        }
        
        total_score = 
        highest_score = 
        lowest_score =  
        average_score = total_score / 5
        
        printf("The total score of students 1 to 5 is: ");
        printf("The average score of students 1 to 5 is: ");
        printf("The highest score of quiz 1 to 5 is: ");
        printf("The lowest score of quiz 1 to 5 is: ");
        printf("The average score of quiz 1 to 5 is: ");
        printf("\n");
        
        return 0;
    }
    In chapter 8 there is not much code that would push me in the right direction for exercise 12 on page 153.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's some help for you
    Code:
    #include <stdio.h>
    
    #define NR_STUDENTS 5
    #define NR_QUIZES 5 
    
    int main(void)
    {
      int i, j;
      int score[NR_STUDENTS][NR_QUIZES];
    
      for (i = 0; i < NR_STUDENTS; i++)
      {
        for (j = 0; j < NR_QUIZES; j++)
        {
          /* 
             Remember, arrays are accessed with index 0 to N-1,
             so in this example we +1 to the index to give the
             user the impression we're dealing with 1 to N
           */
          printf ("Enter the score for student %d for test %d of %d: ", i+1, j+1, NR_QUIZES);
          fflush(stdout); /* Flushes the output stream to ensure you see the above msg on the screen*/
          scanf("%d",&score[i][j]); /* Get over using scanf() asap, its not good */
        }
      }
      
      /* Now dump the array to prove it worked */
      for (i = 0; i < NR_STUDENTS; i++)
        for (j = 0; j < NR_QUIZES; j++)
          printf ("%d %d %d\n", i, j, score[i][j]);
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    Thanks a lot! Reading your code I see that my way of trying to make it a code was totally wrong. I'm really learning from your example. I started to learn C some 4 weeks ago, before I never heard of it and didn't know anything about it. Now I'm starting to learn it and I know already many definitions, but implementing it and putting it all together is the difficult part.

    fflush(stdout); is new to me and thanks for explaining it to me. I will do research on when I can use it. Basically the code is 2 nested For loops. The statements are of course printf. Someone mentioned it before to me that I can use Loops to traverse an array. Seeing it actually work with your great comments is really helpful to me.

    The book I read C Programming a Modern Approach by KN King should have add comments in the code like you have, that would have been much clearer. In some code examples King just posts some code without fully explaining the difficult parts, but only the obvious parts. Does someone know a better book that is better able to explain C to someone who is not a genius in maths? I'd prefer words, lots of words! The more explaination, the better!

    Now I'm analysing your code and trying to understand it and trying to finish it off. Thanks again.
    Last edited by sjalesho; 10-05-2003 at 05:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM