Вы находитесь на странице: 1из 9

Course Code : MCS-011

Course Title : Problem Solving and Programming


Assignment Number : MCA(1)/011/Assign/2010
Maximum Marks : 100
Weightage : 25%
Last Dates for Submission : 15th October, 2010 (For July Session)

Q1:
(a) Write a simple program to find the size of different basic data types in C.

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
char c;
float f;
clrscr();
printf("\nSize of the Integer = %d",sizeof(i));
printf("\nSize of the Float = %d",sizeof(f));
printf("\nSize of the Character = %d",sizeof(c));
getch();
}

(b) Write a program in C for arithmetic operations between two integers. Your program
should guide users with proper message/menu on the console.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch;
clrscr();
printf("Enter the Numbers");
scanf("%d",&a);
scanf("%d",&b);
clrscr();
printf(" 1.Add \n 2.Sub \n 3.Multiply \n 4.Division \n 5. Exit \n");
printf("Enter Your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
c=a+b;
printf("Sum=%d",c);
break;
case 2:
c=a-b;
printf("Differance=%d",c);
break;
case 3:
c=a*b;
case 3:
c=a*b;
printf("Product=%d",c);
break;
case 4:
c=a/b;
printf("Division=%d",c);
break;
default:
printf("Exit");
}
getch();
}

(c) Write a function Pali(Sting S) to find whether S is palindrome or not.


void pali(char s[])
{
char str[10];
strcpy(str,s);
strrev(str);
if(strcmp(s,str)==0)
printf("String is Palindrome");
else
printf("String is Not Palindrome");
}

Q2:
(a) Write a C program to print this triangle:

*
***
*****
*******
*********
***********
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=12;i=i+2)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch();
}
(b) Write a C program to compute the average marks in a subject of all the students in a class.

#include<stdio.h>
#include<conio.h>
struct stud
{
int rollno;
char name[50];
int marks;
}st[10];
void main()
{
int sum=0,i;
float avg;
clrscr();
for(i=0;i<=9;i++)
{
printf("Enter the Roll NO.-->");
scanf("%d",&st[i].rollno);
printf("Enter the Name-->");
scanf("%s",&st[i].name);
printf("Enter the Marks-->");
scanf("%d",&st[i].marks);
}
for(i=0;i<=9;i++)
sum=sum+st[i].marks;
avg=sum/10;
printf("Average = %f",avg);
getch();
}
Q3:
(a) Write a program to get the value of sin (x) using a library function , when x is given in degrees.

#include <stdio.h>
#include<conio.h>
#include <math.h>
int main(void)
{
double result,x;
clrscr();
printf("Enter the Degree");
scanf("%lf",&x);
result = sin(x);
printf("The sin() of %lf is %lf\n", x, result);
getch();
return 0;
}
(b) There are 20 integer values stored in an array. Write a programme to find larges and smallest
value store.
#include<stdio.h>
#include<conio.h>
void main()
{
int val[20],max=-32768,min=32767,i;
clrscr();
for(i=0;i<20;i++)
{
printf("Enter value %d-->",i+1);
scanf("%d",&val[i]);
if(max<val[i])
max=val[i];
if(min>val[i])
min=val[i];
}
printf("\n\nMaximum Value From 20 Nos. is %d",max);
printf("\n\nMinimum Value From 20 Nos. is %d",min);
getch();
}

(c) Write a c program for binary addition of two 8 bit numbers

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void binaryadd(int,int*,int*,int);
int a[8],b[8],c[8];
void main()
{
int n1,n2,i=0,carry=0;
int *m,*n,k,x,y;
clrscr();
printf("Enter 1st Numbers :");
scanf("%d",&n1);
printf("Enter 2nd Numbers :");
scanf("%d",&n2);
oper:
x=n1;
y=n2;
while(n1!=0)
{
a[i]=n1 % 2;
n1/=2;
i++;
}
printf("\nThe binary of %d is : ",x);
for(k=7;k>=0;k--)
printf("%d",a[k]);
printf("\n");
i=0;
while(n2!=0)
while(n2!=0)
{
b[i]=n2 % 2;
n2/=2;
i++;
}
printf("\nThe binary of %d number is :",y);
for(k=7;k>=0;k--)
printf("%d",b[k]);
printf("\n");
i=0;
m=a;
n=b;
carry=0;
binaryadd(i,m,n,carry);
printf("\nThe addition is :");
for(i=7;i>=0;i--)
{
printf("%d",c[i]);
}
getch();
}
void binaryadd(int i,int *m,int *n,int carry)
{
if(*m==1 && *n==1 && carry==1)
{
c[i]=1;
carry=1;
}
if(*m==1 && *n==1 && carry==0)
{
c[i]=0;
carry=1;
}
if(*m==0 && *n==1 && carry==0)
{
c[i]=1;
carry=0;
}
if(*m==0 && *n==1 && carry==1)
{
c[i]=0;
carry=1;
}
if(*m==1 && *n==0 && carry==0)
{
c[i]=1;
carry=0;
}
if(*m==1 && *n==0 && carry==1)
{
c[i]=0;
carry=1;
}
if(*m==0 && *n==0 && carry==0)
{
c[i]=0;
carry=0;
}
if(*m==0 && *n==0 && carry==1)
{
c[i]=1;
carry=0;
}
i++;
m++;
n++;
if(i<=8) /* Base value of recursion */
binaryadd(i,m,n,carry); /* recursion till we reach 8 bit of number */
}
Q4:
(a) Using Pointers,
i) Write the character-counting function

