You are on page 1of 22

Table of contents

1. Expected solutions to Programming for Problem Solving subject old question paper
Dec.2019/Jan. 2020.
2. How much you have to write answers?
3. Assumption of marks distribution

First/Second Semester B.E. Degree Examination, Dec.2019/Jan. 2020


C programming for Problem Solving
Time: 3 hrs. Max. Marks:100
Note: Answer any FIVE full questions, choosing ONE full question from each module.

Module-1
1 a. How do you explain the components of the computer with a block diagram. (08 Marks)
Allotted
Expected answer
Marks
Drawing neat block diagram=04 Marks

08
Explanation of following components of computer. 4x1=04 Marks
1) Input unit:
The input unit helps to enter (feed) data and information into a computer. The devices like
keyboard, mouse and scanner are commonly used as input devices. A keyboard is used to enter
alphanumeric characters and symbols. The mouse is used to pick or select a command from the
monitor screen. A scanner is used to scan an image or to read a barcode and so on.
2) System or Central Processing Unit:
The processing unit comprises a processor, which interprets the program instructions in
memory, controls the flow of data and performs arithmetic and logical operations. The program
instructions are processed one at a time along with the necessary data. The results are sent to
memory and the next instruction is processed. This method is repeated until the program is
executed.
The CPU consists of sub units to perform different operations namely ALU, CU and MU.
a) Arithmetic and Logic Unit:
The arithmetic-logic unit (ALU) is the unit of the computer that performs arithmetic and
logical operations on the data. This section of the machine can be relatively small consisting of
circuits and registers which perform arithmetic (+, -, *, /) and logic (>,<,<=,>=,etc) operations.
Arithmetic-logic units which can add and subtract and perform logical operations form the
backbone for the arithmetic and control operations in computers. To perform scientific
calculations the floating-point number system is used.
b) Control unit:
The control unit controls the overall activities of the components of the computer. It is mainly
used to coordinate the activities among other units. It will send commands signals and controls
the sequence of instructions to be executed. The control unit may be defined as “the parts that
effect the retrieval of instructions in proper sequence and application of the proper signals to
the arithmetic unit and the other parts”.
3) Memory Unit: The memory unit is the unit where all the input data and results are stored.
The computer’s memory can classified into following two types.
- Primary memory : RAM and ROM
Random Access Memory (RAM) is a semiconductor chip. It is also called as volatile memory
because stored information will be lost as soon as power is switched off.
Read Only Memory (ROM) contains instructions for the microcomputers as a read only.
- Secondary memory: Hard Disk Drives (HDDs), Floppy Disks, Compact Disks (CDs), etc.
3) Output Unit:
The output device is used to display or print result from a computer. Monitor, printer and
plotter are commonly used output devices. A monitor is used to display the result in the form of
text and graphics. The printer is used to print the result. A plotter is used to plot or print
graphical result from a computer. Note that a result displayed in a monitor is temporary and it
disappears when the next result is displayed, whereas the output printed using a printer or a
plotter is permanent and these printouts can be used for any business correspondence or
documentation. Normally soft copy is referred to information that is stored on the storage
device. A hard copy refers to a print out showing the information. Monitors and printers are the
god examples for output devices.
b. Describe the types of computers. (06 Marks)
Allotted
Expected answer
Marks
Explanation of following computers 3x2=06 Marks
1 Analog Computers
06
2 Digital Computers
3 Hybrid Computers

c. convert the following mathematical expression into C equivalent statements . (06 Marks)

Allotted
Expected answer
Marks
i) m=pow(x,4)+sqrt(x+(y/k))-4*x+6 02 Marks
ii) x= (-b+sqrt((b*b)-4*a*c))/2*a 02 Marks 06
iii) Area=3.142*r*r+2*3.142*r*h 02 Marks

OR
2 a. How to write a basic structure of a C program? Explain with an examples. (08 Marks)
Allotted
Expected answer
Marks
08
Basic structure 02 Marks
Brief explanation of each section 04 Marks
Example 02 Marks

