Help on Arrays program...


How would I sort the grades from lowest to highest? haven't been able to figure that out yet.

This is what I have so far:

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

double rand50_100()
{
//double value;


//****Is there a better way to write the following line? by better I mean a simpler/more concise way ?
double value = 50 + static_cast <double> (rand()) /( static_cast <double>

(RAND_MAX/(100-50)));
return value;
}
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<<"Average of the arr: "<<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 grade: "<<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 grade: "<<min<<endl;
}

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

//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;
}
Last edited on
You have posted here before, and have provided your code up to that point. That is good.

Now you create a post simply suggesting that we do your homework for you. We won't do that.

Ask questions. Show us what you are struggling with and we can help.

I think the homework itself is very straight-forward. There are literally step-by-step instructions on how to solve the problem. The first step even gives you actual code (almost).

So, here is the help I can give you to get through step 1.

1
2
3
4
5
6
7
int main()
{
     const int x(30);
     double Grades[x];

     /* rest of your code here */
}
Ok how would I sort the grades from lowest to highest? haven't been able to figure that out yet.

This is what I have so far:

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

double rand50_100()
{
//double value;


//****Is there a better way to write the following line? by better I mean a simpler/more concise way ?
double value = 50 + static_cast <double> (rand()) /( static_cast <double>

(RAND_MAX/(100-50)));
return value;
}
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<<"Average of the arr: "<<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 grade: "<<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 grade: "<<min<<endl;
}

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

//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;
}
closed account (E0p9LyTq)
1
2
3
cout<<"Enter the length of the array that you want:"<<endl;
cin>>x;
double grades[x];//initializing the array. 

The current C++ standard will not allow that.

You want a variable sized array you either use a container from the STL, such as std::vector, or use a pointer to heap memory by operator new[].

http://www.cplusplus.com/doc/tutorial/dynamic/
When you post code, please use code tags. Highlight your posted code and click the Format button that looks like "<>". Or click the button first and paste your code between the generated tags. Using the code tags will make your code easier to read and comment on.

In the future, please don't completely delete/replace your earlier posts. It makes responses appear cryptic. My initial response refers to the steps in your homework assignment which has now been removed.

The original steps referred to a maximum of 30 grades or some such, and stated that you needed an array double Grades[x]. Based on that step, I provided a couple of lines of code that are bit clumsy, but would meet that requirement. Without the context of your original post, my suggestion seems a bit akward at best.

//****Is there a better way to write the following line? by better I mean a simpler/more concise way ?
double value = 50 + static_cast <double> (rand()) /( static_cast <double>(RAND_MAX/(100-50)));


For starters, you could simply generate random integers and cast them to doubles.

double value = 50 + (rand() % 51); // implicit cast from int to double

Using % 51 will yield values from 0 - 50 (inclusive), so adding 50 will give you all values from 50 - 100 (inclusive).

Again, because you removed your initial post, current readers will not understand why you need values here that are doubles and why the range from 50 - 100.

If your teacher requires something more that random integers casted to doubles, then you can improve your function later after you get the rest of the program working. My guess is that this code will be sufficient.

Ok how would I sort the grades from lowest to highest? haven't been able to figure that out yet.


Are you required to sort the values? I don't believe that's what the assignment said. (Again, don't delete your initial post.) The comments in the code only say you need to find the average, highest and lowest values. You don't need to sort anything to find those values.

Last edited on
Topic archived. No new replies allowed.