Assignment 2
Assignment 2
36 – 40
iv) The U.S CDC(Center for Disease Control) determine obesity according to Body
Mass Index computed by the formula:
Index = (weight in Kilograms) / (Height in meters)2.
An index of 27.8 or greater for men or 27.3 or greater for non pregnant women is
considered obese. Write a program that prompts for weight, height and sex and
determine whether the user is obese or not. Write a function that returns the BMI
given the height and weight in inches and pounds.
( Note that: 1m = 39.37 inches, 1inch=2.54cm 1kg=2.2pounds 1pound=454gm)
v) Classify the different types of programming errors. A program has been compiled
and linked successfully. When you run this program you face one or more of the
following situations.
Program executed, but no output
It produces incorrect answers
It does not stop running.
What are the possible causes in each case and what steps would you take to
correct them?
Roll No. 31 - 35
So far we have seen the boring printf, which does not allow us to move the cursor around.
Using the additional ncurses library, we can print to arbitrary places on the screen. Since
this is an additional library, not part of the standard C libraries, we must tell the computer
to include the library. The #include <ncurses.h> tells the computer to add the .h header file,
but this unfortunately does not tell the computer to add the compiled library to
the linking process. To do the latter, we must add -lncurses to our cc command.
Explain
Write a tic-tac-toe game.
Roll No. 26 - 30
6. i) Explain recursion.
ii) What is a Stack frame or an activation record? What kind of information is generally
stored in it? Trace the following program by writing the activation record.
int sum(int n) {
if (n < 1) return 0;
return sum(n - 1) * (n - 1) + n;
}
int main()
{
printf(“%d”, sum(5);
}
iii) Write a program to calculate the value for f(4) for the following recursive function
definition:
f(0) = 1
f(n) = (f(n -1) * n) + n
iv) Write a recursive C function which calculates the sum function as defined below
sum(0) = 0
sum(n) = sum(n - 1) + n
Write the iterative function for the same
Roll No. 21 – 25
Roll No. 16 – 20
4. Write a C program for solving simultaneous equations of any degree using CRAMER’s
RULE. Write function DETERMINANT that takes matrix, number of row and column as
argument and returns the determinant.
Write a C program for solving simultaneous equations of any degree using REDUCED
ROW ECHELON FORM.
Roll No. 11 – 15
3. i) Write a C program for converting decimal numbers to and from Roman numerals for any
decimal number up to 3999.
Implement two functions:
If N is between 1 and 3999, inclusive, translate the number N into Roman numerals
and write the result (as a null-terminated string) into ans, assuming ans has enough
space to hold the result. Return 0 for success. Rules for Roman numerals are given
below.
If N is not between 1 and 3999, return 1 if N is too large, 2 if N is zero, and 3
if N is negative. In those cases, the space pointed to by ans must not be modified.
A practical way to write a Roman number is to consider the modern Arabic numeral system,
and separately convert the thousands, hundreds, tens, and ones as given in the chart above.
So, for instance, 1234 may be thought of as “one thousand and two hundreds and three tens
and four”, obtaining M (one thousand) + CC (two hundreds) + XXX (thirty) + IV (four),
for MCCXXXIV. Thus eleven is XI (ten and one), 32 is XXXII (thirty and two) and 2009
is MMIX (two thousand and nine).
. ii) Write a program where you declare a two-dimensional array labScores which contains 25
rows and 12 columns. Each row corresponds to a particular student and each column
corresponds to a particular lab score.
Define the number of rows and columns as constants.
Write a function with a 2-D array as parameter. The function reads data from a file
“input.txt” into this array. Each row in the file “input.txt” contains 12 lab marks
corresponding to a certain student. As shown below:
80 90 70 100 60 90 85 78 93 80 70 98
98 85 100 99 89 90 72 0 78 98 100 65
85 72 95 75 64 88 96 45 55 68 98 89
67 11 28 89 85 90 98 85 87 56 69 22
Add a function to print the scores so that each student's labs appear on a separate line of
output. Include a statement in your main program to call this function. Your output
should be labeled as follows:
Student 1: 80 90 70 100 60 90 85 78 93 80 70 98
Student 2: 98 85 100 99 89 90 72 0 78 98 100 65
.
.
Add a function, say StudentAvg(), which finds and prints the lab average for each student
in the class. A function prototype for this function is shown below.
Roll No. 6 – 10
2. i) Create a user type TIME_info to store the time in hh:mm:ss.sss format and declare &
initialize a variable to current time.
Write a function to read a time into a TIME_info variable.
Write another function to print the time in hh:mm:ss.sss format
Write a function that converts time values given in seconds (e.g., 12345.67) to time
values given in hh:mm:ss.sss (3:25:45.67) format might have the prototype:
o TIME_info convertTime( double realTime )
Write a function for adding two times given in the hh:mm:ss.sss format might have the
prototype
o TIME_info addTimes(TIME_info one, TIME_info two )
Write a function for subtracting two times given in the hh:mm:ss.sss format might have
the prototype
o TIME_info subTimes(TIME_info one, TIME_info two )
Write a function normalize having prototype
timeinfo_t normalize( timeinfo_t originalTime )
that returns a normalized representation for originalTime.
Now modify your addTimes function, so that it produces output in normalized form.
ii) Explain array. A store sells the following 4 products. The products are given in an array
“products” of strings and the prices are in an array “prices” of doubles.
Product Price (Rs)
MP3 Player 5000
WII 10000
DVD Player 2000
Digital Camera 8000
Write a program to show statistics of total sales as follows:
(a) Define the arrays products and prices and initialize them.
(b) Read the sale quantity of each product into an array.
(c) Print the sale table and shows the total sale including total quantity and total sales.
(d) List which product is sold most and its sale quantity and which product is sold least
and its sale quantity.
Roll No. 1 – 5
1. i) Explain Pointers. How are pointers, arrays, and strings related in C? Explain with an
example.
ii) Explain dynamic memory allocation.
iii) Define and differentiate the following. Give examples for each and explain in detail.
o Array and pointer
o Array and structure
o Structure and Union
o Macro and Function
o String and Character array