Saturday, 8 December 2012
Saturday, 10 November 2012
Third sessional exam solution
DHARMSINH DESAI
UNIVERSITY, NADIAD
FACULTY OF
MANAGEMENT & INFORMATION SCIENCE
THIRD SESSIONAL
EXAMINATION
SUBJECT :
COMPUTER PROGRAMMING WITH C
Q.1 Do as
directed.
(A) Indicate
whether the following statements are
True or False and justify your answer :
(i) main()
is an user define function.
Ans : True
Justification : Because it is not a built in function we need
to define the main() function.
(ii) A
pointer variable can be initialized with 0.0 value.
Ans : False
Justification : A pointer variable can be initialize with 0
or null value.
(iii) void is the default return type of the function.
Ans
: False
Justification : int is a default return type of the function.
(iv) The key word used to transfer control
from a function back to the calling function us switch.
Ans : False
Justification
: return keyword is used to transfer
control from a function back to the calling function.
(B) Fill in the blank
(i) A ……………..is a self contained block of program statements
that performs some particular task.
Ans : function
(ii) Consider the following structure definition
struct
st
{
int
a;
float
b[10]
}s;
………………. bytes will be the size of
the above structure.
Ans
: 42
(iii) The type of the actual and formal arguments must be …………….
Ans
: same
(iv) The
function strstr() has ………………. Parameters.
Ans
: Two
(C) What
will be the output of the following programs ?
int a = 100;
void computer(int a)
{
a = a; }
int main()
{
int a = 100;
printf(“%d”,a);
computer(a);
printf(“%d”,a);
return 0;
}
Output: 100
100
(D) How
does structure differ from an array? Explain with example.
Ans : Array is a
collection of a fixed number of components all of the same type: it is a
homogeneous data structure.
structs - we use them to group items of different types; a collection of a fixed number of components in which the components are accessed by name. The components as mention can be of different types. A struct is typically heterogeneous
structs - we use them to group items of different types; a collection of a fixed number of components in which the components are accessed by name. The components as mention can be of different types. A struct is typically heterogeneous
Q-2 Attempt
any Three from the following
(a)
Write
a program that accept a word from the user and prints the following way. For
example, if the word is EXAMINATION, the program will print as :
E
E
X
E
X A
E
X A M
E
X A M I
E
X A M I N
E
X A M I N A
E
X A M I N A T
E
X A M I N A T I
E
X A M I N A T I O
E
X A M I N A T I O N
#include<stdio.h>
#include<conio.h>
void
main()
{
int i,j;
char st[20];
clrscr();
printf("Enter Word : ");
scanf("%s",st);
fflush(stdin);
for(i=0;i<=strlen(st);i++)
{
for(j=0;j<=i;j++)
{
printf("%c",st[j]);
}
printf("\n");
}
getch();
}
(b)
Write a complete C program to calculate x raisto
y using user define function
#include<stdio.h>
#include<conio.h>
void
power()
{
int no,pow,ans,i;
printf("Enter the no : ");
scanf("%d",&no);
printf("Enter the power
:");
scanf("%d",&pow);
ans = no;
for(i=0;i<pow;i++)
{
ans = ans * no;
}
printf("Ans = %d",ans);
}
void
main()
{
int no,pow;
clrscr();
power();
getch();
}
(C) Explain
different types of Storage classes are available in C programming.
Generally four types of storage classes are
there in c.
1.Auto
2.Register
3.Static
4.Extern or Global
(1) Automatic storage class: auto is the default storage class for all local
variables.
-Storage: Memory
-Default initial value: Garbage value
-Scope: Local to the block in which defined
-Life: till the control remains within the
block in which defined.
-
Example :
{
int Count;
auto int Month;
}
(2) Register storage class:
register is used to define local variables that
should be stored in a register instead of RAM. This means that the variable has
a maximum size equal to the register size (usually one word) and cant have the
unary '&' operator applied to it (as it does not have a memory location).
-Storage: CPU registers
-Default initial value: Garbage value
-Scope: Local to the block in which defined
-Life: till the control remains within the
block in which defined
-
Example :
{
register int no;
}
Register should only be used for variables that
require quick access - such as counters. It should also be noted that defining
'register' goes not mean that the variable will be stored in a register. It
means that it MIGHT be stored in a register - depending on hardware and
implimentation restrictions.
(3) Static storage class:
static is the default storage class for global
variables. The two variables below (count and road) both have a static storage
class.
-Storage: Memory
-Default initial value: Zero
-Scope: Local to the block in which defined
-Life: value of variable persists between
different function calls.
-
Example :
{
static int count;
}
static variables
can be 'seen' within all functions in this source file. At link time, the
static variables defined here will not be seen by the object modules that are
brought in.
static can also be
defined within a function. If this is done the variable is initalised at run
time but is not reinitalized when the function is called. This inside a
function static variable retains its value during vairous calls.
(4)
External storage class: extern is used to give a reference of a global variable that is
visible to ALL the program files. When you use 'extern' the variable cannot be
initalized as all it does is point the variable name at a storage location that
has been previously defined.
When you have
multiple files and you define a global variable or function which will be used
in other files also, then extern will be used in another file to
give reference of defined variable or function. Just for understanding extern is used to decalre a global
variable or function in another files.
-Storage: Memory
-Default initial value: Zero
-Scope: global
-Life: As long as program execution does
not come to an end.
-
Example :
File
1: main.c
int count=5;
main()
{
write_extern();
}
File
2: write.c
void write_extern(void);
extern int count;
void write_extern(void)
{
printf("count is %i\n", count);
}
Here
extern keyword is being used to declare count in another file.
(d) Write a complete C program to
print the sum of array element using pointer.
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[10];
int i,sum=0,n;
int *ptr;
clrscr();
printf("Enter no of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ptr = a;
for(i=0;i<n;i++)
{
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements is
%d",sum);
getch();
}
Q.3 Do
as directed
(a) Write a complete C Program to calculate binomial coefficient
using Recursive Function
#include<stdio.h>
##include<conio.h>
void main()
{
int
n,r,ncr;
clrscr();
printf("Enter
the n and r value:");
scanf("%d%d",&n,&r);
ncr=fact(n)/((fact(r))*(fact(n-r)));
printf("n=%d\n",fact(n));
printf("r=%d\n",fact(r));
printf("n-r=%d\n",fact(n-r));
printf("the
ncr =%d\n",ncr);
getch();
}
int fact(int a)
{
int
c=1,i;
for(i=1;i<=a;i++)
{
c=c*i;
}
return(c);
}
OR
#include<stdio.h>
#include<conio.h>
int bin_cof(int n, int
r)
{
if(r==0
|| r==n)
return
1;
if(r<0
|| r>n || n<0)
return
0;
return
bin_cof(n-1, r) + bin_cof(n-1, r-1);
}
void main()
{
int
n,r;
printf("Enter
the value of n\n");
scanf("%d",
&n);
printf("Enter
the value of r\n");
scanf("%d",
&r);
printf("Binomial
Co-efficient (%d %d) = %d", n,r,bin_cof(n,r));
getch();
}
(b)
write a complete C program to read
and print an array of structure. Structure contains the following information.
Employee number,
Employee Name, Joining date, Age, Designation and salary.
Also print employee
information whose basic is the highest in appropriate format
#include<stdio.h>
#include<conio.h>
struct employee
{
int emp_no;
int age;
char emp_name[50];
char emp_jd[15];
char desi[30];
int salary;
};
void main()
{
struct employee emp[10];
int i,n,max,pos=0;
clrscr();
printf("Enter the total no of employees : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Employee No : ");
scanf("%d",&emp[i].emp_no);
printf("Enter the Employy Name :
");
scanf("%s",&emp[i].emp_name);
printf("Enter the joing date (dd/mm/yyyy)
: ");
scanf("%s",&emp[i].emp_jd);
printf("Enter Age : ");
scanf("%d",&emp[i].age);
printf("Enter your designation :
");
scanf("%s",&emp[i].desi);
printf("Enter Salary : ");
scanf("%d",&emp[i].salary);
}
for(i=0;i<n;i++)
{
printf("\n\nEmployee No : %d
",emp[i].emp_no);
printf("\nEmploy Name : %s
",emp[i].emp_name);
printf("\nJoing date (dd/mm/yyyy) : %s
",emp[i].emp_jd);
printf("\nYour Age :
%d",emp[i].age);
printf("\nDesignation :
%s",emp[i].desi);
printf("\nSalary : %d
",emp[i].salary);
}
max = emp[0].salary;
for(i=1;i<n;i++)
{
if(max<emp[i].salary)
{
max = emp[i].salary;
pos = i;
}
}
clrscr();
printf("\n\n\n\n\n\n\t\t");
printf("============================================\n");
printf("\n\t\t");
printf("\n\n\t\tEmployee No : %d
",emp[pos].emp_no);
printf("\n\t\tEmploy Name : %s
",emp[pos].emp_name);
printf("\n\t\tJoing date (dd/mm/yyyy) : %s
",emp[pos].emp_jd);
printf("\n\t\tYour Age : %d",emp[pos].age);
printf("\n\t\tDesignation : %s",emp[pos].desi);
printf("\n\t\tSalary : %d ",emp[pos].salary);
printf("\n\t\t===============================================");
getch();
}
OR
Q.3 Do
as directed
(a) Write a complete c program to check
whether a number is armstrong or not.
a number is armstrong
if the sum of cubes of individual digits of a number is equal to the number
itself.
#include<stdio.h>
#include<conio.h>
void main()
{
int number, sum = 0, temp, rem;
clrscr();
printf("Enter a number : ");
scanf("%d",&number);
temp = number;
while( temp != 0 )
{
rem = temp%10;
sum = sum + rem*rem*rem;
temp = temp/10;
}
if(number == sum)
printf("Entered number is an
armstrong number.");
else
printf("Entered number is not an
armstrong number.");
getch();
}
(b) Explain the following functions with an
example
Subscribe to:
Posts (Atom)