Thread: Help printing elements of array

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    33

    Help printing elements of array

    Hey everyone! I want to print the contents of 2 arrays using a function, and in a specific format. I only want to print specific contents of either array, based off of input from the user. My code works perfectly, compiles under gdb, and provides valid output. I just need help with the last function. I want to display information like

    ID TYPE GROSS DISCOUNT NET
    499230076 1 13500.000 5698.000 7802.000
    298760112 2 12000.000 8529.000 3471.000
    592493811 3 10800.000 9899.000 901.000
    355982306 3 7200.000 10000.000 -2800.000
    ------------------------------------------------


    TOTAL 43500.000 34126.000 9374.000

    The function is at the very bottom. I also call this function last in main.

    Here's my code!
    Code:
    #include<stdio.h>
    /* ASrray definitions*/
    #define STU_ROWSIZE 6
    #define STU_COLSIZE 2
    #define BILL_ROWSIZE 6
    #define BILL_COLSIZE 3
    
    
    /*Function prototypes*/
    int checkID(int stuInfo[][STU_COLSIZE], int idEntered);
    void processUndergrad(double billInfo[][BILL_COLSIZE], int id, int rowNumber);
    void processGraduate(double billInfo[][BILL_COLSIZE], int id, int rowNumber);
    void processPhD(double billInfo[][BILL_COLSIZE], int id, int rowNumber);
    int checkHours(int hours, int type);
    void processFinalReport(int stuInfo[][STU_COLSIZE], double billInfo[][BILL_COLSIZE]);
    
    
    int main(void)
    {
        /* The Student info Array*/
        int stuInfo[STU_ROWSIZE][STU_COLSIZE]={   {394003920, 2},
                                          {388920394, 3},
                                          {499230076, 1},
                                          {298760112, 2},
                                          {592493811, 3},
                                          {355982306, 3}   };
        
        /* The Billing info array*/
        double billInfo[BILL_ROWSIZE][BILL_COLSIZE]={   {0.0, 0.0, 0.0},
                                                {0.0, 0.0, 0.0},
                                                {0.0, 0.0, 0.0},
                                                {0.0, 0.0, 0.0},
                                                {0.0, 0.0, 0.0},
                                                {0.0, 0.0, 0.0}   }; 
        int idEntered, type, id, rowNumber, tuition;
        /* Variable to exit the program */
        int exit=1;
        /* program repetition controller*/
        while(exit==1)
        {
            printf("Enter the student ID. \n");
            scanf("%d", &idEntered);
            while(checkID(stuInfo, idEntered)== -1)
            {
                printf("Invalid student ID.  Please try again. \n");
                scanf("%d", &idEntered);
            }
            int rowID=checkID(stuInfo, idEntered);
                    switch(stuInfo[rowID][1])
            {
                case 1: 
                    processUndergrad(billInfo,idEntered,rowID);
                    break;
                case 2: 
                    processGraduate(billInfo,idEntered,rowID);
                    break;
                case 3: 
                    processPhD(billInfo, idEntered,rowID);
                    break;
            }
            /*Prompt user to exit program*/
            printf("\nDo you want to continue to the next student? If yes enter 1, else enter 0. \n");
            scanf("%d", &exit);
            processFinalReport(stuInfo, billInfo);
        }
        return 0;
    
    
    }
    
    
    int checkID(int stuInfo[STU_ROWSIZE][STU_COLSIZE], int idEntered)
    {
        int i=0,j=0;
        for(i=0;i<STU_ROWSIZE;i++)
        {
            for(j=0;j<STU_COLSIZE;j++)
            {
                if(stuInfo[i][j]==idEntered)
                {
                    return i;
                }
            }
        }
        return -1;
    }
    
    
    void processUndergrad(double billInfo[BILL_ROWSIZE][BILL_COLSIZE],int idEntered, int rowID)
    {
        int hours;
        printf("Enter the number of credit hours \n");
            scanf("%d", &hours);
            while(checkHours(hours, rowID)==0)
            {
                printf("Undergrad students must be enrolled in 3-18 hours. \n");
                    scanf("%d", &hours);
            }
            int tuition=hours*750;
        int scholarshipCompare=tuition*.50;
        int scholarship;
        printf("Enter scholarship amount \n");
        scanf("%d", &scholarship);
        while(scholarship>scholarshipCompare)
        {
            printf("Scholarship can't be greater than 50%% of the tuition. Enter again. \n");
            scanf("%d", &scholarship);
        }
        int finalTuition=tuition-scholarship;
        billInfo[rowID][0]=tuition;
        billInfo[rowID][1]=scholarship;
        billInfo[rowID][2]=finalTuition;
        printf("Undergrad student %d tuition is : \n", idEntered);
        printf("Gross                     $ %.2lf\n", billInfo[rowID][0]);
        printf("Scholarship Amount        $ %.2lf\n", billInfo[rowID][1]);
        printf("Total Tuition Bill        $ %.2lf\n", billInfo[rowID][2]);
    }
    void processGraduate(double billInfo[BILL_ROWSIZE][BILL_COLSIZE], int idEntered, int rowID)
    {    
        int hours;
        printf("Enter the number of credit hours \n");
            scanf("%d", &hours);
            while(checkHours(hours, rowID)==0)
            {
                printf("Graduate students must be enrolled in 3-12 hours. \n");
                    scanf("%d", &hours);
            }
            int tuition=hours*1000;
        int scholarshipCompare=tuition*.75;
            int scholarship;
            printf("Enter scholarship amount \n");
            scanf("%d", &scholarship);
            while(scholarship>scholarshipCompare)
            {
                    printf("Scholarship can't be greater than 75%% of the tuition. Enter again. \n");
                    scanf("%d", &scholarship);
            }
            int finalTuition=tuition-scholarship;
            billInfo[rowID][0]=tuition;
            billInfo[rowID][1]=scholarship;
            billInfo[rowID][2]=finalTuition;
            printf("\nGraduate student %d tuition is : \n", idEntered);
            printf("Gross                     $ %.2lf\n", billInfo[rowID][0]);
            printf("Scholarship Amount        $ %.2lf\n", billInfo[rowID][1]);
            printf("Total Tuition Bill        $ %.2lf\n", billInfo[rowID][2]);
    
    
    }
    void processPhD(double billInfo[BILL_ROWSIZE][BILL_COLSIZE], int idEntered, int rowID)
    {
        int hours;
        printf("Enter the number of credit hours \n");
            scanf("%d", &hours);
            while(checkHours(hours, rowID)==0)
            {
                printf("PhD students must be enrolled in 3-9 hours. \n");
                    scanf("%d", &hours);
            }
            int tuition=hours*1200;
        int stipendCompare=10000;
            int stipend;
            printf("Enter stipend amount \n");
            scanf("%d", &stipend);
            while(stipend<1 ||stipend>10000)
            {
                    printf("Scholarship can't be greater than 10,000. Enter again. \n");
                    scanf("%d", &stipend);
            }
            int finalTuition=tuition-stipend;
            billInfo[rowID][0]=tuition;
            billInfo[rowID][1]=stipend;
            billInfo[rowID][2]=finalTuition;
            printf("Undergrad student %d tuition is : \n", idEntered);
            printf("Gross                     $ %.2lf\n", billInfo[rowID][0]);
            printf("Stipend                   $ %.2lf\n", billInfo[rowID][1]);
            printf("Total Tuition Bill        $ %.2lf\n", billInfo[rowID][2]);
    
    
    }
    int checkHours(int hours, int type)
    {
        switch(type)
        {
            case 1: 
                if(hours<3 || hours>18)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
                break;
            case 2: 
                if(hours<3 || hours>12)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
                break;
            case 3: 
                if(hours<3 || hours>9)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
                break;
        }
    }
    void processFinalReport(int stuInfo[STU_ROWSIZE][STU_COLSIZE], double billInfo[BILL_ROWSIZE][BILL_COLSIZE])
    {
        printf("STUDENT BILLING REPORT \n\n\n\n");
        printf("   ID        TYPE          GROSS          DISCOUNT          NET\n");
        printf
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's stopping you from implementing the function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    33
    I'm aware you can print the contents of an array with a for loop, but in this scenario I don't feel that would help. The function only lists either array as accessible arguments. Therefore, within the function, I don't believe I can call variables like idEntered or rowID to print the contents I want. Moreso, if no information was entered for, say, student #499230076, I wouldn't want to print it. Similarly, if a student's tuition is less than 0, I wouldn't want to print it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is processFinalReport supposed to print? Be precise, e.g., say "print the billing information for the student with the student ID entered by the user", not "print the contents of 2 arrays using a function, and in a specific format".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    33
    Quote Originally Posted by laserlight View Post
    What is processFinalReport supposed to print? Be precise, e.g., say "print the billing information for the student with the student ID entered by the user", not "print the contents of 2 arrays using a function, and in a specific format".
    I believe if you look at my original post, it specifies what I want the function processFinalReport to print. It should print every instance that a student ID was entered, followed by the student type, total tuition, scholarship amount, and then the amount after tuition minus the scholarship. At the bottom it prints totals for the categories. This format should clearly dictate what I want printed

    ID TYPE GROSS DISCOUNT NET
    499230076 1 13500.000 5698.000 7802.000
    298760112 2 12000.000 8529.000 3471.000
    592493811 3 10800.000 9899.000 901.000
    355982306 3 7200.000 10000.000 -2800.000
    ------------------------------------------------


    TOTAL 43500.000 34126.000 9374.000

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Joshua Green
    I believe if you look at my original post, it specifies what I want the function processFinalReport to print.
    No, you only provided example output. That is a Good Thing, but lacked an explanation of what the output is about.

    Quote Originally Posted by Joshua Green
    It should print every instance that a student ID was entered, followed by the student type, total tuition, scholarship amount, and then the amount after tuition minus the scholarship. At the bottom it prints totals for the categories.
    Ah. This means that you need to record the student IDs that were entered, e.g., in an array, and also pass this array to the function. This way, you can then loop over the student IDs and obtain the information to be printed. However, I noticed that you call processFinalReport at the bottom of the main loop. Perhaps this should be called after the loop, i.e., after all the student IDs have been entered?

    By the way, avoid naming your variable exit as there is a function from <stdlib.h> named exit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2015
    Posts
    33
    Quote Originally Posted by laserlight View Post
    No, you only provided example output. That is a Good Thing, but lacked an explanation of what the output is about.
    Oh okay, that makes sense.


    Quote Originally Posted by laserlight View Post
    Ah. This means that you need to record the student IDs that were entered, e.g., in an array, and also pass this array to the function. This way, you can then loop over the student IDs and obtain the information to be printed. However, I noticed that you call processFinalReport at the bottom of the main loop. Perhaps this should be called after the loop, i.e., after all the student IDs have been entered?

    By the way, avoid naming your variable exit as there is a function from <stdlib.h> named exit.
    I'll change the variable exit name to prgQuit or something like that, thank you. I can't modify any of the functions, so there has to be a way to do it without creating a 3rd array

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Joshua Green
    I can't modify any of the functions, so there has to be a way to do it without creating a 3rd array
    I see. In that case, a simple solution exists: create another array that is a copy of stuInfo such that it only contains student information for the given student IDs, then pass this array as the first argument. This is inefficient, but it will satisfy the constraints. One problem is that you cannot use STU_ROWSIZE, but this can be worked around with say, an extra row with say, a specially designated invalid student ID serving as the sentinel value (like how a null character serves to mark the end of a string).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2015
    Posts
    33
    Quote Originally Posted by laserlight View Post
    I see. In that case, a simple solution exists: create another array that is a copy of stuInfo such that it only contains student information for the given student IDs, then pass this array as the first argument. This is inefficient, but it will satisfy the constraints. One problem is that you cannot use STU_ROWSIZE, but this can be worked around with say, an extra row with say, a specially designated invalid student ID serving as the sentinel value (like how a null character serves to mark the end of a string).
    That makes perfect sense. Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with printing elements of an array
    By shaddock in forum C Programming
    Replies: 12
    Last Post: 06-15-2015, 06:38 PM
  2. Please help with Printing duplicate array elements
    By TheSprawl in forum C Programming
    Replies: 9
    Last Post: 11-23-2011, 11:22 PM
  3. Printing out Array Elements
    By BB89 in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2010, 02:00 PM
  4. printing elements of an array using pointers
    By zeebo17 in forum C Programming
    Replies: 3
    Last Post: 05-22-2009, 09:30 PM
  5. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM