0

hi i have following c++ code where i want call function into main, following are my code:

#include <iostream>
#include <numeric>

int main()
{

    using namespace std;

    readData();

    int sumA = accumulate(A, A + sizeof(A) / sizeof(int), 0);
    int sumB = accumulate(B, B + sizeof(B) / sizeof(int), 0);

    cout << ((sumA > sumB) ? "Array A Greater Than Array B\n" : "Array B Greater Than Array A\n");


    return 0;
}

void readData()
{

int A[] = { 1, 1, 8};
int B[] = { 2, 2, 2};
}

I have following error on cli :

test.cpp:3:7: error: storage size of ‘B’ isn’t known
test.cpp:4:7: error: storage size of ‘A’ isn’t known

Where am i wrong here? Thanks

Saraswathi Apavoo
  • 263
  • 2
  • 4
  • 18
  • 1
    '`cout << ((sumA > sumB) ? "Array A Greater Than Array B\n" : "Array B Greater Than Array A\n");`'? What about the case where `sumA == sumB`? – johnsyweb Mar 05 '13 at 13:33
  • how can i compare sums of elements in array..? for above situation how can we writte in better way – d3bug3r Mar 05 '13 at 13:40
  • 1
    Complete lack of understanding of how language works. Get a good book. –  Mar 05 '13 at 14:15
  • Your compiler should emit some more useful errors (such as those here: http://liveworkspace.org/code/2g3oE0$0) but I'd expect anyone with a modicum of [tag:c++] experience to be able to work it out from the errors you gave there. `A` and `B` are not declared in `main()` and therefore cannot be used in that context. You need to rewrite this code to make more sense in your chosen programming language. – johnsyweb Mar 05 '13 at 14:32

2 Answers2

6

The variables A and B are local to the function readData and not accessible from any other function.

Either declare them as global variables (not recommended) or as local variables in main and pass them as arguments to the readData function.

I also recommend you use std::vector instead of plain arrays.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

First of all, be careful with taking the size of arrays in C and C++. Read here for more information: http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/

However do use std::vector instead like this.

#include <iostream>
#include <vector>
#include <numeric>

typedef std::vector<int> int_vec_t;

//Call by reference to set variables in function
void readData(int_vec_t& v1, int_vec_t& v2)
{
  v1 = int_vec_t{1,1,8}; //This only works for C++11
  v2 = int_vec_t{2,2,2};
}

void readUserData(int_vec_t& v)
{
  for(;;)
  {
    int val;
    std::cin>>val;
    if(val == 0) break;
    v.push_back(val);
  }
}

int main()
{
    using namespace std;

    int_vec_t A;
    int_vec_t B;

    readData(A,B);
    //Or
    readUserData(A);
    readUserData(B);

    int sumA = accumulate(A.begin(), A.end(), 0); //Then use iterators
    int sumB = accumulate(B.begin(), B.end(), 0);

    cout << ((sumA > sumB) ? "Array A Greater Than Array B\n" : "Array B Greater Than Array A\n");

    return 0;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
AxelOmega
  • 974
  • 10
  • 18