### Pass a 2D vector to a function

Well, I decided to try and write a simple spreadsheet, so created a single cell first as a struct.
 ``123456`` ``````struct Cell { string text; int number; // Will add in more.. float, double etc. };``````

Then created a vector..
 ``1234`` ``````const int Spreadsheet_Width = 53; const int Spreadsheet_Height = 76; struct Cell Spreadsheet[Spreadsheet_Height][Spreadsheet_Width];``````

I would like to be able to send the vector to a function, so I can initialize the contents of it, but, I can't figure out a way to do it.

Tried `Init( Spreadsheet);` in main, with the function created as..
`void Init(vector <Cell>& Spreadsheet[int Spreadsheet_Height][int Spreadsheet_Width])` but I get red squiggles under some of the wording. I have written programs that send plain vectors with no problem, but a 2D one is stumping me. Could someone point me in the correct direction on getting this to work? If you need the whole program, I can upload it to dropbox where it can be DL'd. Right now, it's 10,418 bytes and growing. Thanks for any help.
This is an ordinary 2-D array:
 `` `` `` Cell Spreadsheet[Spreadsheet_Height][Spreadsheet_Width];``

The equivalent declaration for a 2-D vector would look like this:
 `` `` `` vector> Spreadsheet(Spreadsheet_Height, vector(Spreadsheet_Width));``

Example:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include using namespace std; struct Cell { string text; int number; // Will add in more.. float, double etc. }; void print (const vector>& Spreadsheet) { for (const auto & row : Spreadsheet) { for (const auto & cell : row) { cout << cell.number << ' '; } cout << '\n'; } } void init(vector>& Spreadsheet) { for (size_t i=0; i> Spreadsheet(Spreadsheet_Height, vector(Spreadsheet_Width)); init(Spreadsheet); print(Spreadsheet); }``````

 I would like to be able to send the vector to a function, so I can initialize the contents of it

You might add a constructor to the struct `Cell`, for example to set the numbers to zero. The string is by default initialised as an empty string.
@Chervil

Thanks, that was a big help. I was now able to initialize the full spreadsheet, but I'm having a small problem trying to figure out how to print only a section of it. On the console, I need to print out only the first 19 columns, and the first 18 rows. Then using the arrow keys, I can shift the columns left or right and the rows, up or down. So, if the console is showing columns 0 to 19, and I want to press the right arrow, I now need to show column 0, then columns 2 to 20. Pressing right again will display column 0, then columns 3 to 21. Of course, the same applies to the up/down display, as well. It shows rows 0 to 18, then pressing down, shows row 0 and rows 2 to 19, etc.
The reason for always showing row 0 and column 0, is because they are initialized with 'A' to 'AZ' for row 0, and 1 to 75 for column 0, and I want those always displayed.

I'm not sure how I can stop the entire spreadsheet vector from being displayed.

 On the console, I need to print out only the first 19 columns, and the first 18 rows.

I'm sure there are many ways to do something like that.
This is part of some code I was playing around with.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````const int Spreadsheet_Width = 53; const int Spreadsheet_Height = 76; const int Height = 5; const int Width = 7; struct Rectangle { int top; int left; int bottom; int right; Rectangle() : top(0), left(0), bottom(Height), right(Width) { } void move_left() { if (left > 0) { left--; right = left + Width; } } void move_right() { if (left+Width < Spreadsheet_Width) { left++; right = left + Width; } } void move_up() { if (top > 0) { top--; bottom = top + Height; } } void move_down() { if (top+Height < Spreadsheet_Height) { top++; bottom = top + Height; } } }; void print_window(const vector>& Spreadsheet, const Rectangle& rect) { for (int i=rect.top; i

Elsewhere in the code I declare a Rectangle object and use the keyboard to trigger the calling of the member functions such as `rect.move_right();` and then call the print function, `print_window(Spreadsheet, rect);` along with a few other bits and pieces to position the cursor on the console and so on.

Hope those fragments are of some use, the complete code was a bit of a mess, so it seemed unnecessary to post it.
@Chervil
The last bit of code you showed me looks nice, but I'm just learning about this 2d vector stuff. I'm not sure how to declare the Rectangle object, nor call the member functions. When I tried plugging in the Rectangle struct and print_window function, I get the red squiggle line under Rectangle in the print_window function.
Anyway, I'm having problems with getting it to compile before trying to add the new coding into it.
I'm getting a couple of errors when I try to initialize the Spreadsheet vector
 ``12`` ``````Error 2 error LNK2019: unresolved external symbol "void __cdecl Init_Spreadsheet(class std::vector >,class std::allocator > > > &)" (?Init_Spreadsheet@@\$\$FYAXAAV?\$vector@V?\$vector@UCell@@V?\$allocator@UCell@@@std@@@std@@V?\$allocator@V?\$vector@UCell@@V?\$allocator@UCell@@@std@@@std@@@2@@std@@@Z) referenced in function "int __cdecl main(void)" (?main@@\$\$HYAHXZ) C:\Users\staff\Documents\Visual Studio 2012\Projects\Spreadsheet\Spreadsheet\Spreadsheet.obj Spreadsheet Error 1 error LNK2028: unresolved token (0A0004CF) "void __cdecl Init_Spreadsheet(class std::vector >,class std::allocator > > > &)" (?Init_Spreadsheet@@\$\$FYAXAAV?\$vector@V?\$vector@UCell@@V?\$allocator@UCell@@@std@@@std@@V?\$allocator@V?\$vector@UCell@@V?\$allocator@UCell@@@std@@@std@@@2@@std@@@Z) referenced in function "int __cdecl main(void)" (?main@@\$\$HYAHXZ) C:\Users\staff\Documents\Visual Studio 2012\Projects\Spreadsheet\Spreadsheet\Spreadsheet.obj Spreadsheet``````

My function looks like..
 ``1234567891011121314151617181920212223`` ``````void init_Spreadsheet(vector>& Spreadsheet) { for (size_t i=0; i0 && j<27)) { Spreadsheet[i][0].number = i; // Assign 0 to 75 to start of each Spreadsheet[0][j].text = 'A'+ (j-1);// Spreadsheet[0][1] to Spreadsheet[0][26] eqauals 'A' to 'Z' } if( i == 0 && j > 26) { Spreadsheet[0][j].text = "A"; Spreadsheet[0][j].text += 'A'+(j-27);// Spreadsheet[0][27] to Spreadsheet[0][53] eqauals 'AA' to 'AZ' } else Spreadsheet[i][j].number = 7; } } Spreadsheet[0][0].text = ""; }``````
]
Those are linker errors.

I notice the code has the function named
 `` `` ``init_Spreadsheet``
but the error messages refer to
 `` `` ``Init_Spreadsheet``
One has an upper-case 'I', the other a lower-case 'i'.
@Chervil

Thanks. I totally missed that. I kept looking inside the function and trying to figure what I did wrong there. Now it compiles and displays correctly on screen. I'll call this completed and start another thread if or when, I run into more problems.
 I'm not sure how to declare the Rectangle object, nor call the member functions. When I tried plugging in the Rectangle struct and print_window function, I get the red squiggle line under Rectangle in the print_window function.

I tried to tidy up my code a bit so I could post it here, and ran into some problem with the Rectangle struct. I didn't look too deeply int the details, but there was a name clash with something in the windows.h header. To fix that, I added a namespace. Since this is nothing special, I just called it xyz.

The below code may or may not compile on your system, it uses some non-standard stuff, and some windows functionality.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185`` ``````#include #include #include #include #include #include #include void gotoxy( int column, int line ) { COORD coord; coord.X = column; coord.Y = line; SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord ); } const int Spreadsheet_Width = 53; const int Spreadsheet_Height = 76; const int Left = 331; const int Right = 333; const int Up = 328; const int Down = 336; const int Height = 20; const int Width = 12; namespace xyz { struct Rectangle { int top; int left; int bottom; int right; Rectangle() : top(0), left(0), bottom(Height), right(Width) { } void move_left() { if (left > 0) { left--; right = left + Width; } } void move_right() { if (left+Width < Spreadsheet_Width) { left++; right = left + Width; } } void move_up() { if (top > 0) { top--; bottom = top + Height; } } void move_down() { if (top+Height < Spreadsheet_Height) { top++; bottom = top + Height; } } }; } // --- end namespace xyz --- struct Cell { std::string text; int number; // Will add in more.. float, double etc. }; std::string base26(unsigned int n) { const char * alphabet = "ZABCDEFGHIJKLMNOPQRSTUVWXYZ"; std::string result; result = alphabet[n % 26 + 1]; n /= 26; while (n) { result = alphabet[n % 26 ] + result; n--; n /= 26; } return result; } void print (const std::vector>& Spreadsheet) { std::ostringstream oss; for (const auto & row : Spreadsheet) { for (const auto & cell : row) { oss.clear(); oss.str(""); oss << cell.text << cell.number; std::cout << std::setw(4) << oss.str(); } std::cout << '\n'; } } void init(std::vector>& Spreadsheet) { for (size_t i=0; i>& Spreadsheet, const xyz::Rectangle& rect) { std::ostringstream oss; for (int i=rect.top; i> Spreadsheet(Spreadsheet_Height, std::vector(Spreadsheet_Width)); init(Spreadsheet); // print(Spreadsheet); xyz::Rectangle rect; gotoxy(0,0); print_window(Spreadsheet, rect); // Press ESCAPE key to exit int key = 0; while ((key = getkey() ) != 27) { if (key == Left) rect.move_left(); // cout << "left\n"; else if (key == Right) rect.move_right(); // cout << "right\n"; else if (key == Up) rect.move_up(); // << "up\n"; else if (key == Down) rect.move_down(); // << "down\n"; // else std::cout << key << '\n'; gotoxy(0,0); print_window(Spreadsheet, rect); } }``````

@Chervil

That works like a charm, and looks good too.

If you would like to see what my program is looking like so far, I'll put it in dropbox.

`https://www.dropbox.com/s/gt1hi5aiql7as9w/Spreadsheet.cpp?dl=0 `

It uses a function called Grid that I wrote to display the spreadsheet cells, color, and of course, a gotoXY function

Next, I have to write functions to allow filling and updating, the cells. I've got high confidence I'll be able to.

Thanks you SO MUCH for your help. You went far above simple suggestions and nudges. Really appreciate it.

I do program only for fun and learning. And I've learned quite a bit from you.
Last edited on
@Whitenite1
Thanks, you're welcome. Actually, I've learned stuff from you too. I think the `gotoxy()` function originally came from one of your posts, and a number of other things too. I learn from everyone actually. Just trying to think of answers to some of the questions on this forum is a learning process in itself.

Nowadays I'm programming only for fun or things which interest me. It just happened that something about your project caught my attention so I got a bit caught up in it. I didn't intend my code to be quite so long, but I wanted it to be something which worked, shown in context.

Thanks for the dropbox link, I'll take a look.
@Chervil

Thank you. Oh, by the way, I haven't written instructions yet on the spreadsheet program use. All it can do right now, is add a 'XX' to a cell. To specify which cell, you first press '@' symbol, then a small or capital 'A' or up to an 'AZ' then the row 1 to 75. Press 'Enter'. A 'XX' is then placed in the corresponding cell. Use the cursor keys to shift the cells in the desired directions. The column letter and row numbers, always stay on screen.

Topic archived. No new replies allowed.