Writing a function that adds two matrices - c++

void add_matrices(int matrix_1 [m][n], int matrix_2 [m][n], int matrix_3 [m][n], int num_rows, int num_cols)
{
for (int i = 0; i < num_rows; i++)
{
for (int j = 0; i < num_cols; j++)
{
matrix_3[i][j] = matrix_1[i][j] + matrix_2[i][j];
}
}
} // end of function that adds two matrices
I'm trying to write a function that adds two matrices (2-D arrays). Here is the snippet of code from my program where I define the function. I can post the rest of my code but figured it might be easier to look at.
When I run the code it says "Thread 1: EXC_BAD_ACCESS" on the line that starts with matrix_3. I think the issue lies in the parameters I'm passing to the function. Matrices 1 and 2 are filled in the main and matrix 3 is created in the main but empty.

for (int j = 0; i < num_cols; j++)
This should be:
for (int j = 0; j < num_cols; j++)

Related

How to initialize a character or string array using for loop in c++?

I want to make a 2 dimensional array of 13 rows and 6 columns all initialized with character X. to do so I'm doing this.
char myseats[13][5];
for (int i; i < 13; i++)
{
myseats[i] = { 'X' };
for (int j; j < 5; j++)
{
myseats[j] = {'X'};
}
}
}
however this gives me a error and the array wont initialize with X, same is the case with string. How can I achieve my purpose can anyone please help me?
If you want to access fields of two dimensional array, you should use myseats[i][j] (first you specify row, then the column). Also, your for loops are written incorrectly. When you initialize any variable in the body of for loop, you should assign its value (otherwise i and j can have any value that can be saved in int). Your code should look like that:
char myseats[13][5];
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 5; j++)
{
myseats[i][j] = {'X'};
}
}
Try this:
char myseats[13][5];
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 5; j++)
{
myseats[i][j] = 'X';
}
}
First, the i and j where not initialized in the loop. You need to define at what index you want them to start.
Second, the correct way to access a two dimensional array is with the syntax array[row_index][column_index] = 'your_desired_value_here'.

How do I create a function for adding two matrices? What will it return?

So for my homework I have to create a function for adding two matrices, the numbers are given by the user, and I think I have the function somewhat down, I just have no idea what to return because I know you can't return an array, so I tried making a variable to set it equal to but it's not working? Can someone help me out with this?
This is the part I'm stuck on:
int add_matrix(int a[][10], int b[][10], int c[][10], int Anum_rows, int Anum_cols)
{
int matrix_total = c[0][0];
for (int i = 0; i < Anum_rows; ++i) {
for (int j = 0; j < Anum_cols; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
c[i][j] = matrix_total;
}
return matrix_total;
}
The last j in the c array is underlined with red also in visual studio.
Your code already does what it needs, it stores the resulting matrix in c, which, since arrays are passed as pointers, will still contain the updated values when add_matrix() returns. You just have extra code in the function, which should really not be needed to complete the matrix add operation:
void add_matrix(int a[][10], int b[][10], int c[][10], int Anum_rows, int Anum_cols) {
for (int i = 0; i < Anum_rows; ++i) {
for (int j = 0; j < Anum_cols; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
Good point about Anum_cols. There is no need to pass in the number of columns when it is specified in the argument definition and it could lead to future errors.
#define NUM_COLS 10
void add_matrix(int a[][NUM_COLS], int b[][NUM_COLS], int c[][NUM_COLS], int Anum_rows)
{
for (int i = 0; i < Anum_rows; ++i) {
for (int j = 0; j < NUM_COLS; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
}

Copying values of 2D array into another

I have a 2D array that I want to assign the values of another array. I'm making a game of life simulator and have everything else working but this. My code is this:
for(int i = 0; i < ROWS; i++) {
for (int j = 0; i < COLS; j++) {
current[i][j] = next[i][j];
}
}
current and next are both bool's. I keep getting the error code EXC_BAD_ACCESS in X-Code. I'm unsure what I'm doing wrong
To re-iterate my comment, the innerloop compares i with COL rather than j. So
for(int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
current[i][j] = next[i][j];
}
}
should solve the issue

Changing Values of a Row in a 2D Array Using For Loops

I have a function called zero_row. This function inserts the value of zero into all a row that is specified. The function takes to variables. a (the array) and row (the row in the array). Here is my function
void zero_row (int a [4][5], int row){
for (int i = 0; i < 4; i++) {
a[i][j] = 0;
}
}
I know how to set values of the entire array to zero. As i have a function to do this as well.
void zero_all (int a [4][5]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
my_arr[i][j] = 0;
}
}
}
I cant seem to figure out how to do this using the variable row. I want to use this variable so I can later change the row in my main function, like so: zero_row(a, 3);. Can anyone help?
Thanks!
In your case the first index corresponds to a row and the second index corresponds to a column.
Use
void zero_row (int a [4][5], int row){
for (int j = 0; j < 5; j++) { // 5 instead of 4
a[row][j] = 0;
}
}

optimize 2D array in C++

I'm dealing with a 2D array with the following characteristics:
const int cols = 500;
const int rows = 100;
int arr[rows][cols];
I access array arr in the following manner to do some work:
for(int k = 0; k < T; ++k) { // for each trainee
myscore[k] = 0;
for(int i = 0; i < cols; ++i) { // for each sample
for(int j = 0; j < rows; ++j) { // for each expert
myscore[k] += delta(i, anotherArray[k][i], arr[j][i]);
}
}
}
So I am worried about the array 'arr' and not the other one. I need to make this more cache-friendly and also boost the speed. I was thinking perhaps transposing the array but I wasn't sure how to do that. My implementation turns out to only work for square matrices. How would I make it work for non-square matrices?
Also, would mapping the 2D array into a 1D array boost the performance? If so, how would I do that? Finally, any other advice on how else I can optimize this... I've run out of ideas, but I know that arr[j][i] is the place where I need to make changes because I'm accessing columns by columns instead of rows by rows so that is not cache friendly at all.
Thanks,
Hristo
A general in-place matrix transposition is very difficult, but if you're okay with transposing it to another array, then it's pretty simple.
const int cols = 500;
const int rows = 100;
int arr[rows][cols];
// fill arr[][]
int arrT[cols][rows];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
arrT[c][r] = arr[r][c];
}
}
Of course, depending on how you're getting arr[][], you can just fill arrT[][] directly instead.
However, there may be a simpler solution of simple swapping the order of the loops.
for(int k = 0; k < T; ++k) { // for each trainee
myscore[k] = 0;
for(int j = 0; j < rows; ++j) { // for each expert
for(int i = 0; i < cols; ++i) { // for each sample
myscore[k] += delta(i, anotherArray[k][i], arr[j][i]);
}
}
}
Yes, 1d should be faster than 2d. C and C++ arrays are always 1d (internally).
When you call something like
array[row][col]
the compiler actually calculates
col + row * maxcols
and uses that as the actual index of a 1d array. You might as well do that yourself. Cycling through an entire array will be way faster, and random access will be equally fast as in a 2d array.
for(int i = 0; i < N; ++i) { // for each sample
for(int j = 0; j < E[i]; ++j) { // for each expert
... arr[j][i] ... // each ++j causes a large stride => poor caching
}
}
transpose the loops:
for(int j = 0; j < E[i]; ++j) { // for each expert
for(int i = 0; i < N; ++i) { // for each sample
... arr[j][i] ... // each ++i looks to the next word in memory => good
}
}
Of course, without seeing everything else in the program, I can't say if that would cause a problem. If delta doesn't have side effects, you should be fine.
You want memory accesses to be adjacent. In your case simply swap I and j when accessing arr.