0% found this document useful (0 votes)
7 views

C lab file

Uploaded by

mdnoor20205
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 views

C lab file

Uploaded by

mdnoor20205
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/ 34

JAMIA MILLIA ISLAMIA

DEPARTMENT OF COMPUTER ENGINEERING


C-Lab Questions

Submitted by:
MOHAMMAD NOOR E AIN
SEMESTER - III
COMPUTER ENGINEERING
21BCS061
INDEX
S.NO PRACTICAL QUESTION DATE REMARKS SIGN

1. Q1. Write a menu driven program 18-07-


having following functions:-
a) Linear Search 2022
b) Binary Search
c) Display elements of array
d) Exit.
2. Q2. Write a menu driven program 25-07-
having following functions:-
a) Sort in ascending order 2022
b) Sort in descending order
c) Find largest element in array
d) Exit
3. Q3. Write a menu driven program 01-08-
which performs following operations
on 2022
matrix:-
a) Addition of 2 matrices
b) Multiplication of 2 matrices
c) Transpose of matrix
d) Exit
4. Write a menu driven program to 22-08-
perform following conversion of
number 2022
system:-
a) Decimal to hexadecimal
b) Hexadecimal to decimal
c) Exit
5. Q5. WAP to print spiral matrix. 29-08-
2022
6. Q6. Write a menu driven program to
perform following operations:-
12-09-
a) To find length of a string 2022
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
7. Q7. WAP to perform using 2D array to 26-09-
(attributes -> student_name roll_no
sub1 sub2 sub3 percentage) 2022
1
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)
8. Given a piece of text, write a program 17-10-
to no. of spaces, no. of vowels, no. of
consonants, no. of tabs, no. of sentences 2022
and no. of lines.
9. WAP in C to calculate the difference in 14-11-
no. of days between two dates.(Dates
are entered in as a single string, eg: 2022
“08-11-2021,15-01-1932”)
10. Given 2 arrays of size M and N 14-11-
respectively. The first array is in
ascending order and the second one is 2022
in descending order. Create a third
array either in ascending or descending
order by merging the elements of both
arrays. (Note: Don’t use any sorting
algo)
11. WAP in C using pointers which finds 14-11-
the occurrence of a word in a line of
text and then replace it with another 2022
word of same length.
12. WAP to create an array of structure to 14-11-
store the details of cricketers like
name, age, no. of test matches played, 2022
average run. Display the record by
ascending order of their average run.
13. WAP in C to perform the following 14-11-
operations on complex numbers: Add,
Subtract, Multiply and Divide. Your 2022
program should take input as a string
from
a user and then convert it into complex
numbers.
(Eg: “2.3+0.113i,5+7.1i”)
14. Write a menu driven C program to 14-11-
perform the following operations on
student data using file handling:- 2022
a) Insert Row
b) Delete Row
c) Update Row
d) Display
e) Exit

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

if (array[mid] < val)


low = mid + 1;

else
high = mid - 1;

if (low > high)


{
printf("Not found \n");
return;
}
}
}
int main()
{
int i, j, a, n, choice, val, exit = 1;
printf("Enter number of elements in an array\n");
scanf("%d", &n);
int array[n];
int low = 0, high = n;
printf("Enter the elements :\n");
for (i = 0; i < n; ++i)
scanf("%d", &array[i]);
while (exit)
{
printf("Enter the number to be searched \n");
scanf("%d", &val);

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{

printf("\n Choose an Option- \n 1- Addition of 2 matrices \n 2- Multiplication of 2


matrices \n 3 - Transpose of a Matrix \n 4- Exit \n Enter (1-4) : ");
scanf("%d",&choice);
switch (choice)
{
case 1:
if(m !=p || n != q){
printf("addition not possible ");
break;
}
addMat(m,n,a,b);
break;
case 2: if(m !=p){
printf("multiplication not possible ");
break;
}
printMat(m,n,a);
printf("x\n");
printMat(p,q,b);
printf("=\n");
mulMatrices(m,n,a,b);
break;
case 3:
printf("\n Enter (1-2) to transpose 1st or 2nd matrix :");
scanf("%d",&x);
if(x ==1){
transposeMat(m,n,a);

}else if( x ==2){


transposeMat(p,q,b);

}else{
printf("enter a valid option");
}
break;

8
case 4: isRunning =0;
printf("\n Exiting......");
break;

default: printf("enter a valid choice");


}
}while(isRunning == 1);
}

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

int num = 0, arr[10] = {0}, i = 0, rem;


printf("Enter number:");
scanf("%d", &num);
while (num > 16)
9
{
rem = num % 16;
num = num / 16;
arr[i] = rem;
i++;
}

arr[i] = num;

for (i; i >= 0; i--)


{
if (arr[i] == 10)
{
printf("%c", 'A');
}
else if (arr[i] == 11)
{
printf("%c", 'B');
}
else if (arr[i] == 12)
{
printf("%c", 'C');
}
else if (arr[i] == 13)
{
printf("%c", 'D');
}
else if (arr[i] == 14)
{
printf("%c", 'E');
}
else if (arr[i] == 15)
{
printf("%c", 'F');
}
else
{
printf("%d", arr[i]);
}

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

while (arr[i] != '\0')


{
size++;
i++;
}
i = size - 1;
for (i; i >= 0; i--)
{
for (j = 0; j < 16; j++) {
if (arr[i] == hexDigits[j]) {
sum += j * pow(16, power);
}
}
power++;
}
printf("Hex : %d \n", sum);
}
else if (choice == 3)
{
exit = 0;
}
else
{
printf("Invalid Option \n");
}
}
return 0;
}

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

