Course Code: MCS-011 Course Title: Problem Solving and Programming
Course Code: MCS-011 Course Title: Problem Solving and Programming
Question 1:
: :
(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: );
scanf(%f,&a); printf(Enter the Height of Triangle: ); scanf(%f,&b); ans=triangle(a,b); printf(Area of Triangle is %f,ans); getch(); }
Question 2: (a) Write a C program to print the following triangle: * *** ***** ******* ********* ************
Hint: #include<stdio.h> #include<conio.h> void main() { clrscr(); int n,j,k,i; printf(Enter the number: ); scanf(%d,&n); for(i=1;i<=n;i++) { for(k=i;k<n;k++) { printf(*); } for(j=1;j<=i;j++) { printf(*); } for(j=i;j>1;j--) { printf(*); } printf(\n); } getch(); }
(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. Make necessary assumptions.
Hint: #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[25],i;
printf(Enter the marks of 25 students:\n); for(i=1;i<=25;i++) { scanf(%d,&a[i]); } for(i=1;i<=25;i++) { if(a[i]>50) printf(%d marks are greater than 50%\n); } getch(); }
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.
(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. It will get access to the memory where the variable saved. When you are doing any operations with a referenced variable you can change the value of the variable. Call by value is where a copy of an object is placed in the parameter stack. The called function can access and manipulate that copy at will, but it cannot change the original copy because it has no way of knowing where that original copy is located. Call by Reference, on the other hand, is where the address of an object is placed in the parameter stack. Using extra syntax, the * or the ->, the called function can access and manipulate the original copy at will. #include<stdio.h> #include<conio.h> void swap(int *num1, int *num2); void main() { clrscr(); int x,y; printf(Enter the number: ); scanf(%d,&x); printf(Enter the number: ); scanf(%d,&y); printf(x= %d y= %d \n,x,y); swap(&x,&y); printf(x= %d y= %d,x,y); getch(); } void swap(int *num1, int *num2) { int temp; temp=*num1; *num1=*num2; *num2=temp; }
printf(Enter the elements for 1st 3x3 Matrix:\n); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf(%d\n,&a[i][j]); } } printf(Enter the elements for 2nd 3x3 Matrix:\n); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf(%d\n,&b[i][j]); } } printf(Sum of 3x3 Matrix:\n); for(i=0;i<3;i++) { for(j=0;j<3;j++) { c=[i][j]=a[i][j]+b[i][j]; printf(%d\n,c[i][j]); } }
getch(); }
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.h> #include<conio.h> void main() { clrscr(); char str[1000]; int i=0; printf(Enter the string: ); scanf(%s,str); while(str[i]!=\0) { i++; } printf(There are %d words.i); getchar(); getch(); } #include<stdio.h> #include<conio.h> void main() { clrscr(); char a[1000],b[1000]; int i=0,j=0,k; printf(Enter the string: ); scanf(%s,a); printf(Enter the string: ); scanf(%s,b); while(a[i]!=\0) { i++; } while(b[j]!=\0) { j++; } for(k=0;k<j;k++) { a[i+k]=b[k]; } printf(%s,a); getchar(); getch(); }
i)
(c) What is a pointer? Explain pointer arithmetic with example. 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. The advantages of a pointer over using normal variables are: If you pass the pointer to a variable to a function, then that function can modify the value of the variable directly.
Pointer variables can also be used in arithmetic expressions. The following operations can be carried out on pointer: 1. Pointers can be incremented or decremented to point to different locations like ptr1=ptr2+3; ptr++; --ptr; 2. If ptr1 and ptr2 are properly declared and initialized pointers, the following operations are valid. res=res+*ptr1; *ptr1=*ptr2+5; prod=*ptr1**ptr2; quo=*ptr1/*ptr2; 3. Expressions like ptr1==ptr2,ptr1<ptr2 and ptr2!=ptr1 are permissible provided the pointers ptr1 and ptr2 refer to same and related variables. These comparisons are common in handling arrays. A block of memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type of void. This means that we can assign it to any type of pointer. It takes the following form: ptr=(cast-type *) malloc(byte-size); ptr is a pointer of type cast-type. The malloc returns a pointer to an area of memory with size byte-size Example: x=(int *) malloc(100 *sizeof(int));
Question 5: (a) Explain recursion. Also write a C program for Tower of Hanoi problem with an example of 4 disks.
Hint: The C language allows programmer to write functions that calls themselves and this is called Recursion. #include<stdio.h> #include<conio.h> void TOH(int,char,char,char); void main() { int n; clrscr(); printf(Enter the number of disk: ); scanf(%d,&n); printf(Tower of hannoi problem for %d disk: \n,n); TOH(n,A,B,C); getch(); } void TOH(int n, char A, char B, char C) { if(n<=0) printf(\n Wrong input \n); else if(n==1) printf(\n Move disk from peg %c to peg %c,A,C); else { TOH(n-1,A,C,B); TOH(1,A,B,C); TOH(n-1,B,A,C); } }
(b)Write a C program using structure to find students grades in a class. Make the necessary assumptions.
Hint: #include <stdio.h> #include <ctype.h> #define NAME_LEN 50 #define STUD_LEN 100
int read_line(char str[], int n); int find_students(int students); struct test_result { char name[NAME_LEN+1]; int number; int grade1; int grade2; int midterm; int final; float numeric; }studen[STUD_LEN]; void insert(void); void print(void); int num_students = 0; int main(void) { struct test_result test; printf("Enter the student's name: "); read_line(test.name, NAME_LEN); printf("Enter the student's grade for quiz #1: "); scanf("%d", &test.grade1); printf("Enter the student's grade for quiz #2: "); scanf("%d", &test.grade2); printf("Enter the student's grade for midterm: "); scanf("%d", &test.midterm); printf("Enter the student's grade for final: "); scanf("%d", &test.final); test.numeric = (((test.grade1 + test.grade2) * 1.25) + (test.midterm * 0.25) + (test.final * 0.50)); printf("%s's numeric score for the entire course is %.1f\n", test.name, test.numeric); char code; for (;;) { printf("\n"); printf("Would you like to enter another student record? y(yes) or n(no)?"); scanf(" %c", &code); while (getchar() != '\n') /* skips to end of line */ ; switch (code) { case 'y': insert(); break; case 'n': print(); return 0; default: printf("Invalid entry. Try again.\n"); return 0; } } } int find_students(int students) { int i;
for (i = 0; i < num_students; i++) if (studen[i].number == students) return i; return -1; } void insert(void) { int part_number; if (num_students == STUD_LEN) { printf("Sorry, cannot enter any more students.\n"); return; } studen[num_students].number = part_number; printf("Enter the student name: "); read_line(studen[num_students].name, NAME_LEN); printf("Enter the student's grade for quiz #1: "); scanf("%d", &studen[num_students].grade1); printf("Enter the student's grade for quiz #2: "); scanf("%d", &studen[num_students].grade2); printf("Enter the student's grade for midterm: "); scanf("%d", &studen[num_students].midterm); printf("Enter the student's grade for final: "); scanf("%d", &studen[num_students].final); studen[num_students].numeric = (((studen[num_students].grade1 + studen[num_students].grade2) * 1.25) + (studen[num_students].midterm * 0.25) + (studen[num_students].final * 0.50)); printf("%s's numeric score for the entire course is %.1f\n", studen[num_students].name, studen[num_students].numeric); num_students++; } void print(void) { printf("The average score on quiz1 is\n"); printf("The average score on quiz2 is\n"); printf("The average score on midterm is\n"); printf("The average score on the final is\n"); printf("The average score for the entire course is\n"); } int read_line(char str[], int n) { int ch, i = 0; while(isspace(ch = getchar())) ; str[i++] = ch; while ((ch = getchar()) != '\n') { if (i < n) str[i++] = ch; } str[i] = '\0'; return i; }