Vous êtes sur la page 1sur 17

Course Code Course Title Assignment Number Assignment Marks Weightage Last Date of Submission

: : : : : :

MCS-011 Problem Solving and Programming MCA(1)/011/Assign/2011 100 25% 15th April, 2011 (for January session) 15th October, 2011 (for July session)

There are five questions in this assignment, which carries 80 marks. Rest 20 marks are for viva-voce. Answer all the questions. You may use illustrations and diagrams to enhance the explanations. Please go through the guidelines regarding assignments given in the Programme Guide for the format of presentation.
Question 1:

(a) Write a simple program to find the size of different basic data types in C. Ans. Program 1 : Without Sizeof() /*Program to find the size of different basic without using sizeof()*/ #include<stdio.h> #include<conio.h> void main() { int arr1[2]; char arr2[2]; float arr3[2]; double arr4[2]; int a, b, c, d; clrscr();

ar de w. w

ee g

et k.n

(5 Marks)

data types in c

a= ((int)(&arr1[2])) - ((int)(&arr1[1])); printf("Size of int = %d Bytes\n",a); b= ((int)(&arr2[2])) - ((int)(&arr2[1])); printf("Size of char = %d Bytes\n",b); c= ((int)(&arr3[2])) - ((int)(&arr3[1])); printf("Size of float = %d Bytes\n",c); d= ((int)(&arr4[2])) - ((int)(&arr4[1])); printf("Size of double = %d Bytes\n",d); getch(); } Program 2 : Using Sizeof() #include<stdio.h> #include<conio.h>

-1 of 17-

#include<malloc.h> void main() { int ch; clrscr(); do{ printf("\n\n+--------------------------------------------+\n| A Program to tell Size of basic data types |\n"); printf("+--------------------------------------------+\n| Main Menu |\n+--------------------------------------------+\n1\t>Integer\n2\t->Long\n"); printf("3\t->Short\n4\t->Float\n5\t->Char\n6\t->Double\n7\t->Clear Screen\n0\t->Exit\nSelect datatype :"); scanf("%d",&ch); switch(ch) { case 1: { printf("+--------------------------------------------+\n Size datatype = %i\n+-------------------------------------------+",sizeof(int)); break; } case 2: { printf("+--------------------------------------------+\n Size datatype = %i\n+-------------------------------------------+",sizeof(long)); break; } case 3: { printf("+--------------------------------------------+\n Size datatype = %i\n+-------------------------------------------+",sizeof(short)); break; } case 4: { printf("+--------------------------------------------+\n Size datatype = %i\n+-------------------------------------------+",sizeof(float)); break; } case 5: { printf("+--------------------------------------------+\n Size datatype = %i\n+-------------------------------------------+",sizeof(char)); break; } case 6:

ea .d ww

ee rg

et k.n

of

of

of

of

of

-2 of 17-

{ printf("+--------------------------------------------+\n Size of datatype = %i\n+-------------------------------------------+",sizeof(double)); break; } case 7: { clrscr(); } case 0: { break; } default: { continue; } } }while(ch!=0); }

ea .d ww

ee rg

et k.n

-3 of 17-

(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. (5 Marks) Ans. /*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.*/ #include<stdio.h> #include<conio.h> void andoper(); void oroper(); void notoper(); void main() { char ch; char och; do { clrscr(); printf("A program in C for showing working of different logical operator in C"); printf("\n\nAvailable options :-"); printf("\n\n1 for && operator"); printf("\n\n2 for || operator"); printf("\n\n3 for ! operator"); printf("\n\n\nEnter your choice: "); och=getch(); switch(och) { case '1' : andoper(); break; case '2' : oroper(); break; case '3' : notoper(); break; default : printf("\n\nIncorrect option selected!\a");

ea .d ww

ee rg

et k.n

} void andoper() {

} printf("\n\nDo you want to continue y/n: "); ch=getch(); }while(ch=='y');

-4 of 17-

