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;
}
Related
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;
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've created a 2D array with length and width.
My minGW compiler says nothing to build.
Here's the code:
#include <iostream>
using namespace std;
const int rows = 5;
const int coloms = 5;
int Matrix[rows][coloms];
void ClearMatrix()
{
for(int i=0; i < rows; i++)
{
for(int j=0; j < coloms; j++)
{
Matrix[i][j] = 0;
}
}
}
void ShowMatrix()
{
for(int i; i < rows; i++)
{
for(int j; j < coloms; j++)
{
cout<< Matrix[i][j];
}
}
}
int main()
{
ClearMatrix();
ShowMatrix();
return 0;
}
What am I doing wrong here?
for(int i; i < rows; i++)
{
for(int j; j < coloms; j++)
{
cout<< Matrix[i][j];
}
}
You are accessing i and j which are uninitialized local variables. So, it's an undefined behavior.
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";