C lab file
C lab file
Submitted by:
MOHAMMAD NOOR E AIN
SEMESTER - III
COMPUTER ENGINEERING
21BCS061
INDEX
S.NO PRACTICAL QUESTION DATE REMARKS SIGN
2
Q1. Write a menu driven program having following functions:-
a) Linear Search
b) Binary Search
c) Display elements of array
d) Exit
#include <stdio.h>
void binarySearch(int array[], int val, int high)
{
int low = 0;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (array[mid] == val){
printf("Element is found at index %d \n", mid);
return;
}
else
high = mid - 1;
3
printf("1) for linear search \n 2) for binary search\n 3) to exit \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
for (int i = 0; i < n; i++)
if (array[i] == val)
{
printf("Element is found at index %d \n", i);
break;
}
if (i> n-1)
{
printf("Not found \n");
break;
}
break;
case 2:
binarySearch(array,val,n-1);
break;
case 3:
exit = 0;
break;
default:
printf("Incorrect choice\n");
break;
}
}
return 0;
}
4
Q2. Write a menu driven program having following functions:- a) Sort in ascending order b) Sort
in descending order c) Find largest element in array d) Exit
#include <stdio.h>
int main()
{
int i, j, a, n, choice, isRunning = 1;
printf("Enter number of elements in an array\n");
scanf("%d", &n);
int num[n];
printf("Enter the elements\n");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
while (isRunning)
{
printf("1) for ascending order \n 2) for descending order\n 3) to
exit \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (num[i] > num[j])
{
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in ascending order:\n");
for (i = 0; i < n; ++i)
{
printf("%d\t", num[i]);
}
break;
case 2:
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (num[i] < num[j])
5
{
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in descending order:\n");
for (i = 0; i < n; ++i)
{
printf("%d\t", num[i]);
}
break;
case 3:
isRunning = 0;
break;
default:
printf("Incorrect choice\n");
break;
}
}
return 0;
}
Q3. Write a menu driven program which performs following operations on matrix:- a) Addition
of 2 matrices b) Multiplication of 2 matrices c) Transpose of matrix d) Exit
#include <stdio.h>
void printMat(int m,int n,int a[m][n]){
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
}
6
int addMat(int m,int n, int a[m][n], int b[m][n]){
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]+b[i][j]);
}
printf("\n");
}
return 0;
}
int mulMatrices(int m,int n, int a[m][n], int b[m][n]){
int i,j,k;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
int sum =0;
for(k=0;k<n;k++){
sum += a[i][j]*b[k][j];
}
printf("%d ",sum);
}
printf("\n");
}
return 0;
}
void transposeMat(int m, int n, int a[m][n]){
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[j][i]);
}
printf("\n");
}
}
int main(){
int m,n;
printf("Enter Number of rows for matrix 1: ");
scanf("%d",&m);
printf("\nEnter Number of Cols for matrix 1: ");
scanf("%d",&n);
int a[m][n];
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("\n Enter the value for element a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
7
}
printMat(m,n,a);
int p,q;
printf("Enter Number of rows for matrix 2: ");
scanf("%d",&p);
printf("\nEnter Number of Cols for matrix 2: ");
scanf("%d",&q);
int b[p][q];
for(i=0;i<p;i++){
for(j=0;j<q;j++){
printf("\n Enter the value for element a[%d][%d] : ",i,j);
scanf("%d",&b[i][j]);
}
}
printMat(p,q,b);
int isRunning =1;
int x;
int choice;
do{
}else{
printf("enter a valid option");
}
break;
8
case 4: isRunning =0;
printf("\n Exiting......");
break;
Q4. Write a menu driven program to perform following conversion of number system:- a)
Decimal to hexadecimal b) Hexadecimal to decimal c) Exit
#include <stdio.h>
#include<math.h>
int main()
{
int choice, exit = 1;
char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
while (exit)
{
printf("1) to convert decimal to hexadecimal \n2) to convert hexadecimal
to decimal \nEnter 3 to exit\n\n");
scanf("%d", &choice);
if (choice == 1)
{
arr[i] = num;
}
printf("\n");
}
else if (choice == 2)
{
char arr[10];
int i = 0, size = 0,j=0;
int sum = 0, power=0;
printf("Enter number: \n");
10
// gets(arr);
scanf("%s", arr);
11
Q5. WAP to print spiral of a matrix.
#include<stdio.h>
int main(){
int m,n;
printf("Enter No. of rows for matrix : ");
scanf("%d",&m);
printf("\nEnter No. of columns for matrix : ");
scanf("%d",&n);
int arr[m][n];
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("\n Enter the value for element a[%d][%d] : ",i,j);
scanf("%d",&arr[i][j]);
}
}
int top=0, left=0, bottom=(m-1), right=(n-1);
}
right--;
for (i=right;i>=left;i--){
printf("%d,", arr[bottom][i]);
}
bottom--;
for (i=bottom; i>=top;i--){
printf("%d, ", arr[i][left]);
}
left++;
}
return 0;
}
12
Q6. Write a menu driven program to perform following operations:-
a) To find length of a string
b) To copy a string
c) To concatenate a string
d) To reverse a string
e) To compare strings
f) To check if string is palindrome or not
g) To find substring index
#include <stdio.h>
#include<conio.h>
int strlen(char *str)
{
int i = 0, length = 0;
while (str[i] != '\0')
{
length++;
i++;
}
return length;
}
int main()
{
char str[20], str2[20] , a[20];
int i = 0, length = 0, j, op, exit = 1, count=0;
length = strlen(str);
while (exit)
{
printf("\n \nEnter: \n 1 for strlen\n 2 for strcpy\n 3 for strcat \n 4 for strrev\n
5 for strcmp \n 6 to check for palindrome \n 7 to find a substring \n 8 to exit \n Enter
choice: ");
scanf("%d", &op);
if (op == 1)
{
printf("Enter String: ");
scanf("%s", str);
printf("Length of string is %d \n", strlen(str));
}
else if (op == 2)
{
printf("Enter String: ");
scanf("%s", str);
while (str[i] != '\0')
{
str2[i] = str[i];
i++;
}
printf("The given string is %s \n" , str);
printf("The copied string is %s \n" , str2);
13
}
else if (op == 3)
{
printf("Enter String1: ");
scanf("%s", str);
printf("Enter String2: ");
scanf("%s", str2);
int j = 0;
for (i = strlen(str); i < strlen(str) + strlen(str2); i++)
{
str[i] = str2[j];
j++;
}
printf("The concatenated string is %s \n" , str);
}
else if (op == 4)
{ printf("Enter String: ");
scanf("%s", str);
printf("The reversed string is %s \n" , str);
for (i = strlen(str); i >= 0; i--)
{
printf("%c", str[i]);
}
}
else if (op == 5)
{
count=0;
printf("Enter String1: ");
scanf("%s", str);
printf("Enter String2: ");
scanf("%s", str2);
if(strlen(str2)>strlen(str)) length = strlen(str2);
else length = strlen(str);
for (i = 0; i < length; i++)
{
if (str[i] == str2[i])
{
count++;
}
}
if(count==length){
printf("yes \n");
}
else printf("no");
}
else if (op == 6)
{
count=0;
printf("Enter String: ");
scanf("%s", str);
length = strlen(str);
14
for (i = 0; i < length / 2; i++)
{
if (str[i] == str[length - i - 1])
{
count++;
}
}
if (count == length / 2)
{
printf(" palindrome");
}
else printf("not a palindrome");
}
else if (op == 7)
{
printf("Enter main string : ");
getchar();
gets(str);
printf("Enter sub string : ");
getchar();
gets(str2);
for (i = 0; i < strlen(str); i++)
{
for (j = 0; j < strlen(str2); j++)
{
if (str[i + j] == str2[j])
{
count++;
};
}
if (count == strlen(str2))
{
printf("Substring found at index %d", i);
break;
}
count = 0;
}
else if (op == 8)
{
exit = 0;
}
else
printf("Enter valid option");
}
return 0;
}
15
Q7. WAP to perform using 2D array to (attributes -> student_name roll_no sub1 sub2 sub3
percentage)
a) Display percentage of each student
b) Display highest marks in each subject
c) Display roll no. with highest percentage (If students have same percentage student then
whoever is younger is displayed)
#include <stdio.h>
int main(){
int name[100][25];
int data[100][5];
int choice,num,i=0,j=0;
printf("MADE BY MOHAMMAD NOOR E AIN\n ROLL NO: 21BCS061\n");
printf("Enter the no. of students (<100):");
scanf("%d",&num);
for(i=0;i<num;i++){
printf("Enter the name:");
scanf("%s",&name[i]);
printf("Enter Roll no. :");
scanf("%d",&data[i][0]);
printf("Enter Marks in sub1 :");
scanf("%d",&data[i][1]);
printf("Enter Marks in sub2 :");
scanf("%d",&data[i][2]);
printf("Enter Marks in sub3 :");
scanf("%d",&data[i][3]);
data[i][4] = (data[i][1]+data[i][2]+data[i][3])/3;
}
while (1)
{
printf("\n1. Display percentage of each student \n 2. Display highest
marks in each sub \n 3. Display roll no. with highest percentage \n 4. Exit \n
Enter a choice:");
16
scanf("%d",&choice);
if(choice == 1){
for(i=0;i<num;i++){
printf("\nName: %s, Roll No: %d, Percentage:
%d",name[i],data[i][0],data[i][4]);
}
}else if(choice == 2){
int max1=0,max2=0,max3=0;
for(i=0;i<num;i++){
if(data[i][1]>max1)
max1 = data[i][1];
if(data[i][2]>max2)
max2 = data[i][2];
if(data[i][3]>max3)
max3 = data[i][3];
}
printf("\nMax marks in sub1:%d, sub2:%d, sub3: %d",max1,max2,max3);
}else if(choice == 3){
int highestPer = 0;
int rollNo;
for(i=0;i<num;i++){
if(data[i][4]>highestPer){
highestPer= data[i][4];
rollNo = data[i][0];
}
}
printf("\n the highest percent is %d with roll no %d",highestPer,rollNo);
}
}
return 0;
}
17
Q8)Given a piece of text, write a program to no. of spaces, no. of vowels, no. of
consonants, no. of tabs, no. of sentences and no. of lines.
#include <stdio.h>
void count(char* c){
int
sp_count=0,tab_count=0,vowels_count=0,sen_count=0,line_count=0,cons_cnt=0;
int i;
for(i=0;c[i]!='\0';i++){
if(c[i] == ' ') sp_count++;
if(c[i] == 'a'||c[i] == 'A'||c[i] == 'E'||c[i] == 'e'||c[i] == 'i'||c[i] ==
'I'||c[i] == 'o'||c[i] == 'O'||c[i] == 'u'||c[i] == 'U')
vowels_count++;
else if((c[i] > 'a' && c[i]<'z' ) ||(c[i] > 'A' && c[i]<'Z' ) ){
cons_cnt++;
}
if(c[i] == '\t') tab_count++;
if(c[i] == '.') sen_count++;
if(c[i] == '\n') line_count++;
}
printf("\n Spaces : %d \n Tabs: %d \n vowels: %d \n sentences: %d \n Lines:
%d \n Consonants:%d
",sp_count,tab_count,vowels_count,sen_count,line_count,cons_cnt);
}
void removeSpace(char* c){
int i;
int j=0;
char ch[1000];
int sp_cnt=0;
for(i=0;c[i]!='\0';i++){
if(c[i] != ' ' && sp_cnt ==0){
ch[j] = c[i];
j++;
}else if(c[i] != ' ' && sp_cnt>0){
ch[j] = ' ';
ch[j+1] = c[i];
j+=2;
sp_cnt =0;
}else{
sp_cnt++;
}
}
ch[j] = '\0';
printf("\n %s",ch);
// c = ch;
}
int main(){
char c[1000];
printf("MADE BY MOHAMMAD NOOR E AIN\n ROLL NO: 21BCS061\n");
printf("Enter a String (1000 chars max):");
//scanf("%[^~]",&c);
18
int i=0;
do{
scanf("%c",&c[i]);
i++;
}while(c[i-1] != '~');
c[i-1] = '\0';
count(c);
removeSpace(c);
//printf(" %s",c);
}
Q9) WAP in C to calculate the difference in no. of days between two dates.(Dates are
entered in as a single string, eg: “08-11-2021,15-01-1932”)
#include <stdio.h>
int year_counter(int y) {
int i = 0, ans = 0;
for (i = 1; i < y; i++) {
if (y % 4 == 0) {
ans += 366;
} else {
ans += 365;
}
}
return ans;
}
int month_counter(int m, int y) {
int months[] = { 31,
28,
31,
30,
31,
30,
31,
31,
19
30,
31,
30,
31
};
int ans = 0;
int i;
for (i = 0; i < m; i++) {
ans += months[i];
}
if (y % 4 == 0) {
ans += 1;
}
return ans;
}
int ch2in(char a) {
if (a == '0') return 0;
else if (a == '1') return 1;
else if (a == '2') return 2;
else if (a == '3') return 3;
else if (a == '4') return 4;
else if (a == '5') return 5;
else if (a == '6') return 6;
else if (a == '7') return 7;
else if (a == '8') return 8;
else if (a == '9') return 9;
else return -1;
}
int main() {
printf("MADE BY MOHAMMAD NOOR E AIN\n ROLL NO: 21BCS061\n");
char dates[100];
printf("Enter the dates: ");
fflush(stdin);
scanf("%s", dates);
char d1[20], d2[20];
int i = 0;
int j = 0;
while (dates[i] != ',') {
d1[j] = dates[i];
i++;
j++;
}
d1[j] = '\0';
i++;
j = 0;
while (dates[i] != '\0') {
d2[j] = dates[i];
i++;
j++;
20
}
d2[j] = '\0';
printf("date1 = %s\n", d1);
printf("date2 = %s\n", d2);
int dd1 = 0, mm1 = 0, yy1 = 0;
i = 0;
while (d1[i] != '-') {
dd1 *= 10;
dd1 += ch2in(d1[i]);
i++;
}
i++;
while (d1[i] != '-') {
mm1 *= 10;
mm1 += ch2in(d1[i]);
i++;
}
i++;
while (d1[i] != '\0') {
yy1 *= 10;
yy1 += ch2in(d1[i]);
i++;
}
int dd2 = 0, mm2 = 0, yy2 = 0;
i = 0;
while (d2[i] != '-') {
dd2 *= 10;
dd2 += ch2in(d2[i]);
i++;
}
i++;
while (d2[i] != '-') {
mm2 *= 10;
mm2 += ch2in(d2[i]);
i++;
}
i++;
while (d2[i] != '\0') {
yy2 *= 10;
yy2 += ch2in(d2[i]);
i++;
}
int date1 = dd1 + month_counter(mm1, yy1) + year_counter(yy1);
int date2 = dd2 + month_counter(mm2, yy2) + year_counter(yy2);
if (date1 > date2) {
printf("The difference between the dates: %d", date1 - date2);
} else {
printf("The difference between the dates: %d", date2 - date1);
}
}
21
Q10) Given 2 arrays of size M and N respectively. The first array is in ascending order and
the second one is in descending order. Create a third array either in ascending or
descending order by merging the elements of both arrays.
#include <stdio.h>
int main(){
int m = 7;
int arr1[7] ={11,22,33,44,55,66,77};
int n=6;
int arr2[6] = {99,88,77,56,44,12};
int arr3[13]={0};
int i=0,j=n-1,k=0;
while (i<m)
{
arr3[k] = arr1[i];
k++;
i++;
}
while (j>=0)
{
arr3[k] = arr2[j];
k++;
22
j--;
}
for(int l =0;l<k;l++){
printf("%d ",arr3[l]);
}
return 0;
}
Q11. WAP in C using pointers which finds the occurrence of a word in a line of text and
then replace it with another word of same length.
#include <stdio.h>
#include <string.h>
Q12) WAP to create an array of structure to store the details of cricketers like name, age,
no. of test matches played, average run. Display the record by ascending order of their
average run.
#include <stdio.h>
struct Match{
char name[20];
int age;
int matchesPlayed;
double avgRun;
};
void display(struct Match* mc,int size){
int i,j;
for(i=0;i<size;i++){
for(j=i+1;j<size;j++){
if(mc[i].avgRun<mc[j].avgRun){
struct Match temp = mc[i];
mc[i] = mc[j];
mc[j] = temp;
}
}
}
for(i=0;i<size;i++){
}
}
int main(){
int n,i;
printf("MADE BY MOHAMMAD NOOR E AIN\n ROLL NO: 21BCS061\n");
printf("Enter the no crickters:");
scanf("%d",&n);
struct Match mc[n];
for(i=0;i<n;i++){
printf("\n====PLAYER %d ===== ",i+1);
printf("\nEnter Name (20 chars max):");
scanf("%s",&mc[i].name);
printf("\nEnter age:");
scanf("%d",&mc[i].age);
printf("\nEnter no of matches played:");
scanf("%d",&mc[i].matchesPlayed);
printf("\nEnter avg run :");
scanf("%lf",&mc[i].avgRun);
}
display(mc,n);
return 0;
}
25
Q13) WAP in C to perform the following operations on complex numbers: Add, Subtract,
Multiply and Divide. Your program should take input as a string from a user and then
convert it into complex numbers.
#include <stdio.h>
int charToint(char a)
{
if (a == '0') return 0;
else if (a == '1') return 1;
else if (a == '2') return 2;
else if (a == '3') return 3;
else if (a == '4') return 4;
else if (a == '5') return 5;
else if (a == '6') return 6;
else if (a == '7') return 7;
else if (a == '8') return 8;
else if (a == '9') return 9;
else return 0;
}
int main()
{
printf("MADE BY MOHAMMAD NOOR E AIN\n ROLL NO: 21BCS061\n");
char complex[60];
char c1[20];
char c2[20];
printf("Enter the first complex number, and second complex number:");
scanf("%s", complex);
int i = 0;
int j = 0;
while (complex[i] != ',')
{
c1[j] = complex[i];
//printf("%c ",dates[i]);
i++;
j++;
}
c1[j] = '\0';
i++;
j = 0;
while (complex[i] != '\0')
{
c2[j] = complex[i];
//printf("%c ",dates[i]);
i++;
j++;
}
26
c2[j] = '\0';
float a = 0, b = 0, c = 0, d = 0;
printf("Complex 1: %s\n", c1);
float dec;
i = 0;
while (c1[i] != '+' && c1[i] != '.' && c1[i] != '-')
{
a *= 10;
a += charToint(c1[i]);
i++;
}
if (c1[i] == '.')
{
dec = 0.1;
i++;
while (c1[i] != '+' && c1[i] != '-')
{
a += dec* charToint(c1[i]);
dec *= 1 / 10;
i++;
}
}
if (c1[i] == '.')
{
dec = 0.1;
i++;
while (c1[i] != 'i')
{
b += dec* charToint(c1[i]);
dec *= 1 / 10;
i++;
}
}
27
while (c2[i] != '+' && c2[i] != '.' && c2[i] != '-')
{
c *= 10;
c += charToint(c2[i]);
i++;
}
if (c2[i] == '.')
{
dec = 0.1;
i++;
while (c2[i] != '+' && c2[i] != '-')
{
c += dec* charToint(c2[i]);
dec *= 1 / 10;
i++;
}
}
if (c2[i] == '.')
{
dec = 0.1;
i++;
while (c2[i] != 'i')
{
d += dec* charToint(c2[i]);
dec *= 1 / 10;
i++;
}
}
28
if (a *d + b * c > 0)
{
printf("Multiplying both complex no: %f+%fi\n", a *c - b *d, a *d + b
*c);
}
else
{
printf("Multiplying both complex no: %f%fi\n", a *c - b *d, a *d + b *c);
}
return 0;
}
Q14) Write a menu driven C program to perform the following operations on student data
using file handling:- a) Insert Row b) Delete Row c) Update Row d) Display e) Exit
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct Student
{
char name[100];
int rollNo;
float percentage;
float sub1;
float sub2;
29
float sub3;
}stu;
void updateData()
{
int roll, found = 0;
printf("Enter rollNo no to update data: ");
scanf("%d", &roll);
printf("\nName: ");
getchar();
gets(stu.name);
printf("Enter rollNo No.: ");
scanf("%d", &stu.rollNo);
printf("Enter marks of sub 1: ");
scanf("%f", &stu.sub1);
printf("Enter marks of sub 2: ");
scanf("%f", &stu.sub2);
printf("Enter marks of sub 3: ");
scanf("%f", &stu.sub3);
stu.percentage = (stu.sub1 + stu.sub2 + stu.sub3) / 3.0;
found = 1;
}
fwrite(&stu, sizeof(stu), 1, filePointer1);
}
fclose(filePointer);
fclose(filePointer1);
if (found == 1)
{
filePointer = fopen("data_new.txt", "w");
filePointer1 = fopen("tempFile.txt", "r");
while (fread(&stu, sizeof(stu), 1, filePointer1))
{
fwrite(&stu, sizeof(stu), 1, filePointer);
}
fclose(filePointer1);
fclose(filePointer);
printf("\nData updated.\n");
}
30
else
printf("rollNo No. not found!\n");
}
void putData()
{
FILE *filePointer;
fclose(filePointer);
}
void delete ()
{
int roll, found = 0;
printf("Enter rollNo no to delete data: ");
scanf("%d", &roll);
31
while (fread(&stu, sizeof(stu), 1, filePointer1))
{
fwrite(&stu, sizeof(stu), 1, filePointer);
}
fclose(filePointer1);
fclose(filePointer);
printf("\nData deleted.\n");
}
else
printf("\nrollNo No. not found!\n");
}
void print()
{
FILE *filePointer;
filePointer = fopen("data_new.txt", "r");
if (fread(&stu, sizeof(stu), 1, filePointer) == '\0')
printf("No data.\n");
else
printf("\nName\t\trollNo No\tSub-1\t\tSub-2\t\tSub-3\t Percentage\n");
fclose(filePointer);
int main()
{
printf("MADE BY MOHAMMAD NOOR E AIN\n ROLL NO: 21BCS061\n");
while (1)
{
int choice;
printf("\n1) insert a row.\n2) delete a row.\n 3) update a row.\n4) print
records.\n5)exit.\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
putData();
break;
case 2:
delete();
break;
case 3:
updateData();
32
break;
case 4:
print();
break;
case 5:
exit(0);
break;
}
}
return 0;
}
33