void count(char s[])


{
int l=0;
char *ptr;
ptr=s;
while(*ptr!=NULL)
{
l++;
ptr++;
}
printf("Total Character are=%d",l);
}

ii) Write the string-concatenation program


#include<stdio.h>
#include<conio.h>
void main()
{
char str1[10],str2[5];
char *ptr1,*ptr2;
clrscr();
printf("Enter two Strings");
gets(str1);
gets(str2);
ptr1=str1;
while(*ptr1!='\0')
ptr1++;
ptr2=str2;
while(*ptr2!='\0')
while(*ptr2!='\0')
{
*ptr1++=*ptr2;
*ptr2++;
}
printf("String is --> %s",str1);
getch();
}

(b) Explain pointer arithmetic with example. Also explain advantages of malloc and calloc.

Pointer variables have 3 type of the arithmetic Operation


1. Pointers can be incremented or Decremented Point to Different location like
Ptr1=ptr2+3;
Ptr++;
--Ptr;
Ptr++ will cause the pointer ptr to point the next address value of its type. For Ex. If ptr is
a pointer to float with an initial value of 65526 then after the operation ptr++ or ptr=ptr+1,
the value of ptr would be 65530

2. if ptr1 and ptr2 are properly declared and initialized pointers , then Add, Substract,
Multiply and Division are possible
Res = res+ *ptr1;
*ptr1 = *ptr2+5;
Prod=*ptr1 * *ptr2;
Quo=*ptr1 / *ptr2;

3. Expressions like ptr1 == ptr2,ptr1<ptr2,ptr1>ptr2 and ptr1!=ptr2 are permissible.

Malloc & Calloc The malloc function is one of the functions in standard C to allocate memory.

Advantages of malloc:

 Dynamic memory allocation.


 In general using malloc is faster.
 Casting and type safety.

Advantages of calloc:

 Malloc returns a block of memory that is allocated for the programmer to use, but is uninitialized.
 An alternative is to use the calloc function, which allocates memory and then initializes it.
Q5:
(a) Write a C program for Tower of Hanoi problem with a example of 4 disks .

#include <conio.h>
#include <stdio.h>
void main()
{
void hanoi(char,char,char,int);
char t1='A',t2='B',t3='C';
int n;
clrscr();
printf("\n Enter the no. of disks on Tower A:");
scanf("%d",&n);
if(n<1)
{
printf("\n Nothing to move");
}
else
hanoi(t1,t2,t3,n);
getch();
}
void hanoi(char t1,char t2,char t3,int n)
{
static int step=0;
step++;
printf("\n %c %c %c %d",t1,t2,t3,n);
if(n==1)
{
printf("\n Move top disk from Tower %c ----> %c",t1,t2);
return;
}
hanoi(t1,t3,t2,n-1);
printf("\n %c %c %c %d",t1,t2,t3,n);
printf("\n Move top disk from Tower %c ----> %c",t1,t2);
printf("\n %c %c %c %d",t1,t2,t3,n);
hanoi(t3,t2,t1,n-1);
printf("\n %c %c %c %d steps=%d",t1,t2,t3,n,step);
}
(b) Write a C program to store students records in a file.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
int no,marks[3],rollno,i,j;
char ch[20],op;
clrscr();
fp=fopen("c:\\Student.txt","w");
if(fp==NULL)
{
printf("File Could not open");
exit(0);
}
printf("Enter No. of Student -->");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("\nStudent No. %d",i+1);
printf("\n----------------");
printf("\nEnter Roll No.-->");
scanf("%d",&rollno);
printf("Enter Name -->");
scanf("%s",&ch);
for(j=0;j<3;j++)
{
printf("Enter Marks of Student %d-->",j+1);
scanf("%d",&marks[j]);
}
fprintf(fp,"%d %s %d %d %d \n",rollno,ch,marks[0],marks[1],marks[2]);
}
clrscr();
fclose(fp);
fp=fopen("c:\\Student.txt","r");
if(fp==NULL)
{
printf("File Could not open");
exit(0);
}
printf("Output of the File \n \n \n");
while(!feof(fp))
{
op=fgetc(fp);
printf("%c",op);
}

getch();
}

Вам также может понравиться