0% found this document useful (0 votes)
30 views24 pages

Agff Assignments

Ggf

Uploaded by

subhadeepsamai1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views24 pages

Agff Assignments

Ggf

Uploaded by

subhadeepsamai1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ASSIGNMENTS ON ARRAY

1. Write a program in C to store elements in an array and print them.

#include<stdio.h>
int main()
{
int arr[100],i,n;
printf("Enter the number of array elements : ");
scanf("%d",&n);
printf("Enter the array elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("The Entered array elements :\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}

Output :
Enter the number of array elements : 10
Enter the array elements : 1 5 4 2 36 5 9 4 5 2
The Entered array elements :
1 5 4 2 36 5 9 4 5 2

2. Write a program in C to read n number of values in an array and display them in


reverse order.

#include<stdio.h>
int main() {
int arr[100],i,n;
printf("Enter the number of array elements : ");
scanf("%d",&n);
printf("Enter the array elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("The reverse array elements : \n");
for(i=n-1;i>=0;i--)
printf("%d ",arr[i]);
return 0;
}

Output :
Enter the number of array elements : 10
Enter the array elements : 1 2 3 5 6 4 9 8 7 25
The reverse array elements :
25 7 8 9 4 6 5 3 2 1
3. Write a program in C to find the sum of all elements of the array.

#include<stdio.h>
int main()
{
int arr[100],i,n,sum=0;
printf("Enter the number of array elements : ");
scanf("%d",&n);
printf("Enter the array elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=n-1;i>=0;i--)
sum=sum+ arr[i];
printf("The sum of all elements of array is : %d\n",sum);
return 0;
}

Output :

Enter the number of array elements : 10


Enter the array elements : 1 2 3 4 5 6 7 8 9 10
The sum of all elements of array is : 55

4. Write a program in C to copy the elements of one array into another array.

#include<stdio.h>
int main()
{
int arr[100],i,n,copy[100]={0};
printf("Enter the number of array elements : ");
scanf("%d",&n);
printf("Enter the array elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
copy[i]=arr[i];
printf("The copied array elements is : ");
for(i=0;i<n;i++)
printf(" %d",copy[i]);
return 0;
}

Output :

Enter the number of array elements : 10


Enter the array elements : 1 2 3 4 5 6 7 8 9 10
The copied array elements is : 1 2 3 4 5 6 7 8 9 10
5. Write a program in C to count the total number of duplicate elements in an array.

#include<stdio.h>
int main()
{
int arr[100],copy[100],i,j,n,count=0;
printf("Enter the number of array elements : ");
scanf("%d",&n);
printf("Enter the array elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(arr[i]==arr[j]) {
count++;
break;
}
}
printf("The total number of duplicate elements is: %d\n",count);
return 0;
}

Output :

Enter the number of array elements : 10


Enter the array elements : 1 2 4 1 5 6 2 9 8 3
The total number of duplicate elements is: 2

6. Write a program in C to print all unique elements in an array.

#include <stdio.h>
int main()
{
int i,j,isUnique,n,arr[100];
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter %d elements : ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Unique elements in the array are: ");


for ( i = 0; i < n; i++)
{
isUnique = 1;
for ( j = 0; j < n; j++)
{
if (i != j && arr[i] == arr[j])
{
isUnique = 0;
break;
}
}
if (isUnique==1)
printf("%d ", arr[i]);
}
return 0;
}

Output :

Enter the size of the array: 10


Enter 10 elements : 1 2 3 4 2 6 5 1 8 9
Unique elements in the array are: 3 4 6 5 8 9

7. Write a program in C to merge two arrays of the same size sorted in descending
order.

#include<stdio.h>
int main()
{
int arr1[100],arr2[100],arr3[100],n1,n2;
int i=0, j=0,k=0;
printf("Enter the number of FIRST array elements : ");
scanf("%d",&n1);
printf("Enter the 1st array elements(in decending order) : ");
for(i=0;i<n1;i++)
scanf("%d",&arr1[i]);
printf("Enter the number of SECOND array elements : ");
scanf("%d",&n2);
printf("Enter the 2nd array elements(in decending order) : ");
for(i=0;i<n2;i++)
scanf("%d",&arr2[i]);
while(i<n1 && j<n2)
{
if(arr1[i]>arr2[j])
arr3[k++]=arr1[i++];
else
arr3[k++]=arr2[j++];
}
while(i<n1 )
arr3[k++]=arr1[i++];
while(j<n2)
arr3[k++]=arr2[j++];
printf("The merged array elements in decending order is : ");
for(i=0;i<(n1+n2);i++)
printf("%d ",arr3[i]);
return 0;
}

Output :
Enter the number of FIRST array elements : 5
Enter the 1st array elements(in decending order) : 84 76 54 42 36
Enter the number of SECOND array elements : 5
Enter the 2nd array elements(in decending order) : 71 63 51 38 12
The merged array elements in decending order is : 84 76 71 63 54 51 42 38 36 12

8. Write a program in C to count the frequency of each element of an array.

#include <stdio.h>
int main()
{
int array[100], frequency[100];
int n, i, j, count;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
frequency[i] = -1;
}
for (i = 0; i < n; i++)
{
count = 1;
if (frequency[i] == -1)
{
for (j = i + 1; j < n; j++)
{
if (array[i] == array[j])
{
count++;
frequency[j] = 0;
}
}
frequency[i] = count;
}
}
printf("Element\tFrequency\n");
for (i = 0; i < n; i++)
{
if (frequency[i] != 0)
printf("%d\t%d\n", array[i], frequency[i]);
}
return 0;
}

Output :
Enter the number of elements in the array: 7
Enter 7 elements: 1 2 2 3 4 4 4
Element Frequency
1 1
2 2
3 1
4 3

9. Write a program in C to find the maximum and minimum elements in an array.

#include<stdio.h>
int main()
{
int i,arr[100],n,min,max;
printf("Enter the number of array elements : ");
scanf("%d",&n);
printf("Enter the %d number of elements : ",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
max=arr[0];
min=arr[n-1];
for(i=0;i<n;i++)
{
if(max < arr[i])
max = arr[i];
if(min > arr[i])
min= arr[i];
}
printf("The Maximum element is : %d\n",max);
printf("The Minimum element is : %d\n",min);
return 0;
}

Output :

Enter the number of array elements : 10


Enter the 10 number of elements : 5 9 4 25 63 75 94 81 2 6
The Maximum element is : 94
The Minimum element is : 2
10. Write a program in C to separate odd and even integers into separate arrays.

#include<stdio.h>
int main()
{
int arr[100],odd[100],even[100],i,j=0,n;
int k=0;
printf("Enter the number of elements in array : ");
scanf("%d",&n);
printf("Enter the array elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);

for(i=0;i<n;i++)
{
if(arr[i] % 2 == 0)
even[k++] = arr[i];
else
odd[j++] = arr[i];
}
printf("The even array is : \n");
for(i=0;i<k;i++)
printf("%d ",even[i]);
printf("\nThe odd array is : \n");
for(i=0;i<j;i++)
printf("%d ",odd[i]);
return 0;
}

Output :
Enter the number of elements in array : 10
Enter the array elements : 1 2 3 4 5 6 7 8 9 10
The even array is : 2 4 6 8 10
The odd array is : 1 3 5 7 9

11. Write a program in C to sort elements of an array in ascending order.

#include<stdio.h>
int main()
{
int arr[100],a=0,i,j=0,n;
printf("Enter the number of elements in array : ");
scanf("%d",&n);
printf("Enter the array elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
a = arr[i];
arr[i]=arr[j];
arr[j]=a;
}
}
}
printf("The array in ascending order : ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}

Output :
Enter the number of elements in array : 10
Enter the array elements : 1 9 46 2 5 7 65 36 71 52
The array in ascending order : 1 2 5 7 9 36 46 52 65 71

12. Write a program in C to delete an element at a desired position from an array.

#include<stdio.h>
int main()
{
int arr[100],a=0,i,j=0,n;
int pos;
printf("Enter the number of elements in array : ");
scanf("%d",&n);
printf("Enter the array elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Enter the position to delete an element : ");
scanf("%d",&pos);

while(j!= (pos - 1))


j++;
while(j<n)
{
arr[j]=arr[j+1];
j++;
}
n--;

printf("The array in ascending order : ");


for(i=0;i<n;i++)
printf("%d ",arr[i]);

return 0;
}

Output :
Enter the number of elements in array : 10
Enter the array elements : 1 2 3 4 5 6 7 8 9 10
Enter the position to delete an element : 5
The array in ascending order : 1 2 3 4 6 7 8 9 10

13. Write a program in C to find the second largest element in an array.

#include<stdio.h>
int main()
{
int arr[100],a=0,i,j=0,n;
printf("Enter the number of elements in array : ");
scanf("%d",&n);
printf("Enter the array elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
a = arr[i];
arr[i]=arr[j];
arr[j]=a;
}
}
}
printf("\nThe array in desending order : ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("The second largest element is %d \n",arr[1]);
return 0;
}

Output :
Enter the number of elements in array : 10
Enter the array elements : 1 5 4 9 54 6 2 8 65 23
The array in desending order : 65 54 23 9 8 6 5 4 2 1
The second largest element is 54
14. Write a program in C for adding two matrices of the same size.

#include<stdio.h>
int main()
{
int a[100][100],b[100][100],sum[100][100],i,j;
int r,c;
printf("Enter the matrix row and column : ");
scanf("%d%d",&r,&c);

printf("Enter the first matrix elements :\n");


for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);

printf("Enter the second matrix elements :\n");


for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);

printf("\nThe sum of two matrix :\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%d",a[i][j] + b[i][j]);
printf("\n");
}
return 0;
}

