Summing two Parallel Arrays to display the sum of both arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OverCode
    New Member
    • Aug 2013
    • 1

    Summing two Parallel Arrays to display the sum of both arrays

    I am having a problem with my program to calculate the GPA of a student. The problem that I am having is that I am not able to get my Total Point Value to display the sum of the two arrays. The multiplication for the array is correct and will display correctly, but instead of putting the total into the accumulator it will display the totals in a column. I have tried moving the coding for the calculation out of the loop that converts the letter grade into a point value,and placing it in it's own loop, but I still get the same display output. Any hints as to what I am doing wrong or what I need to do would be great. below is the code that I have so far. I still have a few elements to add to the code, but they will be easy once I get this display to work right.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    // Global const
    
    
    // prototype
    
    
    int main ()
    {	
    // Varialbes, Arrays
    	const int CREDITS = 4;
    	int hours[CREDITS];
    	char letterGrade[CREDITS];
    	char lName[24],fName[24];
    	double totalPoint = 0;
    	int totalH = 0,totalG = 0,totalP1;
    	
    	
    
    // fixed point
    	//cout << fixed << showpoint << setprecision(1) << endl;
    
    // Message display to get student input
    	cout << " This Program will Calculate a Students \n";
    	cout << " Total Hours, Total Points, and Overall GPS \n";
    	cout << endl;
    
    // Studendts Last Name 
    	cout << " Enter The Students Last Name: ";
    		cin >> lName;
    	cout << endl;
    
    // Students First name
    	cout << " Enter The Students Fisrt Name: ";
    		cin >> fName;
    	cout << endl;
    
    // Students Credit Hours for Each Class
    	for (int index = 0;index < CREDITS;index++)
    	{
    			
    			cout << " Enter The Credit Hour for Class " << (index + 1) << ": ";
    			cin >> hours[index];
    			totalH += hours[index]; // Figure total Credit Hours
    		while (hours[index] > 5 || hours[index] < 1)
    		{
    			cout << " !! That is an invalid Credit hour, please enter a valid Credit Hour !! \n";
    			cout << " Enter The Credit Hour for Class " << (index + 1) << ": ";
    			cin >> hours[index];
    		}		
    
    	}
    	cout << endl;
    
    // Students Letter Grade for Each Class
    	for (int index = 0;index < CREDITS;index++)
    	{
    		cout << " Please Enter A,B,C,D, or F for Each Class \n";
    	
    		{
    			cout << endl;
    			cout << " Enter the Letter Grade for Class " << (index + 1) << ": ";
    				cin >> letterGrade[index];
    			cout << endl;
    			while (letterGrade[index] < 65 || letterGrade[index] > 70)
    			{
    				cout << " !! That is an invalid grade letter, please enter a valid grade letter !! \n";
    				cout << endl;
    				cout << " Enter the Letter Grade for Class " << (index + 1) << ": ";
    					cin >> letterGrade[index];
    			}
    		}
    	}
    
    // Figure total points
    
    
    // 
    
    
    	cout << endl;
    // Main Display Output
    	cout << " ____________________________________________\n";
    	cout << "|                                            |\n";
    	cout << "|     Danvilee Area Community College        |\n";
    	cout << "|____________________________________________|\n",
    	cout << endl;
    
    	cout << " Student: " << lName << ", " << fName << endl;
    	cout << endl;
    	cout << " CLASS " << setw(17) << " CREDITS " << setw(10) << " GRADE \n";
    	cout << "____________________________________\n";
    	for (int index = 0;index < CREDITS;index++)
    	{
    		cout << endl;
    		cout << " Class " << (index + 1) << ": " << setw(10) << hours[index] << setw(10) << letterGrade[index] << endl;
    		cout << "____________________________________\n";
    	}
    
    	cout << endl;
    
    // Letter Point Conversion
    	cout << endl;
    
    
    	cout << endl;
    	cout << " Total Credit Hours " << totalH << endl;
    	cout << endl;
    
    	for (int index = 0;index < CREDITS;index++)
    	{
    		totalG += letterGrade[index];
    	}
    	
    	cout << endl;
    
    // Total Point Value
    	for (int index = 0;index < CREDITS;index++)
    	{
    		double totalP = 0;
    		switch(letterGrade[index])
    		{
    			case 'A': letterGrade[index] = 4;
    				break;
    			case 'B': letterGrade[index] = 3;
    				break;
    			case 'C': letterGrade[index] = 2;
    				break;
    			case 'D': letterGrade[index] = 1;
    				break;
    			case 'F': letterGrade[index] = 0;
    				break;
    		}
    	}
    
    // sum of calculations
    	int totalP = 0;
    	for (int index = 0;index < CREDITS;index++)
    	{
    		totalP = letterGrade[index] * hours[index];
    		cout << " Total Points " << totalP << endl;
    	}
    
    	
    
    	cout << endl;
    	system("pause");
    	return 0;
    }
    
    // fucntions
    Last edited by Rabbit; Aug 17 '13, 05:15 AM. Reason: Please use code tags when posting code.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I may not understand your problem exactly but this code will display totals in a column:

    Code:
    // sum of calculations
    	int totalP = 0;
    	for (int index = 0;index < CREDITS;index++)
    	{
    		totalP = letterGrade[index] * hours[index];
    		cout << " Total Points " << totalP << endl;
    	}
    Also, there is no display of totalP after the loop completes so all you are getting is row totals.

    Comment

    Working...