#2d array help

58 messages · Page 1 of 1 (latest)

rustic plaza
#

Can somebody please help fix this code?

#include <iostream>

using namespace std;

int[][] setElem(int[][] arr, int xpos, int ypos, int val){
    arr[ypos][xpos] = val;
    return arr;
}
int main() {
    int width;
    int height;
    cout << "Please enter the width of your terminal!" << endl;
    // TODO: Verify user input
    cin >> width;
    cout << "Please enter the height of your terminal!" << endl;
    cin >> height;
    int frame[height][width];
    for (int y = 0; y < sizeof(frame) / sizeof(frame[0]); y++){
        for (int x = 0; x < sizeof(frame[y]) / sizeof(frame[y][0]); x++){
            frame[y][x] = 0;
        }
    }

    for (int y = 0; y < sizeof(frame) / sizeof(frame[0]); y++){
        for (int x = 0; x < sizeof(frame[y]) / sizeof(frame[y][0]); x++){
            cout << frame[y][x] << " ";
        }
        cout << endl;
    }
    
    frame = setElem(frame, 0, 0, 1);
    cout << "New array!" << endl;
    
    for (int y = 0; y < sizeof(frame) / sizeof(frame[0]); y++){
        for (int x = 0; x < sizeof(frame[y]) / sizeof(frame[y][0]); x++){
            cout << frame[y][x] << " ";
        }
        cout << endl;
    }
}

Here are the errors

easy.cpp:5:4: error: structured binding declaration cannot have type ‘int’
    5 | int[][] setElem(int[][] arr, int xpos, int ypos, int val){
      |    ^~
easy.cpp:5:4: note: type must be cv-qualified ‘auto’ or reference to cv-qualified ‘auto’
easy.cpp:5:4: error: empty structured binding declaration
easy.cpp:5:6: error: expected initializer before ‘[’ token
    5 | int[][] setElem(int[][] arr, int xpos, int ypos, int val){
      |      ^
easy.cpp: In function ‘int main()’:
easy.cpp:31:13: error: ‘setElem’ was not declared in this scope
   31 |     frame = setElem(frame, 0, 0, 1);
      |             ^~~~~~~
prisma sundialBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

coarse cedar
#

er

#

you could not return anything from the setElem function and not assign anything to its result

rustic plaza
#

It's complaining about my function header I think

coarse cedar
#

since arr decays to a pointer anyways

coarse cedar
rustic plaza
#

how do I return a 2d array then?

coarse cedar
#

int** perhaps

#

or use std::vector

#

iirc you also cannot have an int[][] parameter

rustic plaza
#

ye that helped

#

only 1 error

#
easy.cpp: In function ‘int main()’:
easy.cpp:31:21: error: cannot convert ‘int (*)[width]’ to ‘int**’
   31 |     frame = setElem(frame, 0, 0, 1);
      |                     ^~~~~
      |                     |
      |                     int (*)[width]
easy.cpp:5:21: note:   initializing argument 1 of ‘int** setElem(int**, int, int, int)’
    5 | int** setElem(int** arr, int xpos, int ypos, int val){
      |               ~~~~~~^~~
coarse cedar
#

hmm one sec

rustic plaza
#

ill send code

#
#include <iostream>

using namespace std;

int** setElem(int** arr, int xpos, int ypos, int val){
    arr[ypos][xpos] = val;
    return arr;
}
int main() {
    int width;
    int height;
    cout << "Please enter the width of your terminal!" << endl;
    // TODO: Verify user input
    cin >> width;
    cout << "Please enter the height of your terminal!" << endl;
    cin >> height;
    int frame[height][width];
    for (int y = 0; y < sizeof(frame) / sizeof(frame[0]); y++){
        for (int x = 0; x < sizeof(frame[y]) / sizeof(frame[y][0]); x++){
            frame[y][x] = 0;
        }
    }

    for (int y = 0; y < sizeof(frame) / sizeof(frame[0]); y++){
        for (int x = 0; x < sizeof(frame[y]) / sizeof(frame[y][0]); x++){
            cout << frame[y][x] << " ";
        }
        cout << endl;
    }
    
    frame = setElem(frame, 0, 0, 1);
    cout << "New array!" << endl;
    
    for (int y = 0; y < sizeof(frame) / sizeof(frame[0]); y++){
        for (int x = 0; x < sizeof(frame[y]) / sizeof(frame[y][0]); x++){
            cout << frame[y][x] << " ";
        }
        cout << endl;
    }
}
coarse cedar
#

i forgor how c-arrays worked

rustic plaza
#

c++

coarse cedar
#

yeah those are called C-arrays

rustic plaza
#

Makes sense now that I think about it

#

C-Arrays are prolly the fastest tho right

#

cuz I plan on writing some very fast code to process billions of elements a second

stuck current
#
int[][] setElem(int[][] arr, int xpos, int ypos, int val){
    arr[ypos][xpos] = val;
    return arr;
}

looks like java style arrays

rustic plaza
#

Prolly cuz I learned java before this

coarse cedar
prisma sundialBOT
#
What Is a VLA, and Why Is It "Bad"?

A Variable Length Array (VLA) is an array where the size is not constant and depends on a variable.

Example
int size = rand();
int vla[size]; // VLA of type int[size]
int not_vla[10]; // regular array of type int[10]
constexpr int size = 10;
int arr[size]; // also not a VLA, of type int[10]
Why Are VLAs "Bad"?

VLAs have poor compiler support and can lead to unsafe code. The core issue with VLAs is that the compiler doesn't know the size of the stack frame. Without warning flags like -Wvla, it can be easy to create a VLA by accident, even in C++ with some compilers.

Compiler Support

:white_check_mark: available since C99
:no_entry: not available in C++ at all
:no_entry: was never supported by MSVC
:warning: optional feature since C11
:warning: supported as non-standard extension by GCC, clang

coarse cedar
#

oh also you have a VLA

coarse cedar
#
- using namespace std;
rustic plaza
#

well what do u think I should do

stuck current
#

you shouldn't use a VLA, use something like std::vector

rustic plaza
#

I don't know the length of the array

#

Can you explain vectors

prisma sundialBOT
coarse cedar
#
- int** setElem(int** arr, int xpos, int ypos, int val){
+ std::vector<std::vector<int>> setElem(std::vector<std::vector<int>> arr, std::size_t xpos, std::size_t ypos, int val){
#

actually why not just do the actions of setElem within the main function?

rustic plaza
#

wdym

stuck current
#

instead of frame = setElem(frame, 0, 0, 1);, just do frame[0][0] = 1;

coarse cedar
#

yes

rustic plaza
#

I just wanted to practice writing functions

coarse cedar
#

ah

rustic plaza
#

ofc it isnt optimal or efficent

stuck current
#

it's a lot simpler without VLAs

rustic plaza
#

We can use vectors instead but im not sure if they are faster

coarse cedar
#

they aren't slower

stuck current
coarse cedar
#

because its all 1-dimensional in memory right

rustic plaza
#

sizeof(arr) / sizeof(arr[0]) won't work?

stuck current
#

what do you think sizeof(int[]) is

rustic plaza
#

oh

#

nvm

#

makes sense

#

cuz the inner arrays might have different numbers of bytes

#

either way I think vectors are the way to go

coarse cedar
#

they store their size internally and you can access it through .size()

rustic plaza
#

!sloved

#

!solved