0% found this document useful (0 votes)
10 views17 pages

EXP 8 PPS Lab

Uploaded by

hod cse
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)
10 views17 pages

EXP 8 PPS Lab

Uploaded by

hod cse
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/ 17

EX.

NO:8 ARRAYS: 1D AND 2D


1.Write a program in C to read n number of values in an array and display them in
reverse order.
Aim
To write a C program to read n number of values in an array and display them in reverse
order.

Algorithm

1. Start.
2. Prompt the user to input the number of elements, n.
3. Declare an array of size n.
4. Use a loop to input the elements of the array.
5. Use another loop to display the elements of the array in reverse order.
6. End.

Program

#include <stdio.h>
int main() {
int n;
// Prompt user to input the number of elements
printf("Input the number of elements to store in the array: ");
scanf("%d", &n);
int arr[n];
// Input array elements
printf("Input %d number of elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr[i]);
}
// Display values stored in the array
printf("The values stored into the array are:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Display values stored in reverse order
printf("The values stored into the array in reverse are:\n");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output
Input the number of elements to store in the array: 3
Input 3 number of elements in the array:
element - 0: 2
element - 1: 5
element - 2: 7
The values stored into the array are:
257
The values stored into the array in reverse are:
752

Result
Thus the program to read n number of values in an array and display them in reverse order
completed.

2.Write a program in C to copy the elements of one array into another array.
Aim
To Write a C Program to copy the elements of one array into another array.

Algorithm

1. Start.
2. Prompt the user to input the number of elements (n) to be stored in the array.
3. Declare two arrays of size n:
o arr1 for the original array.
o arr2 for the copied array.
4. Use a loop to input the elements of arr1.
5. Use another loop to copy each element of arr1 into arr2.
6. Display the elements of arr1.
7. Display the elements of arr2.
8. End.

Program

#include <stdio.h>
int main() {
int n;
// Prompt user to input the number of elements
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int arr1[n], arr2[n];
// Input elements into the first array
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr1[i]);
}
// Copy elements from the first array to the second array
for (int i = 0; i < n; i++) {
arr2[i] = arr1[i];
}
// Display elements of the first array
printf("The elements stored in the first array are:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr1[i]);
}
printf("\n");

// Display elements of the second array


printf("The elements copied into the second array are:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr2[i]);
}
printf("\n");

return 0;
}

Output
The elements stored in the first array are:
15 10 12
The elements copied into the second array are:
15 10 12

Result
Thus the C Program to copy the elements of one array into another array completed
successfully.
3.Write a program in C to count the total number of duplicate elements in an array.
Aim
To write a C program to count the total number of duplicate elements in an array.

Algorithm

1. Start.
2. Prompt the user to input the number of elements (n) in the array.
3. Declare an array arr of size n.
4. Use a loop to input the elements into the array.
5. Initialize a variable count to 0 to store the count of duplicate elements.
6. Use two nested loops to compare each element with the others:
o Outer loop iterates over the array elements.
o Inner loop checks for duplicates of the current element from the outer loop.
7. Mark an element as visited (e.g., by using a special value like -1) once it has been
counted as a duplicate to avoid counting it multiple times.
8. Increment count when a duplicate is found.
9. Display the total number of duplicate elements.
10. End.

Program
#include <stdio.h>
int main() {
int n;
// Prompt user to input the number of elements
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int arr[n], count = 0;
// Input elements into the array
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr[i]);
}
// Check for duplicate elements
for (int i = 0; i < n; i++) {
if (arr[i] != -1) { // Skip already counted duplicates
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
arr[j] = -1; // Mark this element as visited
}
}
}
}
// Display the total number of duplicate elements
printf("Total number of duplicate elements found in the array is: %d\n", count);

return 0;
}

Output

Input the number of elements to be stored in the array: 3


Input 3 elements in the array:
element - 0: 5
element - 1: 1
element - 2: 1
Total number of duplicate elements found in the array is: 1

Result
Thus the C program to count the total number of duplicate elements in an array was
executed successfully.

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


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

Algorithm

1. Start.
2. Prompt the user to input the number of elements (n) in the array.
3. Declare an array arr of size n.
4. Use a loop to input the elements into the array.
5. For each element in the array, check if it is unique:
o Use a nested loop to count the occurrences of the current element.
o If the count is 1, the element is unique.
6. Display all unique elements.
7. End.

Program

#include <stdio.h>
int main() {
int n;
// Prompt user to input the number of elements
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int arr[n];
// Input elements into the array
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr[i]);
}
// Print unique elements
printf("The unique elements found in the array are:\n");
for (int i = 0; i < n; i++) {
int isUnique = 1; // Assume the element is unique
for (int j = 0; j < n; j++) {
if (i != j && arr[i] == arr[j]) {
isUnique = 0; // Mark as not unique
break;
}
}
if (isUnique) {
printf("%d ", arr[i]);
}
}
printf("\n");
return 0;
}

Output
Input the number of elements to be stored in the array: 4
Input 4 elements in the array:
element - 0: 3
element - 1: 2
element - 2: 2
element - 3: 5
The unique elements found in the array are:
35

Result
Thus the c program to print all unique elements in an array completed.
5.Write a program in C to merge two arrays of the same size sorted in descending
order.
Aim
To Write a program in C to merge two arrays of the same size sorted in descending
order.

Algorithm

1. Start.
2. Prompt the user to input the number of elements (n) for both arrays.
3. Declare two arrays arr1 and arr2 of size n, and a third array merged of size 2n.
4. Use loops to input elements into arr1 and arr2.
5. Merge the two arrays into merged:
o Copy all elements of arr1 into merged.
o Copy all elements of arr2 into merged.
6. Sort the merged array in descending order using a sorting algorithm (e.g., bubble
sort).
7. Display the sorted merged array.
8. End.

Program
#include <stdio.h>
void sortDescending(int arr[], int size) {
int temp;
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] < arr[j]) { // Swap if the current element is smaller
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main() {
int n;
// Input the number of elements for the arrays
printf("Input the number of elements to be stored in the first array: ");
scanf("%d", &n);

int arr1[n], arr2[n], merged[2 * n];


// Input elements into the first array
printf("Input %d elements in the first array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr1[i]);
}
// Input elements into the second array
printf("Input %d elements in the second array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr2[i]);
}
// Merge the two arrays
for (int i = 0; i < n; i++) {
merged[i] = arr1[i];
}
for (int i = 0; i < n; i++) {
merged[n + i] = arr2[i];
}
// Sort the merged array in descending order
sortDescending(merged, 2 * n);
// Display the sorted merged array
printf("The merged array in descending order is:\n");
for (int i = 0; i < 2 * n; i++) {
printf("%d ", merged[i]);
}
printf("\n");

return 0;
}

Output
Input the number of elements to be stored in the first array: 3
Input 3 elements in the array:
element - 0: 1
element - 1: 2
element - 2: 3
Input the number of elements to be stored in the second array: 3
Input 3 elements in the array:
element - 0: 1
element - 1: 2
element - 2: 3
The merged array in descending order is:
332211
Result
Thus the C Program to merge two arrays of the same size sorted in descending order
executed successfully.

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


Aim
To write a C Program to count the frequency of each element of an array.

Algorithm

1. Start.
2. Prompt the user to input the number of elements (n) in the array.
3. Declare an array arr of size n and another array freq of size n to store frequencies.
4. Use a loop to input the elements into arr.
5. Initialize all elements of the freq array to -1.
6. Use nested loops to calculate the frequency of each element:
o For each element, check if it has already been counted (i.e., if its frequency is
not -1).
o Count its occurrences by comparing it with all other elements in the array.
o Store the count in freq and mark duplicates with -1.
7. Display the frequency of all elements with frequency not equal to -1.
8. End.

Program
#include <stdio.h>
int main() {
int n;
// Input the number of elements
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int arr[n], freq[n];
// Input elements into the array
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr[i]);
freq[i] = -1; // Initialize the frequency array
}
// Calculate the frequency of each element
for (int i = 0; i < n; i++) {
if (freq[i] == -1) { // Element not already counted
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
freq[j] = 0; // Mark duplicate as processed
}
}
freq[i] = count; // Store the frequency
}
}
// Display the frequency of each element
printf("The frequency of all elements of the array:\n");
for (int i = 0; i < n; i++) {
if (freq[i] != 0) { // Only display unprocessed elements
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}
return 0;
}

