Hi,
I have done this program that calculates the average and letter grade of 10 grades, but i would like the program to ask the user how many grades he want to enter. plz help, I have tried many ways like --
int size = 0;
Cout <<"How many grades will be entered?";
cin>> size;
float a[size], u[size]
But it does not work.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void displayarray(float[],int); //display unsorted array fxn prototype
void displaysarray(float[],int); //display sorted array fxn prototype
void getarray(float[],float[],int); //read array fxn prototype
void sort(float[],int); //sorts the array
float sumfxn(float[],int); //get sum fxn prototype
float getavg(float,int);//fxn prototype for getting average
void chooselgrade(string&, double);
void displaygrades(string&, double);
int _tmain(int argc, _TCHAR* argv[])
{ char choice;
do{
const int arraySize = 10;
float a[arraySize],u[arraySize];
float sum=0, avg=0;
string lettergrade;
getarray(a,u,arraySize); //call read array
sum=sumfxn(a,arraySize);//call sum fxn & returns sum
avg=getavg(sum,arraySize);//call average fxn & returns value for average
sort(a,arraySize);//call sort fxn
displayarray(u,arraySize); //call display array unsorted
displaysarray(a,arraySize);//call display array sorted
chooselgrade(lettergrade, avg);
displaygrades(lettergrade, avg);
cout <<"Do you want to continue? Press e to exit: ";
cin>>choice;
}
while (choice != 'e');
return 0;
}
void sort(float a[],int arraySize) //sort array fxn
{
for(int pass = 0; pass < arraySize; pass++)
{
for(int j = 0; j < arraySize -1; j++)
{
if(a[j] > a[j+1])
{
float hold = a[j];
a[j] = a[j+1];
a[j+1] = hold;
}
}
}
return;
}
void displayarray(float u[],int arraySize) // display unsorted array fxn
{
cout << "\n\nThe unsorted grades are: ";
for(int i=0; i < arraySize;i++)
cout<< u[i] << " ";
return;
}
void displaysarray(float a[],int arraySize)// display unsorted array fxn
{
cout << "\n\nThe sorted grades are: ";
for(int i=0; i < arraySize;i++)
cout<< a[i] << " ";
return;
}
void getarray(float a[],float u[],int arraySize)// reads array and also returns sum fxn
{
for(int i=0; i < arraySize;i++)
{ cout<<"Enter grade"<<i+1<<": ";
cin>>a[i];
u[i]= a[i];
}
return;
}
float sumfxn(float a[],int arraySize) //fxn calulates and returns the sum
{
float sum=0.0;
for(int i=0; i < arraySize;i++)
sum += a[i];
return sum;
}
float getavg(float sum,int arraySize)//fxn that calculates and returns the average
{
float average;
average= sum/arraySize;
return average;
}
void chooselgrade(string& lgrade, double avg)
{
if(avg > 89)
lgrade = "A";
else if
(avg > 84 )
lgrade = "B+";
else if
(avg > 79 )
lgrade = "B";
else if
(avg > 74 )
lgrade = "C+";
else if
(avg > 69 )
lgrade = "C";
else if
(avg >= 0 )
lgrade = "F";
return;
}
void displaygrades(string& lgrade, double avg)
{
cout<<"\n\nThe average of the grades is: ";
cout<<avg <<" and the letter grade is: "<<lgrade <<"\n";
return;
}