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;
}

Holy crap, it feels like every question these last few days has been about runtime sized arrays. :icon_rolleyes:

>But it does not work.
Right. C++ requires that the size of an array be a compile-time constant. You can't use the value of a variable unless it's also declared const.

The recommended solution, if you really must have a runtime size, is to use the std::vector class instead of an array:

#include <iostream>
#include <vector>

int main()
{
  int size;

  std::cout<<"How many thingies do you want? ";
  std::cin>> size;

  std::vector<int> v ( size );

  // You can largely use v like an array now
}

Do anyone have an idea how i can do this program using pointers. Just an explanation might help thanks.

Normally I'd tell you to search the forum since I've written the same damn code a dozen times in the last few days, but here's a quickie to match the vector code:

#include <iostream>

int main()
{
  int size;

  std::cout<<"How many thingies do you want? ";
  std::cin>> size;

  int *p = new int[size];

  // You can largely use p like an array now

  delete[] p;
}
Be a part of the DaniWeb community

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