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;
}
}
}
}
Related
#include<iostream>
using namespace std;
int main()
{
int arr[] = {40,20,14,20,55,14,22,45,22,447,441,224,421,2,14,1,9};
int size = sizeof(arr) / sizeof(int);
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
for (int i = 0; i < size; i++)
{
int Index_of_Min = i;
for (int j = i+1; j < size; j++)
{
if (arr[j] < arr[Index_of_Min])
{
Index_of_Min = j;
}
swap(arr[Index_of_Min], arr[i] );
}
}
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
in the above program when we run it the number 9, 224 are not getting sorted
i want to understand why the program is not working anyone who knows the solution then your help is much appreciated
and please explain me what was my mistake so i will not repeat it again.
Thank You
Saw the exact same error only a few days ago. Your loop is wrong, specifically the swap is in the wrong place. This is how it should look
for (int i = 0; i < size; i++)
{
int Index_of_Min = i;
for (int j = i+1; j < size; j++)
{
if (arr[j] < arr[Index_of_Min])
{
Index_of_Min = j;
}
}
swap(arr[Index_of_Min], arr[i] );
}
You do the swap after the inner loop has found the index of the minimum element, not while it is finding that index.
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)
}
I need to populate GtkGrid with many buttons, but they are too large and cannot fit on the screen. How can I make it 10px * 10px? set_size_request does not work in this case.
for(int i{0}; i < 128; i++){
for(int j{0}; j < 8; j++){
paint_btn_arr[i][j] = gtk_button_new();
}
}
up_grid = gtk_grid_new();
down_grid = gtk_grid_new();
for(int i{0}; i < 64; i++){
for(int j{0}; j < 8; j++){
gtk_grid_attach(GTK_GRID(up_grid), paint_btn_arr[i][j], i, j, 1, 1);
}
}
for(int i{0}; i < 64; i++){
for(int j{0}; j < 8; j++){
gtk_grid_attach(GTK_GRID(down_grid), paint_btn_arr[i + 64][j], i, j, 1, 1);
}
}
I wrote this code for java first. It should print the 2D array as a spiral. I wanted to try it in c++.. in java; there was a draw method. But c++ is not accepting array as a return type (can done by pointers) so I deleted the draw method and copied inside the main method. I commented the draw method where starts and ends. But now; the for loop which is after the draw method (i commented it too) is not starting. What's the problem; I cannot see it... Thanx for help.
int T ;
scanf("%d", &T);
int num[T];
for(int i = 0; i < T; i++){
scanf("%d", &num[i]);
}
for(int m = 0; m < T; m++){
int n = num[m];
int a[n][n];
//draw -start
int all = n*n;
int x = 0, y=0;
for(int counter=1; counter<=all; counter++){
for(int i = 0; i < n; i++){
a[x][y] = counter++;
y++;}
x++; y--;
for(int i = 0; i < n-1; i++){
a[x][y] = counter++;
x++;}
x--; y--;
for(int i = 0; i < n-1; i++){
a[x][y] = counter++;}
x--; y++;
for(int i = 0; i < n-2; i++){
a[x][y] = counter++;}
y++; x++; n = n-2;}
//draw - end
//this for is not starting
for(int i = 0; i<n; i++){
printf("a");
for(int j = 0; i<n; j++){
printf("a");
printf("%d ", a[i][j]);
}
printf("\n");
}
You decrement n in the biggest cycle.
n = n-2;
This is why n < 0 when you reach the for you speak of and it is not looping.
I am almost certain you did not meant to modify n in this loop.
I neatened up your code and provided my answer in the comments.
int T ;
scanf("%d", &T);
int num[T];
for(int i = 0; i < T; i++){
scanf("%d", &num[i]);
}
for(int m = 0; m < T; m++){
int n = num[m];
int a[n][n];
//draw -start
int all = n*n;
int x = 0, y=0;
for(counter=1; counter<=all; counter++){
for(int i = 0; i < n; i++) {
a[x][y] = counter++;
y++;
}
x++;
y--;
for(int i = 0; i < n-1; i++) {
a[x][y] = counter++;
x++;
}
x--;
y--;
for(int i = 0; i < n-1; i++) {
a[x][y] = counter++;
}
x--;
y++;
for(int i = 0; i < n-2; i++) {
a[x][y] = counter++;
}
y++;
x++;
n = n-2; //n = n - 2; all = n * n times?
}
for(int i = 0; i<n; i++){
printf("a");
for(int j = 0; i<n; j++){
printf("a");
printf("%d ", a[i][j]);
}
printf("\n");
}
}
in particular look at this line
n = n-2; //n = n - 2; all = n * n times?