Documentation section : The documentation section consists of a set of comment lines giving the
name of the program, the author and other details, which the programmer would like to use later.
Link section : The link section provides instructions to the compiler to link functions from the
system library.
Definition section : The definition section defines all symbolic constants.
Global declaration section : There are some variables that are used in more than one function.
Such variables are called global variables and are declared in the global declaration section that is
outside of all the functions.
This section also declares all the user-defined functions.
main () function section : Every C program must have one main function section. This section
contains two parts; declaration part and executable part.
1. Declaration part : The declaration part declares all the variables used in the executable part.
2. Executable part : There is at least one statement in the executable part. These two parts must
appear between the opening and closing braces. The program execution begins at the opening
brace and ends at the closing brace. All statements in the declaration and executable
part end with a semicolon.
Subprogram section : The subprogram section contains all the user-defined functions that are
called in the main () function. User-defined functions are generally placed immediately after the
main () function, although they may appear in any order.

b. Define token. Explain four token available in C program (08 Marks)


Allotted
Expected answer
Marks
Definition 02 Marks
C tokens are the smallest individual elements of every C program, which cannot be further
divided. These acts as building blocks of every C program.
Explanation of following any four C tokens 4*1.5=06
08
1. Keywords
2. Identifiers
3. Constants
4. Operators
5. Strings, and
6. Special Symbols

c. How would you explain logical operator in C program (04 Marks).


Allotted
Expected answer
Marks
Definition 01 Marks
Logical Operators are used to combine two or more conditions/constraints or to complement
the evaluation of the original condition. The result of the operation of a logical operator is a
boolean value either true or false.
Explanation of following logical operators 3*1=03 Marks
Operator Description Example

Logical AND operator. If both the operands are non-zero, then (A && B) is
&& 04
the condition becomes true. false.

Logical OR Operator. If any of the two operands is non-zero, (A || B) is


||
then the condition becomes true. true.

Logical NOT Operator. It is used to reverse the logical state of its


!(A && B)
! operand. If a condition is true, then Logical NOT operator will
is true.
make it false.

Module-2

3 a. Explain how would describe formatted Input and formatted Output statements in C language. (08 Marks)
Allotted
Expected answer
Marks
Definition of formatted Input function 01 Marks
Syntax 1 Marks
Example with brief explanation 02 Marks
1. scanf() function
Purpose: To read data in from standard input (keyboard), we call the scanf() function.
The basic form of a call to scanf() is as follows:
scanf(format_string, list_of_variable_addresses);
format_string is the layout of the type of data being read.
List of variables specify in which variable the incoming data should be stored.

08

If a and b is a variable, then the expression &a, &b means "address of a and b”.

Definition of formatted output function 01 Marks


Syntax 1 Marks
Example with brief explanation 02 Marks
1. printf() function
Purpose: To display the data in the standard format on the display unit .
The basic form of a call to printf() is as follows:
printf (format_string, list_of_expressions);
Where:
format_string is the layout of what's being printed.
The list_of_expressions is a comma-separated list of variables or expressions yielding results to
be inserted into the output

b. How would you explain if… else statement in C- language? Give relevant example (06 Marks)
Allotted
Expected answer
Marks
Definition 01 Marks
if … else statement: It is two way selection statement. It executes either true block of
statements or false block of statements based on a given condition.
Syntax 01 Marks
if (condition)
{
True block of statements;
}
else
{
False block of statements;
}
Statement-X;
Brief explanation 02 Marks
In the above syntax, the given condition will be evaluated for TRUE or FALSE by machine, if
it is True, then the True Block of Statements are executed; otherwise, False Block of
Statements are executed.
06
Relevant Example 02 Marks
C program to find largest of two numbers using simple if … else statement(you can take any
example).
#include<stdio.h> You can also take following example
void main()
{ 1. The given number is positive or
int n1,n2;
negative.
clrscr();
printf(“Enter any two numbers”); 2. To check eligibility of voting.
scanf(“%d%d”,&n1,&n2); 3. Switch is ON or OFF.
if (n1>n2)
{
printf(“\n Largest number is %d”, n1);
}
else
{
printf(“\n Largest number is %d”, n2);
}
getch();

c. Write a program in C to display the grade based on the Marks as follows:


Marks Grades
0 to 39 F
40 to 49 E
50 to 59 D
60 to 69 C
70 to 79 B
80 to 89 A
90 to 100 O

Allotted
Expected answer
Marks
Necessary logic implementation for each of grades 06 Marks
#include<stdio.h>
void main()
{
int marks;
printf("Enter your marks ");
scanf("%d",&marks);
if(marks<0 || marks>100)
{
printf("Wrong Entry");
}
else if(marks<=39)
{
printf("Grade F");
}
else if(marks>=40 && marks<=49)
{
printf("Grade E");
}
else if(marks>=50 && marks<=59)
06
{
printf("Grade D");
}
else if(marks>=60 && marks<=69)
{
printf("Grade C");
}
else if(marks>=70 && marks<79)
{
printf("Grade B");
}
else if(marks>=80 && marks<=89)
{
printf("Grade A");
}
else
{
printf("Grade O");
}
}
OR

4 a) How would you explain switch statement with an example? (08 Marks)

