### help with arrays

I am trying to figure out how to right a program that declares a 10 element double array, using one loop to read 10 values from the user and store them in the array, using a second loop to compute the sum and average of the values on the array, using a third loop to find the largest and smallest values in the array.

This is kind of what I have and I know I'm not exactly close at all..

#include <iostream>
using namespace std;

int main()
{
int large, small;
double myArray[10];

cout << "Please enter a valid number: ";

for (int i=0; i<10; i++);
{
cout << "Enter a number of type double." << endl;
}

for (int i=0;i<10; i++);
{
int sum = 0;
sum += myArray[i];
double avg = sum/10;
}

for (int i = 1; i<10; i++);
{
double small = myArray[0];
double large = myArray[0];
double temp = myArray[i];
large =( large< temp) ? temp : large;
small = (small > temp) ? temp : small;
}
return 0;
}
Last edited on
In your first loop, you need to actually get input from the user. Something like "cin >> myArray[i];" would work.

In the second array, you calculate the average every loop instead of once after the loop. Move it to after the loop and it will work as you intended it to.

In the third loop, you recreate the variables "small", "large", and "temp" every time - move these to before the loop and it should work like you meant it to.

Also, you should put your code between code tags:

[code]
 ``123456`` ``````#include int main() { std::cout << "Hello, world!" << std::endl; }``````
[/code]
Last edited on
Ok so I changed it a little but its not working and Im unsure what to do.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include using namespace std; int main() { int large, small, temp, i; double myArray[10]; cout << "Please enter a valid number: "; for (int i=0; i<10; i++); { cout << "Enter a number of type double." << endl; cin >> myArray[i]; } for (int i=0;i<10; i++); { int sum = 0; sum += myArray[i]; double avg = sum/10; } large =( large< temp) ? temp : large; small = (small > temp) ? temp : small; for (int i = 1; i<10; i++); { double small = myArray[0]; double large = myArray[0]; double temp = myArray[i]; } return 0; } ``````
closed account (3qX21hU5)
Ok first of all I would suggest you use a variable to hold the size of your array like so

 ``1234567`` ``````const int SIZE = 10; int main() { double myArray[SIZE]; // Other stuff }``````

That way you only need to change 1 place if you need to change the size of the array instead of changing the array size and all the of the for loops.

Also delete this line of code we don't need it `int large, small, temp, i;` we will be declaring the variable when we need them in the program which is better then declaring all the variables at the top.

2) You have a semicolon after you for loop on line 15, 23, and 33. That is making it so it only runs the loop once.

3) You sum and average loop looks ok so no need to change that other then the suggestion in number one.

4)
 ``12`` ``````large =( large< temp) ? temp : large; small = (small > temp) ? temp : small;``````

Delete them lines of code not sure what you are trying to do there but you don't need them.

5) Now your largest and smallest need's some work. First lets declare the variable to hold both the largest and smallest right before the for loop (Since we don't want to overwrite them every time through the loop.

So something like

 ``12345`` ``````// Make it a very large number for smallest double smallest = 9999999; // If you are only dealing with positive number make largest 0 // If you have negative numbers make it a very large negative number double largest = 0;``````

Next we need a loop that will check each element in the array to see if it is the smallest or largest element we have seen so far. When it is done with the looping both largest and smallest will hold the correct values.

Here is the whole thing

 ``12345678910`` ``````double smallest = 99999999; double largest = 0; for (size_t i = 0; i != SIZE; ++i) { if (myArray[i] <= smallest) smallest = myArray[i]; if (myArray[i] >= largest) largest = myArray[i]; }``````

After that you should be good and can continue adding to the program. Make sure you understand all the changes and why we made them before you move on. If you have anymore question or need help with anything else just let us know and we would be more then happy to help.
Last edited on
Im sorry Im still stuck on how to do this. This is what I got.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include using namespace std; const int SIZE = 10; int main() { double myArray[SIZE]; for (int i=0; i<10; i++) { cout << "Enter a valid number: " << endl; cin >> myArray[i]; } for (int i=0;i<10; i++) { int sum = 0; sum += myArray[i]; double avg = sum/10; cout<<"The sum is: "<= largest) largest = myArray[i]; cout<<"The smallest is: "<
closed account (3qX21hU5)
Opps skipped the sum for loop but I shouldn't have sorry about that.

First make sure you declare sum and avg outside of the for loop so they stick around and we can use them after the loop is done. Also move your cout statement so that they are called right after the for loop, because we don't want to call them every time we loop through the loop.

 ``1234567`` ``````double sum = 0; double avg = 0; // For loop is here cout<<"The sum is: "<

Next we need to look at what is inside the loop itself.

We are going to delete everything from the loop except this `sum += myArray[i];`. This mean that every time through the loop it will add whatever number is in that element to the sum variable until the loop finishes and then the variable sum will equal the sum of all the numbers.

Now we need to calculate the average outside of the for loop but before the cout statements which would look like this

 ``123`` ``````// Loop is here avg = sum / SIZE; //cout statements here ``````

Now it should be good.

Also your smallest loop is in error also, try and figure out why. Hint: It has something to do with something I just talked about.

I kind of figured it out but when I test it, it says warning: comparison between signed and unsigned integer expressions
closed account (3qX21hU5)
That is coming from the size_t in line 35 I assume `for (size_t i = 0; i != SIZE; ++i)`, if that is the case it it doesn't mean anything is wrong just that there is a possibility you could make something go wrong.

In this case to fix it we just need to go up to our SIZE variable and change it from `const int SIZE = 10;` to `const unsigned SIZE = 10;`. You were getting this warning because you were comparing a unsigned type (size_t) with a signed type (int), and it complained because signed types support negative numbers while unsigned types don't. So you could see if how that might cause a problem sometimes and why the compiler warns about it.
That was it! Thank you!
so
Last edited on
Well here's what I got. I need help to be able to use negative numbers and also it keeps giving me the average and sum for all 10 numbers but I want it to do that for all the numbers combined.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` `````` #include #include using namespace std; const int SIZE = 10; int main() { double myArray[SIZE]; for (int i=0; i<10; i++) { cout << "Enter a valid number: " << endl; cin >> myArray[i]; } for (int i=0;i<10; i++) { int sum = 0; sum += myArray[i]; double avg = sum/10; cout<<"The sum is: "<= largest) largest = myArray[i]; cout<<"The smallest is: "<
Topic archived. No new replies allowed.