while(bottom>=top && right>=left){


for (i=left; i<=right;i++){
printf("%d,", arr[top][i]);
}
top++;
for (i=top; i<=bottom;i++){
printf("%d,", arr[i][right]);

}
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 && j >=0)


{
if(arr1[i] >= arr2[j]){
arr3[k] = arr2[j];
k++;
j--;
}else{
arr3[k] = arr1[i];
k++;
i++;
}
}

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>

void replace(char * line, int m, char * repWord){


int i,j=0;
for(i=m;repWord[j] != '\0';i++,j++){
line[i] = repWord[j];
}
}
int main(){
char line[100],word[10],repWord[10];
printf("MADE BY MOHAMMAD NOOR E AIN\n ROLL NO: 21BCS061\n");
printf("Enter the text:");
gets(line);
printf("Enter the word to find:");
gets(word);
printf("enter the word to replace ");
gets(repWord);
int n = strlen(word);
if(n != strlen(repWord)){
printf("The length of words are not equal");
return 0;
}
int i,j,cnt=0;
for(i=0;line[i]!='\0';i++){
int temp = i;
if(line[i] == word[0]){
for(j=0;word[j] !='\0';j++ ){
if(line[i] == word[j])
23
i++;
}
int chk = i-temp;
if(chk == n){
cnt++;
replace(line,temp,repWord);
}
}
i = temp;
}

printf("The word has %d no of occurrences \n after replacing it is -


%s",cnt,line);

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++){

printf("\n====PLAYER %d ===== ",i+1);


24
printf("\nName:%s",mc[i].name);

printf("\n age: %d",mc[i].age);

printf("\n No of mathces played: %d",mc[i].matchesPlayed);

printf("\n Average Run: %lf",mc[i].avgRun);

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

printf("\n a = %f \n", a);


i++;
while (c1[i] != '.' && c1[i] != 'i')
{
b *= 10;
b += charToint(c1[i]);
i++;
}

if (c1[i] == '.')
{
dec = 0.1;
i++;
while (c1[i] != 'i')
{
b += dec* charToint(c1[i]);
dec *= 1 / 10;
i++;
}
}

printf("\n b = %f \n", b);


// second complex
printf("\nComplex 2: %s\n", c2);
i = 0;

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

printf("\n c = %f \n", c);


i++;
while (c2[i] != '.' && c2[i] != 'i')
{
d *= 10;
d += charToint(c2[i]);
i++;
}

if (c2[i] == '.')
{
dec = 0.1;
i++;
while (c2[i] != 'i')
{
d += dec* charToint(c2[i]);
dec *= 1 / 10;
i++;
}
}

printf("\n d = %f \n", d);


printf("\nAdding both complex no: %f+%fi\n", a + c, b + d);
if (b - d > 0)
{
printf("Substracting both complex no: %f+%fi\n", a - c, b - d);
}
else
{
printf("Substracting both complex no: %f%fi\n", a - c, b - d);
}

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

if ((b *c - a *d) / (c *c + d *d) > 0)


{
printf("Dividing both complex no: %f+%fi\n", (a *c + b *d) / (c *c + d
*d), (b *c - a *d) / (c *c + d *d));
}
else
{
printf("Dividing both complex no: %f%fi\n", (a *c + b *d) / (c *c + d
*d), (b *c - a *d) / (c *c + d *d));
}

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

FILE *filePointer, *filePointer1;


filePointer = fopen("data_new.txt", "r");
filePointer1 = fopen("tempFile.txt", "w");
printf("Enter the data to be updated \n");
while (fread(&stu, sizeof(stu), 1, filePointer))
{
if (stu.rollNo == 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;

printf("Enter the data of Student.\n");

printf("\n Enter Name: ");


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;
filePointer = fopen("data_new.txt", "a");
fwrite(&stu, sizeof(stu), 1, filePointer);
printf("Data written.\n");

fclose(filePointer);
}
void delete ()
{
int roll, found = 0;
printf("Enter rollNo no to delete data: ");
scanf("%d", &roll);

FILE *filePointer, *filePointer1;


filePointer = fopen("data_new.txt", "r");
filePointer1 = fopen("tempFile.txt", "w");

while (fread(&stu, sizeof(stu), 1, filePointer))


{
if (stu.rollNo == roll)
found = 1;
else
fwrite(&stu, sizeof(stu), 1, filePointer1);
}
fclose(filePointer1);
fclose(filePointer);
if (found)
{
filePointer = fopen("data_new.txt", "w");
filePointer1 = fopen("tempFile.txt", "r");

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

filePointer = fopen("data_new.txt", "r");


while (fread(&stu, sizeof(stu), 1, filePointer))
{
printf("%s\t\t%d\t\t%0.1f\t\t%.1f\t\t%0.1f\t\t%0.1f\n", stu.name,
stu.rollNo, stu.sub1, stu.sub2, stu.sub3, stu.percentage);
}
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

You might also like