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

Problems On Control Flow Statements

The document outlines 4 programming assignments related to solving quadratic equations, a guessing game, a number guessing game, and printing multiplication tables. It provides the problem statement, sample outputs, and hints for each assignment involving control flow statements and programming concepts like loops and conditional statements.

Uploaded by

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

Problems On Control Flow Statements

The document outlines 4 programming assignments related to solving quadratic equations, a guessing game, a number guessing game, and printing multiplication tables. It provides the problem statement, sample outputs, and hints for each assignment involving control flow statements and programming concepts like loops and conditional statements.

Uploaded by

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

Problems on control flow statements

Assignment 1: Quadratic equation


Background:
Quadratic equation is an equation of type ax^2 + b^x + c = 0 where a, b, c are real numbers

Problem statement:
User will be prompted to provide coefficients a, b, c.
The program will give solution of the given equation along with the comment on type of equation /
roots

Possible types:

Abnormal cases :

1. Linear equation (a = 0, b, c != 0)
2. Equation with infinite roots ( a= b = c = 0)
3. Inconsistent equation (a, b = 0, c!=0)

Normal cases:

4. Equation with real distinct roots


5. Equation with real and repeated roots
6. Equation with complex roots

7. Any other type?

Hints:

1. sqrt() function is available in ‘math’ library, you may have to use it.
2. You may use ‘multiple ifs ladder’ or ‘if else if ladder’
3. In main you are allowed to use ‘return’ statement at multiple places and program immediately
stops when program flow arrives at any of the return statements.
4. Try to bring output in exactly same format as specified in the documentation
Test cases:

=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 0 0 0

Infinite Solutions.

=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 0 0 5

Inconsistent equation.

=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 0 4 5

Linear equation with single solution x = -1.250000

=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 1 5 2

Real and distinct roots :


x1 = -0.438447
x2 = -4.561553

=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 2 4 2

Real and repeated roots :


x1 = -1.000000
x2 = -1.000000

=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 2 1 2

Complex roots :
x1 = -0.250000 + 0.968246i
x2 = -0.250000 - 0.968246i

=============================================================================
Assignment 2: Guess my number
Problem statement:
 In this game, computer holds a random number in its memory (say between 1 to 100) .
 The user attempts to guess the number.
 Every time the user submits his guess, computer will give feedback to user, by stating
 Your guess is larger than the number you are predicting
OR
 Your guess is smaller than the number you are predicting
 This will continue till the user’s guess does not match with the actual number.
 When the users guess is correct, program will terminate with congratulations message.
 Computer will also mention number of attempts user took to guess the correct number.

Hints:

1) Computer can generate random number. Find out the library function in C that helps this.
2) How do we generate random number within a given range (Say 1 to 100)

Sample run:
Welcome! I have a number between 1 to 100, can you guess it?

Give your Guess : 56


Your guess is smaller than the number you are predicting

Give your Guess : 67


Your guess is smaller than the number you are predicting

Give your Guess : 87


Your guess is larger than the number you are predicting

Give your Guess : 77


Your guess is smaller than the number you are predicting

Give your Guess : 80


Your guess is smaller than the number you are predicting

Give your Guess : 82


Your guess is smaller than the number you are predicting

Give your Guess : 84


Your guess is larger than the number you are predicting

Give your Guess : 83

Congratulations! You have guessed the number in 8 attempts.


Assignment 3: I will guess your number
Problem statement:
 In this game, the roles are switched as compared to assignment 2.
 In this game, user holds a number in his mind (say between 1 to 100) and computer would guess it
 The computer will prompt its guess and wait for reply from the user.
 User would compare the number prompted by the computer (say g) with number in his mind (say x).
The user would reply the following
 ‘L’ if computer’s guess is larger than the number in user’s mind (g > x)
 ‘S’ if computer’s guess is smaller than the number in user’s mind (g < x)
 The above process would continue till your program guesses the correct number. At this point user
will respond with ‘E’
 When the user responds ‘E’, computer will tell how many attempts were required to make the guess
and then the program would terminate.

Hints:
1) You will have to decide a strategy to guess the next number. You may base it on the strategy you
used while acting as user in the previous assignment.
2) Response from the user has to be considered while guessing the next number
3) How will you ensure that user will always respond L / S / E? You may have to trap him in a loop till
he gives a valid reply.
Sample run: (Number in my mind 64)

Welcome! I am going to guess the number in your mind

Hold a number in your mind. Number should be between 1 to 100 (both included).

Every time I give you my guess, please reply as per following :


L : If my guess is larger than the number in your mind
S : If my guess is smaller than the number in your mind
E : If my guess is matching with the number in your mind

My guess is 50 Give reply (L/S/E) : S

My guess is 75 Give reply (L/S/E) : L

My guess is 62 Give reply (L/S/E) : S

My guess is 68 Give reply (L/S/E) : L

My guess is 65 Give reply (L/S/E) : L

My guess is 63 Give reply (L/S/E) : S

My guess is 64 Give reply (L/S/E) : E

Woo-Hoo! I guessed your number in 7 attempts


Thanks for playing, it was fun!
Assignment 4: Tables
Problem statement:
 This program prints tables on screen similar to tables seen in primary school mathematics books.
 User will be asked starting table (say s)
 User will be asked number of tables (say n)
 The output of the program will be display of Tables (see sample example)
 Programmer can assume that n will be small enough to accommodate all tables in screen width

Hints:
1) Nested loops will have to be used
2) In nested loops the inner look works faster and outer loop moves slower
3) Use printf formatting to ensure proper alignments of numbers in the table

Sample run:

Give starting table: 11

Give number of tables: 5

11 12 13 14 15
22 24 26 28 30
33 36 39 42 45
44 48 52 56 60
55 60 65 70 75
66 72 78 84 90
77 84 91 98 105
88 96 104 112 120
99 108 117 126 135
110 120 130 140 150

This program prints multiplication tables:

Give starting table: 171

Give number of tables: 10

171 172 173 174 175 176 177 178 179 180
342 344 346 348 350 352 354 356 358 360
513 516 519 522 525 528 531 534 537 540
684 688 692 696 700 704 708 712 716 720
855 860 865 870 875 880 885 890 895 900
1026 1032 1038 1044 1050 1056 1062 1068 1074 1080
1197 1204 1211 1218 1225 1232 1239 1246 1253 1260
1368 1376 1384 1392 1400 1408 1416 1424 1432 1440
1539 1548 1557 1566 1575 1584 1593 1602 1611 1620
1710 1720 1730 1740 1750 1760 1770 1780 1790 1800

You might also like