Output not getting print after matrix multiplication - c++

In the code below, my aim is to multiply two matrices reflect[3][3] and mat[3][s] where s can be any value 0-10. Here the statement (A) and (B) is not getting printed, please tell me why??
#include<iostream>
# include<math.h>
#include<conio.h>
using namespace std;
int mat[10][10];
int result[3][10];
int reflect[3][3]= {1,0,5,0,1,5,0,0,1};
int i , j,k,s;
void multiply_matrix(int A[3][3], int B[3][10])
{
for(i=0; i<3; i++)
for( j=0; j<10; j++)
result[i][j] = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
cout<<endl;
}
cout<<"Multiplication after matrix: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}
cout<<endl;;
}
}
int main()
{
int i, j,s;
cout<<"Enter the sides of polygon :\n";
cin>>s;
cout<<"Enter the coordinates of polygon :\n";
cout<<"Enter x coordinates ";
for(i=0; i<s; i++)
cin>>mat[0][i];
cout<<"Enter y coordinates ";
for(i=0; i<s; i++)
cin>>mat[1][i];
cout<<"\n\n";
for(i=0; i<s; i++)
mat[2][i] = 1;
cout<<"MAt: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
multiply_matrix(reflect, mat);
cout<<"End"<<endl;
return 0;
}
I have attached a sample output image for reference:
Output of code
I am a newbie and have tried various things but I am just not able to figure my mistake. Thanks in advance for your help.

You have two s in your code. Globally:
int i , j,k,s;
And in main:
int i, j,s;
The one in main you assign a value read from user input, but the global one is 0 always. multiply_matrix uses the global one, hence this loops have zero iterations:
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
and
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}

Related

(C++) How to imagine working of nested loops?

I have no idea what's going on how to imagine that, one more thing like in 3rd for loop there's a condition that k<j but its upper loop j is set to 0 and i is also 0 then as I think k=0 if this is right than 0<0 how's that can be valid????
void printing_subarrays(int *arr,int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
for(int k=i; k<j; k++){
cout<<arr[k]<<", ";
}cout<<endl;
}cout<<endl;
}
}
I don't think it makes much sense for us to explain what's happening, you need to see it for yourself.
Therefore I've added some output lines, which will show you how the values of the variables i, j and k evolve through the loops:
for(int i=0; i<n; i++){
cout<<"i=["<<i<<"]"<<endl;
for(int j=0; j<n; j++){
cout<<"i=["<<i<<"], j=["<<j<<"]"<<endl;
for(int k=i; k<j; k++){
cout<<"i=["<<i<<"], j=["<<j<<"], k=["<<k<<"]"<<endl;
cout<<arr[k]<<", ";
}
cout<<endl;
}
cout<<endl;
}
Did you try maybe running it? Then it should be apparent...
#include <iostream>
#include <vector>
void printing_subarrays(int *arr, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = i; k < j; k++) {
std::cout << arr[k] << ", ";
}
std::cout << "\n";
}
std::cout << std::endl;
}
}
int main() {
int n = 10;
std::vector<int> vec(n);
for (size_t i = 0; i < n; ++i) {
vec[i] = i;
}
printing_subarrays(vec.data(), n);
return 0;
}

Matrix A find k -the number of positive elements

Enter the matrix A(NxM), output it. In each row of the matrix, find k -the number of positive elements. In the rows, all the elements after the kth are increased by the sum of the positive elements of this row. What's wrong with the program? The program incorrectly outputs the elements of the modified array.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
int n, m;
cout << "Введите количество строк: ";
cin >> n;
cout << "Введите количество столбцов: ";
cin >> m;
int A[10][10];
for (int i=0; i<n; i++)
for (int j=0; j<m; j++) {
cout<<"\nA["<<i<<"]["<<j<<"]=";
cin>>A[i][j];
}
cout << "\nМассив A:";
for (int i = 0; i < n; i++) {
cout<<"\n";
for (int j = 0; j < m; j++)
cout<<"\t"<<A[i][j];
}
cout<<endl;
int k;
for (int i = 0; i < n; i++)
{
k=0;
for (int j = 0; j < m; j++)
if(A[i][j]>0)
k++;
}
int sum;
for (int i=0; i<n; i++)
{
sum=0;
for (int j=0; j<m; j++)
if (A[i][j]>0)
sum+=A[i][j];
}
for (int i = k; i < n; i++)
for (int j = 0; j < m; j++)
A[i][j] = A[i][j] + sum;
cout << "Измененный массив A:";
for (int i = 0; i < n; i++) {
cout << "\n";
for (int j = 0; j < m; j++)
cout << "\t" << A[i][j];
}
}
You calculate k for each row, but after the loop k will only store the value of the last row. You need to store k for each row. Then in the loop:
for (int i = k; i < n; i++)
you skip the first k-1 rows, but you should add the sum in each row.
Change the last part of the code to:
int k;
std::vector<int> ks;
for (int i = 0; i < n; i++)
{
k=0;
for (int j = 0; j < m; j++)
if(A[i][j]>0)
k++;
ks.push_back(k);
}
int sum;
for (int i=0; i<n; i++)
{
sum=0;
for (int j=0; j<m; j++)
if (A[i][j]>0)
sum+=A[i][j];
}
for (int i = 0; i < n; i++)
for (int j = ks[i]; j < m; j++)
A[i][j] = A[i][j] + sum;

