M3-R4 Programming and Problem Solving Through C PDF
M3-R4 Programming and Problem Solving Through C PDF
The objectives of this course are to make the student understand programming language,
programming, concepts of Loops, reading a set of Data, stepwise refinement, Functions, Control
structure, Arrays. After completion of this course the student is expected to analyze the real
life problem and write a program in ‘C’ language to solve the problem. The main emphasis of
the course will be on problem solving aspect i.e. developing proper algorithms.
Outline of Course
Lectures = 60
Practical/tutorials = 60
Total = 120
Detailed Syllabus
48
two numbers, Test whether a number is prime, Organize numbers in ascending order,
Find square root of a number, factorial computation, Fibonacci sequence, Evaluate ‘sin x’
as sum of a series, Reverse order of elements of an array, Find largest number in an array,
Print elements of upper triangular matrix, multiplication of two matrices, Evaluate a Polynomial
Character set, Variables and Identifiers, Built-in Data Types, Variable Definition, Arithmetic
operators and Expressions, Constants and Literals, Simple assignment statement, Basic
input/output statement, Simple ‘C’ programs.
5. Arrays 06 Hrs.
One dimensional arrays: Array manipulation; Searching, Insertion, Deletion of an element
from an array; Finding the largest/smallest element in an array; Two dimensional arrays,
Addition/Multiplication of two matrices, Transpose of a square matrix; Null terminated
strings as array of characters, Standard library string functions
6. Functions 06 Hrs.
Top-down approach of problem solving, Modular programming and functions, Standard Library
of C functions, Prototype of a function: Formal parameter list, Return Type, Function call,
Block structure, Passing arguments to a Function: call by reference, call by value, Recursive
Functions, arrays as function arguments.
9. Pointers 06 Hrs.
Address operators, pointer type declaration, pointer assignment, pointer initialization, pointer
arithmetic, functions and pointers, Arrays and Pointers, pointer arrays, pointers and
structures, dynamic memory allocation.
49
RECOMMENDED BOOKS
MAIN READING
SUPPLEMENTARY READING
50
M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE
2. PART ONE is to be answered in the TEAR-OFF ANSWER SHEET only, attached to the
question paper, as per the instructions contained therein. PART ONE is NOT to be
answered in the answer book.
3. Maximum time allotted for PART ONE is ONE HOUR. Answer book for PART TWO will
be supplied at the table when the answer sheet for PART ONE is returned. However,
candidates, who complete PART ONE earlier than one hour, can collect the answer
book for PART TWO immediately after handing over the answer sheet for PART ONE.
PART ONE
(Answer ALL Questions; each question carries ONE mark)
1. Each question below gives a multiple choices of answers. Choose the most
appropriate one.
a) The Value= 62
b) The Value = O62
c) The Value= OX 3C
d) The Value= 3C
51
int main(); { /* begin main */
float F_B; F_C = 13.5;
……
return (0); }/* end main */
a) The Variable F_C is GLOBAL to both the functions main () as well as VF_A.
b) The Variable F_C is LOCAL to the function main();
c) The Variable F_C is LOCAL to the function VF_A.
d) The Variable F_C is EXTERNAL.
# define S 10+2
#include <stdio.h>
int main()
{ /* begin main */
int Result = S + S ;
return (0);
} /* end main*/
a) Result = 10
b) Result = 12
c) Result = 24
d) Result = 20
#include <stdio.h>
int main()
{ /* begin main */
return (0);
} /* end main*/
52
d) I_C= 2 F D = 2.5 F_E = 2.5
a) Call by reference.
b) Call by Value Result.
c) Call by Value.
d) Call by Name.
To manipulate a circle which of the following set of assignment statements will have
to be used ?
#define CUI_Size 10
typedef int AI_1D_01_T [CUI_Size];
int main()
{/* begin main */
AI_1D_01_T AI_1D_A;
The variable AI_1D_A represents
53
1.10 Consider the following C Code
#include <stdio.h>
#include <stdlib.h>
int main ()
{/*begin main */
int I_X=6; int *PI_Y;
PI_Y = (int*) malloc (sizeof (int));
*PI_Y = I_X;
printf(" *PI_Y =%d",*PI_Y);
*PI_Y = 7;
printf (" I_X = %d",I_X);
return(0);
} // end main
a) *PI_Y = 7 I_X = 6
b) *PI_Y = 6 I_X = 7
c) *PI_Y = 7 I_X = 6
d) *PI_Y = 6 I_X = 6
2. Each statement below is either TRUE or FALSE. Identify and mark them
accordingly in the answer book
2.1 In C %x format can be used for Inputting signed Octal Integers (FALSE).
2.2 A Pointer variable content will be the Address of the variable it points to. (TRUE).
2.3 In C , a SINGLE scanf () can be used to read in the values of any number of pre-
declared variables (TRUE).
2.4 Arrays in C are always stored in Column Major fashion (FALSE).
2.5 ! operator is a BINARY Operator in C. (FALSE).
2.6 Recursive functions provide an elegant way of representing recurrences (TRUE).
2.7 Array represents a homogeneous Data Structure (TRUE).
2.8 A structure cannot be a member of an Union in C (FALSE).
2.9 In C *p++ increments the content of the location pointed to by p (TRUE).
2.10 A C Function can return a whole structure as it’s value (TRUE).
3. Match words and phrases in column X with the nearest in meaning in column
Y.
X Y
3.1 Premature exit from within a C Loop a) 1 Byte.
3.2 Character variable will have a size of b) Indentation is essential
3.3 A C Function that do not return a c) Call by Reference.
value will be having
3.4 A string in C is terminated by d) To open a file for writing after discarding
it’s previous content
3.5 To understand the Blocks of C e) An Integer type
3.6 Multiway branching in C can be f) A void type
54
implemented
3.7 All variables declared inside a g) Are Local to that function
function
3.8 A Pointer Parameter in a C Function h) Opening a file in Read mode , retaining
simulates the previous content
3.9 A Linked List represents i) A white space character.
3.10 In C fopen “w” mode is used j) 4 Bytes
k) A ‘\0’ Charcter
l) A dynamic Data Structure
m) Using switch – case statement
n) Can be achieved by break statement
4 Fill in the blanks in 4.1 to 4.10 below, by choosing appropriate words and
phrases given in the list below:
(a) Dividing (b) One or ZERO (c) CPU (d) extern
Register
(e) Optional (f) Randomly (g) At least once (h At run time
PART TWO
(Answer ANY FOUR questions)
5. Consider the following C program Outline that DOES NOT USE any Structured Data
Type like ARRAY or STRUCTURE or POINTER whatsoever ANYWHERE
:
#include <stdio.h>
#include <math.h>
55
#define CI_Min -9999
/* NO OTHER USER DEFINED CONSTANTS, DATA TYPES OR GLOBALS
CAN BE USED*/
/* User Defined Function Prototypes. NO OTHER FUNCTIONS are used */
void VF_Read_Int ( int, int, int*); /* READS and Returns an Integer through
it’s pointer parameter provided it lies between a specific range passed as the
other two parameters . If the value read in within the happens to be
OUTSIDE this range, it will continue to loop & print the message Input
OUT of range ,
Give Again and wait for a proper value to be inputted by the user. */
int main ()
{//begin main int I_Value;
/* You May Employ other Simple Variables */ VF_Read_Int (CI_Max, CI_Min,
&I_Value);
/* Reads in an Integer Value within a Specified Range */
VF_Print_NON_Prime_Factors (I_Value);
/* Displays all the NON Prime Factors of the value I_Value*/
return(0);
}//end main
VF_Read_Int
IF_Test_Prime
. The Function heading is as illustrated below :
56
6. Consider two integer data files F1 and F2 having following features.
a. Number of data values (key) in each file is unknown and the files may be
of different sizes.
b. The values / Integer Keys in both the files F1 & F2 are Sorted in
Descending Order.
c. Same data ( key) can appear more than once in F1 or F2.
d. F1 and F2 may share common data values i.e. same key item may appear
both the files .
Write a C function to merge the two files F1 and F2 to form a third file F3 having the
following features.
7. The following operations are defined on a sorted Doubly linked list of Integers L
where elements are arranged in Descending order from left.
DELETE (L,X) : Delete the integer X from the list L (if it exists).
SHOW-MID (L) : Print the n/2 th element of the list from left where n is the
Number of elements in the current list and we use integer
Division where 5/2 = 2
8.
a. In 2 (two) dimensions, a point can be described by its two coordinates namely
X & Y both of which can be real numbers. A line can be described in the
following manner :
(i) The co-ordinates of its two end points (X1, Y1) & ( X2, Y2 )
(ii) Its gradient ‘m’ & intersection ‘c’ (in the form Y = mx + c)
(iii) The length of the line is also stored along with.
57
c. A quadrilateral can be described by a sequence of 4(four) lines such that one
end point of one line happens to be the starting point of the next line. Specify
a suitable data structure in C to represent a quadrilateral. [ 2 ]
(2)
9.
a. Write a single Recursive C function to generate the n th Fibonacci number
Fib(n) ( n being a +ve non zero integer ) . You cannot use any array, global
variables and/or additional parameters/functions. Trace out the Call & Return
sequences along with return values clearly by a schematic diagram when
your function Fib(n) is invoked from main() with n = 6. Also mention the
TOTAL no. of times any Fib(n) is called for each value of n for invoking Fib(6)
from main(), e.g. Fib(2) is called a total of 4 times etc.
(2+5+2)
b. What will be the value of A(1, 3) if A(m, n) happens to be defined in the
following manner? Specify each computation step in detail .
A(0, n) = n + 1 for n ¸ 0
A(m, 0) = A(m – 1, 1) for m > 0
A(m, n) = A(m – 1,A(m, n - 1)) for m, n > 0
(6)
58
M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE
Assignment 1.
Write a program to find sum of all prime numbers between 100 and 500.
Assignment 2.
Write a program to obtain sum of the first 10 terms of the following series for any positive
integer value of X :
Assignment 3.
Write a program to reverse the digits of a given number. For example, the number 9876
should be returned as 6789.
Assignment 4.
Write a program to compute the wages of a daily laborer as per the following rules :-
Accept the name of the laborer and no. of hours worked. Calculate and display the wages.
The program should run for N number of laborers as specified by the user.
Assignment 5.
Assignment 6.
59
Assignment 7.
Write a function, str_search(char* s1,char* s2, int n) , that takes two strings and an integer,
as arguments and returns a pointer to the nth occurrence of 1st string s1 in 2nd string s2, or
NULL if it is not present.
Assignment 8.
Write a C function to remove duplicates from an ordered array. For example, if input array
contains 10,10,10,30,40,40,50,80,80,100 then output should be 10,30,40,50,80,100.
Assignment 9.
1 1 2 3 5 8 13 ………
Assignment 10.
Write a program which will arrange the positive and negative numbers in a one-dimensional
array in such a way that all positive numbers should come first and then all the negative
numbers will come without changing original sequence of the numbers.
Example:
Original array contains: 10,-15,1,3,-2,0,-2,-3,2,-9
Modified array: 10,1,3,0,2,-15,-2,-2,-3,-9
Assignment 11.
Write a menu driven program to maintain a Telephone Directory having following file
structure:
1. Name : Character type : Length =20 characters.
2. Address : Character type : Length =40 characters.
3. Phone: Character type : Length =12 characters.
Menu
1. Add record(s)
2. Display record(s)
3. Search record(s)
4. Modify record(s)
5. Delete record(s)
6. Backup copy of File
7. Exit
Type your choice= 1,2,3,4,5,6,7— ->
Assignment 12.
60
Write a program to extract words form any text file and store in another file. Sort the words
in alphabetical order and store them in the same file. Read the sorted file and print the
frequency of each word.
Assignment 13.
Write a program to remove all occurrences of word “the” and “The” from an input string. For
example
Assignment 14.
Assignment 15.
Write a program that accepts an input integer ‘n’ in the range 3-9 inclusive, and display the
following pattern on a cleared screen.
Sample input for n=3 Sample input for n=4
Sample output Sample output
3 4
323 434
32123 43234
323 4321234
3 43234
434
4
Assignment 16.
Write a program to count the vowels in free text given as standard input. Read text one
character at a time until you encounter end-of-data. Then print out the number of
occurrences of each of these vowels.
Assignment 17.
Write a program to copy one file to another such that every word is reversed before being
written to the target file. Assume the maximum size of each word is 10 characters and each
word is separated either by new line(s), tab(s) or space(s). For example, if source file
contains “I am an Indian”, the target file should contain “I ma na naidnI”.
61
Assignment 18.
Assignment 19.
Write a program to convert a given decimal number to its binary equivalent and vice versa.
Assignment 20.
Input any positive integer number (n<=9999999). Convert the number into words.
Assignment 21.
Assignment 22.
Write a program to replace ‘a’ with ‘b’, ‘b’ with ‘c’,….,’z’ with ‘a’ and similarly for ‘A’ with
‘B’,’B’ with ‘C’, …., ‘Z’ with ‘A’ in a file. The other characters should remain unchanged.
Assignment 23.
Write a function char* stuff(char* s1,char* s2,int sp, int rp) to stuff string s2 in string s1 at
position sp, replacing rp number of characters (rp may be zero).
62
Assignment 24.
Write a program to display the content of a Text file which means it will behave like TYPE
command of MSDOS. Suppose the name of your program file: FILETYPE.C and
FILETYPE.EXE and the name of the source file is MYFILE.TXT. The following command
should work: C: \PROGRAM> FILETYPE MYFILE.TXT
Assignment 25.
Write a program to input name, address and telephone number of ‘n’ persons (n<=20). Sort
according to the name as a primary key and address as the secondary key. Print the sorted
telephone directory.
63