Agff Assignments
Agff Assignments
#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
#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 :
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 :
#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 :
#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]);
Output :
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
#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
#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 :
#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
#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
#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);
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
#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);
Output :
Enter the matrix row and column : 2 2
Enter the first matrix elements :
1234
Enter the second matrix elements :
5614
#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 :
A matrix is :
1 2
5 4
6 9
B matrix is :
9 4 6
2 5 7
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];
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
#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];
}
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
#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");
}
Output :
Enter the 3*3 matrix elements :
964857231
The Entered matrix is :
9 6 4
8 5 7
2 3 1
#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++;
}
}
}
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
#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>
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;
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;
}
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>
int main() {
int arr[] = {0, 1, 0, 1, 1, 0};
int size = sizeof(arr) / sizeof(arr[0]);
segregate0sAnd1s(arr, size);
return 0;
}
Output :