Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
135 views8 pages

C Array and Matrix Programs

The document contains 6 C program code examples that demonstrate working with arrays: 1) Calculating the average of numbers in an array 2) Finding the largest element in an array 3) Counting the frequency of each element in an array 4) Deleting all duplicate elements from an array 5) Adding two matrices using multi-dimensional arrays 6) Finding the transpose of a matrix

Uploaded by

md robin hossain
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)
135 views8 pages

C Array and Matrix Programs

The document contains 6 C program code examples that demonstrate working with arrays: 1) Calculating the average of numbers in an array 2) Finding the largest element in an array 3) Counting the frequency of each element in an array 4) Deleting all duplicate elements from an array 5) Adding two matrices using multi-dimensional arrays 6) Finding the transpose of a matrix

Uploaded by

md robin hossain
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/ 8

1.

Write a C Program to Calculate Average Using Arrays

#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;

printf("Enter the numbers of elements: ");


scanf("%d", &n);

while (n > 100 || n < 1) {


printf("Error! number should in range of (1 to
100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}

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


printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}

avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
2. Write a C Program to Find the Largest Element of an Array.

#include <stdio.h>
int main() {
int i, n;
float arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);

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


printf("Enter number%d: ", i + 1);
scanf("%f", &arr[i]);
}

// storing the largest number to arr[0]


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

printf("Largest element = %.2f", arr[0]);

return 0;
}
3. Write a C program to count the frequency of each element in an array.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

int main()
{
int arr[MAX_SIZE]; // Declares an array of size 100
int size; // Total number of elements in array
int i, j, k; // Loop control variables

/* Input size of the array */


printf("Enter size of the array : ");
scanf("%d", &size);

/* Input elements in the array */


printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/*
* Find duplicate elements in array
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/* If any duplicate found */
if(arr[i] == arr[j])
{
/* Delete the current duplicate element */
for(k=j; k<size; k++)
{
arr[k] = arr[k + 1];
}

/* Decrement size after removing duplicate element */


size--;

/* If shifting of elements occur then don't increment j */


j--;
}
}
}

/*
* Print array after deleting duplicate elements
*/
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}

return 0;
}

4. Write a C program to delete all duplicate elements from an array

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

int main()
{
int arr[MAX_SIZE]; // Declares an array of size 100
int size; // Total number of elements in array
int i, j, k; // Loop control variables

/* Input size of the array */


printf("Enter size of the array : ");
scanf("%d", &size);

/* Input elements in the array */


printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
/*
* Find duplicate elements in array
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/* If any duplicate found */
if(arr[i] == arr[j])
{
/* Delete the current duplicate element */
for(k=j; k<size; k++)
{
arr[k] = arr[k + 1];
}

/* Decrement size after removing duplicate element */


size--;

/* If shifting of elements occur then don't increment j */


j--;
}
}
}

/*
* Print array after deleting duplicate elements
*/
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}

return 0;
}
5. Write a C Program to Add Two Matrix Using Multi-dimensional Arrays.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

int main()
{
int arr[MAX_SIZE]; // Declares an array of size 100
int size; // Total number of elements in array
int i, j, k; // Loop control variables

/* Input size of the array */


printf("Enter size of the array : ");
scanf("%d", &size);

/* Input elements in the array */


printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/*
* Find duplicate elements in array
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/* If any duplicate found */
if(arr[i] == arr[j])
{
/* Delete the current duplicate element */
for(k=j; k<size; k++)
{
arr[k] = arr[k + 1];
}

/* Decrement size after removing duplicate element */


size--;

/* If shifting of elements occur then don't increment j */


j--;
}
}
}

/*
* Print array after deleting duplicate elements
*/
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}

return 0;
}

6. Write a C Program to Find the Transpose of a Matrix.

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

// Assigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

// Displaying the matrix a[][]


printf("\nEntered matrix: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// Finding the transpose of matrix a


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// Displaying the transpose of matrix a


printf("\nTranspose of the matrix:\n");
for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}

You might also like