Help on Arrays program



Requirements for the program:
Create an array of 30 random double values in the range of 50-100. Name the Array Grades. Fill the array with grades from x assignments. Display the values of the array. Find the average of the values. Search the array for the highest value. Search the array for the lowest value.
*Sort the grades from lowest to highest

PROBLEM:
I am getting an error when I run my program. Any suggestions on how I should fix it would be greatly appreciated..

Also not sure how to sort the grades from lowest to highest...

Here is what I have done so far...
//////////////////////////////////////////

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

double rand50_100()
{

double value = 50 + (rand() % 51);

}
void loadValues(double grades[],int len)
{
int i;

for(i=0;i<len;i++)
{
grades[i] = rand50_100();
}

}

void printValues(double grades[],int len)
{
int i;
for(i=0;i<len;i++)
{
cout<<grades[i]<<" ";
}
cout<<endl;
}

void average(double grades[],int len)
{
int i;//sum=0;
double sum=0,avg;
for(i=0;i<len;i++)
{
sum = sum + grades[i];
}
avg=sum/len;
cout<<"The average : "<<avg<<endl;
}

void highest(double grades[],int len)
{
int i;
double max=0;
for(i=0;i<len;i++)
{
if(grades[i]>=max)
{
max = grades[i];
}
}
cout<<"The highest value of the grades: "<<max<<endl;
}

void lowest(double grades[],int len)
{
int i;
double min;
min=grades[0];
for(i=0;i<len;i++)
{
if(grades[i]<=min)
{
min = grades[i];
}
}
cout<<"The lowest value of the grades: "<<min<<endl;
}

int main()
{
srand(time(NULL));

//step 1
int x;
cout<<"Enter the length of the array that you want:"<<endl;
cin>>x;
double grades[x];//initializing the array.

//step 2
loadValues(grades,x);

//step 3
printValues(grades,x);

//step 4
average(grades,x);

//step 5
highest(grades,x);

//step 6
lowest(grades,x);

return 0;
}
Nice job so far!

When posting code, please use code tags. While editing your post, highlight the code and click the <> button to the right of the edit window.

It's helpful to compile with warnings enabled. When I compiled your code with warnings enabled it found that rand50_100() doesn't return a value. It should be:
1
2
3
4
5
6
double
rand50_100()
{
    double value = 50 + (rand() % 51);
    return value;
}

You could do it with one line (return 50 + (rand() % 51);) but it's a good habit to put the return value in a variable first so you can see it with a debugger if necessary.

double grades[x]; //initializing the array.
This is actually illegal C++, although it's supported by some compilers as an extension. It's better to use vectors instead if you've learned about them.

Also not sure how to sort the grades from lowest to highest...
See http://www.cplusplus.com/reference/algorithm/sort/. To sort your array:
1
2
3
#include<algorithm>
...
sort(grades, grades+x);



Last edited on
Thank you!
Does anyone know how to sort the grades from lowest to highest ? I have no idea..
I addressed this in the last few lines of my post.
Topic archived. No new replies allowed.