KEMBAR78
C ArraysLab | PDF | Matrix (Mathematics) | Mathematical Analysis
0% found this document useful (0 votes)
5 views13 pages

C ArraysLab

Uploaded by

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

C ArraysLab

Uploaded by

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

Data Structure Using C Lab: Assignment Arrays

1.Write a program to input and display elements of an array.


2. Find the sum and average of elements of an array.
3. Find the maximum and minimum element in an array.
4. Reverse the elements of an array.
5. Count the frequency of each element in an array.
6. Implement linear search on an array.
7. Sort the array using bubble sort.
8. Sort the array using selection sort.
9. Sort the array using insertion sort.
10. Merge two sorted arrays into one sorted array.
11. Rotate an array by k positions (left and right).
12. Find the second largest and second smallest element in an array.
13. Rearrange the array so that all even numbers appear before odd numbers.
14. Read and display a matrix of size m×n.
15. Find the sum of all elements of a matrix.
16. Add two matrices.
17. Multiply two matrices.
18. Find the transpose of a matrix.
19. Calculate the sum of the principal and secondary diagonals of a square matrix.
20. Check if a square matrix is symmetric.
21. Find the determinant of a 2×2 or 3×3 matrix.
22. Find the maximum element in each row and each column of a matrix.
23. Find the row with the maximum sum and the column with the maximum sum.
24. Make Third  second row

5.) Count the frequency of each element in an array.


#include <stdio.h>

int main() {
int arr[100], freq[100], n, i, j, count;

printf("Enter size of array: ");


scanf("%d", &n);

printf("Enter %d elements (seperated by ,): ", n);


for(i = 0; i < n; i++) {
scanf("%d,", &arr[i]);
freq[i] = -1; // initialize frequency array
}

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


count = 1;
for(j = i+1; j < n; j++) {
if(arr[i] == arr[j]) {
count++;
freq[j] = 0; // mark as counted
}
}
//checking if already not counted
if(freq[i] != 0)
freq[i] = count;
}

printf("\nElement Frequency\n");
for(i = 0; i < n; i++) {
if(freq[i] != 0)
printf("%d => %d\n", arr[i], freq[i]);
}

return 0;
}

6.) Implement linear search on an array.


#include <stdio.h>

int main() {
int arr[100], n, i, key, found = 0;

printf("Enter size of array: ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


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

printf("Enter element to search: ");


scanf("%d", &key);

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


if(arr[i] == key) {
printf("Element %d found at position %d\n", key, i+1);
found = 1;
break;
}
}

if(!found)
printf("Element not found\n");

return 0;
}
7) BUBBLE SORT
#include <stdio.h>

int main() {
int arr[100], n, i, j, temp;

printf("Enter size of array: ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


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

// Bubble Sort
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

printf("Sorted array (Bubble Sort): ");


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

return 0;
}

8) SELECTION
#include <stdio.h>

int main() {
int arr[100], n, i, j, min, temp;

printf("Enter size of array: ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


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

// Selection Sort
for(i = 0; i < n-1; i++) {
min = i;
for(j = i+1; j < n; j++) {
if(arr[j] < arr[min])
min = j;
}
// swap
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}

printf("Sorted array (Selection Sort): ");


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

return 0;
}

9) Insertion
#include <stdio.h>

int main() {
int arr[100], n, i, j, key;

printf("Enter size of array: ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


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

// Insertion Sort
for(i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

while(j >= 0 && arr[j] > key) {


arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}

printf("Sorted array (Insertion Sort): ");


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

return 0;
}
10) Merge2 sorted into one sorted array
#include <stdio.h>

int main() {
int a[50], b[50], c[100];
int n1, n2, i=0, j=0, k=0;

printf("Enter size of first sorted array: ");


scanf("%d", &n1);
printf("Enter %d sorted elements: ", n1);
for(i=0;i<n1;i++) scanf("%d",&a[i]);

printf("Enter size of second sorted array: ");


scanf("%d", &n2);
printf("Enter %d sorted elements: ", n2);
for(i=0;i<n2;i++) scanf("%d",&b[i]);

i=j=k=0;
while(i<n1 && j<n2){
if(a[i]<b[j]) c[k++] = a[i++];
else c[k++] = b[j++];
}
while(i<n1) c[k++] = a[i++];
while(j<n2) c[k++] = b[j++];

printf("Merged Sorted Array: ");


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

return 0;
}

11)
#include <stdio.h>

int main() {
int arr[100], n, k, i, j, temp;
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++) scanf("%d",&arr[i]);

printf("Enter k (positions to rotate left): ");


scanf("%d",&k);