int n; printf("\n\nYou have selected AND operator"); printf("\n\nPlease enter a number greater than 1 AND less than 9: "); scanf("%d",&n); if(n>1&&n<9) printf("\nThe number is %d: ",n); else printf("\nwrong entry"); } void oroper() { int n; printf("\n\nYou have selected OR operator"); printf("\n\nEnter either 1 or 0: "); scanf("%d",&n); if(n==1||n==0) printf("\nThe number is %d: ",n); else printf("\nWrong entry"); }

void notoper() { int n;

} (c) Ans.

printf("\n\nYou have selected NOT operator"); printf("\n\nEnter 1: "); scanf("%d",&n); if(!n==1) printf("\nWrong entry"); else printf("\nThanks for running this program %c",1);

ea .d ww

ee rg

et k.n

Write a function to find the area of a triangle whose length of three sides is given. (5 Marks) #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> void main() { int x=0,y=0,z=0,i=0,count=0,s=0; double ar=0; clrscr(); count=strlen("Program to calculate area of a triangle given its

-5 of 17-

three sides using Heron's Formula"); printf("Program to calculate area of a triangle given its three sides using Heron's Formula\n"); for(i=0;i<count;i++) { printf("-"); } printf("\nEnter side 1 :"); scanf("%d",&x); printf("Enter side 2 :"); scanf("%d",&y); printf("Enter side 3 :"); scanf("%d",&z); s=(x+y+z)/2.0; ar=sqrt(s*(s-x)*(s-y)*(s-z)); printf("Area =%lf",ar); getch(); }

Question 2:

ea .d ww

ee rg

et k.n

(a) Write a C program to print the following triangle: * *** ***** ******* ********* ************ (5 Marks) Sol. Program 1 : 12 STARS BASE #include<stdio.h> #include<conio.h> void main() { int i,n=6,j; clrscr(); for(i=1;i<=n;i++) {

-6 of 17-

for(j=1;j<=n-i;j++) printf(" "); for(j=1;j<=2*i-1;j++) printf(" *"); if(n==6&&j==12) printf(" *"); printf("\n"); } } getch();

Program 1 : 11 STARS BASE #include<stdio.h> #include<conio.h> void main() { int i,n=6,j; clrscr(); for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) printf(" "); for(j=1;j<=2*i-1;j++) printf(" *"); printf("\n"); }

ea .d ww

ee rg

et k.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. (5 Marks) Sol . /*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%*/ #include<stdio.h> #include<conio.h> void main() { int arr[25]; int i,count=0; clrscr(); for(i=0;i<25;i++) {

-7 of 17-

} printf("\n\nNumber of Students who have scored more than 50 percent = %d",count); getch(); } Question 3:

printf("\n\nEnter marks of student no %d: ",i+1); scanf("%d",&arr[i]); if(arr[i]>50) count=count+1;

(a) 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. Marks) (5

Sol. In a computer language there are two ways that arguments can be passed to a

function/subroutine. 1. Call by value : This method copies the value of an argument into the formal parameter of the subroutine. In this case, changes made to the parameter have no effect on the argument. 2. Call by reference : This is the second way of passing arguments to a subroutine. In this method, the address of an argument is copied into the parameter. Inside the subroutine, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. Example :

#include<stdio.h> #include<conio.h> void swap(int *x, int *y) { int temp; temp = *x; /* save the value at address x */ *x = *y; /* put y into x */ *y = temp; /* put x into y */ } void main() { int a=5,b=6; printf("Before Swap a= %d, b=%d\n",a,b); swap(&a,&b); printf("After Swap a= %d, b=%d",a,b); getch(); }

ea .d ww

ee rg

et k.n

In this program, the variable a is assigned the value 5, and b is assigned the value 6. Then swap( ) is called with the addresses of a and b. (The unary operator & is used to produce the address of the variables.) Therefore, the addresses of a and b, not their values, are passed into the function swap( ).

Output -8 of 17-

(b) Write a C program for addition of two 33 matrices. Sol. #include<stdio.h> #include<conio.h>

(5 Marks)

void add_matrices(int a[][3], int b[][3], int result[][3]); void print_matrix(int a[][3]); void main(void) {

int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} }; int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} }; int r[3][3]; clrscr(); add_matrices(p, q, r); printf("\nMatrix 1:\n"); print_matrix(p); printf("\nMatrix 2:\n"); print_matrix(q); printf("\nResult:\n"); print_matrix(r); getch();

