0% found this document useful (0 votes)
4 views

Assignment 2

The document contains a series of programming tasks and questions related to C programming, including topics such as modular programming, user-defined functions, recursion, graphics using Turbo C, and error classification. It also includes specific programming challenges like creating a tic-tac-toe game, converting decimal numbers to Roman numerals, and calculating BMI. Additionally, it covers concepts like pointers, dynamic memory allocation, and various data structures, providing a comprehensive overview of essential C programming skills.

Uploaded by

pavvankumaar007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 2

The document contains a series of programming tasks and questions related to C programming, including topics such as modular programming, user-defined functions, recursion, graphics using Turbo C, and error classification. It also includes specific programming challenges like creating a tic-tac-toe game, converting decimal numbers to Roman numerals, and calculating BMI. Additionally, it covers concepts like pointers, dynamic memory allocation, and various data structures, providing a comprehensive overview of essential C programming skills.

Uploaded by

pavvankumaar007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Roll No.

36 – 40

8. i) Explain Modular programming. Explain User defined function in C.


ii) Write a C program to use the functions
 numpalindrome(int ) that receives a number and checks if the number is a
palindrome.The numpalindrome checks if the reverse of the given number is
the same.
 Isvowel( char *) that receives a string and counts the number of vowels in a
String.
iii) Explain storage classes in C.

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

7. Graphics in Turbo C using a challenging problem

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.

 Print the game board.


 Your game should be human-vs-computer, and human moves first.
 Computer moves randomly. Note that it must select an unoccupied square; you
cannot simply pick a number from 1-9 at random without checking!
 Before each player takes a move, print out the current game board.
 After each player takes a move, you must check to see if anybody won the game.
(hint: write a function that checks if player X won the game, then call this function
twice, for player 1 and player 2)
(another hint: there are 8 possible winning combinations; a player wins if one of 8
combinations of 3 square all have his mark. You can either write out all 8
combinations in full, or use three for loops -- test the three vertical wins, three
horizontal wins, and two diagonal wins.)
(final hint: begin work on this point by changing the rules of the game. Pretend that
you can only win by going horizontally or diagonally across the middle.
(optional: instead of having the computer move randomly, try to make it play
intelligently.)

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

5. Write recursive approach and iterative approach to


(i) Find the factorial of a number
(ii) Find Greatest Common Divisor (GCD) of two numbers
(iii) To generate Fibonacci sequence
(iv) Reverse ‘n’ characters.
(v) power (x,y) that will compute the nth power of x, where x is a double and n is an int

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:

int DecimalToRoman(int N, char ans[]);

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.

int RomanToDecimal(const char x[]);


Determine if x is a valid Roman numeral for a number between 1 and 3999
inclusive.
If so write the translation into decimal (ordinary base 10) notation into a global
variable ans, and return 0. If x is not a valid Roman numeral, return 1 if x contains
a character other than I, V, X, L, C, D, and M; return 2 if x contains only those
characters but in some illegal combination, such as IL or VIIII. MMMM is also
considered an illegal combination.

The basic multiples of Roman numerals follow a pattern:

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.

void StudentAvg(int labScores [][MAX_LABS], //IN: Lab scores


int numStudents, //IN: # of students in the class
int numLabs) //IN: # of labs recorded per student
Add a function, say labAvg(), which finds and prints the average score made on each
individual lab.

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.

A possible run may look like:


============================================
The number of MP3 Player sold: 3
The number of WII sold: 4
The number of DVD Player sold: 2
The number of Digital Camera sold: 5
============================================
The statistic of sales is as follows:
============================================
Product Unit Price QTY Total Price
-------------- ---------- --- -----------
MP3 Player 5000 3 15000
WII 10000 4 40000
DVD Player 2000 2 4000
Digital Camera 8000 5 40000
Total 14 99000
============================================
Product sold most: Digital Camera 5
Product sold least: DVD Player 2

iii) Analyze the following code.


main()
{
static int a[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
static *p[]={a,a+1,a+2};
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i));
}
}

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

You might also like