adding matrix doubles

Write a function called matrixAdd1 that adds 1.0 to each element of a matrix of doubles . An initial function skeleton is provided --- your job is to write the body of the function. Note that the matrix M has numRows rows and numCols columns, which are passed as parameters to the function. The function should NOT return a value , and should not use cout nor cin.


I am having trouble starting this question.

Would i do something like:

M[r][c]=M[r+1.0][c+1.0];
Not quite. What you stated sets the value at M[r][c] to be equal to the value one diagonal over; in other words, M[1][1]=M[2][2]. What you want to do is something like this:

M[r][c] += 1.0;

Now, since technically 1.0 is the same as 1 in regards to addition (minus the .000000001 decimal difference that tends to happen when you do anything with floating-points), you could simply write it as this:

M[r][c]++;
void matrixAdd1(double M[MAXROWS][MAXCOLS], int numRows, int numCols)
{
for (int r=0;r<numRows;r++)
{


for (int c=0; c<numCols; c++)
{

}

M[r][c]+=1.0;
}

}


would that be correct?
Indeed, that would do exactly what is required- it will add 1.0 to each value in the matrix.
Topic archived. No new replies allowed.