2066 ioe solution c programming
ioe 2066 Jestha c programming
1. a. Explain the block diagram of computer. lllustrate the character I/O
with example.
Answer:
(a)
There are two types of character I/O function
i. Formatted character I/0
ii. Unformatted character I/0
(i) Formatted character I/0
For printing character we use printf function and general form of
format specifier is
% wc
where, w is field width of w column with right justification
eg.
void main ( )
{
char ch = ‘C’;
printf (“In case 1: % c”, ch) ;
printf (In case 2 : % 5c”, ch) ;
printf (“In case 3: % -Sc”, ch) ;
Output:
case 1 : C
case 2 : C
case 3 : C
For inputting, character we use scanf function as.
scanf (“%c” char-var);
scanf (“%c”. & ch);
(ii)Unformatted character I/O
For printing character we use putch ( ) and putchar macro.
Syntax : putchar (char-var);
putch (char – var);
eg. putch (ch);
For reading characters we use getch(), getchar() & getche()
macros.
Syntax : char-var getchar() ;
char-var = getche();
char-var = getch();
eg. ch = getchar() ;
ch = getche() ;
Difference between getch(), getchar() & getche().
getchar ( ) getch( ) getche( )
waits for a character waits for a character and waits for a character and
followed by the Enter key. does not echoes to screen. echoes to screen.
1. b. Write the algorithm and flowchart to find greatest number among 3
numbers entered by the user.
Answer:
Algorithm
Step 1: Start
Step 2: Declare variable a, b, c
Step 3: Read three number a, b & c
Step 4: Check if a is greater than b and a is greater than c
Step 5: If step 4 is true display a is greater and go to step 9 other
wise continue
Step 6: Check if b is greater than c
Step 7: If step 6 is true display b is greater and go to step 9 other
wise continue.
Step 8: Display c is greater
Step 9: Stop
2. a. Explain in brief about the different type of operators available in C language along with their precedence and associativity.
Answer:
Based on utility and actions, C operators are classified as follows:
i. Arithmetic operators
ii. Relational operators
iii. Logical operators
iv. Assignment operators
v. Increment and decrement operators
vi. Conditional operators
vii. Bitwise operators
viii. Special operators
2. b. Write a program in C to generate the following.
*
** *
* * * * *
* * * * * * *
* * * * * * * * *
Answer: #include<stdio.h> #
3. b. Write a program to compute e^x = 1 + x + x^2/2! + x^3/3!………x^n/n!, where n represent number of terms. The program should read the value of n from the user.
Answer: #include<stdio.h> #include<conio.h> #include<math.h>
void main()
{
float x,s=0,nume,deno;
int n,i,j;
clrscr();
printf(“nEnter the value of x and n:”);
scanf(“%f%d”,&x,&n);
for(i=1;i<n;i++)
{
nume=pow(x,i);
deno= 1;
}
for(j=1;j<=i;j++)
{
deno=deno*j; s=s+nume/deno;
}
printf(“nSum=%f”s);
getch();
}
4. a. Write a program to read two integers from the user. Pass it to functions that calculates the HCF and LCM. Display the result from the main function.
Answer:
#include<stdio.h> #include<conio.h>
void calc(int,int,int*,int*);
void main()
{
int a,b,hcf,lcm;
clrscr();
printf(“nEnter two numbers:”);
scanf(“%d%d”,&a,&b);
calc(a,b,&hcf,&lcm);
printf(“nHCF=%dnLCM=%d”,hcf,lcm);,
getch();
}
void calc(int x,int y,int *h,int *l)
{
int i,min;
min=(x<y)?x:y;
for(i=min;i>=1;i–)
{
if(x%i==0 && y%i==0)
{ *h=i;
break; }
}
*l=(x*y)/(*h);
}
4. b. What is an array? Write a program to read name of students and sort them in alphabetical order.
Answer:
An array is a collection of data items of similar data types in
contiguous memory location. It is derived data type.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20][25],temp[25];
int i,n,j;
clrscr();
printf(“How many students?”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf”nEnter Name:”);
scanf(“%s”,str[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+i;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf(“nSorted Information:n”);
}
for(i=0;i<n;i++)
printf(“n%s”,str[i]);
getch();
}
5. a. In how many ways can we pass arguments to a function? Explain with example.
Answer:
We can pass arguments to a function in two ways:
i.Pass by value — Pass by value means to call the function by
passing the value as argument to the function. In this method,
changes made to the formal argument in the called function
has no effect on the .values of actual argument in the calling
function.
e.g.
#include<stdio.h>
#include<conio.h>
void sum(int,int);
void main()
{
int a=5,b=6;
sum(a,b);
getch();
}
void sum(int x, int y)
{
printf(“sum=%d”,x+y);
}
ii.Pass by reference — Pass by reference means to call the function by passing address (reference) as argument to the function. In this method, we can change the value of actual argument from the called function.
e.g.
#include<stdio.h>
#include<conio.h>
void sum(int *,int *);
void main()
{
int a=5,b=6:
sum(&a,&b);
getch();
}
void sum(int *x, int *y)
{
printf(“sum=%d”,*x+*y);
}
5. b. Write a program to search a given number from the list of N numbers. You should read the number to be searched from the user.
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,x,flag=0;
clrscr();
printf(“nEnter the no. of elements:”);
scanf(“%d,&n);
printf(“Enter numbers:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
printf(“nEnter the number to be searched:”);
scanf(“%d”,&x);
}
for(i=0;i<n;i++)
{
if(x==a[i])
{
printf(“nNumber found”);
flag=1;
break;
}
}
if(flag==0)
printf(“nNumber not found);
getch();
}
6. a. What do you understand by pointer? Why are they used? Explain.
Answer:
Pointer is a variable that represents the location(address) of a data rather than the value. A pointer is declared as
data_type *variable;
e.g. int *a; float *b;
Here, a is a integer pointer variable that can hold the address of integer variable and b is a float.pointer variable that can hold the address of float variable.
i.e. int x; float y;
a=&x; b=&y;
Pointer are used because
i.It enables us to access a variable that is defined outside the function.
ii.More than one value can be returned from the function logically by the use of pointer.
iii.The use of pointer in character strings results in saving memory space..
iv.Dynamic Memory Allocation feature can be used with the help of pointer.
6. b. Write a program to read the marks obtained by the students of a class in one subject. You should use malloc() so that only required memory is occupied. Pass the numbers to a function that calculates average. Display the average from the calling function.
Answer: #include<stdio.h>
#include<conio.h> float fun(float *,int);
void main()
{
float *m,avg; int n,i;
clrscr();
printf(“nEnter the number of students:”); scanf(“%d”,&n);
m=(float*)malloc(n*sizeof(float)); for(i=0;i<n;i++)
{
printf(“Enter the mark of student%d”,i+1);
scanf(“%f”,(m+i));
}
avg=fun(m,n);
printf(“nAverage=%f”,avg);
getch();
}
float fun(float *m,int n)
{
float a=0; int i;
for(i=0;i<n;i++)
{
a=a+*(m+i);
return(a) }
}
7. a. What do you understand by structure? Differentiate between array and structure.
b. Define a structure to hold the roll no. of a student and marks obtained by him in 5 subjects. Declare an array to hold the data of 20 students. Pass this to a function that displays the marks of student who has a highest total marks.
7. a.
Stucture is a collection of data item of same or different datatypes
in contiguos memory location.
Structure element may contain n simple variables,arrays, pointers
or even other structure.
Differences
Array
a. Array is a collection of similar data types only.
b. Each array element is accessed by array name followed by
index within square bracket as, array_name[index]
c. Whenever we declare an array with fixedSize it allocates
some space in memory.
e.g void main()
{
int a[10],i;
printf(“Enter array elements”);
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
printf(“nArray elements:”);
for(i=0;i<10;i++)
printf(“n%d”,a[i]);
getch();
}
Structure
a. Structure is a collection of similar and dissimilar datatypes.
b. Each structure element is accessed by using a dot operator (.)
as, structurename.structure_element
c. In case of structure before declaring a structure we have to
define a structure data type along with its elements to inform
the compiler about it.
e.g. struct student
{
char name[20];
int roll;
};
void main()
{
struct student s;
printf(“nEnter name and roll of student”);
scanf(“%s%d”,s.name,&s.roll);
printf(“nname=%s and roll=%d”,s.name,s.roll);
getch();
}
7. b.
#include<stdio.h>
#include<conio.h>
struct STUDENT
{
int roll;
float marks[5],
};
void fun(struct STUDENT []);
void main{)
{
struct STUDENT s[20];
int i,j;
clrscr();
for(i=0;i<20;i++)
{
printf(“Enter Roll no.”);
scanf(“%d”,&s[i].roll);
for(j=0;j<5;j++)
{
printf(“nEnter marks:”);
scanf(“%f”,&s[i].mark[j]);
}
}
fun(s);
getch();
}
void fun(struct STUDENT s[])
{
float total[20]={0},max;
struct STUDENT t;
int i,j;
for(i=O;i<20;i++)
{
for(j=0;j<5;j++)
{
total[i]=total[iJ+s[i}.marks[j];
}
max=total[0];
t=s[0];
for(i=1;i<20;i++)
{
if(max<total[i])
{
max=total[i];
t=s[i];
}
}
printf(“nMarks of student having highest totaln”);
for(i=0;i<5;i++)
printf(“%ft”,t.marks[i]);
}
8. a. Describe basic file operations. Write statement used to open a file for these operations.
b. Write a program to read name and phone number of N number of people. Write them on a file and close it. Reopen the tile and dislay the data on it.
8. a.
Basic file operations in C are
i. Naming a file
ii. Opening a file
iii. Reading data from a file
iv. Writing data to a file
v. Closing file
There are two distinct way to perform file operations in C. The
first one is low-level I/O and UNIX system calls. The second
method is referred to as the high-level I/O operations and uses
functions in C’s standard I/O library.
The statement used to open a file for reading data is
file_pointer=fopen(“file_name”,”r”);
The statement used to open a file for writing data is
file_pointer=fopen(“file_name”,”w”);
The statement used to close the file is
fclose(file_pointer);
8. b.
#include<stdio.h>
#include<conio.h>
struct record
{
char name[50];
char ph[15];
};
void main()
{
struct record s;
FILE *fp;
int i,n;
cirscr();
printf(“nEnter the number of people”);
scanf(“%d”,&n);
fp=fopen(“abc.txt”,”wb”);
if(fp==NULL)
{
printf(“nFile cannot be opened”)•
getch();
exit(1);
}
for(i=0;i<n;i++)
{
printf(“nEnter name and phone no.”);
scanf(“%s%s”,s.name,s.ph);
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
fp=fopen(“abc.txt”,”rb”);
if(fp==NULL)
{
printf(“nFile cannot be opened”);
getch();
exit( 1);
}
while(fread(&s,sizeof(s),1,fp)==1)
printf(“nName=%stPhone no.=%s”,s.name,s.ph);
getch();
}
