error in passing 2-d array to function - c++

The program is just for passing complete 2-d array to function.I am able to run the problem by hook or by crook but i didnt understood.I have written a program which i should have written threotically and which i have written to make it working(in comments)
can anyone please explain me this issue??
#include<iostream>
#include<conio.h>
void print(bool *a);
using namespace std;
int main()
{
bool arr[3][3]={1,1,1,1,0,0,0,1,1};
print(arr[0]);//**This IS working but why we need subscript 0 here only print(arr) should work?..**
getch();
return 0;
}
void print(bool *a)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<*(a+i*3+j)<<"|";//**cant we use cout<<a[i][j] here??In 1 d array it is working fine**
}
cout<<"--";
}
}

void print(bool *a)
should be
void print(bool a[][3])
the compiler needs to know the size of second dimension in order to compute offset for addressing.
void print(bool a[][3], int rowSize)
{
for(int i=0;i<rowSize;i++)
{
for(int j=0;j<3;j++)
{
cout<<a[i][j]<<"|";
}
cout<<"--";
}
In C++, you should prefer using vector<vector <bool> > over 2D dynamic array arr.

Use:
void print(bool a[][3])
which is the correct prototype if you want to call print(arr);
Then you can use a[i][j] to access array elements in the print function body.
arr is an array of array 3 of bool and when passed to print function call the arr expression is converted to a pointer to an array 3 of bool.

Related

How do I print a vector of vectors of string in C++?

I'm trying to print a vector of vectors of strings, by moving the logic to a function. My program is compiling normally, but it does not print my vectors.
This is the function call on main:
showInst(vectInst);
This is the prototype in .hpp:
void showInst(vector<vector<string>> vectInst);
Here is the implementation .cpp:
void showInst(vector<vector<string>> vectInst) {
for(i=0; i<vectInst.size(); i++){
for(j=0; j<vectInst[i].size(); j++){
cout << vectInst[i][j];
}
}
}
This one is the prototype of the function that receives the vector of vector to initialize it
void initInst(vector<vector<string>> vectInst, int numbInst);
This is the .cpp
void initInst(vector<vector<string>> vectInst, int numbInst) {
int i, j;
string inst;
for(i=0; i<numbInst; i++){
vector<string> vect;
for(j=0; j<4; j++){
cin >> inst;
vect.push_back(inst);
}
vectInst.push_back(vect);
}
}
Call on the main:
vector<vector<string>> vectInst;
initInst(vectInst, 2);
First in function showInst: don't pass your vector by value: this produces an useless copy of every single string/vector. Use a const reference instead:
void showInst(vector<vector<string>> const & vectInst);
The code of the function is correct, although we'd prefer for-range loops:
for (auto const & string_vec : vectInst) {
for (auto const & str : string_vec) {
cout << str;
}
}
Your vector is probably empty, or maybe this function isn't even called. The problem must be somewhere else.
I suggest you print some brackets at the beginning and end of this function.
After edition
Your vector is indeed empty as you are (also) passing it by value in initInst function. This is a local copy you are modifing, not the original.
Pass it by reference:
void initInst(vector<vector<string>> & vectInst, int numbInst) {
^
initInst() is modifying a copy of the vector you declare in main(). To make it modify the vector you have in main(), you need to pass it by reference rather than by value.
Change this:
void initInst(vector<vector<string>> vectInst, int numbInst) {
To this:
void initInst(vector<vector<string>> &vectInst, int numbInst) {

C++ Passing by pointer and passing by reference

#include<iostream>
using namespace std;
class A{
int *numbers[5];
public:
void assignment(int ** x){
for(int i=0;i<5;i++)
numbers[i]=x[i]; //not changing just the value of *numbers[i] but the pointer numbers[i]
}
void print(){
for(int i=0; i<5;i++)
cout<< *numbers[i]<<endl;
}
};
int main(){
int *x[5];
for(int i; i<5;i++){
x[i]= new int(i);
cout<<*x[i]<<endl;
}
cout<<endl;
A numbers;
numbers.assignment(x);
numbers.print();
return 0;
}
My question is very specific. I want to do the same thing as the code above but instead of passing the argument of function assignment(int **) by pointer to do it by reference. How can I achieve that?
Use:
void assignment(int* (&x)[5]) ...
edit: for the comment "if the length... wasn't standard...", you can use a template:
template<int N> void assignment(int* (&x)[N]) ...
The compiler will automatically deduce N.

passing 1d and 2d arrays by reference in c++

I am new to c++ and have a couple questions regarding passing arrays by reference to functions (so that the arrays are modified by the function). I realize there are similar questions that have been asked already, but there are a few points that I think were not covered in those previous questions (at least from what I saw). From what I have gathered so far, one can pass an array by reference by doing the following:
#include<iostream>
using namespace std;
void modify_array(int* a);
int main()
{
int array[10];
modify_array(&array[0]);
for(int i=0;i<10;i++)
{
cout<<array[i]<<endl;
}
}
void modify_array(int* a)
{
int i;
for(i=0;i<10;i++)
{
*(a+i)=i;
}
}
This makes sense to me but if I change the function to:
void modify_array(int* a)
{
int i;
for(i=0;i<10;i++)
{
a[i]=i; //line changed
}
}
This also works. Is there a difference? Or is the second just a short cut? Also in the case of passing 2d arrays I would have guessed that the following code would work:
#include<iostream>
using namespace std;
void modify_array(int* a);
int main()
{
int array[10][10];
modify_array(&array[0][0]);
}
void modify_array(int* a)
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
a[i][j]=i*j;
}
}
}
But this doesn't. From what I have seen in other related questions, you would do something like:
void modify_array(int (*a)[10])
{
int i,j;
//a[i][j]= blah blah blah;
}
or,
void modify_array(int (&a)[10][10])
{
int i,j;
//a[i][j]= blah blah blah;
}
What is the difference between these latter two function definitions? What do experienced c++ programmers recommend using: the (*a)[10][10] notation or the (&a)[10][10] notation?
Writing *(a+i)=i; or a[i]=i; are equivalent. The first is seen as an offset applied to the pointer to the array and assigning the value to the pointee, while the second is assigning the value to the element of the array.
However, when passing a pointer to a function modify_array(int* a), it cannot deduce that the pointee is a 2D array, and does not know what size to offset to address the other lines of the array, with a[i][j]=i*j;. For the compiler, it can only access the first dimension of the array.
The proper way to do what you need is this
#include<iostream>
using namespace std;
void modify_array(int (&a)[10][10]);
int main()
{
int array[10][10];
modify_array(array);
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
cout<<array[i][j]<<endl;
}
}
}
void modify_array(int (&a)[10][10])
{
int i;
for(i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
a[i][j]=i*j;
}
}
}
Live example
The function is expecting an int array of 10x10, passed by reference.