Allotted
Expected answer
Marks
Definition of switch statement: 01 Marks
Syntax of switch statement: 02 Marks
Explanation 02 Marks
switch( expression )
{
case value-1: Block-1;
Break;
case value-2: Block-2;
Break;
case value-n: Block-n;
Break;
default: Block-1;
Break;
}
Statement-x;
Relevant Example 03 Marks
/* C-program to develop simple calculator*/
#include<stdio.h>
void main()
{
int a,b;
char op;
printf("Enter arthemetical expression\n");
08
scanf("%d%c%d",&a,&op,&b);
switch(op)
{
case '+':
printf("%d\n",a+b);
break;
case '-':
printf("%d\n",a-b);
break;
case '*':
printf("%d\n",a*b);
break;
case '/':
if (b!=0)
printf("%d\n",a/b);
else
printf("\nDivision by zero error\n");
break;
default:
printf("\nInvalid operator\n");
break;
}
}

b. How the while loop differs from do..while loop. (06 Marks)
Allotted
Expected answer
Marks
Any 6 valid difference between while and do..while loop 06 Marks 06
c. Write a program in c to check whether the given integer if palindrome or not.(06 Marks)
Allotted
Expected answer
Marks
Declaration of required variables 01 Marks
Implementation of necessary logic to reverse a given number 03 Marks
Conditional statement to check palindrome or not palindrome 01 Marks
Displaying of result 01 Marks
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0)
{ 06
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if orignal and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}
Module-3

5 a. Define an array. How would you explain declaration and initialization of one dimensional array? (06
Marks)
Allotted
Expected answer
Marks
Definition of an array 01 Marks
“An array can be defined as a collection of related data elements of similar data type stored in
computer’s memory continuously.
The data elements can be processed by using a common name and different index value that starts from 0
and ends with ArraySize-1”.

Illustration of 1D array declaration with syntax and example 02 Marks

where,
data_type: the data type of data to be stored and processed in computer’s memory like int,float, char,
double, long int, etc.
array_name : valid identifier
SIZE: the integer constant indicating maximum number of data elements can be stored.
Examples:
int a[20];
char name[25]; 06

Illustration of 1D array initialization with syntax and example 03 Marks


Compile Time Array Initialization: The process of assigning the values during the declaration of arrays
in the declaration part of main() is called compile time initialization.

b. Write a program in C to implement binary searching technique. (06 Marks)


Allotted
Expected answer
Marks
Declaration of required variables 01 Marks
Implementation of necessary logic to search given number 04 Marks
Displaying of result 01 Marks
// Binary Search Program using single dimension array

#include<stdio.h>
#include<stdlib.h>
void main()
06
{
int a[10],i,n,low,high,mid,key;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("\nEnter %d values in ascending order\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be searched: ");
scanf("%d",&key);
printf("Array Elements are");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
low=0;
high=n-1;
while (low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("\nSearch is Successful Number found ..!\n");
exit(0);
}
else if (key<a[mid])
high=mid-1;
else if (key>a[mid])
low=mid+1;
}
printf("\n Search unsuccessful, Number not found.... \n ");
}

c. How do you explain with examples, the string manipulation functions? (08 Marks)
Allotted
Expected answer
Marks
Explanation of any four following string manipulation functions with syntax and example
4*2=08 Marks

08

OR
6 a. Write a program to read N integers and to arrange then in ascending order using bubble sort technique.
(06 Marks).
Allotted
Expected answer
Marks
Declaration of required variables 01 Marks
Implementation of necessary logic to sort given number 04 Marks
Displaying result 01 Marks
// Bubble sort technique to sort N numbers in ascending order.
#include<stdio.h> 06
void main()
{
int a[25];
int n,i,j,temp;
printf("\n Enter the value of n ");
scanf("%d",&n);
printf("\n Enter %d integer values ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n Sorted Integer Values \n");
for(i=0;i<n;i++)
{
printf("\n %d\n",a[i]);
}
}

