I have tried without error, [2x2] * [2x2] and [2x3] * [3x2].
The "odd one" is the output from the [3x2] * [2x3]. its output looks something like this: row 1: [9 10 11], row 2: [39 44 49], row 3: [69 78 87 0 0 0297 0]. Rows 3's output is adding elements unlike it did with the previous trials of 2x2 and the (2x3 * 3x2).
Here's a snippet of my code where I believe the problem may lie.
int sum;
outputVec.resize(vec1.size());
for(int i = 0; i < vec1.size(); i++) {
for(int k = 0; k < vec2[i].size(); k++) {
sum = 0;
for(int j = 0; j < vec1[i].size(); j++) {
sum += (vec1[i][j] * vec2[j][k]);
}
outputVec[i].push_back(sum);
}
}
for(int i = 0; i < outputVec.size(); i++) {
for(int j = 0; j < outputVec[i].size(); j++) {
printf("%3d",outputVec[i][j]);
}
cout << endl;
}
for(int i = 0; i < vec1.size(); i++)
{
for(int k = 0; k < vec2[i].size(); k++)
...
vec2 doesn't necessarily have as many rows as vec1 (as in the (3x2) * (2x3) case). You're reading past the end of vec2 and getting undefined behavior.
Related
(r1,c1) and (r2,c2) are number of Rows and columns of matrix a[][] and matrix b[][]
matrix c[][] is the matrix multiplication of a[][] and b[][]
The user enters the number of rows and columns of a[][] and b[][] and enters elements of both.
this is the output given by the complete program for specific a[][], b[][]
the expected output of the program for the same a[][] and b[][] is:
c[2][2]={{7,14},{17,34}}
However, I cant seem to find the error in the logic.
the following part of the code is the logic to carry out the matrix multiplication
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
c[i][j] = 0;
for (k = 0; k < c1; k++) {
c[i][j] += a[i][j] * b[k][j];
}
}
}
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
You are doing math incorrectly.
According to mat multiplication,
for(i=0 ; i<r1 ; i++)
{
for(j=0 ; j<c2 ; j++)
{
c[i][j]=0;
for(k=0 ; k<c1 ; k++)
{
c[i][j]+=a[i][j]*b[k][j];
//--^-- should be k
}
}
}
Some strategic renaming gives
for(int row = 0; row < r1; row++)
{
for(int column = 0; column < c2; column++)
{
c[row][column] = 0;
for(int element = 0; element < c1 ; element++)
{
c[row][column] += a[row][column] * b[element][column];
}
}
}
where you can see more clearly that the multiplication is wrong - you're multiplying with the same element from a in every step.
It should be
c[row][column] += a[row][element] * b[element][column];
I guess that this could help you to solve your problem:
c[i][j]+=a[i][k]*b[k][j];
This program is meant to take in a square matrix of integers and outputs the largest sub-square-matrix sum.
The first line of input is an integer which indicates the dimension of the square matrix, followed by the actual matrix row-by-row.
My program works almost perfectly except it does not work when using small matrices with negative values. Can anyone help me optimise the code, I cant see where its is going wrong
Example Input1:
3
1 2 3
4 5 6
7 8 9
Output: 45
Example Input2:
3
1 2 3
4 5 6
-7 -8 -9
Output: 16
NB: Since the largest square matrix is [2 3; 5 6] which sums to 16
My code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int Numberofelements,n,counter = 0,sum=0,result = 0,Maximumvalue = -1, *pointervalue = NULL;
int count = 0;
cin>>n;
int mat[n][n];
int TempMatrix[n][n];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cin>>mat[i][j];
if(mat[i][j]<0){
count++;
}
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j< n; ++j)
{
sum = sum + mat[i][j];
}
if (sum > 0 )
{
counter++;
}
sum = 0;
}
Numberofelements = counter;
for (int j = 0; j < n; j++)
{
sum = 0;
for (int i = 0; i < Numberofelements; i++)
{
sum = sum + mat[i][j];
}
TempMatrix[0][j] = sum;
for (int i=1; i<n-Numberofelements+1; i++)
{
sum = sum+(mat[i+Numberofelements-1][j] - mat[i-1][j]);
TempMatrix[i][j] = sum;
}
}
for (int i=0; i<n-Numberofelements+1; i++)
{
sum = 0;
for (int j = 0; j < Numberofelements; j++)
{
sum = sum + TempMatrix[i][j];
}
if (sum > Maximumvalue)
{
Maximumvalue = sum;
pointervalue = &(mat[i][0]);
}
for (int j = 1; j < n-Numberofelements+1; j++)
{
sum = sum + (TempMatrix[i][j+Numberofelements-1] - TempMatrix[i][j-1]);
if (sum > Maximumvalue)
{
Maximumvalue = sum;
pointervalue = &(mat[i][j]);
}
}
}
for (int i = 0; i < Numberofelements; i++)
{
for (int j = 0; j < Numberofelements; j++)
{
result+=*(pointervalue + i*n + j);
}
}
cout << result;
return 0;
}
by now I have this code
int main() {
int cols;
cout << "cols";
cin >> cols;
int rows;
cout << "rows";
cin >> rows;
char** charArray = new char*[rows];
for (int i = 0; i < rows; ++i) {
charArray[i] = new char[cols];
}
// Fill the array
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
charArray[i][j] = char('1' + i + j );
}
}
// Output the array
for (int i = 0; i < rows; ++i) {
for (int j = i; j < cols ; ++j) {
cout << charArray[i][j];
}
cout << endl;
}
// Deallocate memory by deleting
for (int i = 0; i < rows; ++i) {
delete[] charArray[i];
}
and the output is like
1 2 3
2 3 4
How can I make it to be
1 2 3
4 5 6
I tried a lot, im a newbie in programming so can you please explain me what's the matter with this problem! Thanks a lot!!
Just change this:
charArray[i][j] = char('1' + i + j );
by:
charArray[i][j] = char('1' + i*cols + j);
BTW:
You have a typo in the output array loop:
for (int j = i; j < cols ; ++j) {
Should be:
for (int j = 0; j < cols ; ++j) {
Array filling loop should be
int count = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
charArray[i][j] = char('1' +count );
count++;
}
}
try with above code
You are building a matrix, here right. So it's 2-dimensional.
What the output means is that your matrix looks like:
1 2 3
2 3 4
Which is only natural, since, while filling it, you wrote for each element at coordinate i,j to be value 1+i+j. That means if i and j both equal 1 (second line, second column, since index start at 0), you put value the 1+1+1=3.
If you want to get the matrix:
1 2 3
4 5 6
You'll have to fill it with
charArray[i][j] = char('1' + i*ncols + j );
This way when you fill the first line, i = 0, so it's the same.
But then on the second line, i=1, so you get 1 + 3 + 0 = 4. And then 1+3+1=5, etc.
Edit: people are fast here. By the time I write my answer, already 2 others posted theirs :D
I am trying to fill an array of 52 with the numbers 0 - 12. Once it hits 12, it needs to go back to 0 - 12 again. You might have already guessed it's a deck of cards. My code is below and doesn't work. It prints 0 - 12 one time, but then prints the address of the array I believe for the remainder of the iterations left.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int myArray[52];
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 13; i++)
{
myArray[i] = i;
}
}
for (int k = 0; k < 52; k++)
{
cout << myArray[k] << endl;
}
//system("pause");
return 0;
}
Can someone please help me with this brain fart?
int myints[52];
for (int idx = 0; idx < 52; idx++)
{
myints[idx] = idx % 13;
}
Modulus of 13 will range from 0 to 12.
You're indexing the same first 12 elements of the array in the inner loop for every iteration of the outer loop.
Try changing it to something like this
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 13; i++)
{
myArray[i + 13 * j] = i;
}
}
So what I am trying to do is multiply one 2d vector by another 2d vector.
I come from Java,Python and C# so I am pretty much learning C++ as I go along.
I have the code down to generate the vector and display the vector but I can't seem to finish the multiplication part.
v1 is another matrix that is already generated.
vector<vector<int> > v2 = getVector();
int n1 = v1[0].size();
int n2 = v2.size();
vector<int> a1(n2, 0);
vector<vector<int> > ans(n1, a1);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < 10; k++) {
// same as z[i][j] = z[i][j] + x[i][k] * y[k][j];
ans[i][j] += v1[i][k] * v2[k][j];
}
}
}
displayVector(ans);
My guess for where I am going wrong is in the inner-most loop. I can't figure out what to actually put in place of that 10 I have there now.
When you multiply matrices, the number of columns of the matrix on the left side must equal the number of rows of the matrix on the right side. You need to check that that is true, and use that common number for your size of the k variable:
int nCommon = v1.size();
assert(v2[0].size() == nCommon);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < nCommon ; k++) {
ans[i][j] += v1[i][k] * v2[k][j];
}
}
}
For you inner loop, you should do something like this
ans[i][j] = 0;
for (int k = 0; k < n2; k++) {
ans[i][j] += v1[i][k] * v2[k][j];
}
I don't know where the 10 comes from.