Output :
Enter the matrix row and column : 2 2
Enter the first matrix elements :
1234
Enter the second matrix elements :
5614

The sum of two matrix :


6 8
4 8
15. Write a program in C for the multiplication of two (square) matrices.

#include <stdio.h>
int main()
{
int a[25][25], b[25][25], c[25][25], i, j, m, n, k, r, s;
printf("Enter the rows and colums of A matrics: ");
scanf("%d%d", &m, &n);
printf("Enter the rows and colums of B matrics: ");
scanf("%d%d", &r, &s);
if(n==r)
{
printf("\nEnter the elements of A matrix : \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
scanf("\t%d", &a[i][j]);
}
printf("\nEnter the elements of B matrix : \n");
for (i = 0; i < r; i++)
{
for (j = 0; j < s; j++)
scanf("\t%d", &b[i][j]);
}
printf("\nA matrix is : \n");
for (i = 0; i < m; i++)
{
printf("\n");
for (j = 0; j < n; j++)
printf("\t%d", a[i][j]);
}
printf("\nB matrix is :\n");
for (i = 0; i < r; i++)
{
printf("\n");
for (j = 0; j < s; j++)
printf("\t%d", b[i][j]);
}
for(i = 0; i < m; i++)
{
printf("\n");
for (j = 0; j < s; j++)
{
c[i][j] = 0;
for (k = 0; k < r; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
printf("\nThe Multiplication of two matrics : \n");
for(i = 0; i < m; i++)
{
printf("\n");
for (j = 0; j < s; j++)
printf("\t%d", c[i][j]);
}
}
else
printf("\nMatrix Multiplication is not possible. ");
return 0;
}

Output :

Enter the rows and colums of A matrics: 3 2


Enter the rows and colums of B matrics: 2 3

Enter the elements of A matrix :


125469

Enter the elements of B matrix :


946257

A matrix is :

1 2
5 4
6 9

B matrix is :

9 4 6
2 5 7

The Multiplication of two matrics :

13 14 20
53 40 58
72 69 99
16. Write a program in C to find the transpose of a given matrix.

#include<stdio.h>
int main()
{
int a[100][100],b[100][100],i,j;
int r,c;
printf("Enter the matrix row and column : ");
scanf("%d%d",&r,&c);
printf("Enter the matrix elements :\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is : \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
b[j][i]=a[i][j];

printf("\nThe transpose matrix is : \n");


for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
printf("\t%d",b[i][j]);
printf("\n");
}
return 0;
}

Output :
Enter the matrix row and column : 3 3
Enter the matrix elements :
123456789

The matrix is :
1 2 3
4 5 6
7 8 9

The transpose matrix is :


1 4 7
2 5 8
3 6 9
17. Write a program in C to find the sum of the right diagonals of a matrix.

#include<stdio.h>
int main()
{
int a[100][100],i,j;
int sum=0,n;
printf("Enter the square matrix row : ");
scanf("%d",&n);
printf("Enter the matrix elements :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is : \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(i+j==(n-1))
sum+=a[i][j];
}

printf("\nThe sum of the right diagonals is : %d ",sum);

return 0;
}

Output :
Enter the square matrix row : 3
Enter the matrix elements :
1 2 3 4 56 7 8 9 4

The matrix is :
1 2 3
4 56 7
8 9 4

The sum of the right diagonals is : 67


18. Write a program in C to print or display the lower triangular of a given matrix.

#include<stdio.h>
int main()
{
int a[100][100],i,j,n;
printf("Enter the square matrix row & column : ");
scanf("%d",&n);
printf("Enter the matrix elements :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is : \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(i!=j && i<j)
a[i][j]=0;
}
printf("\nThe lower triangular matrix is : \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
return 0;
}

Output :
Enter the square matrix row & column : 3
Enter the matrix elements :
123456789

The matrix is :
1 2 3
4 5 6
7 8 9
The lower triangular matrix is :
1 0 0
4 5 0
7 8 9
19. Write a program in C to calculate the determinant of a 3 x 3 matrix.

#include<stdio.h>
int main()
{
int a[100][100],i,j,n,det;
printf("Enter the 3*3 matrix elements : \n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("The Entered matrix is : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("\t%d",a[i][j]);
printf("\n");
}

det = a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2]) - a[1][0]*(a[0][1]*a[2][2]-a[2][1]*a[0][2]) +


a[2][0]*(a[0][1]*a[1][2]-a[1][1]*a[0][2]);
printf("\n The determination of the 3*3 matrix is : %d\n",det);
return 0;
}

Output :
Enter the 3*3 matrix elements :
964857231
The Entered matrix is :
9 6 4
8 5 7
2 3 1

The determination of the 3*3 matrix is : -52

20. Write a program in C to accept a matrix and determine whether it is a sparse


matrix.

#include <stdio.h>
int main() {
int a[10][10], i, j, m, n;
int count = 0;
int sparse[3][10], k;
printf("Enter the rows matrix : ");
scanf("%d", &m);
printf("Enter the column matrix : ");
scanf("%d", &n);
printf("Enter the matrix elements : ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nThe Entered matrix : \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("\t%d", a[i][j]);
}
printf("\n");
}
printf("\n\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (a[i][j] != 0) {
count++;
}
}
}

