Thread: C Array

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    C Array

    Hi,

    I am making a program which will record 10 students results in arrays of 4 subjects and output their percentage grade, average percentage of all subjects for each student, pass and fail rate and average rate.

    This is what i have so far:



    [code]

    Code:
    /* Student results  */[/QUOTE]
    
    // This program calculates percentage marks, average percentage marks, pass and fail results and top and bottom results. //
    
    
    #include <stdio.h>                     
    #include <cstdlib>                    
    #include <string.h>                    
    #include <iostream.h>                  
    #define STUDENTS 10                 
    #define EXAMS 4                        
    
    int minimum(int [][EXAMS], int, int);
    int maximum(int [][EXAMS], int, int);
    float average(int [], int);
    void printArray(int [][EXAMS], int, int);
    
    main()
    {
          int student,
              studentGrades[STUDENTS][EXAMS] = {{65, 55, 50, 65},
                                                                          {50, 60, 40, 50},
                                                                          {70, 62, 55, 79},
                                                                          {60, 50, 30, 40},
                                                                          {70, 60, 40, 70},
                                                                          {65, 67, 45, 89},
                                                                          {40, 50, 40, 13},
                                                                          {20, 35, 40, 70},
                                                                          {40, 30, 40, 60},
                                                                          {70, 66, 60, 95}};
              
              printf("The grade array is: \n");
              printArray(studentGrades, STUDENTS, EXAMS);
              printf("\n\nLowest grade: %d\nHighest grade: %d\n",
                 minimum(studentGrades, STUDENTS, EXAMS),
                 maximum(studentGrades, STUDENTS, EXAMS));
              
              for (student = 0; student <= STUDENTS - 1; student++)
                 printf("The average grade for student %d is %.2f\n",
                 student, average(studentGrades[student], EXAMS));
              system("pause");    
              return 0;
    }
    
    				/* Declare array */               
    
    char        student[10][20];                   // Declaring student number with names
                student [0]= "Ben";                     // Student number, 0 with name "Ben"
                student [1]= "Pat";                     // Student number, 1 with name "Pat"
                student [2]= "Dan";                     // Student number, 2 with name "Dan"
                student [3]= "Tal";                     // Student number, 3 with name "Tal"
                student [4]= "Stu";                     // Student number, 4 with name "Stu"
                student [5]= "kim";                     // Student number, 5 with name "Kim"
                student [6]= "Tom";                     // Student number, 6 with name "Tom"
                student [7]= "Tim";                     // Student number, 7 with name "Tim"
                student [8]= "Pete";                    // Student number, 8 with name "Pete"
                student [9]= "Don";                     // Student number, 9 with name "Don"
    
    				/* Initialise array */
    
           char student [10] [4];				    // Initialising an array with an input of 10 and number of subjects of 4
                student [1][1];                     // Student number 1, subject number 1
                student [1][2];                     // Student number 1, subject number 2
                student [1][3];                     // Student number 1, subject number 3
                student [1][4];                     // Student number 1, subject number 4
    
                    /* Declare subject names and marks obtainable*/
                    
             subject [1] [Mathematics][80];        // Subject number, name and marks obtainable
             subject [2] [Software systems][75];       // Subject number, name and marks obtainable
             subject [3] [Business systems][60];       // Subject number, name and marks obtainable
             subject [4] [Hydraulics and pneumatics][100];        // Subject number, name and marks obtainable
    
    //------------------- printf ------------------------------//
    
    printf("Welcome! Enter the name of the student and press Enter...\n");
    
    printf("To exit the program without any action, type END... \n\n");
    
    /* Test to see if the grade entered is valid. If the grade is invalid, it prints a message and exits the program */
    
    if ((grade < 0.0) || (grade > 100.0))
    {
               printf("You have entered an invalid grade!\n\n");
               system("PAUSE");
               return 1;
    }
    if (Grade >= passingGrade)
    {
              printf("Congractulations. You have passed!\n\n");
    }
    else
    {
              printf("Bad luck. You need to work harder next time.\n\n");
    }
              
                  	//---------------------- Functions------------------------//
    
                   // percentage grade              // calculates the percentage grade
                   
                   int main();
                   if(ben >= 90)
                   grade = ("90 / 100 * 100");
    
                   // average percentage grade      // calculates the average percentage grade
    
                   average grade = ("total grade for ben / 4");
    
                   // pass or fail rate                      // calculates pass or fail rates
                   
                   if (grade >= 33.3%);
    {
                   grade = 'pass';
                   printf("You have passed!\n");
    }
    {
                   if (grade <= 33.3%);
                   grade = 'fail';
                   printf("You have failed\n!");
    }              
       				// Total average grade and pass rate              // calculate the average grade using "float"
                   
       				int main();
       {
          			float [studentdetails10] [20];
           			Average(StudentDetails);
                    if(grade >= passgrade) 
                   {
                            if(averageben <= 100)
                            grade = A+;
                            printf("You have passed very impressively with an A+ grade!\n");
                            if(averageben <= 90)
                            grade = A;
                            printf("You have passed impressively with an A grade.\n"); 
                            if(averageben >= 80)
                            grade = B;
                            printf("You have passed with an B grade.\n");
                            elseif (averageben >= 60)
                            grade = C;
                            printf("You have passed with an C grade.\n");
                            elseif (averageben >= 55)
                            grade = D;
                            printf("You have passed with an D grade.\n");
                            elseif (averageben >= 40)
                            grade = E;
                            printf("You have just passed with an E grade.\n");
                            else
                            grade = F;
                            printf("You have failed! You should be dissapointed with yourself!\n\n");   
                   } 
    
                     // Exit program function					// Exits program by typing END and pressing Enter
               {
                    void _exit(
                    	int status;
                      exit( END );
                    );
               }
    {
      					/* Print contents of array */
    
    printf("The results for the following student are displayed as...\n\n"
    show(results);
    Printf("Press any key to exit...\n\n");
    }
    scanf("%d");
    
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    [code]

    This is what i have so far. I am now abit stuck and dont know where to improve. it does not compile.


    Any help and comments appreciated.

    Thanks.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    #include <stdio.h>                     
    #include <cstdlib>                    
    #include <string.h>                    
    #include <iostream.h>
    Do you even know what language you're using?

    If you're using C, stick to C libraries. If you're using C++, stick to C++ libraries. What you're doing will not compile with a C-only compiler because you're combining the two libraries.

    For the rest of it, you're just doing things that you're not supposed to do. You have to be able to verbally describe to yourself and others exactly what you're trying to do, every single step of the way. If you are unable to do that, then your ability to write a program that produces the right answer is probably non-existant.

    If you can describe exactly what you're trying to do for every line of C code, but you're writing C-gibberish (ie. This is if the compiler is yelling at you for what you think is valid C.), then you should be reviewing the language.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd ditch the #include <iostream.h> because it's C++ and bound to cause problems if you're using C and C++ libraries, as MacGyver has stated, above.

    Here:
    Code:
    if (Grade >= passingGrade)
    {
              printf("Congractulations. You have passed!\n\n");
    }
    else
    {
              printf("Bad luck. You need to work harder next time.\n\n");
    }
              
                  	//---------------------- Functions------------------------//
    
                   // percentage grade              // calculates the percentage grade
                   
                   int main();
                   if(ben >= 90)
                   grade = ("90 / 100 * 100");
    
                   // average percentage grade      // calculates the average percentage grade
    
                   average grade = ("total grade for ben / 4");
    
                   // pass or fail rate                      // calculates pass or fail rates
                   
                   if (grade >= 33.3%);
    {
                   grade = 'pass';
                   printf("You have passed!\n");
    }
    Your code style/syntax needs work:

    if (grade >= 33.3%); is poor because grade doesn't know anything about %. It's just a poor data type, and you've saddled it with this lousy % sign, you see? Lose that, and remember that PERCENT MEANS HUNDREDTHS, so it just becomes 0.33, not the incomprehensible to the data type, 33%.

    Then you have a semi-colon at the end of the if grade line of code. Remove it. Semi-colons go at the end of a complete line of executable code, which is always the next bit of code after an if () statement.

    This is one statement:
    if (grade >= 0.33) grade= pass;

    so is this:
    if (grade >= 0.33)
    grade = pass;

    (There is no difference to the compiler between these two statements, btw).

    I've highlighted some troubles in red, above. You can't do arithmetic on words! Ben's test score may be >= 0.90, but "ben" will never be >= 0.90.

    Looks like some pseudo code was still left in the code?

    You may need to comment out large areas of the program to get something to compile. Then you can add areas back in, as you get them coded properly.

    These things should be done in an incremental manner, it's a big mistake to try and compile some new and large blocks of code, all at once. Especially for a beginner. "Meter by meter, life is sweeter."

    Adak

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > /* Declare array */
    All the code after this line is outside of a function, and will not compile.

    Adak is right, you should start a new project and copy your code in a few lines at a time, pressing compile as you go. Do not add more code if what you have doesn't compile. And don't wait until the end to see if it will run or not, keep testing as you go. This will force you to think about the order in which you need to add things.

    If you get to a part where an incremental addition fails to compile / run, then post what you have.

    Simply writing a whole mess, then dumping it on a message board for someone else to fix isn't going to work.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Now i have sorted the code out.

    This is what i have now:

    Code:
    /* Student results */
    
    #include <stdio.h>
    #include <cstdlib>
    #define STUDENTS 10
    #define EXAMS 4
    
    int minimum(int [][EXAMS], int, int);
    int maximum(int [][EXAMS], int, int);
    float average(int [], int);
    void printArray(int [][EXAMS], int, int);
    
    main()
    {
          int student,
              studentGrades[STUDENTS][EXAMS] = {{65, 55, 50, 65},
                                                {50, 60, 40, 50},
                                                {70, 62, 55, 79},
                                                {60, 50, 30, 40},
                                                {70, 60, 40, 70},
                                                {65, 67, 45, 89},
                                                {40, 50, 40, 13},
                                                {20, 35, 40, 70},
                                                {40, 30, 40, 60},
                                                {70, 66, 60, 95}};
              
              printf("The array is: \n");
              printArray(studentGrades, STUDENTS, EXAMS);
              printf("\n\nLowest grade: %d\nHighest grade: %d\n",
                 minimum(studentGrades, STUDENTS, EXAMS),
                 maximum(studentGrades, STUDENTS, EXAMS));
              
              for (student = 0; student <= STUDENTS - 1; student++)
                 printf("The average grade for student %d is %.2f\n",
                 student, average(studentGrades[student], EXAMS));
              system("PAUSE");  
              return 0;
    }
    
    
    /* Find the minimum grade */
    int minimum(int grades[][EXAMS], int pupils, int tests)
    {
        int i, j, lowGrade = 100;
        
        for(i = 0; i <= pupils - 1; i++)
              for(j = 0; j <= tests - 1; j++)      
              if (grades[i][j] < lowGrade)
              lowGrade = grades[i][j];
        
        return lowGrade;
    }
    
    /* Find the maximum grade */
    int maximum(int grades[][EXAMS], int pupils, int tests)
    {
        int i, j, highGrade = 0;
        
        for (i = 0; i <= pupils - 1; i++)
            for (j = 0; j <= tests - 1; j++)
            if (grades[i][j] > highGrade)
               highGrade = grades[i][j];
        
        return highGrade;
    }
    
    /* Determine the average grade for a particular exam */
    float average(int setOfGrades[], int tests)
    {
          int i, total = 0;
          
          for (i = 0; i <= tests - 1; i++)
              total += setOfGrades[i];
              
          return (float) total / tests;
    }
    
    /* Print the array */
    void printArray(int grades[][EXAMS], int pupils, int tests)
    {
         int i, j;
         
         printf("                         [0]    [1]    [2]    [3]   [4]    [5]    [6]    [7]    [8]    [9]    [10]");
               printf("\nstudentGrades[%d] ", i);
         
         for (i = 0; j <= pupils - 1; i++) {
             printf("\nstudentGrades[%d] ", i);
               
         for (j = 0; j <= tests - 1; j++)
             printf("%-5d", grades[i][j]);
         system("pause");
         }
    }

    This code compiles and runs. One problem with this code is that it does nt print the array out for some reason.

    I need to expand this code further like to calculate the average total grade for one student with all his subjects, pass or fail rates and somehow store their names into the array.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    *sigh*

    Code:
    #include <stdio.h>
    #include <cstdlib>
    You're still using a combo of C and C++ with these headers. cstdlib is the C++ way of specifying stdlib.h.

    In addition, not really as important, but you really should explicitly declare main() to return an int. I know it's defaulted to return an int, but there is a reason why my compiler spit out a "no type specified. Defaulting to int" warning.

    This is the output when I run your program:

    Code:
    The array is:
                             [0]    [1]    [2]    [3]   [4]    [5]    [6]    [7]
    [8]    [9]    [10]
    studentGrades[15]
    
    Lowest grade: 13
    Highest grade: 95
    The average grade for student 0 is 58.75
    The average grade for student 1 is 50.00
    The average grade for student 2 is 66.50
    The average grade for student 3 is 45.00
    The average grade for student 4 is 60.00
    The average grade for student 5 is 66.50
    The average grade for student 6 is 35.75
    The average grade for student 7 is 41.25
    The average grade for student 8 is 42.50
    The average grade for student 9 is 72.75
    Press any key to continue . . .
    Code:
    for (i = 0; j <= pupils - 1; i++)
    Notice anything wrong?

    Fixing that error results in this:

    Code:
    The array is:
                             [0]    [1]    [2]    [3]   [4]    [5]    [6]    [7]
    [8]    [9]    [10]
    studentGrades[15]
    studentGrades[0] 65   55   50   65   Press any key to continue . . .
    
    studentGrades[1] 50   60   40   50   Press any key to continue . . .
    
    studentGrades[2] 70   62   55   79   Press any key to continue . . .
    
    studentGrades[3] 60   50   30   40   Press any key to continue . . .
    
    studentGrades[4] 70   60   40   70   Press any key to continue . . .
    
    studentGrades[5] 65   67   45   89   Press any key to continue . . .
    
    studentGrades[6] 40   50   40   13   Press any key to continue . . .
    
    studentGrades[7] 20   35   40   70   Press any key to continue . . .
    
    studentGrades[8] 40   30   40   60   Press any key to continue . . .
    
    studentGrades[9] 70   66   60   95   Press any key to continue . . .
    
    
    Lowest grade: 13
    Highest grade: 95
    The average grade for student 0 is 58.75
    The average grade for student 1 is 50.00
    The average grade for student 2 is 66.50
    The average grade for student 3 is 45.00
    The average grade for student 4 is 60.00
    The average grade for student 5 is 66.50
    The average grade for student 6 is 35.75
    The average grade for student 7 is 41.25
    The average grade for student 8 is 42.50
    The average grade for student 9 is 72.75
    Press any key to continue . . .
    You should probably format your output better, and not pause each and every time a student's grades are printed.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you looking to include the student's name by using a parallel string array (that would be where Ben is name[0], and his scores are in scores[0]), or make up structs or what do you have in mind for this?

    For beginner's I'd favor a parallel array. You don't want to skip that concept by jumping ahead.

    Please make the changes MacGyver mentioned, and post your new code up, perhaps with your stab at using parallel arrays.

    Then we'll know just where you're at with the program.

    Adak

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Thanks for the help.

    I have sorted both probs out.

    On the 3rd line, studentGrades comes out as [-1] !

    When i mentioned student names, i meant that instead of using student numbers, i would like to use names for their grades.

    I would also like to add pass and fail rates, if grade is below pass rate the program will exit. This is what i have:

    // pass or fail rate
    {
    if (grade >= 0.33)grade = pass;
    printf("You have passed!\n");

    if (grade <= 0.33)grade = fail;
    printf("You have failed\n!");
    }
    Where do i insert this ?
    Last edited by fab04; 04-12-2007 at 11:18 AM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by fab04 View Post
    Thanks for the help.

    I have sorted both probs out.

    On the 3rd line, studentGrades comes out as [-1] !

    When i mentioned student names, i meant that instead of using student numbers, i would like to use names for their grades.

    I would also like to add pass and fail rates, if grade is below pass rate the program will exit. This is what i have:


    Where do i insert this ?
    You're getting a bad print out on studentGrades, because the variable i is not initialized to anything first:
    Code:
    int i, j;
         
         printf("                         [0]    [1]    [2]    [3]   [4]    [5]    [6]    [7]    [8]    [9]    [10]");
               printf("\nstudentGrades[&#37;d] ", i);    /* I'd just remove this */
         
         for (i = 0; j <= pupils - 1; i++) {            /* Change this j to an i --> we don't like having to repeat ourselves */
             printf("\nstudentGrades[%d] ", i);
    And that is no good. Since you have the studentGrades being print right below it anyway, I'd just remove the upper printf statement.

    I understood that you wanted to have the student's names printed out, and not just their numbers. You'll need some data structure (maybe a parallel array of char's) to hold their names.

    You can have the grade is passing or failing output, anywhere you want to have it. I haven't seen the output you want yet, so I can't say where it should go in your program.

    Be careful about exiting from your program too soon. Even after one student fails, others will still need to have their data run through the program. One failing grade may not kick a student out of the course, also. He / she may have good grades to be reported, as well.

    Adak

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    I have removed the printf statement and changed the j to an i.


    Like Adak mentioned, i need to use a parallel array to declare the names of the students and the subject names.

    This is what i have done to declare the student names:

    char student[10][20]; // Declaring student number with names
    student [0]= Ben;
    student [1]= Pat;
    student [2]= Dan;
    student [3]= Tal;
    student [4]= Stu;
    student [5]= kim;
    student [6]= Tom;
    student [7]= Tim;
    student [8]= Pete;
    student [9]= Don;

    These are the subject names:

    char student[10] [4];
    student [1][1]
    student [1][2]
    student [1][3]
    student [1][4]

    /* Declare subject names and marks obtainable*/

    subject [1] [Mathematics][80]
    subject [2] [Software systems][75]
    subject [3] [Business systems][60]
    subject [4] [Hydraulics and pneumatics][100]


    How do i put this all together in the program??

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by fab04 View Post
    I have removed the printf statement and changed the j to an i.


    Like Adak mentioned, i need to use a parallel array to declare the names of the students and the subject names.

    This is what i have done to declare the student names:

    char student[10][20]; // Declaring student number with names
    student [0]= Ben;
    student [1]= Pat;
    student [2]= Dan;
    student [3]= Tal;
    student [4]= Stu;
    student [5]= kim;
    student [6]= Tom;
    student [7]= Tim;
    student [8]= Pete;
    student [9]= Don;

    These are the subject names:

    char student[10] [4];
    student [1][1] /* This will be the 'a' in Pat what's this doing?? */
    student [1][2]
    student [1][3]
    student [1][4]

    /* Declare subject names and marks obtainable*/

    subject [1] [Mathematics][80]
    subject [2] [Software systems][75]
    subject [3] [Business systems][60]
    subject [4] [Hydraulics and pneumatics][100]


    How do i put this all together in the program??
    You can't have two arrays called "student". If one set is for subjects, let's call that one "subjects", instead.

    Arrays are initialized quite differently, also. There is the first initialization, but later attempts to do the same need to be more exact. For instance:

    Code:
    char student[][] = 
    { 
        {"Ben" };
        { "Alice" };
        { "Zeke" };
    };
    Should compile OK. The compiler will count up the number of rows and columns, and size the array correctly.

    Later, you can't do that "let the compiler count it out" trick. You have to do the counting for the assignments you want to make.

    Your subjects arrays will be initialized like the students array, above. As to where that should be done, I don't know. Where do you want or need them to be done? Somewhere before they are printed out, of course.

    If you're going to have 1 array start it's data at array subscript 0, then have ALL the arrays start their data at subscript 0, You'll get confused and you've got enough to learn and remember.

    See if you can get the arrays initialized and then printed out with the right values. Then think about where you want things to be printed out on the page.

    Show the code so I can refer to the updated version, please.

    Also, what compiler are you using?

    Adak

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    OK>

    This is what i have now:

    Code:
    /* Student results */
    
    #include <stdio.h>
    #include <stdlib.h>
    #define STUDENTS 10
    #define EXAMS 4
    
    int minimum(int [][EXAMS], int, int);
    int maximum(int [][EXAMS], int, int);
    float average(int [], int);
    void printArray(int [][EXAMS], int, int);
    
    main()
    {
    char        student[10][20];                   // Declaring student number with names
                student [0]= { "Ben" };                     
                student [1]= { "Pat" };                     
                student [2]= { "Dan" };                     
                student [3]= { "Tal" };                     
                student [4]= { "Stu" };                     
                student [5]= { "kim" };                     
                student [6]= { "Tom" };                    
                student [7]= { "Tim" };                     
                student [8]= { "Pete" };                    
                student [9]= { "Don" };                    
                
    char        subject [0] =  [Mathematics]
                subject [1] = [Software Systems]
                subject [2] = [Business systems]
                subject [3] = [H.P]       
          int student,
              studentGrades[STUDENTS][EXAMS] = {{65, 55, 50, 65},
                                                {50, 60, 40, 50},
                                                {70, 62, 55, 79},
                                                {60, 50, 30, 40},
                                                {70, 60, 40, 70},
                                                {65, 67, 45, 89},
                                                {40, 50, 40, 13},
                                                {20, 35, 40, 70},
                                                {40, 30, 40, 60},
                                                {70, 66, 60, 95}};
              
              printf("The array is: \n");
              printArray(studentGrades, STUDENTS, EXAMS);
              printf("\n\nLowest grade: %d\nHighest grade: %d\n",
                 minimum(studentGrades, STUDENTS, EXAMS),
                 maximum(studentGrades, STUDENTS, EXAMS));
              
              for (student = 0; student <= STUDENTS - 1; student++)
                 printf("The average grade for student %d is %.2f\n",
                 student, average(studentGrades[student], EXAMS));
              system("PAUSE");  
              return 0;
    }
    
    
    /* Find the minimum grade */
    int minimum(int grades[][EXAMS], int pupils, int tests)
    {
        int i, j, lowGrade = 100;
        
        for(i = 0; i <= pupils - 1; i++)
              for(j = 0; j <= tests - 1; j++)      
              if (grades[i][j] < lowGrade)
              lowGrade = grades[i][j];
        
        return lowGrade;
    }
    
    /* Find the maximum grade */
    int maximum(int grades[][EXAMS], int pupils, int tests)
    {
        int i, j, highGrade = 0;
        
        for (i = 0; i <= pupils - 1; i++)
            for (j = 0; j <= tests - 1; j++)
            if (grades[i][j] > highGrade)
               highGrade = grades[i][j];
        
        return highGrade;
    }
    
    /* Determine the average grade for a particular exam */
    float average(int setOfGrades[], int tests)
    {
          int i, total = 0;
          
          for (i = 0; i <= tests - 1; i++)
              total += setOfGrades[i];
              
          return (float) total / tests;
    }
    
    /* Print the array */
    void printArray(int grades[][EXAMS], int pupils, int tests)
    {
         int i, j;
         
         printf("                         [0]    [1]    [2]    [3]   [4]    [5]    [6]    [7]    [8]    [9]    [10]");
         
         for (i = 0; i <= pupils - 1; i++) {
             printf("\nstudentGrades[%d] ", i);
               
         for (j = 0; j <= tests - 1; j++)
             printf("%-5d", grades[i][j]);
         system("pause");
         }
    }
    I want it to show the student and subject names and then display their results.

    The compiler i am using is Dev-c++.

    Thanks in advance.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure exactly what you expect your output to look like.
    Code:
    subject0
    -------- ... ---
             [0] [1] ... [N]
    student0
    student1
    ...
    studentN
    
    
    subject1
    ...
    Is that what you mean? A different grid for each subject? If so, then pretty much just start at the top, and read it, to figure out what order you print things.
    Code:
    for each subject
        display subject line
        display divider line(s)
        for each student
            display student name
            for each grade in this subject
                display each grade
    If that's not what you had in mind, well you get the idea, it all works pretty much the same way.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Would an array of student structures not be easier to read?
    Not to mention its more future-proof...

    Code:
    struct subject_t {
    	char name[20];
    	unsigned short int grade;
    };
    
    
    typedef struct student_t {
    	char name[20];
    	struct subject_t grades[EXAMS];
    } student;
    
    
    int main(void)
    {
    	student MyStudents[STUDENTS];
    	
    	// blah blah
    
    	return 0;
    }

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by fab04 View Post
    OK>

    I want it to show the student and subject names and then display their results.
    You've got a start, but the process is backward for you to get the practice you need. You need to be suggesting the solutions, posting up your code to see what's right or wrong with it, or asking questions about the algorithm.

    Instead, you're waiting for us to give you idea's, snippets of code, nudges for a good algorithm. Then you're adding that into your program. That's upside down.

    You'll never get the practice of writing code and figuring out how an algorithm should be put together to solve a problem, that way. On your next assignment, you'll be just that much more behind your classmates who have been more actively working at this. Trying this type of loop, learning that kind of syntax error and how to avoid it, etc.

    Using an array of structs would indeed be "better", but too advanced in this case. The OP started with parallel arrays, and no structs, I believe that's what the instructor or assignment, required.

    So get busy and when you have a coding or algorithm problem, please show us your attempt and the output, and what you want it to be, instead. Or paste up your error messages and the code that is causing them. But please don't look for basic idea's - "how should I proceeed, I have no clue?" type of general question.

    Adak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM