0% found this document useful (0 votes)
27 views26 pages

Lab 4

The document contains 10 programming problems and their solutions in C language. The problems involve array manipulation tasks like reversing an array, copying elements between arrays, counting duplicate elements, merging and sorting arrays. Matrix operations like addition and multiplication are also included.

Uploaded by

s.mohaiminmahin
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)
27 views26 pages

Lab 4

The document contains 10 programming problems and their solutions in C language. The problems involve array manipulation tasks like reversing an array, copying elements between arrays, counting duplicate elements, merging and sorting arrays. Matrix operations like addition and multiplication are also included.

Uploaded by

s.mohaiminmahin
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/ 26

Bangladesh University of Business and Technology

(BUBT)

Lab Report-4

Submitted by : Submitted to :
Name : Sumon Saha
Name : Tanzid Ahsan Nirob
Intake : 44(DH) Designation : Assistant Professor.
ID : 20235203049
Section :2 Department : CSE
Department : CSE
P-1- Write a program in C to read n number of values in an array and display them in reverse
order.

Code-
#include<stdio.h>
void main(){
int n;
printf("please enter a positive number : ");
scanf("%d",&n);
int myNumber[n];
for(int i = 0; i < n; i++){
printf("please enter index %d : ",i);
scanf("%d",&myNumber[i]);
}
printf("\n");
for(int i= n-1; i >=0; i--){
printf("The value of index %d : %d \n",i ,myNumber[i]);
}
getch();
}
P-2 - Write a program in C to copy the elements of one array into another array.
Code-
#include<stdio.h>

int print(int,int[]);
void main(){
int n,i;
printf("please enter a positive number : ");
scanf("%d",&n);
int myNumber[n];
int myNumber2[sizeof(myNumber)];
for(i = 0; i < n; i++){
printf("put the value of mynumber array index %d : ",i);
scanf("%d",&myNumber[i]);
}
for(i = 0; i < n; i++){
myNumber2[i]=myNumber[i];
}
printf("value of myNumber : \n");
print(n,myNumber);
printf("value of myNumber2 : \n");
print(n,myNumber2);
return 0;
}
int print(int a,int b[]){
int i = 0;
int n = a;
for(i = 0; i < n; i++){
printf("the value of array index %d : %d \n",i ,b[i]);
}
}

Output:
P-3 - Write a program in C to count the total number of duplicate elements in an array.
Code-
#include<stdio.h>

void main(){
int n,i,sum=0;
printf("please enter a positive number : ");
scanf("%d",&n);
int myNumber[n];
for(i = 0; i < n; i++){
printf("put the value of mynumber array index %d : ",i);
scanf("%d",&myNumber[i]);
}
for(i = 0; i < n; i++){
for(int j = i + 1; j < n; j++) {
if(myNumber[i] == myNumber[j]){
sum++;
break;
}
}
}
printf("Total number of duplicate elament %d",sum);
return 0;
}

Output

P-4 - Write a program in C to merge two arrays of the same size sorted in descending order.
Code-
#include <stdio.h>

