This is my GPA Calculator, for a homework assignment. Please share what you would have added or remove from the code. Arrays is a requirement in this code.

``````# include <iostream>
using namespace std;

int main ()
{
float b [100], c [100], a, a1, average_GPA, total_credits, Cummalative_GPA;

cout <<"Enter The # of Courses:  ";
cin>>a;
cout <<"-------------------------------------------- \n";
a1=0; total_credits=0;

for (int i=1; i<=a;i++)
{
cout <<"Enter the credit hours for course "<< i <<":     " ;
cin >> b[i];
cout<<"Enter the grade you earned for the course \n";
cout<<"(A= 4, B= 3, C= 2, D= 1)              :";
cin>>c[i];
cout <<"-------------------------------------------- \n";
average_GPA = b[i] * c[i]; a1 = average_GPA + a1;total_credits = b[i] + total_credits;
}

Cummalative_GPA = a1 / total_credits;
cout<< "Your cummalative GPA for the semester is a \n" <<"  (" << Cummalative_GPA <<")\n";

return (0);

}``````

## All 12 Replies

1. why is a a float? The number of courses can not have fractions -- you can't take 1 1/2 courses can you? And you should use a more descriptive name for that variable and make it an integer.

2. arrays are always numbered from 0 to <number of elements>. The loop at line 13 should be `for(i = 0; i < a; i++)` 3. lines 18 and 19. array c is a float array. Since you only want to enter whole numbers (1, 2, 3 or 4) then you should make it an array of integers, not floats.

4. average_GPA on line 21 I think is wrong. Isn't the GPA the (sum of all semester hours) divided by (sum of all grades). I think you want to wait until after you get the sums before doing the division.

4. line 21. Put a carrage return after each semicolon. Yes it will compile, but is not good style to have all those statements on the same line, and is more difficult to debug.

out of the topic question:
What does credit actually mean? In my country, we don't have those and I couldn't find much info about it on the web?

I'll bet you have something similar. Each course is worth X number of credit hours -- one credit hour for each hour of classtime per week (or something like that). You must accumulate a certain number of credit hours to be awarded a Bachelors degree. The requirements for each college/university is different so you have to check with the school you want to attend. Here are some useful google links to read for more information

Hello and thank you for the quick response.

This is the criteria I had to follow based on professors specifications.

“Write a program to calculate your GPA using two "float"
arrays. The first one keeps track of credit hours and the
second keeps track of the numerical value for the grade
you earned. The final output should print your GPA based
on the data you enetred.

Here is a sample output:

Enter the number of courses: 3.0
Enter the credit hours for course 1: 3.0
Enter the grade you earned for course 1: 4.0
Enter the credit hours for course 2: 2.0
Enter the grade you earned for course 2: 3.5
Enter the credit hours for course 3: 1.0
Enter the grade you earned for course 3: 2.0

Your GPA is = ((3.0 x 4.0 + 2.0 x 3.5 + 1.0 x 2.0)/(3.0+2.0+1.0) = 3.5 ”
 Enter the number of courses: 3.0 3.0 requires float] Your GPA is = ((3.0 x 4.0 + 2.0 x 3.5 + 1.0 x 2.0)/(3.0+2.0+1.0) = 3.5 [This is how I came up with the average_GPA]
 arrays are always numbered from 0 to <number of elements>. The loop at line 13 should be for(i = 0; i < a; i++) [The results here starts off with course 1 therefore the loop starts at 1, not 0.]

>>The results here starts off with course 1 therefore the loop starts at 1, not 0
Not relavent -- all arrays still start at 0. If you want to display 1 instead of 0 then just add 1 to the loop counter when displaying it, line 15 for exmple: `cout <<"Enter the credit hours for course "<< (i+1) <<": " ;` As for the rest -- just chalk it up to dumb requirements. Nobody is going to enter that they took 3.5 courses.

Oh i see, thanks.

My program i similar that i am working on to calculate my GPA from a records stored in a data file.

A+ 4 11
A 4 10
A- 3.7 9
B+ 3.3 8
B 3 7
B- 2.7 6
C+ 2.3 5
C 2 4
C- 1.7 3
D+ 1.3 2
D 1 1
F/WU 0 0

Grade Points are determined by multiplying the Value of a grade by the number of credits of the course. Mathematically, this can be expressed as:
Grade Points = Value x Credits
The GPA requires two sums: the summation of all the grade points earned for the term you are calculating: Points (PTS); and the total number of credits taken during the term: Hours (HRS).
Σ(credits) = PTS
Σ(credits) = HRS
To determine the GPA, divide the Points (PTS) by the Hours (HRS):
GPA= PTS/ HRS
You will be given a file named GPA.dat that will have the number of credits each course was worth and a grade for that course as a number eg: 5 credit B- will be 5 7
Write a program that gets 1 semester of grades and calculates the GPA. After it calculates and prints the GPA it should print the total number of credits each grade received.
Example: if an A was receive for a 3 credit course and a 4 credit course. It would print 7 credits of A (or instead of A: 7 credits with a value of 10).
This program must use arrays.

Would it be better to use an array to do this or a struct?

I have some code written out but no where else to go.

``````#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MaxSize = 100;

struct StudentInfo
{
int StudentID;
char test1 [9];
char test2 [2];
};//Struct

int main ()
{
StudentInfo Student[MaxSize];

ifstream RecordsIn;
cout << "Reading Numbers ... " << endl;
RecordsIn.open("GPA.dat");
if ( ! RecordsIn )
{
cerr << "Could not open GPA.dat. Program Closing. " << endl;
system("pause");
exit (100);
}// If

int index= 0;
while((RecordsIn >> Student[index].StudentID) && (index < MaxSize))
{
RecordsIn >> Student[index].test1;
RecordsIn >> Student[index].test2;
index++;
}//While

cout << "\nStudentID   Test1   Test2\n";
for (int k = 0; k < index; k++)
{
cout << setw(5) << Student[k].StudentID;
cout << setw(13) << Student[k].test1;
cout << setw(6) << Student[k].test2 << endl;
}// For

RecordsIn.close();
system("pause");
return 0;
}//Main``````

The first array should be a float because it isn't the number of courses taken, it's the number of credit hours taken. In other words, you don't want to ask the user to input the number of courses taken. According to the requirements, you want them to input the number of credit hours taken, which CAN be a decimal.

For example, at my university, lab courses were worth 0.5 semester hours (or 0.5 credits) because they only met once per week. Other courses were worth 3.0 semester hours. At the end of the day, when calculating GPA, an 'A' in a 3 credit course carries 6X the amount of weight towards my overall GPA as a 0.5 credit course. However, it always seemed like the fewer the credits the course was worth, the more work was involved. Those half-credit chemistry labs were a royal pain.

The second array for a course grade should also be a decimal. According to the project requirements, it looks like you're not just supposed to have choices of 1, 2, 3, or 4. Traditionally ...
4 = A
3.8 = A-
3.5 = B+
3.0 = B
2.8 = B-
2.5 = C+
etc.

Weird school :)

