Structure Programming Approach
Structure Programming Approach
II
Structured Programming Approach
Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 80
Consider the following example where the cast operator causes the division
of one integer variable by another to be performed as a floating-point
operation:
#include <stdio.h>
main()
{
int sum = 17, count = 5;
double mean;
When the above code is compiled and executed, it produces the following
result:
Value of mean : 3.400000
It should be noted here that the cast operator has precedence over division,
so the value of sum is first converted to type double and finally it gets
divided by count yielding a double value.
1
Vidyalankar : F.E. SPA
sum = i + c;
printf("Value of sum : %d\n", sum );
}
When the above code is compiled and executed, it produces the following result:
Value of sum : 116
sum = i + c;
printf("Value of sum : %f\n", sum );
}
2
Prelim Question Paper Solution
When the above code is compiled and executed, it produces the following
result:
Value of sum : 116.000000
Q.1(b) Explain any three string library functions defined in string [5]
header file string.h with suitable example.
(A) String-Handling Functions [1 mark]
Fortunately, the C library supports a large number of stringhandling functions
that can be used to carry out many of the string manipulations discussed so
far. Following are the most commonly used stringhandling functions.
Function Action
strcat( ) concatenates two strings
strcmp( ) compares two strings
strcpy( ) copies one string over another
strlen( ) finds the length of a string
We shall discuss briefly how each of these functions can be used in the
processing of strings.
string1 and string2 are character arrays. When the function strcat
executed, string2 is appended to string1. It does so by removing the null
character at the end of string1 and placing string2 from there. The
string at string2 remains unchanged. For example, consider the following
three strings:
0 1 2 3 4 5 6 7 8 9 0 1
Part1 = V E R Y \0
0 1 2 3 4 5 6
Part2 = G O O D \0
0 1 2 3 4 5 6
Part3 = B A D \0
3
Vidyalankar : F.E. SPA
0 1 2 3 4 5 6
Part2 = G O O D \0
We must make sure that the size of string1 (to which string2 is
appended) is large enough to accommodate the final string.
strcat function may also append a string constant to a string variable. The
following is valid:
strcat(part1, “GOOD”) ;
C permits nesting of strcat functions. For example, the statement
strcat (strcat (string1, string2), string3) ;
is allowed and concatenates all the three strings together. The resultant
string is stored in string1.
string1 and string2 may be string variable or string constants. Examples are:
strcmp (name1, name2) ;
strcmp (name1, “John”) ;
strcmp (“Rom”, “Ram”) ;
4
Prelim Question Paper Solution
Our major concern is to determine whether the strings are equal; if not,
which is alphabetically above. The value of the mismatch is rarely
important. For example, the statement
strcmp(“their”, “there”) ;
will assign the string “DELHI” to the string variable city. Similarly, the
statement
strcpy(city1, city2) ;
will assign the contents of the string variable city2 to the string variable
city1. The size of the array city1 should be large enough to receive the
contents of city2.
Q.1(c) Explain any three terms related to function by giving suitable [5]
example :
(i) Function call
(ii) Function definition
(iii) Function declaration
(iv) Arguments of a function.
5
Vidyalankar : F.E. SPA
Defining a Function
The general form of a function definition in C programming language is as
follows:
return_type function_name( parameter list)
{
body of the function
}
e.g. : int fact (int n)
{ if (n == 1)
return 1;
else
return (n fact (n1));
}
6
Prelim Question Paper Solution
Program : [3 marks]
1. Factorial of a given number using call by value
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n,ans;
clrscr();
ans=fact(n);
7
Vidyalankar : F.E. SPA
getch();
}
int fact(int n)
{
int i,fac =1;
for(i=0;i<=n;i++)
fac=fac*i;
return fac;
}
8
Prelim Question Paper Solution
9
Vidyalankar : F.E. SPA
increment ( );
increment ( );
getch ( );
}
increment ( )
{
static int i;
printf (“%d”, i);
i + +;
}
Q.3(a) Define a structure called cricket that will describe following [10]
information. Player name, country name, best score, batting
average. Develop a program that will store information of 25
cricket players using the structure.
(i) Display the name of those cricketers in decaending order with
respect to their batting average
(ii) Display the name of the player with highest batting average.
10
Prelim Question Paper Solution
Q.3(b) What is file? What are the types of files? What are the different [10]
file handling modes?
(A) In C programming, file is a place on disk where a group of related data is stored.
11
Vidyalankar : F.E. SPA
#include <stdio.h>
#include <conio.h> [1 mark]
void main ( )
{
int i, j;
for (i = 1; i <= s; i++)
{ [3 marks]
for (j = 1; j <= i; j++)
{
printf (“ * ”);
}
printf (“\n”);
} [2 marks]
getch ( );
}
13
Vidyalankar : F.E. SPA
{
printf("Enter a value:");
scanf("%d", &a[i] [j]) ;
}
}
printf("The number of rows for matrix 2 will be %d\n",n);
printf("Enter the columns of matrix 2:");
scanf("%d", &p);
printf("Enter the elements of matrix 2:\n");
for (i=0;i<=n1;i++)
{
for(j=0;j<=p1;j++)
{
printf("Enter a value:");
scanf("%d", &b[i] [j]) ;
}
}
MatMul (a, b, m, n, p);
getch();
}
void MatMul (int a [10] [10], int b[10][10], int m, int n, int p)
{
int i, j , k, c [10] [10] ;
for (i=0;i<=m1; i++)
{
for(j=0;j<=p1;j++)
{
c[i] [j]=0;
for(k=0;k<=n1;k++)
{
c[i] [j]=c[i] [j]+a[i] [k]*b[k] [j];
}
}
}
printf("The resultant matrix is:\n");
for(i=0;i<=m1;i++)
{
for(j=0;j<=p1;j++)
{
printf("%d\t", c[i] [j]) ;
}
14
Prelim Question Paper Solution
printf("\n");
}
}
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Example [3 marks]
Try the following example to understand all the bitwise operators available
in C programming language:
#include<stdio.h>
main()
{
unsignedint a =60; /* 60 = 0011 1100 */
unsignedint b =13; /* 13 = 0000 1101 */
int c =0;
15
Vidyalankar : F.E. SPA
When you compile and execute the above program it produces the following
result:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
16
Prelim Question Paper Solution
Q.5(b) Explain general structure and flow chart of : else if ladder [5]
(A) else if ladder [3 marks]
General structure :
if (c1)
{
b1
}
else if (c2)
{
b2
}
else if (c3)
{
b3
}
else if (cn)
{
bn
}
else
{
else body
}
Flow chart : [2 marks]
Y N
c1
Y N
c2
b2
Y N
cn
bn else body
stop
17
Vidyalankar : F.E. SPA
Q.5(c) State the difference between gets() and puts() function. [5]
(A) Using getchar and gets Functions [2½ marks]
We have discussed, as to how to read a single character from the terminal,
using the function getchar. We can use this function repeatedly to read
successive single characters from the input and place them into a character
array. Thus, an entire line of text can be read and stored in an array. The
reading is terminated when the newline character (‘\n’) is entered and the
null character is then inserted at the end of the string. The getchar
function call takes the form:
char ch;
ch = getchar( );
Every time a character is read, it is assigned to its location in the string line
and then tested for newline character. When the newline character is read
(signaling the end of line), the reading loop is terminated and the newline
character is replaced by the null character to indicate the end of character
string.
When the loop is exited, the value of the index c is one number higher than
the last character position in the string (since it has been incremented after
assigning the new character to the string). Therefore the index value c-1
gives the position where the null character is to be stored.
str is a string variable declared properly. It reads characters into str from
the keyboard until a newline character is encountered and then appends a
null character to the string. Unlike scanf, it does not skip whitespaces. For
example the code segment
char line [80];
gets (line) ;
printf (“%s”, line);
18
Prelim Question Paper Solution
reads a line of text from the keyboard and displays it on the screen. The
last two statements may be combined as follows:
printf(“%s”, gets(line)) ;
The function putchar requires one parameter. This statement is equivalent to:
printf(“%c”, ch);
We have used putchar function to write characters to the screen. We can use
this function repeatedly to output a string of characters stored in an array
using a loop.
Example: char name[6] = “PARIS”
for (i=0, i<5; i++)
putchar(name[i];
putchar(‘\n’) ;
Another and more convenient way of printing string values is to use the
function puts declared in the header file <stdio.h>. This is a one parameter
function and invoked as under:
puts ( str );
where str is a string variable containing a string value. This prints the value
of the string variable str and then moves the cursor to the beginning of the
next line on the screen. For example, the program segment
char line [80] ;
gets (line) ;
puts (line) ;
reads a line of text from the keyboard and displays it on the screen. Note
that the syntax is very simple compared to using the scanf and printf
statements.
Q.5(d) Write a program to take n from user and print first n terms of [5]
Fibonacci series.
(A) #include<stdio.h> [5 marks]
#include<conio.h>
void main()
19
Vidyalankar : F.E. SPA
{
int a = 0,b = 1, c, i, n;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
printf("Fibonacci Series\nO\nl\n");
for(i=l;i<=n-2;i++)
{
c=a+b;
printf("%d\n",c) ;
a=b;
b=c;
}
getch();
}
Overview : [3 marks]
Many times the length of program matters for the execution. Sometimes
code gets expand which becomes tedious to read and understand. Therefore
it is necessary that the code should be compact. ’Recursion’ is a concept
where we call same function inside function itself. Recursion is heavily used
whenthere is need of repeated execution of some task. Though recursion is
advantageous, it proves inefficient with respect to time and space
complexity. Calling the same function again and again leads to increase in
execution time. For every next pass, the details of previous pass are pushed
on to the stack (data structure) which increases the space requirements of a
program. Recursion may seem strange and complicated at first glance, but it
is often the most direct way to code an algorithm, and once you are familiar
with recursion, the clearest way of doing so.
20
Prelim Question Paper Solution
21
Vidyalankar : F.E. SPA
clrscr();
printf("Enter a string:");
gets(a);
while(a[n]!='\0')
{
n++;
for (i=0;i<=(n1);i++)
{
rev [n i 1] = a[i];
}
for(i=0;i<=n-l;i++)
{
if(a[i]!=rev[i]) break;
}
if(i==n)
printf("The string is palindrome.");
else
printf("The string is not palindrome.");
getch();
}
Q.6(c) What are the various factors need to be considered while designing [4]
problem definition of a particular program?
(A) The first step in software development is to specify the problem to be solved.
This may be more demanding than it sounds. Software development
professionals can tell plenty of stories of how they demonstrated a program, or
an early prototype, and found that the client’s first comment was to request a
change or addition to the program’s original purpose. [1 mark]
The specification stage is also called analysis. The skills required have more
to do with understanding a business or organization than using a
programming language. The more thorough the analysis and specification are,
the less effort will be needed at a later stage to re-specify the task that
the program is to perform. Professionals who specialize in this work are
called systems analysts. [1 mark]
Key components of a program’s specification are its input, its output, and the
relationship between them. Software developers have long prepared sample
input/output as part of program specification. Most of our programming
exercises include sample I/O. [1 mark]
22
Prelim Question Paper Solution
As software has become more complex, the specification may include entire
screens, such as forms with which to get input or windows to display results.
Note that the specification phase does not address the critical question of
how to arrive at the desired output from a given set of user input. That
issue is left for the design phase. [1 mark]
23