wrong code output

This code is supposed to print out mean, max and min value of an array in a structure but it is returning wrong values. Can someone please tell me what I am doing wrong. Please note that no changes is to be made on the main. Thanks


#include <iostream>
#include <cstdlib>

typedef struct {
int data[10];
float mean;
int min;
int max;
} A;
A compute(A a)
{
int i;
int j;
int data[10];
int min = data[0];
int max = data[0];
float mean = 0;
float sum = 0;

for (i=0; i<=10; i++)
{
sum += data[i];
mean = sum/10;
}
for (j=0; j<=10; j++)

if (data[j] < min)
{ min = data[j];
}

else if (data[j] > max)
{
max = data[j];
}
}

int main()
{
srand(NULL);
A a;
for (int i = 0; i < sizeof(a.data) / sizeof(a.data[0]); i++)
{
a.data[i] = rand();
std::cout << a.data[i] << std::endl;
}
a = compute(a);
std::cout << "mean: " << a.mean << std::endl;
std::cout << "min: " << a.min << std::endl;
std::cout << "max: " << a.max << std::endl;
return 0;
}
Last edited on
...but it is returning wrong values

What are the wrong values?
What values is the program supposed to display?

Last edited on
the array of ten numbers is randomly generated by the computer.
values to be displayed are mean, maximum number and minimum number of the array.

the output of the code is giving me wrong numbers for all the parameters when compared with the list of numbers generated.
Also, on your for loops: for (i=0; i<=10; i++) should be like for (i=0; i<10; i++). The reason is because the array element begins at 0 and your size of the array is 10. The total number of elements from 0 to 9 is 10.
Furthermore, on your A compute(A a) , what value are you planning to return to a = compute(a); (on main function)?
Last edited on
I am trying to return min, max and mean to compute (a)
Could you post the full instructions of the assignment?
Last edited on
Supposed that the code is given:

#include <iostream>
#include <cstdlib>
typedef struct {
int data[10];
float mean;
int min;
int max;
} A;
A compute(A a);
int main()
{
srand(NULL);
A a;
for (int i = 0; i < sizeof(a.data) / sizeof(a.data[0]); i++)
{
a.data[i] = rand();
std::cout << a.data[i] << std::endl;
}
a = compute(a);
std::cout << "mean: " << a.mean << std::endl;
std::cout << "min: " << a.min << std::endl;
std::cout << "max: " << a.max << std::endl;
return 0;
}
Write the function, compute(A a), to find the smallest/highest/mean value in the
array of the structure A, and return the structure.
Firstly, don't post the same question in two places.

Basically, your problem is that you don't understand what a struct is and how to use one. Stop trying to solve this problem. Go back to whatever you were meant to learn about struct from, and learn about struct.
Thanks.
I have amended my code to a.data, a.min, a.max and a.mean in the compute (a) function and it is still returning same values.

If you can just break it down so I know what I am doing wrong so I understand the struct better.
The compute function is meant to return something, isn't it? Your compute function doesn't return anything.
Please how do i get it to return the three values?
A function can return only one object.
Write the function, compute(A a), to find the smallest/highest/mean value in the
array of the structure A, and return the structure.

Your function returns object of type A. The A is a struct. It has member variables. While the function returns only one A object, the object contains many values, including the ones that you want.


Why do you use the typedef T U; syntax where the T is an unnamed struct type defined on the spot?
That syntax is used in C, but in C++ one can write:
1
2
3
struct A {
  // members
};
Thanks guys for making me think. I figured it out and returned the value.

Topic archived. No new replies allowed.