Professional Documents
Culture Documents
Two Dimensional Arrays
Two Dimensional Arrays
Two Dimensional Arrays
#include <stdio.h>
#define R 4
#define C 4
int i, k = 0, l = 0;
k++;
}
n--;
if (k < m)
m--;
if (l < n)
l++;
int main()
{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
if (n < 0 || m < 0)
return INT_MAX;
else if (m == 0 && n == 0)
return cost[m][n];
else
if (x < y)
else
int main()
{26,48,35,25,11},
{66,10,49,100,17},
{78,60,53,2,85},
{87,29,12,28,90} };
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];
scanf("%d", &array[row][col]);
isUpper = 1;
isUpper = 0;
if(isUpper == 1)
printf("\n");
else
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;
scanf("%d%d",&m,&n);
int i, j;
int mat[m][n];
scanf("%d",&mat[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if ((i + j) == (m -1))
diag += mat[i][j];