Subject : Computer Programming
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:
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.
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.
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.
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).
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.
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
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
b) Give the output of the following program and justify your answer with reason.
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.
a=x*2 + y/5-z*y;
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.
printf(“Enter a number”);
printf(“n %d is not prime”,n);
printf(“n %d is prime”,n);
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.
printf(“n How many numbers”);
printf(“n Enter %d numbers t”,n);
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.
void output(employee e[MAX],intn);
void sort(employee e[MAX],intn);
printf(“how many data?”);
void output(employee e[MAX],intn)
void sort(employee e[MAX],intn);
struct employee 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.
printf(“value before swap is i:%d,j:%d n”,i’j);
printf(“value after swap is i:%d,j:%dn”,i,j);
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.
printf(“Do you want to continue”);
printf(“content of ODD”);
printf(“content of EVEN);
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