ea .d ww

ee rg

et k.n

void add_matrices(int a[][3], int b[][3], int result[][3]) { int i, j; for(i=0; i<3; i++) { for(j=0; j<3; j++) { result[i][j] = a[i][j] + b[i][j]; } } } void print_matrix(int a[][3]) {

-9 of 17-

int i, j; for (i=0; i<3; i++) { for (j=0; j<3; j++) { printf("%d\t", a[i][j]); } printf("\n"); } }

(c) Write a C program for finding GCD of two given numbers.


Sol.

ea .d ww

ee rg

et k.n
(5 Marks)

#include<stdio.h> #include<conio.h> int gcd(int a, int b); void main() { clrscr(); printf("\nGCD(%2d,%2d) = [%d]", 6,4, gcd(6,4)); getch(); } int gcd(int a, int b) { int temp; while(b) {

-10 of 17-

temp = a % b; a = b; b = temp; } return(a); } Output :

Question 4:

(a) Write C programme for followings: i) Counting the number of words in a given string Sol. #include <stdio.h> #include <conio.h>

void main() { char str[50]; int i,count,countc; clrscr(); printf("Enter a string : "); gets(str); count=0; i=0; while(str[i]!='\0') { if(str[i]==' ') count++; i++; } printf("The total number of words are %d ",count+1); getch(); } Output:

ea .d ww

ee rg

et k.n

-11 of 17-

ii) Concatenating two given strings (25 =10 Marks) Sol. #include<stdio.h> #include<conio.h> void main() { char first_s[] = "Hello"; char second_s[] = "World"; int i = 0; int x = 0; char long_s[sizeof(first_s) + sizeof(second_s)]; clrscr(); while(first_s[i]) { long_s[i] = first_s[i]; ++i; } long_s[i] = ' '; ++i;

while(second_s[x]) { long_s[i] = second_s[x]; ++i; ++x; } long_s[i] = '\0'; /* don't forget the null */

ea .d ww

ee rg

et k.n

printf(long_s); putchar('\n'); getch(); }

(b) What is a pointer? Explain pointer arithmetic with example. Also explain use of malloc function in C programming with an example Sol.

(10 Marks)

Pointer : A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer.

-12 of 17-

Pointer declaration: A pointer is a variable that contains the memory location of another variable. The syntax is as shown below. You start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable. type * variable name Example: int *ptr; float *string; Pointer Arithmetic Like other variables pointer variables can be used in expressions. For example if p1 and p2 are properly declared and initialized pointers, then the following statements are valid. y=*p1**p2; sum=sum+*p1; z= 5* - *p2/p1; *p2= *p2 + 10;

C allows us to add integers to or subtract integers from pointers as well as to subtract one pointer from the other. We can also use short hand operators with the pointers p1+=; sum+=*p2; etc., we can also compare pointers by using relational operators the expressions such as p1 >p2 , p1==p2 and p1! =p2 are allowed. /*Program to illustrate the pointer expression and pointer arithmetic*/ #include< stdio.h > main() { int ptr1,ptr2; int a,b,x,y,z; a=30;b=6; ptr1=&a; ptr2=&b; x=*ptr1+ *ptr2 6; y=6*- *ptr1/ *ptr2 +30; printf(\nAddress of a +%u,ptr1); printf(\nAddress of b %u,ptr2); printf(\na=%d, b=%d,a,b); printf(\nx=%d,y=%d,x,y); ptr1=ptr1 + 70; ptr2= ptr2; printf(\na=%d, b=%d,a,b); }

ea .d ww

ee rg

et k.n

Malloc Function
Syntax: #include <stdlib.h> Description: The function malloc() returns a pointer to a chunk of memory of size size, or NULL if there is an error. The memory pointed to will be on the heap, not the stack, so make sure to free it when you are

-13 of 17-

