-2
#include <iostream>
using namespace std;

int main (int argc , char** argv) {
const int size = 5;
int arr [size]={};
int y = arr[0];
int x = arr[1];
int c = arr[2];
int v = arr[3];
int b = arr[4]; 

std::cin>>y,x,c,v,b;
int sum =0;
for (int i =0; i<size;i++){
sum+=arr[i];}
std::cout<<sum/size;
}

Don't know what went wrong, keep getting 0 as solution. Its compiling fine but It seems to have a mistake somewhere.

  • 2
    Try reading a basic introductory textbook on C++, rather than relying on guesswork and prayer. There are so many things wrong (and invalid assumptions you're making about what the code will do) that any explanation would be meaningless to you before you have learned the basics that any basic introduction can give you. – Peter Apr 08 '17 at 13:23
  • @Peter Such comments are best accompanied by a link to our [list of good C++ books](http://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO Apr 08 '17 at 13:47
  • Thanks Agnew - didn't have the link handy. – Peter Apr 08 '17 at 13:55
  • Do you get warnings when compiling? For example "warning: right-hand operand of comma has no effect". If you do not get warnings, read up on build configuration and increase the warning level. How many integers is your program asking you to input? Should be 5, shouldn't it? If I compile your code, I only can enter one number. Those are hints which could lead you to one problem or another and you should mention them here. – Yunnosch Apr 08 '17 at 14:00

2 Answers2

0

Because you are only inputing values to y,x,c,v and b. What you are doing is just inputing values to the integers, separated from the array. You need some sort of referencing.

int y=arr[0];
...

here you make the integers equal the value of corresponding array, which values are 0 by default.

Notice even the way you do cin is wrong.

Do this instead: std::cin >> y>> x>> c>> v>> b;

Now you have cin to these 5 integers. But the value of the elements of the array is still 0!

You can do something like assigning the integers back to the array.

arr[0]=y;
...

But this is just so much work. An alternative way is to use pointers. It's the better solution. Here's a tutorial about pointer: http://www.cplusplus.com/doc/tutorial/pointers/

int *y = &arr[0];
Veii Xhen
  • 334
  • 2
  • 12
0

You are attempting to assign the input to integer variables, do that this way:
std::cin>>y>>x>>c>>v>>b;

But then only these variables have those values, there is no connection to the array elements.

In order to fix that, change the integer variables to integer references:

int &y = arr[0];  
int &x = arr[1];  
int &c = arr[2];  
int &v = arr[3];  
int &b = arr[4];  

That makes them be identical to the variables (or in this case array members).
I.e. if you change one of them the other changes too (actually its just two names for the same piece of memory).

Your code seems to take some inefficient detours, but is otherwise doing the job.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54