Output

Input the number of elements to be stored in the array :3


Input 3 elements in the array :
element - 0 : 25
element - 1 : 12
element - 2 : 43
Expected Output :
The frequency of all elements of an array :
25 occurs 1 times
12 occurs 1 times
43 occurs 1 times

Result
Thus the C Program to count the frequency of each element of an array completed.

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


Aim
To write C Program to find the maximum and minimum elements in an array.

Algorithm

1. Start.
2. Prompt the user to input the number of elements (n) in the array.
3. Declare an array arr of size n.
4. Use a loop to input the elements into arr.
5. Initialize two variables max and min to the first element of the array.
6. Use a loop to iterate through the array:
o If the current element is greater than max, update max.
o If the current element is smaller than min, update min.
7. Display the values of max and min.
8. End.

Program
#include <stdio.h>
int main() {
int n;
// Input the number of elements
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int arr[n];
// Input elements into the array
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr[i]);
}
// Initialize max and min to the first element
int max = arr[0], min = arr[0];
// Find the maximum and minimum elements
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
// Display the results
printf("Maximum element is: %d\n", max);
printf("Minimum element is: %d\n", min);
return 0;
}

Output
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 45
element - 1 : 25
element - 2 : 21
Expected Output :
Maximum element is : 45
Minimum element is : 21

Result
Thus the C Program to find the maximum and minimum elements in an array.
8.Write a program in C to separate odd and even integers into separate arrays.
Aim
To write a C Program to separate odd and even integers into separate arrays.

Algorithm

1. Start.
2. Prompt the user to input the number of elements (n) in the array.
3. Declare an array arr of size n.
4. Declare two arrays: evenArr and oddArr to store even and odd numbers, respectively.
5. Use a loop to input the elements into arr.
6. Initialize two counters: evenCount and oddCount to 0.
7. Use a loop to iterate through arr:
o If an element is divisible by 2 (even), store it in evenArr and increment
evenCount.
o Otherwise (odd), store it in oddArr and increment oddCount.
8. Display the elements of evenArr and oddArr.
9. End.

Program
#include <stdio.h>
int main() {
int n;
// Input the number of elements
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int arr[n], evenArr[n], oddArr[n];
int evenCount = 0, oddCount = 0;
// Input elements into the array
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr[i]);
}
// Separate odd and even integers
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) { // Check if even
evenArr[evenCount++] = arr[i];
} else { // Check if odd
oddArr[oddCount++] = arr[i];
}
}
// Display the even elements
printf("The Even elements are:\n");
for (int i = 0; i < evenCount; i++) {
printf("%d ", evenArr[i]);
}
printf("\n");
// Display the odd elements
printf("The Odd elements are:\n");
for (int i = 0; i < oddCount; i++) {
printf("%d ", oddArr[i]);
}
printf("\n");
return 0;
}

Output
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
Expected Output :
The Even elements are :
42 56 32
The Odd elements are :
25 47

Result
Thus the C Program for separating odd and even integers into separate arrays
completed successfully.

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


Aim
To Write a C Program to find the second largest element in an array.
Algorithm

Algorithm

1. Start.
2. Prompt the user to input the size of the array (n).
3. Declare an array arr of size n.
4. Use a loop to input elements into the array.
5. Initialize two variables largest and secondLargest:
o Set largest to the smallest possible integer.
o Set secondLargest to the smallest possible integer.
6. Use a loop to iterate through the array:
o If the current element is greater than largest:
 Update secondLargest to largest.
 Update largest to the current element.
o Else if the current element is greater than secondLargest and not equal to
largest:
 Update secondLargest to the current element.
