Adding Rows and Columns 2d - c

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);
}

Related

Why is the multiplication doubling in this C loop?

The code is supposed to take inputs to form a 3x3 Matrix and then multiply each term by the diagonal element of that line, but, for some reason that i don't know, it multiplies two times by the diagonal when the column index is bigger than the row index.
#include <stdio.h>
#define R 3
int a[R][R], i, j;
int main(void) {
for (i = 0; i < R; i++) {
for (j = 0; j < R; j++) {
printf("\nInsira o n%i%i ", i, j);
scanf("%i", &a[i][j]);
}
}
for (i = 0; i < R; i++) {
for (j = 0; j < R; j++) {
a[i][j] = a[i][j] * a[i][i];
}
}
for (i = 0; i < R; i++) {
printf("\n");
for (j = 0; j < R; j++) {
printf("%i ", a[i][j]);
}
}
}
input:
9 8 7
6 5 4
3 2 1
output:
81 648 567
30 25 100
3 2 1
The diagonal value for a given row is being changed before that row has been fully multiplied, so once the column goes past the diagonal, the multiplies are using the new value of that diagonal rather than the old value.
You can fix it (and improve the speed) as follows:
for (i = 0; i < R; i++) {
int tmp = a[i][i];
for (j = 0; j < R; j++) {
a[i][j] *= tmp;
}
}
Also, as mentioned, both i and j should be local variables.

symmetric matrix 2-d array in c program

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 $

Sorting matrix by even numbers in rows

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;
}
}
}

Calculating and printing the sums of the diagonals of a matrix

This is a program that is supposed to calculate the sum of all the diagonals in the matrix and then print them out.
ex. if the matrix is
1 2 3 4 5
2 3 4 5 6
0 1 1 2 5
5 5 5 5 5
7 8 9 7 7
the output should be
17 13 13 10 5
15 17 13 13 10
14 15 17 13 13
13 14 15 17 13
7 13 14 15 17
#include <stdio.h>
int main()
{
int n, sum=0, i, j, sub_i, sub_j, sub1_i, sub1_j;
scanf("%d ", &n);
int array1[n][n];
for(i=0;i<n;i++){
for(j=0; j<n; j++){
scanf("%d", &array1[i][j]);
}
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
sub_i=i;
sub_j=j;
sub1_i=i;
sub1_j=j;
sum=0;
if(j>i){
while(sub_j<n){
sum+=array1[sub_i][sub_j];
sub_i++;
sub_j++;
}
while(sub_j<n){
array1[sub_i][sub_j]=sum;
sub1_i++;
sub1_j++;
}
}
if(i>j){
while(sub_i<n){
sum+=array1[sub1_i][sub1_j];
sub_i++;
sub_j++;
}
while(sub1_i<n){
array1[sub1_i][sub1_j]=sum;
sub1_i++;
sub1_j++;
}
}
}
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf("%d ", array1[i][j]);
}
printf("\n");
}
return 0;
}
When i run the program it prints the array as if no value was assigned to the matrix. Can someone point out what is happening?
Quoting the comment by Weather Vane:
The program alters the array it is examining — see array1[sub_i][sub_j]=sum; — and then prints incorrect values, since you can't correctly sum the diagonals of an array that is changing.
The OP already realize that
... what you are telling me is to assign the values to another array and print that.
Yes, that is way easier:
#include <stdio.h>
int main(void)
{
int n;
// Checking the input is always a good idea, but you
// may prefer something less brutal, in case of error
if (scanf("%d", &n) != 1 || n < 1)
return 1;
int mat[n][n];
for (int i = 0; i < n; ++i) {
for (int j= 0; j < n; ++j) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
// Calculate and store the sum of the diagonals. Note that
// it could be done in the previous loop, but it may be better
// to refactor those snippets into separate functions.
int diagonals[2 * n + 1];
for (int i = 0; i < 2 * n + 1; ++i)
diagonals[i] = 0; // consider 'memset' instead of this loop
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
diagonals[n + i - j] += mat[i][j];
}
}
// Now print the correct values in their position
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
printf("%4d", diagonals[n + i - j]);
}
printf("\n");
}
return 0;
}
Testable HERE.
You can do like the follow:
#include <stdio.h>
#define N 5
int main()
{
int array[N][N] = {
1, 2, 3, 4, 5,
2, 3, 4, 5, 6,
0, 1, 1, 2, 5,
5, 5, 5, 5, 5,
7, 8, 9, 7, 7};
for(int i = 1; i < N; ++i)
for(int j = 1; j < N; ++j)
array[i][j] += array[i - 1][j - 1];
for (int i = 0; i + 1 < N; ++i)
for (int j = 0; j + 1 < N; ++j)
if (i == j)
array[i][j] = array[N - 1][N - 1];
else if (i > j)
array[i][j] = array[N - 1][N - 1 - i + j];
else
array[i][j] = array[N - 1 - j + i][N - 1];
for (int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j)
printf("%d ", array[i][j]);
printf("\n");
}
return 0;
}

Converting to column major fashion with two dimensional array

I am trying to load my matrix so that consecutive numbers will go down the columns using pointer/addressing methods. Currently my program prints the consecutive numbers across the rows.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j;
int twodArray[5][5], *twodArrayptr;
int add = 0;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
twodArray[i][j]= (i*5) + j;
}
}
twodArrayptr = &(twodArray[0][0]);
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
printf("%d ", *(twodArrayptr +((i*5)+j)));
}
printf(" \n");
}
}
If your point is to load the matrix with the opposite order, go with 4py's solution, if your point is to make use of the pointer to access the array in column order, then your solution is below.
You were SO CLOSE, your problem is here (you had i and j swapped). To change from row/col major ordering, you can do:
printf (" %2d", *(twodArrayptr + ((j * MAX) + i)));
Also note the main is type int and therefore returns a value.
Putting it together, you could do something like:
#include <stdio.h>
#define MAX 5
int main (void)
{
int i, j, twodArray[MAX][MAX] = {{0}}, *twodArrayptr = (int *)twodArray;
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
twodArray[i][j] = (i * MAX) + j;
printf (" %2d", twodArray[i][j]);
}
putchar ('\n');
}
putchar ('\n');
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++)
printf (" %2d", *(twodArrayptr + ((j * MAX) + i)));
putchar ('\n');
}
return 0;
}
Example Use/Output
$./bin/rowcol2d
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
0 5 10 15 20
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
Also note:, you do not need to use the variadic function printf to output a single character use putchar instead :)
Change the assignment logic to twodArray[i][j]= (j*5) + i;
Works like a charm.
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
twodArray[i][j]= (j*5) + i;
}
}

Resources