for(i=0;i<k;i++){
temp=arr[0];
for(j=0;j<n-1;j++) arr[j]=arr[j+1];
arr[n-1]=temp;
}

printf("Array after left rotation: ");


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

(Q 12) Find the Second largest and second smallest element in an array
#include <stdio.h>

int main() {
int arr[100], n, i, j, temp;
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++) scanf("%d",&arr[i]);

// sort the array (simple bubble sort)


for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}

printf("Second Smallest = %d\n", arr[1]);


printf("Second Largest = %d\n", arr[n-2]);
return 0;
}

(Q 13) Rearrange the array so that all even numbers appear before odd numbers.
#include <stdio.h>

int main() {
int a[10];

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


printf("Enter The %d Elements:",i);
scanf("%d",&a[i]);

}
int p[10]; int j=0;
for (int i=0;i<10;i++){
if (a[i]%2==0) {
p[j++]=a[i];
}

}
for (int i=0;i<10;i++){
if (a[i]%2 !=0) {
p[j++]=a[i];
}

}
for ( int i=0;i<10;i++){
printf("%d ",p[i]);
}

return 0;
}
(Q 14) Read & Display Matrix m X n

#include <stdio.h>
int main() {
int a[10][10], m, n, i, j;
printf("Enter rows and cols: ");
scanf("%d %d",&m,&n);

printf("Enter elements:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}

printf("Matrix is:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
(Q15) Find the sum of all Elements of 2D Matrix
#include <stdio.h>
int main() {
int a[10][10], m, n, i, j;
printf("Enter rows and cols: ");
scanf("%d %d",&m,&n);

printf("Enter elements:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}

printf("Matrix Sum is:\n");


int summ=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
summ +=a[i][j];
}

}
printf("%d\n",summ);
return 0;
}

(Q16) Add Two Matrices now


#include <stdio.h>
int main() {
int a[10][10], m, n, i, j;
int b[10][10];
printf("Enter rows and cols: ");
scanf("%d %d",&m,&n);

printf("Enter Matrix for 1st elements:\n");


for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter elements for second Matrix:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&b[i][j]);
}
}
int s[10][10];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
s[i][j]=a[i][j]+b[i][j];
}
printf("\n");
}
for (int i=0;i<m;i++){
for(int j=0;j<n;j++){
printf("%d ",s[i][j]);
}
printf("\n");
}
return 0;
}

(Q17) Multiply Two Matrices


#include <stdio.h>
int main() {
int a[10][10], m, n, p,q,i, j;
int b[10][10], ans[10][10];
printf("Enter rows and cols: ");
scanf("%d %d",&m,&n);
printf("Enter rows and cols: ");
scanf("%d %d",&p,&q);
if (n==p){
printf("Go Ahead\n");

}
else{
printf("Multiplication Not Possible in this case \n");
}

printf("Enter elements for First Matrix:\n");


for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter elements for Second Matrix:\n");
for(i=0;i<p;i++){
for(j=0;j<q;j++){
scanf("%d",&b[i][j]);
}
}

printf("Original Matrix :\n");

int cr=n;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
ans[i][j]=0;
for (int k=0;k<cr;k++){
ans[i][j] =ans[i][j]+(a[i][k]*b[k][j]);

}
}
printf("\n");
}
printf("-------------Product--------------\n");

for(i=0;i<m;i++){
for(j=0;j<q;j++){
printf("%d ",ans[i][j]);
}
printf("\n");
}
return 0;
}

(Q18) Find the Transpose of a Matrix


#include <stdio.h>
int main() {
int a[10][10], m, n, i, j;
printf("Enter rows and cols: ");
scanf("%d %d",&m,&n);

printf("Enter elements:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}

printf("Original Matrix :\n");

for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("-------------Transpose Matrix--------------\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[j][i]);
}
printf("\n");
}
return 0;
}

(Q19) Calculate the sum of the Principle & Secondary diagonals of the square matrix.

(Q20) Check if a square matrix is symmetric

#include <stdio.h>
int main() {
int a[10][10], m, n, i, j;
printf("Enter rows and cols: ");
scanf("%d %d",&m,&n);

printf("Enter elements:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}

printf("Original Matrix :\n");


int f=1;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if (a[i][j]!=a[j][i]){
f=0;
break;
}

}
if (!f) break;

}
if (f){
printf("Symmetric\n");
}
else{
printf("Not Symmetric\n");
}

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

(Q21) Find the determinant of a 2X2 or 3X3 Matrix


(Q22) Find the Maximum element in each row and each column of a Matrix
(Q23) Find the Row with the maximum sum and the column with maximum sum.

You might also like