M. Arslan Sarwar: Code: PF-C3
M. Arslan Sarwar: Code: PF-C3
Prepared by
M. Arslan Sarwar
Code: PF-C3
Output:
Input 10 elements in the array :
element - 0 : 1
element - 1 : 1
2
element - 2 : 2
element - 3 : 3
element - 4 : 4
element - 5 : 5
element - 6 : 6
element - 7 : 7
element - 8 : 8
element - 9 : 9
3
2- Write a program in C to read n
number of values in an array and
display it in reverse order
#include <stdio.h>
void main()
{
int i,n,a[100];
printf("Input the number of elements to store in the array :");
scanf("%d",&n);
4
printf("\n\n");
}
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
5
3- Write a program in C to find the sum
of all elements of the array
#include <stdio.h>
void main()
{
int a[100];
int i, n, sum=0;
printf("Input the number of elements to store in array :");
scanf("%d",&n);
Output
Input the number of elements to store in array :3
Input 3 elements in the array :
element - 0 : 2
6
element - 1 : 5
element - 2 : 8
Sum of all elements stored in the array is : 15
4- Write a program in C to
copy the elements of one
array into another array
#include <stdio.h>
void main()
{
int arr1[100], arr2[100];
int i, n;
printf("Input the number of elements to be stored in the
array :");
scanf("%d",&n);
Output
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 15
element - 1 : 10
element - 2 : 12
8
5- Write a program in C to count a
total number of duplicate elements
in an array
#include <stdio.h>
int main()
{
int a[100];
int i, n, duplicates=0;
printf("Input the number of elements to store in array :");
scanf("%d",&n);
9
}
10
6- Write a program in C to count and
print all unique elements in an array
#include <stdio.h>
int main()
{
int a[100];
int i, n, uniques=0, isUnique;
printf("Input the number of elements to store in array :");
scanf("%d",&n);
11
printf("%5d", a[i]);
uniques++;
}
}
Output 1
Input the number of elements to store in array :5
Input 5 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 1
12
7- Find the maximum and minimum
element in an array
#include <stdio.h>
void main()
{
int arr1[100];
int i, mx, mn, n;
printf("Input array size:");
scanf("%d",&n);
mx = arr1[0];
mn = arr1[0];
13
if(arr1[i]<mn)
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
}
Output 1
Input array size :3
Input 3 elements in the array :
element - 0 : 45
element - 1 : 25
element - 2 : 21
Maximum element is : 45
Minimum element is : 21
14
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
even[j] = arr1[i];
j++;
}
else
{
odd[k] = arr1[i];
k++;
}
}
15
{
printf("%d ", odd[i]);
}
printf("\n\n");
}
Output
Input array size :5
Input 5 elements in the array :
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
Output
Input the size of array : 4
Input 4 elements in the array:
17
element - 0 : 1
element - 1 : 8
element - 2 : 7
element - 3 : 10
Input the value to be inserted : 5
Input the Position, where the value to be inserted :2
The current list of the array :
1 8 7 10
void main()
{
int arr1[3][3],i,j;
18
}
}
The matrix is :
1 2 3
4 5 6
7 8 9
19
11- Addition of two Matrices
#include <stdio.h>
void main()
{
int arr1[5][5],brr1[5][5],crr1[5][5],i,j,n;
20
}
}
/* calculate the sum of the matrix */
for(i=0;i<n;i++)
for(j=0;j<n;j++)
crr1[i][j]=arr1[i][j]+brr1[i][j];
printf("\nThe Addition of two matrix is : \n");
for(i=0;i<n;i++){
for(j=0;j<n;j++)
printf("%d\t",crr1[i][j]);
}
printf("\n");
}
Output:
Input the size of the square matrix (less than 6): 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
The Addition of two matrix is :
6 8
10 12
21
12- Transpose of a Matrix
#include <stdio.h>
void main()
{
int arr1[50][50],brr1[50][50],i,j,r,c;
printf("\nInput the rows and columns of the matrix : ");
scanf("%d %d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
brr1[j][i]=arr1[i][j];
22
}
}
23
#include <stdlib.h>
void main()
{
int arr1[50][50], brr1[50][50];
int i, j, r1, c1, r2, c2, flag =1;
24
}
25
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
26
Do these programs of your own.
Please do these programs with your self-effort without copying others or
taking hint. Design their logic, implement and test by self so that your problem-
solving skill improves.
Note: All programs done in class, given as homework or assignment are part of
syllabus.
1. Write a program in C to find the second largest element in an array.
2. Write a program in C that input an array of size n, an index between 0 to n-1 and deletes the
element from given index such that array size would be n-1 after deletion.
3. Write a program in C that input an array of size n, a value x, then search and delete the element
from given array at all occurrences. Array size must be n-m after deletion in case x occurs m times.
4. Write a program in C that input an array of size n, a value x, and inserts x into given array.
Considering user will always enter array sorted in ascending order, so the array must remain
sorted after insertion. Array size would be n+1 after insertion.
5. Write a program in C that input two arrays a, b and search if b array exist in a array or not. If exist,
it must also tell the index from which it starts in a array. Consider that size of b array will always
be less or equal to a array
6. Write a program in C that input two arrays a, b. Considering that a and b array are given in sorted
form in ascending order, you need to combine them in third array c such that c is also sorted in
ascending. If size of a array is n, size of b is m, size of c will be n+m.
7. Write a program in C to count the frequency of each element of an array.
8. Write a program in C to sort elements of array in ascending order using 1) Bubble sort 2)Selection
sort 3) Insertion sort.
9. Write a program in C to search element in given array using sequential/linear search.
10. Write a program in C to search elements from array given in ascending order using binary search.
11. Write a program in C to check whether given matrix is symmetric (transpose of matrix is same as
itself).
12. Write a program in C for subtraction of two Matrices.
13. *Write a program in C for multiplication of two Matrices.
14. Write a program in C to find sum of diagonal elements of a matrix.
15. Write a program in C to find sum of elements above the diagonal of a matrix.
27
16. Write a program in C to find sum of elements below the diagonal of a matrix.
17. Write a program in C to accept a matrix and determine whether it is a sparse matrix (more then
half elements of matrix are zero).
18. Write a program in C to check whether a given matrix is an identity matrix.
19. Write a program in C to check whether a given matrix is a diagonal matrix.
20. Write a program in C to check whether all rows of a given matrix are same or not.
21. Write a program in C to check whether all columns of a given matrix are same or not.
28