0% found this document useful (0 votes)
7 views28 pages

M. Arslan Sarwar: Code: PF-C3

The document contains course notes prepared by M. Arslan Sarwar, focusing on solved and unsolved programming problems for practicing arrays in C. It includes various example programs demonstrating array manipulation, such as storing, reversing, summing elements, copying arrays, counting duplicates, and handling matrices. The notes are intended to supplement the course book rather than replace it.

Uploaded by

alikealeen
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)
7 views28 pages

M. Arslan Sarwar: Code: PF-C3

The document contains course notes prepared by M. Arslan Sarwar, focusing on solved and unsolved programming problems for practicing arrays in C. It includes various example programs demonstrating array manipulation, such as storing, reversing, summing elements, copying arrays, counting duplicates, and handling matrices. The notes are intended to supplement the course book rather than replace it.

Uploaded by

alikealeen
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/ 28

Course notes

Prepared by

M. Arslan Sarwar

Code: PF-C3

Contents: Solved and unsolved programming problems for practice of Arrays


Note: These Handouts are not replacement of your course book, rather you are advised to
read them in parallel to the book.

1- program in C to store elements in an


array and print it
#include <stdio.h>
void main()
{
int arr[10];
int i;
printf("Input 10 elements in the array :\n");
for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}

printf("\nElements in array are: ");


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

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

Elements in array are: 1 1 2 3 4 5 6 7 8 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);

printf("Input %d number of elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}

printf("\nThe values store into the array are : \n");


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

printf("\n\nThe values store into the array in reverse are :\n");


for(i=n-1;i>=0;i--)
{
printf("% 5d",a[i]);
}

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

The values store into the array are :


2 5 7

The values store into the array in reverse are :


7 5 2

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);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}

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


{
sum += a[i];
}

printf("Sum of all elements stored in the array is : %d\n\n",


sum);
}

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);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
/* Copy elements of first array into second array.*/
for(i=0; i<n; i++)
{
arr2[i] = arr1[i];
}
7
/* Prints the elements of first array */
printf("\nThe elements stored in the first array are :\n");
for(i=0; i<n; i++)
{
printf("% 5d", arr1[i]);
}

/* Prints the elements copied into the second array. */


printf("\n\nThe elements copied into the second array are :\n");
for(i=0; i<n; i++)
{
printf("% 5d", arr2[i]);
}
printf("\n\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

The elements stored in the first array are :


15 10 12

The elements copied into the second array are :


15 10 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);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}

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


{
for(int j=0; j<n; j++)
{
if(a[i]==a[j] && i!=j)
{
duplicates++;
break; //only inner loop will break
}
}

9
}

printf("Number of duplicates in the array are : %d\n\n",


duplicates);
}
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

Number of duplicates in the array are : 2

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);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
printf("\nHere are unique elements of array: ");
for(i=0; i<n; i++)
{
isUnique=1;
for(int j=0; j<n; j++)
{
if(a[i]==a[j] && i!=j)
{
isUnique = 0;
break; //only inner loop will break
}
}
if(isUnique==1){

11
printf("%5d", a[i]);
uniques++;
}
}

printf("\nNumber of unique elements in the array are : %d\n\n",


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

Here are unique elements of array: 2 3 4


Number of unique elements in the array are : 3

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);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}

mx = arr1[0];
mn = arr1[0];

for(i=1; i<n; i++)


{
if(arr1[i]>mx)
{
mx = arr1[i];
}

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

8- Separate odd and even integers in


separate arrays
#include <stdio.h>
void main()
{
int arr1[10], even[10], odd[10];
int i,j=0,k=0,n;
printf("Input array size :");
scanf("%d",&n);

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++;
}
}

printf("\nThe Even elements are : \n");


for(i=0;i<j;i++)
{
printf("%d ",even[i]);
}

printf("\nThe Odd elements are :\n");


for(i=0;i<k;i++)

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

The Even elements are :


42 56 32
The Odd elements are :
25 47

9- Insert New value in the array at


given position
#include <stdio.h>
void main()
{
int arr1[100],i,n,p,x;
printf("Input the size of array : ");
scanf("%d", &n);
16
printf("Input %d elements in the array:\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("Input the value to be inserted : ");
scanf("%d",&x);
printf("Input the Position, where the value to be inserted :");
scanf("%d",&p);

printf("The current list of the array :\n");


for(i=0;i<n;i++)
printf("% 5d",arr1[i]);
/* Move all data at right side of the array */
for(i=n;i>=p;i--)
arr1[i]= arr1[i-1];
/* insert value at given position */
arr1[p-1]=x;

printf("\n\nAfter Insert the element the new list is :\n");


for(i=0;i<=n;i++)
printf("% 5d",arr1[i]);
printf("\n\n");
}

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

After Insert the element the new list is :


1 5 8 7 10

10- Read a 2D array of size 3x3 and print


the matrix
#include <stdio.h>

void main()
{
int arr1[3][3],i,j;

printf("Input elements in the matrix :\n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("element - [%d][%d] : ",i,j);
scanf("%d",&arr1[i][j]);

18
}
}

printf("\nThe matrix is : \n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",arr1[i][j]);
}
printf("\n");
}
Output:
Input elements in the matrix :
element - [0][0] : 1
element - [0][1] : 2
element - [0][2] : 3
element - [1][0] : 4
element - [1][1] : 5
element - [1][2] : 6
element - [2][0] : 7
element - [2][1] : 8
element - [2][2] : 9

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;

printf("Input the size of the square matrix (less than 6):


");
scanf("%d", &n);

/* Stored values into the array*/


printf("Input elements in the first matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}

printf("Input elements in the second matrix :\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);

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);

printf("Input elements in the first matrix :\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}

printf("\nThe matrix is :\n");


for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",arr1[i][j]);

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
brr1[j][i]=arr1[i][j];
22
}
}

printf("\n\nThe transpose of a matrix is : ");


for(i=0;i<c;i++){
for(j=0;j<r;j++){
printf("%d\t",brr1[i][j]);
}
}
printf("\n");
}
OUTPUT:
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

The matrix is:


1 2
3 4
The transpose of a matrix is :
1 3
2 4

13- Accept two matrices and check


whether they are equal
#include <stdio.h>

23
#include <stdlib.h>

void main()
{
int arr1[50][50], brr1[50][50];
int i, j, r1, c1, r2, c2, flag =1;

printf("Input Rows and Columns of the 1st matrix :");


scanf("%d %d", &r1, &c1);

printf("Input Rows and Columns of the 2nd matrix :");


scanf("%d %d", &r2,&c2);
printf("Input elements in the first matrix :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Input elements in the second matrix :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}

24
}

/* Comparing two matrices for equality */


if(r1 == r2 && c1 == c2)
{

for(i=0; i<r1; i++)


{
for(j=0; j<c2; j++)
{
if(arr1[i][j] != brr1[i][j])
{
flag = 0;
break;
}
}
}
}
else
{
flag=0;
}
if(flag == 1 )
printf("Two matrices are equal.\n\n");
else
printf("Two matrices are not equal\n\n");
}
Output:
Input Rows and Columns of the 1st matrix :2 2
Input Rows and Columns of the 2nd matrix :2 2

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

Two matrices are equal.

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.

Two-Dimensional Array Programs:

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

You might also like