It is suppose to read all this information into the array from a file though and requires no input from the user at all.

``````#include<iostream>
#include <string>
using namespace std;
int main()
{
char select,fullname;
int i,z,code[20];
char n,y;
int select1;
int studentnumber[100];
char courses[7][99];
int totalmark[7];
int credits[7];
float gpa[7];
cout<<"\n Welcome to grade calcuating program\n\n";
cout<<"Do you want to start the program? <Y/N> :";
cin>>select;
switch(select)
{
case 'n':
exit(0);
break;
case 'y':
system("cls");
for(i=0;i<1;i++)
{
cin>>studentnumber[i];
cin>>fullname;
cout<<"how many courses did you take:";
cin>>courses[i],z;
cout<<"how many credits does"<<courses[i]<<"have<1-5>?:";
cin>>credits[i];
cout<<"\nstudent number "<<studentnumber;
cout<<"\nstudent name"<<fullname;
cout<<"\ncources taken this term "<<courses[i];
}

for(i=0;i<z;i++)
{
cout<<"Enter cource#"<<i<<"code :";
cin>>code[i];
cout<<"Enter many credis does "<<code<<"have<1-5>?:";
cin>>credits[i];
cout<<"Enter total mark from "<<code<<"<1-100>";
cin>>totalmark[i];
if((totalmark[i]>= 95)&&(totalmark[i]<=100))
{gpa[i]+=4.00;}
else if((totalmark[i]>=90)&&(totalmark<95))
{gpa[i]+=3.70;}
else if ((totalmark[i]>=85)&&(totalmark[i]<90))
{gpa[i]+=3.50;}
else if ((totalmark[i]>=80)&&(totalmark[i]<85))
{gpa[i]+=3.30;}
else if((totalmark[i]>=75)&&(totalmark[i]<80))
{gpa[i]+=3.00;}
else if((totalmark[i]>=70)&&(totalmark[i]<75))
{gpa[i]=2.70;}
else if((totalmark[i]>=65)&&(totalmark[i]<70))
{gpa[i]+=2.50;}
else if((totalmark[i]>=60)&&(totalmark[i]<65))
{gpa[i]+=2.30;}
else if((totalmark[i]>=55)&&(totalmark[i]<60))
{gpa[i]+=2.00;}
else if((totalmark[i]>=50)&&(totalmark[i]<55))
{gpa[i]+=1.70;}
else if((totalmark[i]>=0)&&(totalmark[i]<50))
{gpa[i]+=0.00;}
gpa[i]+=gpa[i]*credits[i];
gpa[i]+=gpa[i]/credits[i];
}
cout<<"student number:"<<studentnumber;
cout<<"student name:"<<fullname;
for(i=0;i<z;i++)
{
cout<<"\ncourse taken this term"<<courses[i];
cout<<"\ncourse credits "<<credits[i];
cout<<"\ncource mark "<<totalmark[i];
cout<<"\ncourse gpa "<<gpa[i];
cout<<"\n-----------------------\n";
}
default:
cout<<"please select Y or N .Any other key is not acceptable";
break;
}
system("pause");
return 0;
}``````

The last post before yours was from 2007, erbay. I get that you want to show of...whatever that was, but keep in mind that resurrecting old threads isn't something that should be done lightly because it brings that old thread to the front of the list at the cost of pushing recent threads down.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.