2D array of a class object call by reference

I searched a lot on stackoverflow and google but nothing as my case. I want to declare a 2d array of my class box. It works normal, then i need a print function for it. If i printed the array inside main(), it was fine but now with a printer function i seem to get a lot of errors. Please help me with my mistake.
#include <iostream>
class box
{
private:
char life;
public:
box();
void display();
void input_alive();
void input_dead();
};
box::box()
{
life = '0';
}
void box::display()
{
std::cout << " " <<life <<" ";
}
void box::input_alive()
{
life = '1';
}
void box::input_dead()
{
life = '0';
}
void printer(box *array, int yy, int xx)
{
int i, j;
for(i=0; i<yy; i++) //PRINTER
{
for(j=0; j<xx; j++)
{
array[i][j].display();
if (j+1 == xx) //just newline for separate rows
std::cout << std::endl;
}
}
}
int main()
{
int row=5, col=5;
box arr[row][col];
arr[3][4].input_alive();
arr[1][1].input_alive();
printer(arr, row, col);
return 0;
}
this syntax of passing array by reference works fine in normal int/char arrays, but why not here. If i put printer function in main, it works fine :(. Do i have to use new or what? or how do i pass box array into function? thanks.
First of all, C++ doesn't support variable length arrays, so your code is not standards compliant and therefore is not portable.
Second, you can avoid all the pain by using std::array:
#include <array>
template <size_t ROWS, size_t COLS>
void printer(std::array<box, ROWS>, COLS>& arr)
{
for(int i=0; i<ROW; ++i)
{
for(int j=0; j<COL; ++j)
{
// do something with arr[i][j]
}
}
int main()
{
const int row=5;
const int col=5;
std::array<std::array<box, row>, col> arr;
arr[3][4].input_alive();
arr[1][1].input_alive();
printer(arr);
return 0;
}
Your solution is to forget about raw arrays. Instead, you use std::vector or std::array and write a Matrix class with operator(), as explained in the C++ FAQ. See also the next item in the FAQ for reasons why you'd want to prefer the (x, y) form to [x][y], but consider that there are good programmers who prefer the latter syntax (using a proxy class), so the case is perhaps not as clear as the FAQ says it is.
In any case, you need a Matrix class with a std::vector or std::array implementation. Raw arrays are the wrong tool for this task.

possible scope issue c++

#include<iostream>
#include<vector>
using namespace std;
class t{
public:
t();
void updateSize();
int getSize();
void insert();
int get(int a);
private:
int size;
vector<int> v;
};
t::t(){
size =0;
}
void t::updateSize(){
size++;
}
int t::getSize(){
return size;
}
int t::get(int a){
return v[a];
}
void t::insert(){
v.push_back(size);
++size;
}
int main(){
t xa;
xa.insert();
xa.insert();
xa.insert();
xa.insert();
cout<<xa.get(3);//expect to output 3 but instead outputs 0
return 0;
}
this code is supposed to increment the size every time I call insert, and put an integer of with the value of that size in a vector at the same index of that size. But for some reason it does not put the updated size into my vector.
You're inserting 3 elements but you're reading the 4th (since the indexing is 0 based).
The program you posted will print "3". Proof read your code.