Program using arrays

ok I am trying to write a program for school and I have no clue how to do this program. If anyone can help it would be awesome and much appreciated. I will write the things needed below if anyone can help me out!

Thanks

-First declare an array that is large enough to hold at most 25 floating point values.

-You are to ask the user to enter a value in the range of 5 to 25. The program should continue to ask the user to enter a value until he successfully enters a value in this range. You're free to use the function we had in the notes to actually get the proper value.

-You are then to get the indicated number of values from the user and store them in the previously-defined array.

-After you have gotten all the numbers from the user, you are to find and display the sum and the average of all the values the user entered.

-You are then to display the average of all the values the user entered that are greater than 100.0.

-Finally, you are to display all the numbers the user entered, but only display three per line.
show me some code, baby!
I would if I knew how to even start haha
if you are new to c++ this might help -> http://cplusplus.com/doc/tutorial/

else...

-First declare an array that is large enough to hold at most 25 floating point values

if you are not familiar with arrays this might help -> http://cplusplus.com/doc/tutorial/arrays/

-You are to ask the user to enter a value in the range of 5 to 25. The program should continue to ask the user to enter a value until he successfully enters a value in this range. You're free to use the function we had in the notes to actually get the proper value.

I don't think this is a problem, since your teacher gives you permission to use a known function.

-You are then to get the indicated number of values from the user and store them in the previously-defined array.

If I were you, I'd use a for loop with the loop variable iterating through the size of the array and call the input function my teacher gave me for each element of the array. It would be good to have a variable to hold the number of the entries. It's up to you to decide wether the user is asked to provide this number in the beginning or if the data input procedure ends with a special input value, thus setting this number. If you are not familiar with loops this will enlighten you -> http://cplusplus.com/doc/tutorial/control/

-After you have gotten all the numbers from the user, you are to find and display the sum and the average of all the values the user entered.

It would be good to declare a variable to hold the sum and one to hold the average. Initialize both to zero and as you enter the numbers in the array (in the previous step) also add them to sum. Then set average to sum/number_of_entries

-You are then to display the average of all the values the user entered that are greater than 100.0.

This can be done with a simple loop with a simple 'if' command inside. (find more about the 'if' control statement in the same link I gave you for loops above)

-Finally, you are to display all the numbers the user entered, but only display three per line.

Doesn't look too hard to me... But if you do all the above and get stuck here I'll tell you how to do it.

Now, show me some code!
Last edited on
Like this, you won't get any help .
You shall do those by yourself and you shall ask for help when you are stuck.

Well , here are some hints for you ;
Array decleration
double numbers[30] ;
Sum of number ;
1
2
3
4
int i ;
double sum = 0.0;
for( i = 0 ;  i < 30 ; i++) // Suppose there are 30 numbers
sum += numbers[i];


You can do the rest.

Okay here is the code I have so far. I have gotten to where I need to display the numbers greater than 100 and average them together. I will put my code below so maybe yall can help.
Thanks a bunch!


#include <iostream> // include system information

int main()
{
cout << "Darren Hamel CSCE 1020 Lab 11 dch0110@my.unt.edu" << endl << endl;


float A[25], sum, avr;
int num, a, count;

do{
cout << "How many values will you enter (5-25)? ";
cin >> num ;
if (num < 5 || num > 25)
cout << "The value was not between 5 and 25. Try again."<< endl;
}
while (num < 5 || num > 25);

cout << endl;
cout << "Please enter " << num << " values." << endl;

for (a=0; a<25; a++)
cin >> A[a];

sum = 0.0;
avr = 0.0;
for (a=0; a<25; a++)
sum += A[a];
cout << "The sum is: " << sum << endl;
avr = sum / num;
cout << "The average is: " << avr << endl;

return 0;
}
Nice! One thing that needs to be fixed is this:

1
2
for (a=0; a<25; a++)
cin >> A[a];


it should be like this:
1
2
for (a=0; a<num; a++) //<- enter numbers up to num
cin >> A[a];


same here:
1
2
for (a=0; a<25; a++) //<- make this for (a=0; a<num; a++)
sum += A[a];


Another thing:
if you include iostream you must also write below it using namespace std; or write std::cin and std::cout everytime you want to use cin and cout.

Displaying the values that are higher than 100.0 can be done like this:
1
2
3
4
5
for (a=0; a<num; a++)
{
     if (/*...*/) //<- you find what must be placed in here
          cout << A[a] << endl;
}


Displaying 3 values per line can be done like this:
1
2
3
4
5
6
7
for (a=0; a<num; a++)
{
     cout << A[a] << ' ';

     if (/*something that has to do with 'a' being divisible by three...*/)
          cout << endl; //<- newline
}
Thank you so much that was so much help!
You were so much help!
You are welcome! :D
Topic archived. No new replies allowed.