b. How would you explain the declaration and initialization of string variable. (06 marks)
Allotted
Expected answer
Marks
Declaration String variable with syntax and example 03 Marks
The string variable(s) must be defined in the declaration part of main() function before they are used in
executable part by using following syntax.

Example: char str[15];


char name[25]; 06
Initialization String variable with syntax and example 03 Marks
Syntax:
char string_variable[size]=”initial value”;

c. Write a program to multiply 2 matrices, by assuring their multiplication compatibility. (08 Marks)
Allotted
Expected answer
Marks
Declaration of required variables 01 Marks
Implementation of necessary logic to check compatibility 01 Marks
Implementation of necessary logic to read matrix A 01 Marks
Implementation of necessary logic to read matrix B 01 Marks
Implementation of necessary logic to perform matrix multiplication AXB 03 Marks
08
Displaying result 01 Marks
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
printf("\n Enter order of matrix A");
scanf("%d%d",&m,&n);
printf("\n Enter order of matrix B");
scanf("%d%d",&p,&q);
if (n!=p)
printf("\n Multiplication of A and B is not possible");
else
{
printf("\n Multiplication of A and B is possible");
printf("\n Enter the elements of A \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the elements of B \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n Matrix A \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
printf("\n Matrix B \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\n Resultant Matrix C \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("\t %d",c[i][j]);
}
printf("\n");
}
}
return(0);
}

Module-4

7 a. How would you illustrate the elements of user defined functions with examples.(10 Marks)
Allotted
Expected answer
Marks
Listing out the elements of user defined functions 01 Marks
1. Function declaration or prototype.
2. Function call.
3. Function definition.
Explanation of each of these functions with examples 3*3=9 Marks
1. Function declaration or prototype:
The function must be declared in the global declaration part of the C program i.e. before the
main().
It provides three details of a function to C-compiler i.e. return type, function name and number
of inputs.

Example:

10

2. Function call:
The user defined function is called by the main() function to do specific task by passing the
number of inputs.

Example:
3. Function definition:
This is the subprogram, which can be written before or after the main(). It also contains group
of statements to do specific task.
Syntax:

Example:

b. Write a program in C to find the factorial of a given number using functions. (05 Marks)
Allotted
Expected answer
Marks
Declaration of required variables 01 Marks
Implementation of necessary logic to factorial of a given number 04 Marks

05
c. Explain how call by value differs from call by reference while invoking a function. (05 Marks)
Allotted
Expected answer
Marks
Any five valid difference between call by value and call by reference 05 Marks

05

OR
8 a. How would explain the categories of user defined functions?(10 Marks)
Allotted
Expected answer
Marks
Listing out category of user defined functions based on number of arguments and return value
02 Marks.
1. Functions with no arguments and no return value
2. Functions with arguments and no return value:
3. Functions with arguments and return value:
3. Functions with no arguments and return value:
10
Explanation of each of these function types 4*2= 08 Marks

4. Functions with no arguments and no return value


These functions do not receive any inputs from the calling function main() and do not return
any value back to it. But, these functions do specific task. i.e. there is no data transmission
between calling and called function.
5. Functions with arguments and no return value:
These functions receive inputs from the calling function main() to do specific task; but, do not
return any value back to it. The processed data will be displayed by it.

4. Functions with arguments and return value:


These functions receive inputs from the calling function main() to process and return a value
back to it. i.e. data transmission takes place between calling and called function.

5. Functions with no arguments and return value:


These functions do not receive any inputs from the calling function main(); but, return a value
back to it.

b. Write a program in C to compute the Fibonacci series up to ‘N’ terms using recursion. (06 Marks)
Allotted
Expected answer
Marks
Declaration of required variables 01 Marks
Implementation of necessary logic to compute Fibonacci series up to ‘N’ terms 04 Marks
Displaying the result(series) 01 Marks

06
c. List the storage class specifiers. Explain any one of them.(04 Marks)
Allotted
Expected answer
Marks
Listing out storage class specifiers 01 Marks

04

Explanation of any one storage class specifiers 03 Marks.

Module-5

