Displaying Scores in Table

This is a discussion on Displaying Scores in Table within the C Programming forums, part of the General Programming Boards category; Lesson after lesson.. It's me again guys... just finish the second lab exercise.... hmmm... And now our lesson is about ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    12

    Displaying Scores in Table

    Lesson after lesson.. It's me again guys... just finish the second lab exercise.... hmmm... And now our lesson is about multiple arrays... =) I had already this code here:

    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
    int score[5][3], total[5];
    int st, sc;
    clrscr();
    	for(st=0;st<=4;st++)
    	{
    	total[st]=0;
    		for(sc=0;sc<=2;sc++)
    		{
    		scanf("%i",&score[st][sc]);
    		total[st]=total[st]+score[st][sc];
    		printf("%i",total[st]);
    		}
    	}
    }
    That code will total 5 scores. My problem is how to add code so that the program will do something like this:

    Program:

    Enter Student Name: John

    Enter 3 Scores of the Student:
    10
    10
    10

    (screen clears)

    Enter Student Name: Ronald

    Enter 3 Scores of the Student:
    9
    10
    9

    (screen clears)

    Enter Student Name: Karlo

    Enter 3 Scores of the Student:
    9
    8
    8

    (screen clears)
    (Final Output)

    Students 1 2 3 Total
    John 10 10 10 30
    Ronald 9 10 9 28
    Karlo 9 8 8 25

    John Got the Highest Total Score of 30.

    (terminate)

    Hmmm... Hope this info helps... Thanks in advance... ^^

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by jappy512 View Post
    That code will total 5 scores. My problem is how to add code so that the program will do something like this:

    Program:

    Enter Student Name: John

    Enter 3 Scores of the Student:
    10
    10
    10

    (screen clears)

    Enter Student Name: Ronald

    Enter 3 Scores of the Student:
    9
    10
    9

    (screen clears)

    Enter Student Name: Karlo

    Enter 3 Scores of the Student:
    9
    8
    8

    (screen clears)
    (Final Output)

    Students 1 2 3 Total
    John 10 10 10 30
    Ronald 9 10 9 28
    Karlo 9 8 8 25

    John Got the Highest Total Score of 30.

    (terminate)

    Hmmm... Hope this info helps... Thanks in advance... ^^
    You can use array of structures to do this.
    1. Declare a structure having name and score as its members.
    2. In main, declare an array of that structure and input the required fields(name and 3scores) using loop.
    I dont know how to make the screen clear after every input of student. You can use clrscr() but that's a non-standard function which I used with TC.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ready for parallel arrays?

    As the name implies, you use two arrays, one for char's (for the name of the student), and one for int's (for their scores and total)

    The index is the trick here, because name[0] is John, and scores[0][0], [0,1],[0,2],[0,3], and scores[0,4] are john's marks and total. The 0 index, keeps both name and scores, linked together.

    Code:
      for(i = 0; i < numOfStudents; i++) {
        printf("\n%s : ", names[i]);
        for(j = 0; j < numOfTests+1; j++)
          printf("%d %d %d %d" , scores[i][j]);
      }
    That way it's a name, then all his scores and total, then newline and the next name, and all that students numbers.

    You could also do this with structs (in an array of structs), but you probably haven't had structs yet.

    Note that the name string, like all C strings MUST have an end of string char on it. Also, there is no second index (like maybe j), on the line of code where the names are printed. You only want the first index there. That's the whole char string on row i.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    12
    Hmm.. How to make a table using gotoxy function on that? =)

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you are printing up a table row by row, and never have to go back to an earlier row, or down to a later row, and return -

    then you don't need to use gotoxy(), at all. You have excellent formatting ability using just printf().

    Remember that %3d will print your numbers in a field 3 digits wide, and right adjusted. %5.2f will print a float in a field 5 digits wide, and include just 2 digits after the decimal point.

    If for some reason you *have* to use gotoxy(), then put it right before the printf() line of code. You'll need to add a set of braces around the printf() and gotoxy() lines, to make them work as they should, in the body of the for loop.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    12
    Thanks for the reply adak.. Here is the code i made using gotoxy() function.

    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
    int x, y, score[3][3], col=15, lin=4, sum[5];
    char name[5][20];
    clrscr();
    gotoxy(25,2);
    printf("Quiz Score");
    gotoxy(3,2);
    printf("Student Name");
    for(x=0;x<=4;x++)
    	{
    	gotoxy(6,lin);
    	gets(name[x]);
    	for(y=0;y<=2;y++)
    		{
    		gotoxy(col,lin);
    		scanf("%i",&score[x][y]);
    		col=col+15;
    		}
    	lin=lin+2;
    	col=15;
    	}
    getche();
    }
    I'm able to create an output that will enter name of the student and 3 scores in a table form. But I can't display it to the next name. I want to enter 5 students and each has 3 scores and at the end of each scores will be the total score.

    And lastly will display who got the highest score..

    To have a clear picture of the output:

    Student Name Quiz Scores Total Score

    John 50 50 50 150
    Ron 45 50 50 145
    Don 45 45 50 140
    and so on ....

    John got the highest total score of 150....


    Thanks in Advance.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm able to create an output that will enter name of the student and 3 scores in a table form. But I can't display it to the next name. I want to enter 5 students and each has 3 scores and at the end of each scores will be the total score.

    And lastly will display who got the highest score..

    To have a clear picture of the output:

    Student Name Quiz Scores Total Score

    John 50 50 50 150
    Ron 45 50 50 145
    Don 45 45 50 140
    and so on ....

    John got the highest total score of 150....

    Thanks in Advance.
    So inside your for loop where you enter scores, you need a sum variable:

    Code:
    for(i = 0, sum = 0; i <= 2; i++) {
      scanf("%d", &score);
      //print your score here  
      sum += score;   //C expression for sum = sum + score
    }
    //print your sum here before you loop back for another student's name
    I'm sideswiped by this: "But I can't display it to the next name." What does that mean?

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    12
    Thanks Adak... here is my code now... about the problem earlier, i cannot enter the second name.


    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
    int x, y, score[3][3], col=20, lin=4, sum=0;
    char name[5][20];
    clrscr();
    gotoxy(25,2);
    printf("Quiz Score");
    gotoxy(3,2);
    printf("Student Name");
    for(x=0;x<=4;x++)
    	{
    	gotoxy(6,lin);
    	gets(name[x]);
    	for(y=0;y<=2;y++)
    		{
    		gotoxy(col,lin);
    		scanf("%i",&score[x][y]);
    		col=col+10;
    		sum = sum + score[x][y];
    		}
    		gotoxy(col,lin);
    		printf("%i",sum);
    	lin=lin+2;
    	col=15;
    	}
    getche();
    }

    output will be:

    Student Name Quiz Score

    jan 10 10 10 30

    _ <<---- cursor will be here and it wont align with the 1st one....

    the next one will suppose to enter another name and another 3 scores... can you help me with my program... had no idea now....


    Thanks in advance

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why do you want to use gotoxy() ?

    1) You don't need it, at all for this, as far as I can tell. In fact, it makes your code a bit more difficult to write.

    2) gotoxy() is non-standard C nowadays. (Although Windows has a very similar function called SetConsoleCursorPosition() ).

    Tell me what's up with using gotoxy(), and don't worry about the code, for now. We need to get in synch with this, first.

    gets() <<===== Grrrrr!

    fgets() <<=====
    Last edited by Adak; 12-15-2009 at 11:01 AM.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    12
    I'm sorry for that... we are required to use gotoxy() function to be able to align the names with their corresponding score in like a table form.. as what our teacher said so..

    I'm not familiar with fgets yet but I will use it now....

    I'm stuck with that gotoxy().... but it is required in our program... Thanks for the time Adak...


  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jappy512 View Post
    I'm sorry for that... we are required to use gotoxy() function to be able to align the names with their corresponding score in like a table form.. as what our teacher said so..

    I'm not familiar with fgets yet but I will use it now....

    I'm stuck with that gotoxy().... but it is required in our program... Thanks for the time Adak...

    gotoxy() is fine, but it is NOT required to align columns correctly, in a table (usually). Only if you have to back up to a previous row or column, is gotoxy() needed. Naturally, we'll use gotoxy(), as the instructor wishes, regardless.

    You're code is close, but lots of details in this assignment.

    Back in a bit after I run your code.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I gave your code some tweaks.


    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
      int x, y, score, max_score, lin=4, col, sum;
      char name[2][20];
    
      clrscr();
    
      gotoxy(3,2);
      printf("Student Name         1     2     3     Total\n  ============================================\n");
    
      max_score = 0;
      col = 0;
    
      for(x=0;x<=4;x++)
      {
        gotoxy(5,15);
        printf("Student's Name is: ");
        //gets(name[0]);
        fgets(name[0], sizeof(name[0]), stdin);
    
        gotoxy(3,4+x);
        printf("%s", name[0]);
        col = 24;  //col = sizeof(name[0]) + left margin + spacer
        sum = 0;
    
        for(y=0;y<=2;y++)
        { 
          gotoxy(5,17);
          printf("Enter score [%d of 3] #%d: ", y+1, y+1);             
          scanf("%i",&score);
          gotoxy(col, 4+x);
          printf("%d", score);
          col = col + 6;
          sum = sum + score;
          gotoxy(5,17);
          printf("                            ");
        }  
        if(sum > max_score) {
           max_score = sum;
           //name[1] will always hold the highest scoring student's name
           strcpy(name[1], name[0]); 
        }
        name[0][0] = '\0';
        gotoxy(5,15);
        printf("                                          ");
        gotoxy(5,17);
        printf("                             ");
        gotoxy(col+2, x+4);
        printf("%d",sum);
        lin=lin+2;
        //col=15;
        score = getchar(); 
      } 
      gotoxy(5,17);
      printf("The highest scoring student is: %s", name[1]);        
      printf("\n With a score of: %d ", max_score);
      getche();
      return 0;
    }
    
    /* 
    Enter Student Name: John
    
    Enter 3 Scores of the Student:
    10
    10
    10
    
    (screen clears)
    
    Enter Student Name: Ronald
    
    Enter 3 Scores of the Student:
    9
    10
    9
    
    (screen clears)
    
    Enter Student Name: Karlo
    
    Enter 3 Scores of the Student:
    9
    8
    8
    
    (screen clears)
    (Final Output)
    
    Students 1 2 3 Total
    John 10 10 10 30
    Ronald 9 10 9 28
    Karlo 9 8 8 25
    
    John Got the Highest Total Score of 30.
    
    
    */

    I didn't use a score array, because it wasn't needed - but of course, it could be done using a score array.

    I would change your formatting options in your code editor, to make tabs into spaces (5 spaces is plenty), for this forum. Tabs, as tabs, don't work so well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 06:53 PM
  2. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 06:38 PM
  3. Displaying a table of arbitrary values?
    By cbmurdock in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:59 AM
  4. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 06:06 PM
  5. Displaying a table
    By hkguy in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2004, 07:49 AM

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