Wa0000

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

One-Dimensional Arrays

1. C program to count total number of elements divisible by a specific number in an


array.
#include <stdio.h>
#define MAX 5
int main(){
int arr[MAX]={0};
int i;
int b;
int count=0;
printf("Enter array elements:\n");
for(i=0; i<MAX; i++)
{
scanf("%d", &arr[i]);
}
printf("enter the number:\n");
scanf("%d",&b);
for(i=0;i<MAX;i++)
{
if(arr[i]%b ==0)
count++;

}
printf("Total elements divisible by %d is %d\n",b, count);
return 0;
}
2. Write a c program to cyclically permute the element of an array. (In right to left
direction).
#include <stdio.h>
#include <stdlib.h>
void print(int* a,int n){
printf("printing ........\n");
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
int* cyclicallyPermute(int* a,int n)
{
int temp=a[0];
for(int i=0;i<n;i++)
{
if(i==n-1)
a[i]=temp;
else
a[i]=a[i+1];
}
return a;
}
int main()
{
int n;
printf("enter array length: ");
scanf("%d",&n);
int* a=(int*)(malloc(sizeof(int)*n));
printf("enter elements: \n");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array before permutation\n");
print(a,n);
a=cyclicallyPermute(a,n);
printf("array after permutation\n");
print(a,n);
return 0;
}

3.Split the given array into K sub-arrays such that maximum sum of all sub arrays is
minimum.

#include <conio.h>

bool check(int mid, int array[], int n, int K)

int count = 0;

int sum = 0;

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


{

if (array[i] > mid)

return False;

sum += array[i];

if (sum > mid) {

count++;

sum = array[i];

count++;

if (count <= K)

return true;

return false;

int solve(int array[], int n, int K)

int start = 1;

int end = 0;

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

end += array[i];

int answer = 0;

while (start <= end) {

int mid = (start + end) / 2;


if (check(mid, array, n, K))

answer = mid;

end = mid - 1;

else {

start = mid + 1;

return answer;

int main()

int array[] = { 1, 2, 3, 4 };

int n = sizeof(array) / sizeof(array[0]);

int K = 3;

cout<<solve(array, n, K);

4.Find element at given index after a number of rotations.

#include <conio.h>

int findElement(int arr[], int ranges[][2],int rotations, int index)

for (int i = rotations - 1; i >= 0; i--)

{
int left = ranges[i][0];

int right = ranges[i][1];

if (left <= index && right >= index)

if (index == left)

index = right;

else

index--;

return arr[index];

int main()

int arr[] = { 1, 2, 3, 4, 5 };

int rotations = 2;

int ranges[2][2] = { { 0, 2 }, { 0, 3 } };

int index = 1;

cout << findElement(arr, ranges, rotations, index);

return 0;

5.Maximum sum of consecutive differences in an circular array.

#include <conio.h>

int maxSum(int arr[], int n)


{

int sum = 0;

sort(arr, arr + n);

for (int i = 0; i < n/2; i++)

sum -= (2 * arr[i]);

sum += (2 * arr[n - i - 1]);

return sum;

int main()

int arr[] = { 4, 2, 1, 8 };

int n = sizeof(arr)/sizeof(arr[0]);

cout << maxSum(arr, n) << endl;

return 0;

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