9 a. Define structure. How would you declare and initialize structure variable? Give examples.
Allotted
Expected answer
Marks
Definition of structure: 01 Marks
Defining structure 1 Marks
struct <tag_name>
{
type1 member1;
type2 member2;
type3 member3;

type-n member-n; 07
};
Declaration of Structure Variables with example : 01 Marks
Syntax:
struct <tag_name> <list of variables separated by comma>;
Initialization of structure variable 01 Marks
Syntax:
struct <tag_name> structure variable={Initial values separated by comma};
Example for structure definition, declaration and initialization 03 Marks
b. Define a Pointer. How the pointers are declared and initialized.(06 Marks)
Allotted
Expected answer
Marks
Definition of pointer 01
Declaration of Pointer syntax and example :01+01=02Marks
Pointers must be defined in the declaration part of the main program before they are used in
executable part by using following syntax.
Syntax
data_type *pointer_name;
where,
data_type : It may be any data type like int, float, char, double, structure type etc.
pointer name: valid identifier
Initialization of Pointers syntax and example=01+02=03 Marks
The pointers can be assigned (initialized) with the address of other variables by using the
following syntax. 06
Syntax:
pointer=&variable
Where,
Pointer : valid pointer
& : ampersand sign (Meaning: address of)
Variable: Well defined variable.
Example for declaration and initialization 03 Marks
int n=7; /* normal or scalar variable */
int *ip; /* integer pointer ip to store address of n variable*/
ip=&n; /* Address of n variable will be stored in ip pointer*/

c. Write a C program to read details of 10 students and print the marks of the student if his name is given as
input. (07 Marks)

Allotted
Expected answer
Marks
Declaration of required variables 01 Marks
Implementation of necessary logic to read 10 students details such as Roll_No, Name, and
Marks 02 Marks
Condition to match the name of the student and print the marks of the student. 04 Marks
#include<stdio.h>
#include<conio.h>
struct student
{ 07
int Rollno;
int Marks;
char Name[20];
char G;
};
struct student s[20];
int n,i;
void main()
{
char namesearch[20];
printf("Enter the number of students\n");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
printf("Enter the details of %d students\n",i);
printf("Enter the Rollno\n");
scanf("%d",&s[i].Rollno);
printf("Enter the Name\n");
scanf("%s",s[i].Name);
printf("Enter the Marks\n");
scanf("%d",&s[i].Marks);
printf("Enter the Grade\n");
s[i].G=getche();
printf("\n");
}
printf(“Enter the name of the student to display his marks\n”);
scanf(“%s”, namesearch);
printf("The names of student is as follows\n");
for(i=1;i<=n;i++)
{
if(strcmp(s[i].name, namesearch)
{
printf("Rollno=%d\t Name=%s\t Marks=%d\t and
Grade=%c\n",s[i].Rollno,s[i].Name,s[i].Marks,s[i].G);
}
}
getch();
}

OR

10 a. Write a program in C to add two numbers using pointers.(05 Marks)


Allotted
Expected answer
Marks
Declaration of required normal variables 01 Marks
Declaration of required pointer variables 01 Marks
Initialization of pointer variables 01 Marks
Implementation of logic to add two numbers 01 Marks
Displaying result 01 Marks

#include <stdio.h>
int main()
{
int n1, n2, *p, *q, sum; 05

printf("Enter two integers to add\n");


scanf("%d%d", &n1, &n2);
p = &n1;
q = &n2;
sum = *p + *q;
printf("Sum of the numbers = %d\n", sum);
return 0;
}
b. How would you explain the categories of preprocessor directives in C?(10 Marks)
Allotted
Expected answer
Marks
Listing out the C- preprocessor directives 01 Marks
Explanation of each preprocessor directive with syntax and example 3*3=09 Marks
1 Macro substitution directives:
These are used to replace an identifier (symbolic constant) with equivalent strings.
These are used to define symbolic constants by using #define directive.
Syntax:
# define <identifier> <equivalent string>
Examples:
# define N 100
# define MAX 50
# define MIN 0
# define PI 3.1452
2 File inclusion directives:
The file inclusion directives help the programmers to include external header files or C files in
to C program by using # include directive.
We are able to access functions and macro from the included external files. Syntax:
# include “filename” or # include <filename>
10

3. Compiler control directives:


The compiler control directives help the programmers to define or undefined macros based on
given condition. These can be used to write portable programs.(program written for one
machine works on another machine)
Directives Explanation
#ifdef It will test, whether macro is defined
#endif It is the end of if
#ifndef It will test, whether macro is not defined
#if the if … else sequence
#else
#elif the else … if sequence

c. How would you explain the nested structure.(05 Marks)


Allotted
Expected answer
Marks
Explanation of nested structure by taking simple example 05 Marks

05
***************************ALL THE BEST***************************

You might also like