if (count >= ((m * n) / 2)) {


k = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (a[i][j] != 0) {
sparse[0][k] = i;
sparse[1][k] = j;
sparse[2][k] = a[i][j];
k++;
}
}
}
printf("The matrix elements (which are nonzero) row, col and value : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < count; j++) {
printf("\t%d", sparse[i][j]);
}
printf("\n");
}
printf("The Matrix is a sparse matrix.");
return 0;
}

Output :
Enter the rows matrix : 4
Enter the column matrix : 5
Enter the matrix elements : 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0
The Entered matrix :
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0

The Matrix is a sparse matrix.The matrix elements (which are nonzero) row, col and value :
0 0 1 1 3 3
2 4 2 3 1 2
3 4 5 7 2 6

21. Write a program in C to find a pair with given sum in the array.

#include <stdio.h>
int main() {
int array[100], n, sum;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
printf("Enter the sum to be found: ");
scanf("%d", &sum);
int found = 0;
printf("Pairs with the given sum %d are:\n", sum);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (array[i] + array[j] == sum) {
printf("(%d, %d)\n", array[i], array[j]);
found = 1;
}
}
}
if (!found)
printf("No pairs found with the given sum.\n");
return 0;
}

Output :
Enter the number of elements in the array: 5
Enter 5 elements:
12345
Enter the sum to be found: 6

