Okay, so I'm just starting to use this site, and I know my title is a bit confusing, but I will try to explain it here as best as I can. So I have to take a matrix from the input, count even and odd numbers in each row, then write the matrix with rows that have more even numbers first. Example:
Input:
3 3 // Dimensions
1 2 3 // Elemets
4 5 6
7 8 9
Output:
1 2 3 // Original matrix
4 5 6
7 8 9
1 2 // Number of even and odd elements in each row
2 1
1 2
4 5 6 // Matrix sorted by even numbers in rows
1 2 3
7 8 9
I did everything except figuring out how to write out the final matrix. Any help?
#include<stdio.h>
#include<stdlib.h>
int main() {
int row, column, temp, count;
int even[100], odd[100];
for (int i = 0; i < 100; i++)
even[i] = odd[i] = 0;
scanf("%d %d", &row, &column);
count = row;
int* matrix = (int*)malloc(row * column * sizeof(int));
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++)
scanf("%d", &*(matrix + i * column + j));
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++)
printf("%d ", *(matrix + i * column + j));
printf("\n");
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
temp = *(matrix + i * column + j);
if (temp % 2 == 0)
even[i]++;
else odd[i]++;
}
}
for (int i = 0; i < count; i++)
printf("%d %d\n", even[i], odd[I]);
return 0;
}
If there are some errors with variable names, I was translating most of them here directly, so maybe I missed some of them, sorry in advance.
You can create the swap function first, then compare the even value between the rows.
// Swap 2 elements
void swap_elt(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// swap row m and row n
void swap(int *matrix, int m, int n, int column) {
for (int j = 0; j < column; ++j)
{
swap_elt(matrix + m * column + j, matrix + n * column + j);
}
return;
}
The for loop for compare the even value between the rows:
for (int i = 0; i < count; i++) {
for (int j = i; j < count; j++)
{
if (even[i] < even[j]) {
swap(matrix, i, j, column);
}
}
}
Test:
3 3
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
1 2
2 1
1 2
4 5 6
1 2 3
7 8 9
I won't say that this is 100% the answer but I tried my best (Sorry that my solution is a little different, I think I do understand your question but I can't make my answer better. Edit: I also include user3386109's idea too (at the bottom of my answer).
#include <stdio.h>
int main ()
{
int row, col;
scanf ("%d %d%*c", &row, &col);
int mtx[row][col];
for (int x=0; x<row; x++)
{
for (int y=0; y<col; y++) scanf ("%d%*c", &mtx[x][y]);
}
//Printing the original matrix
for (int x=0; x<row; x++)
{
for (int y=0; y<col; y++)
{
if (y) printf (" %d", mtx[x][y]);
else printf ("%d", mtx[x][y]);
}
printf ("\n");
}
int odd[row], even[row]; //Make a var to store odd/even value each row
for (int x=0; x<row; x++)
{
odd[x]=even[x]=0; //Set to 0 before doing an operation, for safety
for (int y=0; y<col; y++)
{
if (mtx[x][y]%2) odd[x]++; //If it is odd, odd[which_row] incremented
else even[x]++; //Else, even gets incremented
}
printf ("%d %d\n", even[x], odd[x]);
}
int temp;
for (int x=0; x<row-1; x++)
{
for (int y=x+1; y<row; y++)
{
if (even[x]<even[y]) //Same "concept" like bubble sort
{
for (int z=0; z<col; z++)
{
temp=mtx[x][z]; //Swapping the content of the matrix
mtx[x][z]=mtx[y][z];
mtx[y][z]=temp;
}
temp=even[x]; //Swap the value of even[x]
even[x]=even[y];
even[y]=temp;
}
}
}
//Print the result
for (int x=0; x<row; x++)
{
for (int y=0; y<col; y++)
{
if (y) printf (" %d", mtx[x][y]);
else printf ("%d", mtx[x][y]);
}
printf ("\n");
}
return 0;
}
Sample IO:
3 3
1 3 5
7 4 6
2 9 3
1 3 5
7 4 6
2 9 3
0 3
2 1
1 2
7 4 6
2 9 3
1 3 5
user3386109's idea:
for (int x=maxEven; x>=0; x--)
{
for (int y=0; y<row; y++)
{
if (even[y]==x)
{
for (int z=0; z<col; z++)
{
if (z) printf (" %d", mtx[y][z]);
else printf ("%d", mtx[y][z]);
}
printf ("\n");
break;
}
}
}
Related
I got into some problem when I wanted to make a program in C that rotate 90 degrees counterclockwise a matrix that has a different value of row and column. This is the code:
#include <stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void transpose(int row, int column, int matrix[row][column])
{
for (int i = 0; i < row; i++)
{
for (int j = i; j < column; j++)
{
swap(&matrix[i][j], &matrix[j][i]);
}
}
}
void reverseColumns(int row, int column, int matrix[row][column])
{
for (int i = 0; i < column; i++)
{
for (int j = 0, k = column - 1; j < k; j++, k--)
{
swap(&matrix[j][i], &matrix[k][i]);
}
}
}
int main()
{
int row, column;
scanf("%d %d", &row, &column);
int matrix[row][column];
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
scanf("%d", &matrix[i][j]);
}
}
int rotation_times;
scanf("%d", &rotation_times);
rotation_times = rotation_times % 4;
for(int i = 0; i < rotation_times; i++)
{
transpose(row, column, matrix);
reverseColumns(row, column, matrix);
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
I wanted to rotate a 2x3 sized matrix 2 times, this is the input:
2 3
1 1 1
2 3 4
2
This is the expected output:
4 3 2
1 1 1
And this is the actual output:
0 0 422510592
4 3 2
I'm trying to see what's wrong by printing 3 rows and 3 columns and this is what I got:
0 0 -2119237632
4 3 2
1 1 1
Does anyone know what happened and how to fix it? Thanks in advance.
Write a program to read matrix A that has a size of B x K and display the largest element in each column. Use pointers to access elements in an array.
Note: I need to use functions to solve this problem.
Input
4 4
1 3 2 1
8 4 3 2
1 2 3 4
9 8 7 6
Output
9 8 7 6
Here is what I've been trying so far:
#include <stdio.h>
void MAX(int *ptr, int b, int k)
{
int i, j, y;
for (j = 0; j < k; j++)
{
y = 0;
for (i = 0; i < b; i++)
{
if (y < *ptr)
{
y = *ptr;
}
ptr++;
}
printf("%d ", y);
}
}
int main()
{
int b, k;
scanf("%d %d", &b, &k);
int arr[b][k];
for (int i = 0; i < b; i++)
{
for (int j = 0; j < k; j++)
{
scanf("%d", &arr[i][j]);
}
}
MAX(&arr[0][0], b, k);
return 0;
}
From what I've been trying so far, I don't get the output that matches the question. I've been looking for what's wrong, but I still didn't find it. So, could someone tell me what do I miss?
int main() {
int n = 0;
int matrix[n][n];
printf("Insert the order of the matrix:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
for a 2x2 matrix the output should be 0 1 1 2, but it is 1 2 1 2, and for a 3x3 matrix it should be 0 1 2 1 2 3 2 3 4 but it shows 2 3 4 2 3 4 2 3 4
The issue is that my output is always just my first row of the matrix, repeated n times. Any help?
#include <stdio.h>
int main() {
int n = 0;
printf("Insert the order of the matrix:");
scanf("%d", &n);
int matrix[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
}
You declare the array with size n equal zero. Then the length of a row is zero, hence the offset for each consecutive row is zero, too. That means all rows are stored at the same place. As a final result, the last row's values overwrite all the previous rows.
Do input n before declaring matrix[n][n].
the program should this:
the matrix use 1,2,...,n on first line and 2,3,...n,n-1 on second etc, for instance :
input :
5
expected output:
1 2 3 4 5
2 3 4 5 4
3 4 5 4 3
4 5 4 3 2
5 4 3 2 1
i get try maybe anyone can help me to solve this program.
this my program before:
#include <stdio.h>
void makeSymmetricMatrix(int n) {
int i,j;
int matrix[n][n];
for( i = 0; i<n; i++){
int count = 1;
for( j = 0; j <n; j++){
if(i == j){
matrix[i][j] = 0;
}else{
matrix[i][j] = count++;
}
}
}
for( i = 0; i<n; i++){
for( j = 0; j <n; j++){
printf("%d", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int n = 5;
makeSymmetricMatrix(n);
}
I really need your correction about my program
Try this function.
void makeSymmetricMatrix(int n)
{
int i, j, count, decrease;
int matrix[n][n];
for(i = 0; i < n; i++)
{
decrease = 0;
count = i+1;
for(j = 0; j < n; j++)
{
matrix[i][j] = count;
if(count == n)
decrease = 1;
if(decrease == 1)
count--;
else
count++;
}
}
for( i = 0; i<n; i++){
for( j = 0; j <n; j++){
printf("%d", matrix[i][j]);
}
printf("\n");
}
}
Basically it stores a variable decrease which gets triggered when count reaches the maximum value (n here). Once that happens, numbers are stored in a reverse order.
Result for n = 6:
123456
234565
345654
456543
565432
654321
can be done using :
void makeSymmetricMatrix(int n) {
int i,j;
int matrix[n][n];
for (i = 0; i<n; i++) {
int v = i+1, offset = 1;
for (j = 0; j <n; j++){
matrix[i][j] = v;
if (v == n)
offset = -1;
v += offset;
}
}
for( i = 0; i<n; i++){
for( j = 0; j <n; j++){
printf("%d ", matrix[i][j]);
}
putchar('\n');
}
}
in each line the first value equals the rank of the line plus 1 and by default the next value is the current plus 1
each time the value reaches n the rest of the values of the same line are the previous minus 1
After the change compilation and execution :
pi#raspberrypi:/tmp $ gcc -Wall m.c
pi#raspberrypi:/tmp $ ./a.out
1 2 3 4 5
2 3 4 5 4
3 4 5 4 3
4 5 4 3 2
5 4 3 2 1
pi#raspberrypi:/tmp $
How do I add rows and columns to a 3X4 array?
output:
1 2 3 4 10
5 6 7 8 26
9 10 11 12 42
Do I use a for loop? I can't get the logic.
int main()
{
int arr[3][4], r, c;
for (r=0; r < 3; r++)
{
for (c=0; c < 4; c++)
{
arr[r][c] = 1+r+c;
printf("%d ", arr[r][c]);
}
printf("\n");
}
system("PAUSE");
return 0;
}
Currently, your matrix contains the following:
1 2 3 4
2 3 4 5
3 4 5 6
Change arr[r][c] = 1+r+c; to arr[r][c] = 1+c+(COLS*r); where COLS is the number of columns that the matrix has.
1 2 3 4
5 6 7 8
9 10 11 12
You can now iterate through each row and compute the sum:
int i, j;
for (i = 0; i < ROWS; ++i) {
int sum = 0;
for (j = 0; j < COLS; ++j) {
sum += arr[i][j];
}
printf("%d\n", sum);
}
int arr[3][4], r, c, i = 1;
for (r=0; r < 3; r++){
int sum = 0;
for (c=0; c < 4; c++){
arr[r][c] = i++;
sum += arr[r][c];
printf("%2d ", arr[r][c]);
}
printf("%d\n", sum);
}