I’m trying to add two rows of my array together in a function and its not doing it and I can’t tell why its not as the code looks right and doesn’t error out. I have tried using a * and & to pass it by reference but I always get code errors. Thanks
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
void addRow(int arr[100][100], int firstrow,int secondrow,int rows, int cols);
void addRow(int arr[100][100], int firstrow,int secondrow,int rows, int cols){
int i =0;
int j = cols;
while(i<rows){
arr[secondrow][j]+=arr[firstrow][j];
i++;
j++;
}
print(arr,rows,cols);
}
The array is passed correctly, it’s your code that’s not doing the additions right.
You set
jtocolsat the beginning, and move it in the increasing order withj++. As the result, all your accesses to array elements are past the end of the row. Loop exit condition is not right either, unless your matrices are always square (in which case there is no point to pass separate counts for rows and columns).This should work: