Two Dimensional Arrays

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Two Dimensional Arrays

1. Write a C program to print contents of the given matrix in spiral form.


Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

#include <stdio.h>

#define R 4

#define C 4

void spiralPrint(int m, int n, int a[R][C])

int i, k = 0, l = 0;

while (k < m && l < n)

for (i = l; i < n; ++i)

printf("%d ", a[k][i]);

k++;

for (i = k; i < m; ++i)

printf("%d ", a[i][n - 1]);

}
n--;

if (k < m)

for (i = n - 1; i >= l; --i)

printf("%d ", a[m - 1][i]);

m--;

if (l < n)

for (i = m - 1; i >= k; --i)

printf("%d ", a[i][l]);

l++;

int main()

int a[R][C] = {{1,2,3,4},

{5,6,7,8},

{9,10,11,12},

{13,14,15,16}};
spiralPrint(R, C, a);

return 0;

2.You are given a two-dimensional 3*3 array starting from A [0][0]. Write a program to add
the alternate elements of the array and print its sum.

(It should print two different numbers the first being sum of A[ 0][ 0], A [0][ 2], A[ 1] [1],
A [2][ 0], A [2][ 2] and A [0][ 1], A [1] [0], A [1][ 2], A [2][ 1])

Input :

1 2 3
4 5 6
7 8 9
Output:
25
20

#include <bits/stdc++.h>
#include<conio.h>
using namespace std;
void sumAlternate(int* arr, int n)
{
int sum1 = 0, sum2 = 0;
for (int i = 0; i < n * n; i++)
{
if (i % 2 == 0)
sum1 += *(arr + i);
else
sum2 += *(arr + i);
}
cout << "Sum of alternate elements : " << sum1
<< ", " << sum2 << endl;
}
int main()
{
int mat[3][3] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
sumAlternate(&mat[0][0], n);
return 0;
}

3.Write a C program to find the minimum sum path from (0,0) to (N-1,N-1) in
given N x Nmatrix. Only right , down moves are allowed.

Input

12 71 62 10 4
26 48 35 25 11
66 10 49 100 17
78 60 53 2 85
87 29 12 28 90

Output:

12+26+48+10+49+53+12+28+90=32

#include<stdio.h>

#include<limits.h>

#define R 5

#define C 5

int min(int x, int y, int z);

int minCost(int cost[R][C], int m, int n)

if (n < 0 || m < 0)

return INT_MAX;

else if (m == 0 && n == 0)

return cost[m][n];

else

return cost[m][n] + min( minCost(cost, m-1, n-1),

minCost(cost, m-1, n),


minCost(cost, m, n-1) );

int min(int x, int y, int z)

if (x < y)

return (x < z)? x : z;

else

return (y < z)? y : z;

int main()

int cost[R][C] = { {12,71,62,10,4},

{26,48,35,25,11},

{66,10,49,100,17},

{78,60,53,2,85},

{87,29,12,28,90} };

printf(" %d ", minCost(cost, 2, 2));

return 0;

4.Write a program to check whether the given matrix is upper triangular matrix or not.

#include <stdio.h>

#define MAX_ROWS 3

#define MAX_COLS 3

int main()
{

int array[MAX_ROWS][MAX_COLS];

int row, col, isUpper;

printf("Enter elements in matrix of size %dx%d: \n", MAX_ROWS, MAX_COLS);

for(row=0; row<MAX_ROWS; row++)

for(col=0; col<MAX_COLS; col++)

scanf("%d", &array[row][col]);

isUpper = 1;

for(row=0; row<MAX_ROWS; row++)

for(col=0; col<MAX_COLS; col++)

if(col<row && array[row][col]!=0)

isUpper = 0;

if(isUpper == 1)

printf("\nThe matrix is Upper triangular matrix.\n");


for(row=0; row<MAX_ROWS; row++)

for(col=0; col<MAX_COLS; col++)

printf("%d ", array[row][col]);

printf("\n");

else

printf("\nThe matrix is not Upper triangular matrix.");

return 0;

5.Write a program to find the sum of elements in the zig-zag sequence in a given matrix.

Input:
1 2 3
4 5 6
7 8 9
Output:
1+2+3+5+7+8+9=35

#include<stdio.h>

#include<conio.h>

int main()

{
int m, n, sum = 0, row1 = 0, col_n = 0, diag = 0;

printf("enter the order of the matrix\n");

scanf("%d%d",&m,&n);

int i, j;

int mat[m][n];

printf("enter the matrix elements:\n");

for(i = 0; i < m; i++)

for(j = 0; j < n; j++)

scanf("%d",&mat[i][j]);

for(i = 0; i <= 0; i++)

for(j = 0; j < n-1; j++)

row1 = row1 + mat[i][j];

for(j = n-1; j == n-1; j--)

for(i = 0; i < m; i++)

col_n = col_n + mat[j][i];


}

for(i=0;i<m;i++)

for(j=0;j<n;j++)

if ((i + j) == (m -1))

diag += mat[i][j];

if(j == 0 && i == m-1)

col_n = col_n -mat[i][j];

printf("Sum of Zig-Zag pattern is %d ", diag + row1 + col_n);

You might also like