user20412196
user20412196

Reputation: 11

how do I have a function create a 2D array, return the array and size variable.. and then push it to a printArray function?

So this is what I have

#include <iostream>
using namespace std;

int createArray(){
int array[10][10] = {0};
 int x, y;
 int size = 0;

 while(x != 0 && y !=0){
    cin >> x >> y;
        while( x<0 || x>10 || y<0 || y>10){
            cout << "error" << endl;
            cin >> x >> y;
        }
    if(x>0 && y >0){
        array[x-1][y-1] = 1;
    }
    if(size < x){
        size = x;
    }
    if (size < y){
        size = y;
    } 
    
 } 
return(array, size);
}

int printArray(int array[10][10],int size){
    int i, j;
    for(i=0; i<size; i++){
        for(j=0; j<size; j++){
            cout << array[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}


int main() {
/* 
What happens here? I thought I could do something like..

auto [arrayA, sizeA] = createArray(); 
printArray(arrayA, sizeA);
but that's not working

*/
}

I've been messing around for the last few hours, and it hasn't been working. I wouldn't be surprised if there is something sloppy left over in the code from my different attempts, but I am a little brain fried, ha! This is an assignment for school, and I have a lot more functions to make work similarly, but I can't seem to make it happen. I can write them straight through in main() and it works, but I specifically need to do it with functions.

Upvotes: 1

Views: 33

Answers (1)

pm100
pm100

Reputation: 50190

you cannot do this in c++

return(array, size);

well you can, but it doesnt do what you think. It just returns the size (lookup up comma operator)

Many many times in c++ assigments there are stupid rules about what you can and cannot use.

You can used std::pair to return 2 things.

I would use a vector of vector rather that the fixed size array

Upvotes: 1

Related Questions