Course Code : MCS-011 Course Title : Problem Solving and Programming Assignment Number : MCA(1)/01 1/Assign/201 1 Assignment Marks : 100 Weightage : 25% Last Date of Submission : 15 th April, 2011 (for January session) 15 th 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 Progra mme Guide for the format of presentation. Question 1: (a) Write a simple program to find the size of different basic data types in C. (5 Marks) Ans. Program 1 : Without Sizeof() /*Program to find the size of different basic data types in c 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(); 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-
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Weightage : 25%Last Date of Submission : 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. (5 Marks)
Ans. Program 1 : Without Sizeof()/*Program to find the size of different basic data types in cwithout 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();
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);
#include<malloc.h>void main(){int ch;clrscr();do{printf("\n\n+--------------------------------------------+\n| AProgram 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->ClearScreen\n0\t->Exit\nSelect datatype :");scanf("%d",&ch);
(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 logicaloperator in C.Your program should guide users with proper message/menu on theconsole.*/#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){
three sides using Heron's Formula");printf("Program to calculate area of a triangle given its threesides 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:
(a) Write a C program to print the following triangle:
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 pointerarithmetic*/#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);
/*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)