Thread: 2 Dimensional Array

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

    Exclamation 2 Dimensional Array

    my program is to calculate the student gpa first prompt the user to enter the score and display the grade and also calculate the total grade
    this is my program i stuck in the half way please help:

  2. #2
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Here is what I saw...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define column_size 30
    #define row_size 50
    
    char scoretograde(int score);
    void compute_grade_distribution(int list_size,const char letter_grades[],int distribution[]);
    void input_table(int *rows,int *column,int arr[][column_size]);
    void output_table(int rows,int columns,const int arr[][column_size]);
    
    int main()
    {
    
            int test_score[row_size][column_size];
            int rows,columns,score;
            float GPA;
    
            input_table(&rows,&columns,test_score);
            output_table(rows,columns,test_score);
    
    
            return 0;
    }
    
    void input_table(int *no_of_rows,int *no_of_columns,int arr[][column_size])
    {
            int i,j;
        printf("\n");
            printf("\t\t\tKOLEJ TUNKU ABDUL RAHMAN\n");
            printf("\t\t\t------------------------\n");
            printf("\n");
            printf("Enter number of rows    : ");
            scanf("&#37;d",&(*no_of_rows));
            printf("Enter number of columns : ");
            scanf("%d",&(*no_of_columns));
    
            for(i=1;i<=*no_of_rows;i++)
                    for(j=1;j<=*no_of_columns;j++)
                    {
                            printf("Enter value of test score [%d][%d] :",i,j);
                    scanf("%d",&arr[i][j]);
    
                    }
    }
    void output_table(int rows,int columns,const int arr[][column_size])
    {    
            int i,j;
    
        system("cls");
            printf("\n");
            printf("\t\t\tEXAMINATION PERFORMANCE REPORT\n");
            printf("\t\t\t------------------------------\n");
            printf("\n");
        printf("Student No\tAACS1084\tAACS1123\tGrade Point Average\n");
            printf("----------\t--------\t--------\t-------------------\n");
    
            for(i=1;i<=rows;i++){
                    printf("\t\n%3d",i);
            for(j=1;j<=columns;j++)
                    printf("\t\t%3d",arr[i][j]);
            }
    
            printf("\n\n");
            printf("AVERAGE GPA = %d\n ");
            printf("\n");
            printf("HIGHEST GPA =  %d     obtained by student No.=\n");
            printf("LOWEST  GPA =  %d     obtained by student No.=\n");
       
    
    }
    Here is a start. You need to pass data into input_table(), have it operate on some data, and then return something. In other words

    Code:
           int i,j;
            printf("\n");
            printf("\t\t\tKOLEJ TUNKU ABDUL RAHMAN\n");
            printf("\t\t\t------------------------\n");
            printf("\n");
            printf("Enter number of rows    : ");
            scanf("%d",&(*no_of_rows));
            printf("Enter number of columns : ");
            scanf("%d",&(*no_of_columns));
    should appear before input_table() in main().

    Now, for the second thing.
    Code:
     for(i=1;i<=*no_of_rows;i++)
                    for(j=1;j<=*no_of_columns;j++)
                    {
                            printf("Enter value of test score [%d][%d] :",i,j);
                    scanf("%d",&arr[i][j]);
    
                    }

    When you loop over a multidimensional array, you would do something like the following
    Code:
    /*This loop runs in quadratic time. But I don't think it's relevant to the issue at hand*/
    int i, j;
    i=1;
    j=1;
    
    for(i=1;i<=no_of_rows[i][j];i++) {
         for(j=1;j<=no_of_columns[i][j];j++){
         / *do stuff*/
        }
    }
    In other words, you want 'i' to be the first row in the array, then you use another loop to iterate over the colums in first array. Then you increase the value of 'i' so it it would the second row in the array and then iterate over the columns in the second row. Of course this would be differenf if this was an array of pointers.

    Just my 2 cents on the problem.
    Last edited by Overworked_PhD; 11-25-2007 at 03:52 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