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;
Related
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;
}
I'm getting the (main.cpp:15:13: error: ‘imax’ was not declared in this scope) error while compiling this code.
Here's the task:
Given k number and kxk z matrix. Get new b[i] vector which elements are taken from the product of the elements preceding the last max element
#include <iostream>
using namespace std;
int indexMax (int z[20][20], int k){
for (int i = 0; i < k; i++){
int max = z[i][0];
int imax = 0;
for(int j = 0; j < k; j++){
if (z[i][j]>=max)
max=z[i][j];
imax=j;
}
}
return imax;
}
int product(int z[20][20], int k){
int imax=indexMax(z,k);
int i;
int P=1;
for(int j = 0; j < imax-1; j++){
P*=z[i][j];
}
return P;
}
int main()
{
int z[20][20],b[20],k;
cout << "k=";
cin >> k;
cout << "Matrix " << k << "x" << k << ":\n";
for (int i = 0; i < k; i++){
for (int j = 0; j < k; j++){
cin >> z[i][j];
}
}
cout << "b[i]:\n";
for (int i = 0; i < k; i++){
b[i] = product(z, k);
cout << b[i] << " ";
}
return 0;
}
imax is scoped by the outer for loop and hence not accessible outside it. The fix is to move the declaration outside the for loop (I initialized it here in case k == 0 but leaving the assignment imax = 0 in the loop as to not change behavior):
int indexMax (int z[20][20], int k){
int imax = 0;
for (int i = 0; i < k; i++){
int max = z[i][0];
imax = 0;
for(int j = 0; j < k; j++){
if (z[i][j]>=max)
max=z[i][j];
imax=j;
}
}
return imax;
}
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)
}
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;
}
}
}
}
I wanted to do a coding where it reads a 4x4 matrices and sum them up. I dont know where I did wrong. My result is it keeps on asking to enter the elements. I just wanted a 4x4. Can anyone help me?
#include <iostream>
using namespace std;
const int SIZE = 4;
double sumColumn(const double m[][SIZE], int rowSize, int columnIndex)
{
int sum = 0;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
sum = sum + m[i][j];
}
}
return sum;
}
int main()
{
double m[SIZE][SIZE], sum = 0;
cout << "Enter the elements of the matrix" << endl;
for (int i = 0; i<SIZE; i++)
for (int j = 0; j<SIZE; j++)
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
cout << sum << endl;
return 0;
}
A good practice is using curly braces even if the content of the "for" has one line, but in your case must be in the next way
for (int i = 0; i<SIZE; i++) {
for (int j = 0; j<SIZE; j++) {
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
}
}
Greetings
The complete code is:
#include <iostream>
using namespace std;
const int SIZE = 4;
double sumColumn(const double m[][SIZE], int rowSize, int columnIndex)
{
int sum = 0;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
sum = sum + m[i][j];
}
}
return sum;
}
int main()
{
double m[SIZE][SIZE], sum = 0;
cout << "Enter the elements of the matrix" << endl;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
}
}
cout << sum << endl;
return 0;
}