How to correctly divide square matrix?

const int n = 4, m = 4;
int i, j, k, sum;
srand(time(NULL));
int mat[n][m];
printf("Matrix( %d, %d): \n",n,m);
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
mat[i][j] = rand()%100-50;
printf("%4d", mat[i][j]);
}
cout<<endl;
}
for(i = 0; i < n/2; i++)
{
for(j = 0; j < m/2; j++)
{
mat[i][j] = 0;
}
}
How to divide square matrix on 4 equal square blocks? My code divide only 1st part. I think I need to use n/2 < n, but how to do it in cycle?
First block:
for (int i=0; i<n/2; i++) {
for(int j=0; j<m/2; j++) {
// your code
}
}
Second block:
for (int i=0; i<n/2; i++) {
for(int j=m/2; j<m; j++) {
// your code
}
}
Third block:
for (int i=n/2; i<n; i++) {
for(int j=0; j<m/2; j++) {
// your code
}
}
Forth block:
for (int i=n/2; i<n; i++) {
for(int j=m/2; j<m; j++) {
// your code
}
}
Here is a more compact code that can fetch your matrix blocks by rows sequentially:
for (int p = 0; p <= 1, p++)
{
for (int q = 0; q <= 1, q++)
{
for(i = p*n/2; i < (p+1)*n/2; i++)
{
for (j = q*m/2; j < (q+1)*m/2; j++)
{
mat[i][j] = 0;
}
}
}
}

2D Vector of Doubles sorting/editing issue

I am working on a 2D vector of doubles matrix solver and my matrices keep turning out incorrectly.
The intended matrix output at "Level 3" is {(top, left to right)3,4 over (bottom, left to right)1,2} but my code keeps outputting {2,1 over 3,4}. I keep trying to invert the values but whenever I do so I keep getting an out of bounds exception.
vector<double> gauss(mat B) {
int n = B.A.size();
for (int i=0; i<n-1; i++) {
// Search for maximum in this column
double maxEl = abs(B.getSlot(i,i));
int maxRow = i;
for (int k=i+1; k<n; k++) {
if (abs(B.getSlot(k,i)) > maxEl) {
maxEl = abs(B.getSlot(k,i));
maxRow = k;
}
}
// Swap maximum row with current row
for (int k=i; k<n+1;k++) {
double tmp = B.getSlot(k,maxRow);
B.editSlot(k,maxRow,B.getSlot(k,i));
B.editSlot(k,i,tmp);
}
cout << "\n\nlevel 3: \n";
B.display();
}
B.display();
// Solve equation Ax=b for an upper triangular matrix A
vector<double> x(n);
for (int i=n-1; i>=0; i--) {
x[i] = B.getSlot(i,n)/B.getSlot(i,i);
for (int k=i-1;k>=0; k--) {
B.editSlot(k,n,B.getSlot(k,n)-B.getSlot(k,i)*x[i]);
}
}
return x;
}
int main() {
int n = *Size of array is properly retrieved*
mat my_mat(n,n);
// Read input data
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
double myDub;
inFile >> myDub;
my_mat.editSlot(i,j,myDub);
}
}
// Calculate solution
vector<double> x(n);
x = gauss(my_mat);
}
And my class code is as follows
class mat
{
public:
mat(int x,int y){
int row = x;
int col = y;
n=row;
A = vector<vector<double>>(row,vector<double>(row,0));
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
cout<<setw(6)<<A[i][j];
}
cout<<endl;
}
}
void editSlot(int x, int y, double val){A[y][x] = val;}
double getSlot(int x, int y){return A[y][x];}
void display()
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout<<setw(6)<<A[j][i];
}
cout<<endl;
}
cout<<endl;
}
vector<vector<double>> A;
private:
int n;
};
Try to change your input loop:
// Read input data
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
double myDub;
inFile >> myDub;
my_mat.editSlot(j, i, myDub); // < - j, i swapped.
}
}
Can you provide sample input that you use and what output you expect?

2d array addition in c++

i am trying to add two 2D arrays in C++ with my following code by i am getting output as this
333 333, but i want out as 2 rows
{
int a[2][3], b[2][3], i , j;
cout<<"First Matrix"<<endl;
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cin>>a[i] [j];
}
}
cout<<"Second Matrix"<<endl;
for(int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cin>>b[i][j];
}
}
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cout<<a[i] [j] + b[i] [j];
}
cout<<" ";
}
cout<<endl;
_getch();
}
Last for loop is wrong. You have to move cout's.
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cout<<a[i] [j] + b[i] [j];
cout<<" ";
}
cout<<endl;
}
Also your variables i and j are unused because you are declaring new ones in for loops with int i=0; and int j=0;.
How about changing the line that prints spaces to print a newline?
cout<<" ";
becomes
cout<<"\n";
You don't put any newlines in your code, so of course it won't print out on a new line. replace cout<<" "; with cout<<std::endl; and you should get each row on a new line.
Replace the last piece of code by this:
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cout<<a[i] [j] + b[i] [j] << ' ';
}
cout<< "\n";
}
cout << "\n";