done with it. It is basically used in Dynamic Memory Allocation. Example: typedef struct data_type { int age; char name[20]; } data; data *person; bob = (data*) malloc( sizeof(data) ); if( person != NULL ) { person->age = 22; strcpy( person->name, "Robert" ); printf( "%s is %d years old\n", person->name, person->age ); } free( person ); Question 5:

(a) Explain recursion. Also write a C program for Tower of Hanoi problem with a example of 4 disks . (10 Marks) Sol : Recursion is the process in which a function calls itself. A function is called recursive if a statement within the body of a function calls the same function. Sometimes called circular definition, recursion is thus the process of defining something in terms of itself. Tower of Hanoi problem with 4 disks #include<stdio.h> #include<conio.h> /********** Function Declaration begins **********/ void TOH(int,char,char,char); /********** Function Declaration ends **********/

ea .d ww

ee rg

et k.n

void main() { int n; clrscr(); printf("\n\t\t Program for Tower of Hanoi problem's solution"); printf("\n\n\t\t Enter number of disk:"); scanf("%d",&n); printf("\n Tower of hanoi problem for %d disk.\n",n); TOH(n,'A','B','C'); getch(); } /********** Computing TOH **********/ /********** Function Definition begins **********/ void TOH(int n, char A, char B, char C)

-14 of 17-

{ if(n<=0) printf(" \nWrong 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); } } /********** Function Definition ends **********/ Output : Program for Tower of Hanoi problems solution Enter number of disk:4 Tower of hanoi problem for 4 disk. Move disk from peg A to peg B Move disk from peg A to peg C Move disk from peg B to peg C Move disk from peg A to peg B Move disk from peg C to peg A Move disk from peg C to peg B Move disk from peg A to peg B Move disk from peg A to peg C Move disk from peg B to peg C Move disk from peg B to peg A Move disk from peg C to peg A Move disk from peg B to peg C Move disk from peg A to peg B Move disk from peg A to peg C Move disk from peg B to peg C

ea .d ww

ee rg

et k.n

(b) Write a C program using structure to find students grades in a class.Make the necessary assumptions. (10 Marks) Sol. GRADE A B C D E F MARKS RANGE > 75 65 - 75 55 - 65 45 - 55 35 - 45 < 35

-15 of 17-

/*Write a C program using structure to find students grades in a class*/ #include<stdio.h> #include<conio.h> void entry(); void report(); struct grade { char name[10]; int rollno; int marks; char grd; }; struct grade student[4]; int i; void main() { char ch,ch2; do { clrscr(); printf("\n\nPress 1 to Enter data"); printf("\n\nPress 2 to View Grades"); printf("\n\nPlease enter your choice: "); ch=getch(); switch(ch) { case '1' : entry();break; case '2' : report();break; default : printf("\n\nWrong entry!\a"); } printf("\n\nDo you want to continue y/n: "); ch2=getch(); }while(ch2=='y'); } void entry() { char ch3; printf("\n\n\n\nPlease enter data for 4 students"); for(i=0;i<4;i++) { clrscr(); printf("\nEnter data for student number %d",i+1); printf("\n\nEnter Name of the student: "); scanf("%s",&student[i].name); printf("\nEnter Roll no: "); scanf("%d",&student[i].rollno); printf("\nEnter marks: "); scanf("%d",&student[i].marks); if(student[i].marks>75) student[i].grd='A'; else if(student[i].marks>65&&student[i].marks<75) student[i].grd='B'; else if(student[i].marks>55&&student[i].marks<65)

ea .d ww

ee rg

et k.n

-16 of 17-

student[i].grd='C'; else if(student[i].marks>45&&student[i].marks<55) student[i].grd='D'; else if(student[i].marks>35&&student[i].marks<45) student[i].grd='E'; else if(student[i].marks<35) student[i].grd='F'; } printf("\n\nTo view Grades Press 2"); ch3=getch(); if(ch3=='2') report();

} void report() { clrscr(); printf("\n\nGrades obtained by these four students"); for(i=0;i<4;i++) { printf("\n\n%s %d %d %c",student[i].name,student[i].rollno,student[i].marks,student[i].grd); } }

ea .d ww

-------------------End------------------

ee rg

et k.n

-17 of 17-

Vous aimerez peut-être aussi