Reputation: 77
So the question itself is "Develop a function that adds the elements of the lin1 and lin2 lines of a matrix, putting the result in lin3". I assume that we should add the content of lin1 with lin2 and store the result in lin3. For example, in the matrix given, if i put lin1=0, lin2=1 and lin3=2, it should do 1+4=5 (in the first iteration of i) If i put lin1=2, lin2=1 and lin3=0, it should do 7+4=11
This is what I came up with. I think it's close to the solution... It prints "57" which seems to be the sum of the rows of the first 2 columns. MAXCOLS100 is a macro which I assigned the value of 3.
void sum_lines_matrixesNx100(int mNx100[][MAXCOLS100], int lin1, int lin2, int lin3, int columns){
int i;
for(i=0; i<columns; i++){
mNx100[lin3][i] = mNx100[lin1][i] + mNx100[lin2][i];
}
printf("\n");
}
int main()
{
int mNx100[][MAXCOLS100] = {{1,2,3},{4,5,6},{7,8,9}};
sum_lines_matrixesNx100(mNx100,0,1,2, 3);
printf("%d", mNx100[2][MAXCOLS100]);
return 0;
}
Upvotes: 0
Views: 258
Reputation: 84561
Your add is fine, your printf
... printing (most likely one-past-the-end of your the 3rd row of matrix invoking Undefined Behavior), see: C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3) for the array/pointer conversion involved.
Presuming you have:
#define MAXCOLS100 100
somewhere. you are attempting to print the element at mNx100[2][MAXCOLS100]
which is one after the end of the 3rd row of your matrix. (technically within the 2D array itself, but not legal to address in that manner)
Your valid column indexes for the 3rd row being from 0 -> MAXCOLS100-1
. The address mNx100[2][MAXCOLS100]
is valid for purposes of addressing the end of the 3rd row, but is not a proper index for data retrieval. (On access mNx100[2]
is converted to a pointer to the first element in a 1D array of int
with MAXCOLS100
elements)
What you want is to print the first 3-elements of the first 3-rows (the remainder will have been initialized zero by virtue of your initialization)
A simple matrix print in your case could be:
void prnmtrx (int (*m)[MAXCOLS100], size_t rows, size_t cols)
{
size_t i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf (j ? " %2d" : "%2d", m[i][j]);
putchar ('\n');
}
}
Where it is called by:
prnmtrx (mNx100, 3, 3);
A complete example would be:
#include <stdio.h>
#define MAXCOLS100 100
void sumrows (int mNx100[][MAXCOLS100], int lin1, int lin2, int lin3, int columns)
{
int i;
for (i = 0; i < columns; i++) {
mNx100[lin3][i] = mNx100[lin1][i] + mNx100[lin2][i];
}
}
void prnmtrx (int (*m)[MAXCOLS100], size_t rows, size_t cols)
{
size_t i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf (j ? " %2d" : "%2d", m[i][j]);
putchar ('\n');
}
}
int main()
{
int mNx100[][MAXCOLS100] = {{1,2,3},{4,5,6},{7,8,9}};
sumrows (mNx100, 0, 1, 2, 3);
prnmtrx (mNx100, 3, 3);
return 0;
}
Example Use/Output
$ ./bin/mtrxaddrows
1 2 3
4 5 6
5 7 9
Look things over and let me know if you have further questions.
Upvotes: 1