Thread: Can I further Optimize this script?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    6

    Can I further Optimize this script?

    Hello Again,

    My first semester of C++ programming has come to an end, and I ask one more time for advice, not for help in the class, but to help me learn from some mistakes i may have made in this script. (Which was the final project of the term) IF anybody could throw me some pointers, move some stuff around, whatever, it would be greatly appreciated. This script works exactly as it was inteded, but as I see it, I am sure there are ways to make it more concise, and efficient. Thanks again for your help in advance,

    -- Jeff

    Code:
                                                                                                                                                                                                                                                                   
    //Jeffrey Gross
    //Final Using Arrays 
    
    
    #include <iostream>
    #include <iomanip>
    #include <fstream> //included so we can utilize the input and output of external files
    #include <string> //include so we are able to use strings to store student names
    
    using namespace std;
    
    //Declare all of the user defined functions which are used
    void calculateAverage(int test1,int test2,int test3,int test4,int test5, int& student_average);
    char calculateGrade(int& student_average);
    int student_counter(ifstream& input2, int& student_count);
    
    
    int main()
    {
    string studentName;
    int test1,test2,test3,test4,test5;
    char grade = ' ';
    int student_average = 0;
    int student_count = 0;
    double class_average = 0;
    int class_total = 0;
    
    //declaration of various arrays
    int grades[50];
    string student_name[10];
    int average[10];
    char letter_grade[10];
    
    //declaration of all of the various counters
    int counter = 0;
    int name_count = 0;
    int grade_count = 0;
    int average_count = 0;
    int letter_grade_count = 0;
    
    
    ifstream infile;//input file stream variable
    ifstream infile2;
    ofstream outfile;   //output file stream variable
    
    infile.open("studentgrades.txt");  //open input file
    infile2.open("studentgrades2.txt");  //open input file
    outfile.open("results.out");   //open output file
    
    
    //Print the Heading of the output file!
    outfile << "Student" << setw(15) << "Test1" << setw(10) << "Test2" << setw(10) << 
    		"Test 3" << setw(10) << "Test 4" << setw(10) << "Test 5" << setw(10) << 
    		"Average" << setw(10) << "Grade" << setw(10) << endl;
    
    //count the number of students in the class
    
    student_counter(infile2,student_count);
    //cout << student_count << endl; -- This was here in order to test the functionality of the call
    
    
    //While there is still data to be read in the input file do the following:
    for (counter = 0; counter <= student_count; counter++)
    {
    
    //extract the data from the text file
    infile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5;
    
    //calculate the average based on the input
    calculateAverage(test1,test2,test3,test4,test5,student_average);
    
    
    //store the average for the particular student in the "average" array.
    average[average_count] = student_average;
    average_count++;
    
    if (studentName == "end") 
    {
    	break;
    }
    
    //stores the letter grade for this particular student inside of an array
    letter_grade[letter_grade_count] = calculateGrade(student_average);
    letter_grade_count++;
    
    //stores the name of the student in the student_name[x] array
    student_name[name_count] = studentName;
    name_count++;
    
    //stores all of the students 5 grades, these are stored in their appropriate spots
    //for example student A will occupy grades[0] through grades[4] and so on...
    grades[grade_count] = test1;
    grade_count++;
    grades[grade_count] = test2;
    grade_count++;
    grades[grade_count] = test3;
    grade_count++;
    grades[grade_count] = test4;
    grade_count++;
    grades[grade_count] = test5;
    grade_count++;
    
    class_total = class_total + student_average; //keeps a running total of the students total averages
    }
    
    //set the values of the counters to 0 to assure proper loop functionality
    counter = 0;
    name_count = 0;
    grade_count = 0;
    average_count = 0;
    letter_grade_count = 0;
    
    //execute this loop while there are still students to be read
    while (counter <= student_count)
    {
    //outputs the name of the student in the student_name[x] array
    outfile << setw(6) << student_name[name_count];
    name_count = name_count + 1;
    
    //outputs all of the students 5 grades these are taken from their appropriate spots
    //for example student A will output grades[0] through grades[4] and so on...
    outfile << setw(15) << grades[grade_count];
    grade_count++;
    outfile << setw(10) << grades[grade_count];
    grade_count++;
    outfile << setw(10) << grades[grade_count];
    grade_count++;
    outfile << setw(10) << grades[grade_count];
    grade_count++;
    outfile << setw(10) << grades[grade_count];
    grade_count++;
    
    //using the appropriate average (which was calculated in the previous loop)
    //we output the average for the student, and we calculate the appropriate grade
    //for the corresponding average and output that as well
    outfile << setw(10) << average[average_count];
    average_count++;
    
    //using the letter_grade array we extract and output the appropriate grade for each student
    outfile << setw(10) << letter_grade[letter_grade_count];
    letter_grade_count++;
    
    outfile << setw(10) << endl;
    
    counter = counter + 1;
    if (counter == student_count) //if the counter is equal to student_count break this loop
    {
    	break;
    }
    }
    
    class_average = class_total/student_count; //calculates the value  of the class average
    //Print Final stats to the output file
    outfile << endl;
    outfile << setw(20) << "Total Number of Students = " << student_count << 
    setw(20) << "Class Average = " << class_average << endl;
    
    infile.close();
    outfile.close();
    return 0;
    }
    
    //This function allows the program to count the number of students in the class
    int student_counter(ifstream& input2, int& student_count)
    {
    string studentName;
    int test1,test2,test3,test4,test5;
    
    //While there is still data to be read in the input file do the following:
    while (input2)
    {
    input2 >> studentName >> test1 >> test2 >> test3 >> test4 >> test5;
    
    if (studentName == "end") 
    {
    	return student_count; //Returns the amount of students to the main function
    }
    
    student_count++;
    
    }
    }
    
    
    
    
    
    
    //calculates the Students average, and passes it back to the main function
    void calculateAverage(int test1,int test2,int test3,int test4,int test5, int& student_average)
    {
     
    	student_average = ((test1+test2+test3+test4+test5))/5;
    
    
    }
    
    
    
    
    
    
    //Calculates the students grade, and returns the appropriate character
    char calculateGrade(int& student_average)
    {
    char grade; //placeholder for students grade
    
    	
    switch((student_average) / 10)
    {
    
    case 10:
    case 9:
    	grade = 'a';
    	return grade;
    
    case 8:
    	grade = 'b';
    	return grade;
    
    case 7:
    	grade = 'c';
    	return grade;
    
    case 6:
    	grade = 'd';
    	return grade;
    
    default: 
    	grade = 'f';
    	return grade;
    }
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It might be helpful to paste the contents of the input file and the expected output format as well (and there's no rule that says code tags can only be used for code).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    6
    Sure -- Will do -- Here are the input and output Files


    Here is the first input File (the second one is identical but has a different name
    Code:
    Balto 85 83 77 91 76 
    Mickey 80 90 95 93 48 
    Minnie 78 81 11 90 73 
    Doc 92 83 30 69 87 
    Goofy 23 45 96 38 59 
    Duckey 60 85 45 39 67 
    Grumpy 27 31 52 74 83 
    Sunny 93 94 89 77 97 
    Piggy 79 85 28 93 82 
    Pluto 85 72 49 75 63
    end

    and here is the resulting output

    Code:
    Student          Test1     Test2    Test 3    Test 4    Test 5   Average     Grade
     Balto             68        83        77        91        76        82         b
    Mickey             80        90        95        93        48        81         b
    Minnie             78        81        11        90        73        66         d
       Doc             92        83        30        69        87        72         c
     Goofy             23        45        96        38        59        52         f
    Duckey             60        85        45        39        67        59         f
    Grumpy             27        31        52        74        83        53         f
     Sunny             93        94        89        77        97        90         a
     Piggy             79        85        28        93        82        73         c
     Pluto             85        72        49        75        63        68         d
    
    Total Number of Students = 10    Class Average = 69
    Code:
    
    

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    void calculateAverage(int test1,int test2,int test3,int test4,int test5, int& student_average);
    char calculateGrade(int& student_average);
    I would expect the calcAverage function to return the average just like your calcGrade returns the grade.

    Also, the calcGrade function does not modify the average, so passing it by reference is not needed.

    grade = 'a';
    return grade;
    Why not just
    Code:
    return 'a';
    ?

    Those are cosmetic changes really, but you asked for it
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    These changes could be considered meaningless, but I'm tired and going to bed.

    This instead?

    Code:
    case 10:
    case 9:
    	grade = 'a';
    	break;
    case 8:
    	grade = 'b';
    	break;
    case 7:
    	grade = 'c';
    	break;
    case 6:
    	grade = 'd';
    	break;
    default: 
    	grade = 'f';
    }
    	return grade;
    }
    ++student_count; instead of student_count++;

    ++grade_count; instead of grade_count++;

    student_average = ((test1+test2+test3+test4+test5))/5; doesnt need double parenthesis.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 12:57 AM
  2. Script in games
    By Shakti in forum Game Programming
    Replies: 7
    Last Post: 09-27-2006, 12:27 AM
  3. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 11:30 AM
  4. Passing arguments to script.....
    By suwie in forum C Programming
    Replies: 5
    Last Post: 09-25-2004, 11:10 PM
  5. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM