1

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Addendum: Arrays are awesomely stupid and limited in ability. Their behaviour made sense back in the 1970s when you didn't have the resources to sling blocks of data around, but now it's counter-intuitive. For example, in int printArray(int array[10][10],int size) you probably think array is an array. It's not. It's been decayed to a pointer, and this causes a lot of new programmers a lot of trouble when they try to compute its size.
Thank you for your help. I'll explore the std::pair idea, but I am definitely going to email the teacher about the vector idea. I'm trying to follow the rules to the T, but some of them do feel a little silly.
@user20412196 Do what you have to do to pass the class. And pray to whatever deities you believe in that you have an instructor enlightened and modern enough to allow use of C++ language constructs from the 1990s instead of C constructs from the 1970s when learning C++. Otherwise, why not take a class in C? That way you're at least learning the right language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.