Course Code Course TitleQuestion 1: : : MCS-011 Problem Solving and Programming (a) Write a simple program to find the size of different basic data types in C. Hint: #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“Size of integer is %d”,sizeof(int)); printf(“Size of float is %f “, sizeof(float)); printf(“Size of character is %c”,sizeof(char)); getch(); } (b) Write a program in C for showing working of different logical operator in C. Your program should guide users with proper message/menu on the console. Hint: #include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b,c,d,m,t; float x,y,j.k; printf(“Enter 6 interger numbers: “); scanf(“%d\n%d\n%d\n%d\n%d\n%d\n”,&a,&b,&c,&d,&m,&t); printf(“Enter 4 decimal numbers: “); scanf(“%f\n%f\n%f\n%f\n”,&x,&y,&j,&k); if((a>b && c<d) || (x>y && j<k)||(m!=t)) printf(“its demo for logical operator”); else printf(“logical error is there in program”); getch(); } (c) Write a function to find the area of a triangle whose length of three sides is given. Hint: #include<stdio.h> #include<conio.h> float triangle (float b, float h) { float result; result=(b*h)/2; return(result); } void main() { clrscr(); float a,b,ans; printf(“Enter the Base of Triangle: “); 1 www.solutioncab.com (A complete solution site) h> #include<conio.h> void main() { clrscr(). scanf(“%d”.j>1. } (b) Write a C program to read the internal test marks of 25 students in a class and show the number of students who have scored more than 50% in the test.j--) { printf(“*”). } for(j=i. 2 www.&a). int a[25].ans).&n).h> #include<conio.i++) { for(k=i.i.j. } getch(). Make necessary assumptions.h> void main() { clrscr(). getch().b). } for(j=1. printf(“Enter the Height of Triangle: “).solutioncab. ans=triangle(a.k<n.k++) { printf(“*”).i. } printf(“\n”).k.j++) { printf(“*”).scanf(“%f”. int n. printf(“Area of Triangle is %f”.com (A complete solution site) . printf(“Enter the number: “).i<=n. Hint: #include<stdio.&b).j<=i. for(i=1. } Question 2: (a) Write a C program to print the following triangle: * *** ***** ******* ********* ************ Hint: #include<stdio. scanf(“%f”. int x. scanf(“%d”. printf(“Enter the number: “).&x). the called function can access and manipulate the original copy at will.&a[i]). on the other hand. *num2=temp.y. int a[3][3]. (a) Hint: If you are calling by reference it means that compilation will not create a local copy of the variable which you are referencing to.i<=25.h> void swap(int *num1.printf(“Enter the marks of 25 students:\n”). The called function can access and manipulate that copy at will. #include<stdio. *num1=*num2. for(i=1. printf(“Enter the number: “). } void swap(int *num1. Call by Reference. Using extra syntax. } Question 3: What is calling by reference? How it is different from call by value? Write a C function to swap two given numbers using call by reference mechanism. printf(“x= %d y= %d \n”. Call by value is where a copy of an object is placed in the parameter stack. int *num2).i.y). printf(“x= %d y= %d”.h> #include<conio.x. scanf(“%d”. void main() { clrscr().com (A complete solution site) .solutioncab. temp=*num1.h> void main() { clrscr(). is where the address of an object is placed in the parameter stack. swap(&x.i<=25. Hint: #include<stdio. } (b) Write a C program for addition of two 3×3 matrices. } for(i=1. 3 www.c[3][3]. getch(). int *num2) { int temp. When you are doing any operations with a referenced variable you can change the value of the variable.x. the * or the ->.&y). It will get access to the memory where the variable saved.y).h> #include<conio.&y). but it cannot change the original copy because it has no way of knowing where that original copy is located.i++) { scanf(“%d”.b[3][3]. } getch().i++) { if(a[i]>50) printf(“%d marks are greater than 50%\n”).j. for(i=0. printf(“Enter any 2 values:\n”).i++) { for(j=0.printf(“Enter the elements for 1st 3x3 Matrix:\n”). } (c) Write a C program for finding GCD of two given numbers.&a[i][j]). for(i=0. int n) { while(m!=n) { if(m>n) m=m-n.j++) { scanf(“%d\n”.i<3.h> #include<conio.solutioncab. } } getch().j++) { c=[i][j]=a[i][j]+b[i][j].i++) { for(j=0. scanf(“%d\n%d”.&b[i][j]).&m.i++) { for(j=0. } return(m).com (A complete solution site) .j++) { scanf(“%d\n”.h> int gcd(int m.gcd(m.j<3.c[i][j]). printf(“GCD= %d”.n. } } printf(“Enter the elements for 2nd 3x3 Matrix:\n”).i<3. printf(“%d\n”.j<3. } 4 www. for(i=0. } } printf(“Sum of 3x3 Matrix:\n”).j<3. Hint: #include<stdio.&n). else n=n-m.int).gcd(int. } void main() { clrscr().i<3. int m.n)): getch(). b). The advantages of a pointer over using normal variables are: If you pass the pointer to a variable to a function. char a[1000]. int i=0.b[1000].solutioncab. then that function can modify the value of the variable directly. while(str[i]!=‟\0‟) { i++.k<j.k.Question 4: (a) Write C programme for followings: i) Counting the number of words in a given string ii) Concatenating two given strings Hint: #include<stdio.k++) { a[i+k]=b[k].”i). getchar(). } while(b[j]!=‟\0‟) { j++. printf(“Enter the string: “). printf(“Enter the string: “). } printf(“%s”. printf(“Enter the string: “). Also explain use of malloc function in C programming with an example Hint:A pointer in C is the address of variable of a function. int i=0. char str[1000]. 5 www. } for(k=0. } i) (c) What is a pointer? Explain pointer arithmetic with example. getchar().h> void main() { clrscr(). while(a[i]!=‟\0‟) { i++. } #include<stdio.h> void main() { clrscr(). scanf(“%s”. getch(). } printf(“There are %d words.a).com (A complete solution site) .a).str).h> #include<conio.j=0. scanf(“%s”.h> #include<conio. scanf(“%s”. getch(). } } (b)Write a C program using structure to find students grades in a class.solutioncab. else if(n==1) printf(“\n Move disk from peg %c to peg %c”.h> #include <ctype. Make the necessary assumptions.h> #define NAME_LEN 50 #define STUD_LEN 100 6 www.B). printf(“Enter the number of disk: “). The malloc function reserves a block of memory of specified size and returns a pointer of type of void. Expressions like ptr1==ptr2. } void TOH(int n. char C) { if(n<=0) printf(“\n Wrong input \n”). If ptr1 and ptr2 are properly declared and initialized pointers.A.char. Hint: #include <stdio. printf(“Tower of hannoi problem for %d disk: \n”.C). ptr is a pointer of type cast-type. TOH(„n‟. 2. the following operations are valid.C). char A. TOH(n-1. char B. prod=*ptr1**ptr2. It takes the following form: ptr=(cast-type *) malloc(byte-size). Pointers can be incremented or decremented to point to different locations like ptr1=ptr2+3.A. else { TOH(n-1. A block of memory may be allocated using the function malloc.‟B‟. Hint: The C language allows programmer to write functions that calls themselves and this is called Recursion.C). Also write a C program for Tower of Hanoi problem with an example of 4 disks.A.Pointer variables can also be used in arithmetic expressions. These comparisons are common in handling arrays. clrscr().B.‟A‟. The malloc returns a pointer to an area of memory with size byte-size Example: x=(int *) malloc(100 *sizeof(int)).ptr1<ptr2 and ptr2!=ptr1 are permissible provided the pointers ptr1 and ptr2 refer to same and related variables. #include<stdio.‟C‟). scanf(“%d”. res=res+*ptr1.h> #include<conio. *ptr1=*ptr2+5.char.n). void main() { int n. TOH(1.com (A complete solution site) .h> void TOH(int.B.A. 3. The following operations can be carried out on pointer: 1.&n). ptr++. quo=*ptr1/*ptr2. Question 5: (a) Explain recursion. getch().char). This means that we can assign it to any type of pointer. --ptr.C. break.grade2)..numeric). Try again. return 0. void print(void). 7 www. for (.midterm * 0. printf("%s's numeric score for the entire course is %. scanf("%d".grade1 + test. &test.grade1).int read_line(char str[]. scanf(" %c". test. int n). scanf("%d". case 'n': print(). switch (code) { case 'y': insert().1f\n". int grade2. &test. test. &test. float numeric.) { printf("\n").50)).midterm). scanf("%d". printf("Enter the student's grade for final: "). int grade1. default: printf("Invalid entry.name. while (getchar() != '\n') /* skips to end of line */ . }studen[STUD_LEN]. int main(void) { struct test_result test. } } } int find_students(int students) { int i.final * 0. int num_students = 0. printf("Enter the student's grade for quiz #2: "). read_line(test. return 0. int find_students(int students).name. struct test_result { char name[NAME_LEN+1].25) + (test. int final.numeric = (((test. void insert(void).com (A complete solution site) .final).25) + (test. test. &test. printf("Would you like to enter another student record? y(yes) or n(no)?"). char code.\n"). scanf("%d".grade2) * 1. int midterm. printf("Enter the student's grade for midterm: "). &code).solutioncab. NAME_LEN). printf("Enter the student's name: "). int number. printf("Enter the student's grade for quiz #1: "). &studen[num_students].number == students) return i.50)). printf("Enter the student's grade for quiz #2: "). &studen[num_students]. } int read_line(char str[]. } void print(void) { printf("The average score on quiz1 is\n"). read_line(studen[num_students]. num_students++. scanf("%d". printf("The average score for the entire course is\n"). printf("The average score on the final is\n").numeric). scanf("%d". printf("Enter the student's grade for midterm: "). } studen[num_students]. printf("%s's numeric score for the entire course is %.final * 0. scanf("%d". return.name. scanf("%d". studen[num_students]. } 8 www. } void insert(void) { int part_number. i < num_students. i++) if (studen[i].grade2) * 1. studen[num_students]. printf("Enter the student's grade for final: "). printf("Enter the student's grade for quiz #1: "). return i. &studen[num_students].grade2).grade1 + studen[num_students]. i = 0. cannot enter any more students. if (num_students == STUD_LEN) { printf("Sorry. while(isspace(ch = getchar())) .number = part_number. printf("The average score on quiz2 is\n"). while ((ch = getchar()) != '\n') { if (i < n) str[i++] = ch.grade1).\n").midterm).midterm * 0.final). studen[num_students].name. printf("The average score on midterm is\n").numeric = (((studen[num_students].com (A complete solution site) . printf("Enter the student name: ").solutioncab.25) + (studen[num_students]. str[i++] = ch.for (i = 0. } str[i] = '\0'. &studen[num_students]. int n) { int ch. return -1.25) + (studen[num_students]. NAME_LEN).1f\n".