Adding 858993460 to get the write answer?
Mar 5, 2015 at 2:00am UTC
My code works beautifully. I needed to add and multiply matrix strings.
However! I have no idea why I have to add 858993460 to my "Matrixmulti" code for it to work. Any ideas? I'm thinking that number is equal to NULL but im pretty new to this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*
Name: Jacob Chesley
Date started: 3/4/15
Date last edited: 3/4/15
Name of file: Matrix operations
Name of project: Matrix operations
*/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
const int SIZE = 3;
void matrixmulti(int matrixtimes[SIZE][SIZE], int matrix1[SIZE][SIZE], int matrix2[SIZE][SIZE])
{
for (int i = 0; i < SIZE; i++)
{
for (int q = 0; q < SIZE; q++)
{
for (int h = 0; h < SIZE; h++)
{
matrixtimes[i][q] += matrix1[i][h] * matrix2[h][q];
}
cout << "Matrix" << i << q << " equals: " << matrixtimes[i][q] + 858993460 << endl;
}
}
}
void matrixaddition(int matrixadd[SIZE][SIZE], int matrix1[SIZE][SIZE], int matrix2[SIZE][SIZE])
{
for (int i = 0; i < SIZE; i++)
{
for (int q = 0; q < SIZE; q++)
{
matrixadd[i][q] = matrix1[i][q] + matrix2[i][q];
cout << matrix1[i][q] << " plus " << matrix2[i][q] << " equals: " << matrixadd[i][q] << endl;
}
}
}
int main()
{
int matrixadd[SIZE][SIZE],
matrix1[SIZE][SIZE],
matrix2[SIZE][SIZE],
matrixtimes[SIZE][SIZE];
for (int i = 0; i < SIZE; i++)
{
for (int q = 0; q < SIZE; q++)
{
cout << "Please enter matrixone" << i << q << ": " ;
cin >> matrix1[i][q];
cout << endl;
cout << "Please enter matrixtwo" << i << q << ": " ;
cin >> matrix2[i][q];
cout << endl;
}
}
matrixaddition(matrixadd, matrix1, matrix2);
matrixmulti(matrixtimes, matrix1, matrix2);
return 0;
}
Mar 5, 2015 at 2:01am UTC
Also, any recommendations to make this code just a little better? This is extra credit work so I just want to make it look pretty.
Mar 5, 2015 at 2:32am UTC
Figured it out. I forgot to initialize my matrix strings.
Topic archived. No new replies allowed.