ioe solution 2069 c programming
2069 Chaitra
Subject : Computer Programming
<b>
1. What are the features of a good computer program? Explain the process involved in converting a program
written in high level language to anexecutable program.
1 What are the features of a good program?Explain the process involved in converting
a program written in high level language to an executable program.
ans. The features of a good computer program are:
a) Integrity
b) Clarity
c) Simplicity
d) Efficiency
e) Nodularity
f) Generality
Integrity
the calculations used in the program should be very accurate.It must provide the desired output(functionally correct) for the given input.It must do the work according to the specification.
Clarity
The program should be well readable to aid maintenance later this can be
provided in line documentation or external documentation.On inline documentation
the function of each piece of code is defined within the program itself. In the external documentation, a separate reports include the working principle of each module inside the program.
Simplicity
the program should be able to express logic in a considerably simple way.This features enhances integrity and clarity. Same problem can be done in two or more ways, but one needs to choose the simplest way to solve the problem.
Efficiency
The program should have a good compromise between time and space used.i.e.it should run as fast as possible(time) with the minimum memory requirements (space).
Modularity
program should be separated to different logicaland self contained modules. If the entire program is divided into simpler contained modules then one can easily understand
what”s happening inside it.
Generality
The program should be as general as possible within certain limits.program written in high level language must be translated into machine level by compiler or interpreter.
A program written in high level language is called source program and when a program is converted by assembler or compiler, it is known as object program. The compilation is shown in the block diagrams:
source ——– object program —————— link—————-executable
program ——– (machine code) ——————- —————-program
||
||
||
library sub
program
steps
a) Write program in any editor or compiler (e.g. c source code has extension.c) compile the program
using compiler for checking errors.
b) If there is no any type of errors arised, then the compiler produces object file (i.e. .obj file)
c) Link the program using a linker if no errors occurs, the linker produces an executable program located
in a disc file with an .EXE extension and the same name as the object file.
d) execute the program you should test to determine whether it functions properly. If not, start again with step
a and make modifications and additions to your source code.
2. a) Why do we include <stdio.h> in our program? Can we writea C program without any header file? Justify
your answer.
b) Give the output of the following program and justify your answer with reason.
#include<stdio.h>
int main()
{
int x=3,y=5,z=7;
int a,b;
a=x*2+y/5-z*y;
b=++x*(y-3)/2-z++*y;
printf(“a=%d”,a);
printf(“b=%d”,b);
return 0;
}
ans
ANSWER NO.
2 a) <stdio.h> is a header file that contains the information about the input and output statements of
a C program. The defination of such statements are defined in header file <stdio.h>. This statements
<stdio.h> is included in our program.
We cannot write any program in C without header file beacuse header file contains the informations
about the statements used in C. For examole <math.h> header file contains the informations about the mathematical functions in C.
b)
#include<stdio.h>
int main()
{
int x=3,y=5,z=7;
int a,b;
a=x*2 + y/5-z*y;
b=++x*(y-3)/2-z++*y
printf(“a=%d”,a);
printf(“b=%d”,b)
return0;
}
a=5
here,
a=5 because a is declared as integer.
3. a) What is the importance of control structure in programming? Compare if – else – if ladder and switch
construct. Which is better?
b) Write a C program to generate following pattern using unformatted output function.
ANSWER NO. 3 a) Control structure helps to divide the program into many modules on the basis of decision. Here, only
a block of statement is executed on the basis of true and false value. Thus, programming becomes easier.
if-else-if ladder || switch
||
-An expression is evaluated and the code block is || -An expression is evaluated and the code block is
selected based on the result of expression. || selected based on value of expression.
||
-Each if has its own layout. || -Each case is referring back to the original value
|| of expression.
||
-No break statement is required. || -Break statement is required.
||
||
4. a) What is a function? Why is it necessary in programming?
b) Write a program to find whether a number is prime or not using function. The function should take the
number as an argument and return true or false to the main program.
ANSWER NO. 4 a) A function is a group of number of statements into a unit. This unit can be involved from some other
parts of the program.
The function is necessary due to following reasons :
-It makes top down modular programming, the high level logic of the overall problem is solved first
while the details of each lower level functions is addressed later.
-The length of the source program can be reduced by using funcyions at appropriate places.
-It becomes uncomplicated to locate and seperate a faulty function for further study.
b)
#include<stdio.h>
#include<conio.h>
int prime(int,int);
void main()
{
int n,i;
printf(“Enter a number”);
scanf(%d”,&n);
i=prime(n,n-1);
if(i=1)
printf(“n %d is not prime”,n);
else
printf(“n %d is prime”,n);
getch();
}
int prime(intn,inti)
{
int r;
while(1)
{
r=n%i;
if(r==0)
break;
else
prime(n,i-1);
}
return(i)
}
5. a) How can we pass two dimensional arrays from one function to another? Explainwith example.
b) Write a program in C to find the second largest number in an array from the user.
ANSWER NO. 5 a) To pass a two-dimensional array to a function as an argument, it is required to pass the starting address
of the memoryare reserved for the array as done in one-dimensional array. The starting address is stored
in array name. So, we have to pass the name of the array as argument of the function. Passing two-
dimensional arrays to a function by passing array name is also passing by reference inside the called
function, actual working area for the pssed array is the actual memory area reserved for it. So, any
operation done on the array insidecalled function is actually done in thier actual locations. Thus,
arrays are not required to return to the calling function. The size of all dimensions except the first
must be included in the function prototype & in function declaration. The size of those dimensions of
the arrays must e exactly same as in array declaration.
b)
#include<stdio.h>
#include<conio.h>
void main()
{
int nums[20],i,j,n,temp;
printf(“n How many numbers”);
scanf(“%d”,&n);
printf(“n Enter %d numbers t”,n);
for(i=0;i<n;i++)
scanf(“%d”,&nums[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n;j++)
{
if(nums[i]<nums[j])
{
temp=nums[i];
nums[i]=num[j];
nums[j]=temp;
}
}
printf(“n result=%d”,nums[i]);
getch();
}
6. What is advantage of using structure? Create an array of structure named Employee With name and salary
as structure member and the array of structure is passed to a function which sorts in ascending order
on the basis of salary and display the sorted array from main.
ANSWER NO. 6)
The main advantage of structure is that it helps the programmer for handeling a group of logically related data items.
#include<stdio.h>
#include<conio.h>
#define MAX[25]
int i,n;
struct employee
{
char name[25];
int salary;
}e[MAX];
void main()
{
void output(employee e[MAX],intn);
void sort(employee e[MAX],intn);
printf(“how many data?”);
scanf(“%d”,&n);
for(i=0;i<n;i++);
{
printf(“Record=%d n”,i+1);
printf(“Enter namen”);
scanf(“%s”,e[i].name);
fflush(stdin);
printf(“Enter salary”);
scanf(“%d”,e[i].salary);
}
printf(“unsorted datan”);
output(e,n);
printf(“sorted data”);
sort(e,n);
getch();
}
void output(employee e[MAX],intn)
{
for(i=0;i<n;i++)
printf(“%s%d”,e[i].name,e[i].salary);
}
void sort(employee e[MAX],intn);
{
struct employee temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(e[i].salary<e[j].salary)
{
temp=e[i];
e[i]=e[j];
e[j]=temp;
}
}
}
7. a) Explain call by reference. How are pointers used in call by reference?
b) Using pointer concept, write a program to count the number of characters and the number of words in a
line of text entered by the user.
ANSWER NO. 7 a) It means sending the address of the arguments- the address of actual arguments of the called function.
Using these addresses we are actuallywrking on the actual argument. So, changes will be reflected in
the calling function. This technique of passing arguments is called call by reference.
b)
#include<stdio,h>
#include<conio.h>
#include<math.h>
void main()
{
int i=10,j=20;
clrscr();
printf(“value before swap is i:%d,j:%d n”,i’j);
swap(i,j);
swap(&i,&j);
printf(“value after swap is i:%d,j:%dn”,i,j);
getch();
}
swap(intx,inty)
{
int temp;
temp=x;
x=y;
y=temp;
}
swap(int*x,int*y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
8. a) Differentiate between text file and binary file.
b) Write a program to read integers from user until uer says “no”. After reading the data write all the
odd numbers to a file named ODD and all the even number to a file named EVEN.
ANSWER NO. 8
a) Differences between text file and binary file are as follows :
TEXT File || BINARY File
||
– Stream of characters that a computer can process. || – Same as TEXT file but collection is in bytes.
||
– It is not only processed sequentially but only in || – No special processing of data occurs and each
forward direction. || byte is transferred to or from disk unprocessed.
||
– No sound, graphics & videos supported. || – Sound, graphics & videos are supported.
b)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp,*fpo,*fpe;
int num,i;
char ch[10];
do
{
printf(“Enter number”);
scanf(“%s”,ch);
putw(num,fp);
printf(“Do you want to continue”);
scanf(“%s”,ch);
}while(strcmp(“no”,ch));
fclose(fp);
fpo=fopen(“odd.dat”,’w’);
fpe=fopen(“even.dat”,’w’);
while(fread(&num,size of(num),fp==1))
{
if num%2==0)
putw(num,fpe);
else
putw(num,fpo);
}
fclose(fp);
fclosee(fpo);
fclosee(fpe);
printf(“content of ODD”);
fpo=fopen(“odd.dat”,’r’);
while((num=0 getw(fpe))!=EOF)
printf(“%d”,num);
fclose(fpo);
printf(“content of EVEN);
fpe=fopen(“even.dat”,’r’);
while((num=getw(fpe)!=EOF)
printf(“%dt”,num);
fclose(fpe);
getch();
}
||
9. a) Differentiate “Logical if” with “Arithematic if” in FORTRAN with suitable example.
b) Compare “Compute goto” statement (FORTRAN) and “Switch” (C Program). Write a program to read a day
number and display whether it is Sunday, Monday, Tuesday, Wednesday, Thursday< Friday and Saturday
using both concept.
ANSWER NO. 9 a) Differneces between LOGICAL “if” and ARITHEMATIC “if” are as follows :
logical if || arithematic if
||
– Uses relations & logical operations. || – Doesnot use relational & logical operation.
||
– Checks whether the value is true or false. || – Checks whether the vlaue is positive, egative or zero.
||
– General form is : || – General form is :
if (condiotion) statement || if (expression) k1,k2,k3
||
– For example : || – For example :
write(*,*)’Enter x and y’ || real x
read (*,*)x,y || read (*,*)x
if (x.gt.y) goto 1 || if (x) 1,2,3
x=x-5 || 1.write(*,*)’-ve’
goto 2 || goto 100
1.x=x+5 || 2.write(*,*) ‘0’
2.write(*,*)x || goto 100
end || 3.write (*,*) ‘+ve’
|| goto 100
|| 100.Stop
|| End
||
||
||