Pairs with the given sum 6 are:


(1, 5)
(2, 4)
22. Write a program in C to rotate an array by N positions.

#include <stdio.h>
int rotate_array(int arr[], int n, int positions) {
positions = positions % n; // Handle cases where positions > n
int temp[positions];
for (int i = 0; i < positions; i++) {
temp[i] = arr[n - positions + i];
}
for (int i = n - 1; i >= positions; i--) {
arr[i] = arr[i - positions];
}
for (int i = 0; i < positions; i++) {
arr[i] = temp[i];
}
}

int main() {
int n, positions;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the number of positions to rotate: ");
scanf("%d", &positions);

rotate_array(arr, n, positions);

printf("Rotated array:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}

Output :
Enter the number of elements in the array: 5
Enter 5 elements:
12345
Enter the number of positions to rotate: 2

Rotated array:
45123
23. Write a program in C to return the minimum number of jumps to reach the end of
the array.

#include <stdio.h>
int minJumps(int arr[], int size) {
int maxReach = 0, currentJump = 0, jumps = 0;
for (int i = 0; i < size - 1; i++) {
maxReach = (i + arr[i] > maxReach) ? i + arr[i] : maxReach;
currentJump--;
if (i == currentJump) {
jumps++;
currentJump = maxReach;
}
}
return jumps;
}

int main() {
int arr[] = {1, 3, 5, 8, 9, 2, 6, 7};
int size = sizeof(arr) / sizeof(arr[0]);
int minJumpsNeeded = minJumps(arr, size);
printf("Minimum number of jumps to reach the end: %d\n", minJumpsNeeded);

return 0;
}

Output :
Minimum number of jumps to reach the end: 3

24. Write a program in C to move all zeroes to the end of a given array.

#include <stdio.h>

void moveZeroes(int arr[], int size) {


int nonZeroIndex = 0;

for (int i = 0; i < size; i++) {


if (arr[i] != 0) {
arr[nonZeroIndex] = arr[i];
nonZeroIndex++;
}
}

for (int i = nonZeroIndex; i < size; i++) {


arr[i] = 0;
}
}
int main() {
int arr[] = {0, 1, 0, 3, 12};
int size = sizeof(arr) / sizeof(arr[0]);
moveZeroes(arr, size);
printf("Array after moving zeroes to the end: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Output :
Array after moving zeroes to the end: 1 3 12 0 0

25. Write a program in C to find the row with the maximum number of 1s.

#include <stdio.h>
int findRowWithMaxOnes(int mat[][100], int rows, int cols) {
int rowIndex = 0, maxOnes = 0;
for (int i = 0; i < rows; i++) {
int onesCount = 0;
for (int j = 0; j < cols; j++) {
if (mat[i][j] == 1) {
onesCount++;
}
}
if (onesCount > maxOnes) {
rowIndex = i;
maxOnes = onesCount;
}
}
return rowIndex;
}

int main() {
int mat[][100] = {{0, 1, 1}, {0, 0, 1}, {1, 1, 1}};
int rows = 3, cols = 3;

int rowWithMaxOnes = findRowWithMaxOnes(mat, rows, cols);


printf("Row with the maximum number of 1s: %d\n", rowWithMaxOnes);
return 0;
}

Output :
Row with the maximum number of 1s: 2
26. Write a program in C to replace every element with the greatest element on its
right side.

#include <stdio.h>
void replaceWithGreatestOnRight(int arr[], int size) {
int max = arr[size - 1];
arr[size - 1] = -1;
for (int i = size - 2; i >= 0; i--) {
int temp = arr[i];
arr[i] = max;
max = (max > temp) ? max : temp;
}
}

int main() {
int arr[] = {17, 18, 23, 12, 19};
int size = sizeof(arr) / sizeof(arr[0]);
replaceWithGreatestOnRight(arr, size);
printf("Array after replacing elements with greatest on right: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output :
Array after replacing elements with greatest on right: 23 19 19 19 19
27. Write a program in C to return only the unique rows from a given binary matrix.

#include <stdio.h>
int isDuplicateRow(int mat[][100], int row, int cols, int previousRows[]) {
for (int i = 0; i < row; i++) {
if (mat[row][0] == mat[i][0]) {
for (int j = 0; j < cols; j++) {
if (mat[row][j] != mat[i][j]) {
break;
}
}
if (j == cols)
return 1;
}
}
return 0;
}

void printUniqueRows(int mat[][100], int rows, int cols) {


int previousRows[rows];
int uniqueCount = 0;
for (int i = 0; i < rows; i++) {
if (!isDuplicateRow(mat, i, cols, previousRows)) {
previousRows[uniqueCount] = i;
uniqueCount++;
}
}
printf("Unique rows:\n");
for (int i = 0; i < uniqueCount; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat[previousRows[i]][j]);
}
printf("\n");
}
}

int main() {
int mat[][100] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 1, 0}};
int rows = 4, cols = 3;
printUniqueRows(mat, rows, cols);
return 0;
}

Output :
Unique rows:
100
010
001
28. Write a program in C to segregate 0s and 1s in an array.

#include <stdio.h>

void segregate0sAnd1s(int arr[], int size) {


int left = 0, right = size - 1;

while (left < right) {


while (arr[left] == 0 && left < right) {
left++;
}
while (arr[right] == 1 && left < right) {
right--;
}

if (left < right) {


int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}
}

int main() {
int arr[] = {0, 1, 0, 1, 1, 0};
int size = sizeof(arr) / sizeof(arr[0]);

segregate0sAnd1s(arr, size);

printf("Array after segregating 0s and 1s: ");


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Output :

Array after segregating 0s and 1s: 0 0 0 1 1 1

You might also like