arrays

I have this problem that I am working on that i have to create an array of 10 random numbers and then display it, find the highest value, find the lowest value, find the number of odd numbers, total the entire array, and print the duplicate values (only once). and we have to do all of this by writing functions.

void print_Data(float input_Array[], int LENGTH)
{
for (int i = 0; i < LENGTH; ++i) {
cout << input_Array[i] << ", ";
}
}

int arraySum(float input_Array[], float sum)
{
for (int index = 0; index < LENGTH; index++) {
sum == sum + input_Array[index];
}

return sum;
}

this is the code to print the array and the code to total the array, i can t get them to work and I cant figure it out.
Hello mpj91bv,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Also watch your indenting.

Since I see problems in the code you posted it would be helpful to see all the code to see if there any other problems. In this case code that can be compiled and ran is worth more than the pieces you have posted.

In the line sum == sum + input_Array[index]; the "==" means to compare. What yo want is "=" which means set, or set what is on the left to what is on the right. You could also write the line as:
sum += input_Array[index];

The first function looks OK for now. Without more code I can not say why it does not work.

Suggestion: These days type "double" is the preferred floating point type because it has better precision than a "float" and will store a number better.

In "arraySum" it would be better to define "sum" inside the function than to pass it as a parameter.

Hope that helps,

Andy
Topic archived. No new replies allowed.