7. Display the value of secondLargest.
8. End.

Program
#include <stdio.h>
#include <limits.h> // For INT_MIN
int main() {
int n;
// Input the size of the array
printf("Input the size of array: ");
scanf("%d", &n);
int arr[n];
// Input elements into the array
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d: ", i);
scanf("%d", &arr[i]);
}
// Initialize largest and secondLargest
int largest = INT_MIN, secondLargest = INT_MIN;
// Find the largest and second largest elements
for (int i = 0; i < n; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
// Check if a second largest element exists
if (secondLargest == INT_MIN) {
printf("There is no second largest element in the array.\n");
} else {
printf("The Second largest element in the array is: %d\n", secondLargest);
}
return 0;
}

Output
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 9
element - 2 : 1
element - 3 : 4
element - 4 : 6
Expected Output :
The Second largest element in the array is : 6

Result
Thus the C Program to find the second largest element in an array.

10. Write a program in C for the subtraction of two matrices.


Aim
To write a C Program for the subtraction of two matrices.

Algorithm for Subtraction of Two Matrices

1. Start.
2. Input the dimensions of the matrices, i.e., the number of rows (r) and columns (c).
3. Declare two matrices A[r][c] and B[r][c] for the two input matrices and a result
matrix C[r][c] for storing the result.
4. Input the elements of matrix A and matrix B:
o For matrix A, prompt the user to enter each element.
o Similarly, for matrix B, prompt the user to enter each element.
5. Perform the subtraction:
o For each element of the result matrix C[i][j], compute C[i][j] = A[i][j] - B[i]
[j].
6. Display the result matrix C.
7. End.

Program

#include <stdio.h>
int main() {
int r, c;
// Input the dimensions of the matrices
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
int A[r][c], B[r][c], C[r][c];
// Input elements of matrix A
printf("Enter the elements of matrix A:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
// Input elements of matrix B
printf("Enter the elements of matrix B:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("B[%d][%d]: ", i, j);
scanf("%d", &B[i][j]);
}
}
// Subtract the matrices and store the result in matrix C
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
// Display the result matrix C
printf("\nThe result of A - B is:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}

return 0;
}

Output
Enter the number of rows: 2
Enter the number of columns: 2
Enter the elements of matrix A:
A[0][0]: 5
A[0][1]: 8
A[1][0]: 6
A[1][1]: 3
Enter the elements of matrix B:
B[0][0]: 2
B[0][1]: 1
B[1][0]: 4
B[1][1]: 7
The result of A - B is:
37
2 -4
Result
Thus the C Program for subtracting two matrices completed.

11.Write a program in C to find the transpose of a given matrix.


Aim
To write a C Program to find the transpose of a given matrix.

Algorithm

1. Start.
2. Input the number of rows (r) and columns (c) of the matrix.
3. Declare the matrix A[r][c] to store the input matrix and a matrix T[c][r] for the
transpose.
4. Input the elements into the matrix A:
o Use a nested loop to take input for each element A[i][j].
5. Find the Transpose:
o For each element, set T[j][i] = A[i][j] (this swaps the rows and columns).
6. Display the original matrix A.
7. Display the transpose matrix T.
8. End.
9.

Program

#include <stdio.h>
int main() {
int r, c;
// Input the rows and columns of the matrix
printf("Input the rows and columns of the matrix: ");
scanf("%d %d", &r, &c);
int A[r][c], T[c][r];
// Input elements in the first matrix
printf("Input elements in the first matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("element - [%d],[%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
// Display the original matrix
printf("\nThe matrix is:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
// Find the transpose of the matrix
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
T[j][i] = A[i][j];
}
}
// Display the transpose matrix
printf("\nThe transpose of a matrix is:\n");
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++) {
printf("%d ", T[i][j]);
}
printf("\n");
}
return 0;
}

Ouput

Input the rows and columns of the matrix : 2 2


Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The matrix is :
12
34
The transpose of a matrix is :
13
24

Result
Thus the C Program to transpose of a given matrix completed successfully.

You might also like