void main()
{
int arr1[100], arr2[100], arr3[200];
int s1, s2, s3;
int i, j, k;
printf("Input the number of elements to be stored in the first array :");
scanf("%d",&s1);
printf("Input %d elements in the array :\n",s1);
for(i=0;i<s1;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("Input the number of elements to be stored in the second array :");
scanf("%d",&s2);
printf("Input %d elements in the array :\n",s2);
for(i=0;i<s2;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr2[i]);
}
s3 = s1 + s2;
for(i=0;i<s1; i++)
{
arr3[i] = arr1[i];
}
for(j=0;j<s2; j++)
{
arr3[i] = arr2[j];
i++;
}
for(i=0;i<s3; i++)
{
for(k=0;k<s3-1;k++)
{
if(arr3[k]<=arr3[k+1])
{
j=arr3[k+1];
arr3[k+1]=arr3[k];
arr3[k]=j;
}
}
}
for(i=0; i<s3; i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
P-5 - Write a program in C to count the frequency of each element of an array.
Code-
#include <stdio.h>
void main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;
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]);
fr1[i] = -1;
}
for(i=0; i<n; i++)
{
ctr = 1;
for(j=i+1; j<n; j++)
{
if(arr1[i]==arr1[j])
{
ctr++;
fr1[j] = 0;
}
}
if(fr1[i]!=0)
{
fr1[i] = ctr;
}
}
printf("\nThe frequency of all elements of array : \n");
for(i=0; i<n; i++)
{
if(fr1[i]!=0)
{
printf("%d occurs %d times\n", arr1[i], fr1[i]);
}
}
}
P-6 Write a program in C to separate odd and even integers into separate arrays.
#include <stdio.h>
void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
printf("Input the number of elements to be stored in the array :");
scanf("%d",&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)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}
printf("\nThe Even elements are : \n");
for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}
printf("\nThe Odd elements are :\n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
P-7 Write a program in C to delete an element at a desired position from an array.
Code-
#include <stdio.h>
void main(){
int arr1[50],i,pos,n;
printf("Input the size of array : ");
scanf("%d", &n);
printf("Input %d elements in the array in ascending order:\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("\nInput the position where to delete: ");
scanf("%d",&pos);
i=0;
while(i!=pos-1)
i++;
while(i<n){
arr1[i]=arr1[i+1];
i++;
}
n--;
printf("\nThe new list is : ");
for(i=0;i<n;i++)
{
printf(" %d",arr1[i]);
}
printf("\n\n");

}
Output-

P-8 - Write a program in C for a 2D array of size 3x3 and print the matrix.
Code-
#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]);
}
}
printf("\nThe matrix is : \n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",arr1[i][j]);
}
printf("\n\n");
}
P-9 - Write a program in C for adding two matrices of the same size.
Code-
#include <stdio.h>
void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n;
printf("Input the size of the square matrix (less than 5): ");
scanf("%d", &n);
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]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",arr1[i][j]);
}
printf("\nThe Second matrix is :\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",brr1[i][j]);
}
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++){
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",crr1[i][j]);
}
printf("\n\n");
}

P-10 - 10. Write a program in C for the multiplication of two square matrices.
Code-
#include <stdio.h>
void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;
printf("\nInput the rows and columns of first matrix : ");
scanf("%d %d",&r1,&c1);
printf("\nInput the rows and columns of second matrix : ");
scanf("%d %d",&r2,&c2);
if(c1!=r2){
printf("Mutiplication of Matrix is not possible.");
printf("\nColumn of first matrix and row of second matrix must be same.");
}
else
{
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]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",arr1[i][j]);
}
printf("\nThe Second matrix is :\n");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
printf("%d\t",brr1[i][j]);
}
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
crr1[i][j]=0;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
sum=0;
for(k=0;k<c1;k++)
sum=sum+arr1[i][k]*brr1[k][j];
crr1[i][j]=sum;
}
}
printf("\nThe multiplication of two matrices is : \n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d\t",crr1[i][j]);
}
}
}
printf("\n\n");
}
P-11 - Write a program in C to find the sum of the right diagonals of a matrix.
Code-
#include <stdio.h>
void main()
{
int i,j,arr1[50][50],sum=0,n;
printf("Input the size of the square matrix : ");
scanf("%d", &n);
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]);
if (i==j) sum= sum+arr1[i][j];
}
}
printf("The matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n ;j++)
printf("% 4d",arr1[i][j]);
printf("\n");
}
printf("Addition of the right Diagonal elements is :%d\n",sum);
}
P-12- 12. Write a program in C to find the sum of rows and columns of a matrix.
Code-
#include <stdio.h>
void main()
{
int i,j,k,arr1[10][10],rsum[10],csum[10],n;
printf("Input the size of the square matrix : ");
scanf("%d", &n);
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("The matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n ;j++)
printf("% 4d",arr1[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
{
rsum[i]=0;
for(j=0;j<n;j++)
rsum[i]=rsum[i]+arr1[i][j];
}
for(i=0;i<n;i++)
{
csum[i]=0;
for(j=0;j<n;j++)
csum[i]=csum[i]+arr1[j][i];
}
printf("The sum or rows and columns of the matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("% 4d",arr1[i][j]);
printf("% 8d",rsum[i]);
printf("\n");
}
printf("\n");
for(j=0;j<n;j++)
{
printf("% 4d",csum[j]);
}
printf("\n\n");
}

You might also like