0% found this document useful (0 votes)
10 views16 pages

C Practical Questions - 095504

C language questions

Uploaded by

mercydaves01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

C Practical Questions - 095504

C language questions

Uploaded by

mercydaves01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

C Practical Exercise Questions

Problem Set 1: Basic Arithmetic Operations


Write a program that prompts a user to enter two integer numbers.
Read in the numbers and calculate sum, difference, product, and
quotient. Output the results as shown below.
This is the console output from the screenshot as plane text:
Enter two integers: 4 3
The sum is 7
The product is 12
The difference is 1
The quotient is 1
Please make sure that you write the string describing what input the
user should provide exactly as shown in the screen shots. In particular,
make sure that this string includes all necessary white space, e.g.,
blanks. Due to how the tests are performed, you will receive 0 points if
there is a mismatch.

Problem Set 2: Larger Numbers


Write a program that prompts a user to enter two integer numbers.
Read in the numbers. Output the larger of the two numbers followed
by "is larger". If the numbers are equal, please print "These numbers
are equal."
Here is the console output from the screenshots as plane text:
Enter two integers: 24 59
59 is larger.
Enter two integers: 10 10
These numbers are equal.
Please make sure that you write the string describing what input the
user should provide and the output statements exactly as shown in the
screen shots. In particular, make sure that these strings includes all
necessary white space, e.g., blanks. Due to how the tests are
performed, you will receive 0 points if there is a mismatch.

Problem Set 3: Radius of a Circle


Write a program that prompts a user to enter the radius of a circle (as
an integer number). The program should then calculate the diameter,
the circumference, and the area. The results are to be printed to
screen. Use 3.14159 for the constant pi. Your output should resemble
the screen shot shown below.
This is the console output from the screenshot as plane text:
Enter the circle radius: 2
Diameter is 4
Circumference is 12.5664
Area is 12.5664
Please consider checking that your input variable is valid, e.g., by
thinking about what values it can assume.
Please make sure that you write the string describing what input the
user should provide and the output statements exactly as shown in the
screen shots. In particular, make sure that these strings includes all
necessary white space, e.g., blanks. Due to how the tests are
performed, you will receive 0 points if there is a mismatch.
Problem Set 4: Three Numbers
Write a program that reads three integer numbers from the keyboard.
Based on these three numbers, the integer sum, the integer average,
and the integer product are to be calculated. Finally, also find the
smallest and the largest of the three numbers (type integer). Display
the results as shown below.
This is the console output from the screenshot as plane text:
Input three different integers: 1 2 3
Sum is 6
Average is 2
Product is 6
Smallest is 1
Largest is 3

Please make sure that you write the string describing what input the
user should provide and the output statements exactly as shown in the
screen shots. In particular, make sure that these strings includes all
necessary white space, e.g., blanks. Due to how the tests are
performed, you will receive 0 points if there is a mismatch.

Problem Set 5: Odd or Even


Write a program involving an if... else control statement that reads an
integer and determines and prints whether it is odd or even. Output
the results as shown below. [Hint: Use the modulus operator. An even
number is a multiple of two. Any multiple of two leaves a remainder of
zero when divided by 2.]
This is the console output from the screenshots as plane text:
Enter an integer: 0
The integer 0 is even.
Enter an integer: 1045
The integer 1045 is odd.
Please make sure that you write the string describing what input the
user should provide and the output statements exactly as shown in the
screen shots. In particular, make sure that these strings includes all
necessary white space, e.g., blanks. Due to how the tests are
performed, you will receive 0 points if there is a mismatch.

Problem Set 6: Number Systems Table


Use a for loop to write a program that prints a table of the decimal,
octal and hexadecimal equivalents of the decimal numbers in the range
1 to 256. You can use the stream manipulators dec, oct and hex to
display integers in decimal, octal and hexadecimal formats,
respectively. Use tabs ('\t') to display table headers and the numbers,
respectively. Your program output should follow the example shown
below.
Problem Set 7: ASCII-Code
C can represent uppercase letters, lowercase letters and special
symbols. You can print a character by enclosing that character in single
quotes, as with
cout << 'A'; // print an uppercase A
You can print the integer equivalent of a character using static_cast as
follows:
cout << static_cast< int >( 'A' ); // print 'A'
This is called a cast operation. When the preceding statement executes,
it prints the value 65 (on systems that use the ASCII character set).
Write a program that prints the integer equivalent of a character typed
at the keyboard as follows:
Store the input in a variable (named symbol) of type char and use the
command cin >>symbol; to read it in.
Afterwards cast symbol into the integer i. Then use the following
source code to print out the data and ask for another input, which than
should be read in:
cout<<"The integer equivalent of "<<symbol<<" is "<<i<<endl<<endl;
cout<<"Enter a character: ";
Use a sentinel controlled while loop (with sentinel 'a').
Test your program several times using uppercase letters, lowercase
letters, digits and special characters (such as #). Follow the screen shots
below.

This is the console output from the screenshot as plane text:

Enter a character: A
The integer equivalent of A is 65

Enter a character: 1
The integer equivalent of 1 is 49

Enter a character: 0
The integer equivalent of 0 is 48

Enter a character: #
The integer equivalent of # is 35

Enter a character: a
Please make sure that you write the string describing what input the
user should provide and the output statements exactly as shown in the
screen shots. In particular, make sure that these strings includes all
necessary white space, e.g., blanks. Due to how the tests are
performed, you will receive 0 points if there is a mismatch.

Problem Set 8: Building a menu using do-while and switch


A menu in a computer program presents a list of alternatives for a user
to choose from. Implement the menu shown below by nesting a switch
statement inside a do-while loop.
The sentinel ‘0’ is used to exit the do- while loop. The user input is to be
extracted from the input stream into a variable of type char.
Do not forget to implement a default case for the switch statement to
process invalid inputs in this case the text "Not a valid choice. Choose
again." should be displayed.
This is the console output from the screenshot as plane text:
Select 1 to select first choice.
Select 2 to select second choice.
Select 0 to exit.
Enter your choice and press Return: 1
You have selected the first choice.

Select 1 to select first choice.


Select 2 to select second choice.
Select 0 to exit.
Enter your choice and press Return: 2
You have selected the second choice.
Select 1 to select first choice.
Select 2 to select second choice.
Select 0 to exit.
Enter your choice and press Return: 0
Good bye.
Please make sure that you start your menu with an empty line using
std::cout << std::endl;
(see above). Then end (almost all of) your std::cout statements with
std::cout << std::endl;
(see above);

Problem Set 9: Fuel Consumption


Drivers are concerned with the mileage obtained by their automobiles.
One driver has kept track of several tankfuls of gasoline by recording
kilometers driven and liters used for each tankful.
First ensure that the output always 2 digits after the . , by using fixed
and setprecision().
Develop a C program uses a sentinel controlled while loop, however in
the loop there are several cases where the user data is invalid and
should not be used. When a negative value is given for km it should
stop. Read in a value for the km driven from the user and preced it with
the prompt "Enter kilometers driven (-1 to quit): " .
1. Afterwards check km for the following conditions:
If a value of 0 is passed for km, the output "No distance travelled.\n"
should be given
If a negative value is passed for km the program should not calculate
anything and the loop should break
in all other cases the program should continue as described below
If the value entered for km > 0, then the user should be prompted
"Enter liters used: " and you should read in the value as a double. Then
check liters entered with the following conditions:
In case a negative value was entered, the negative value and the km
driven for this run should not be considered further! The output "No
negative liter.\n" should be given.
In the other cases the user entered valid data. The output "Fuel
consumption this tankful per 100 km: " should be given, followed by
the corresponding value and a single linebreak.
2. In case the loop was not aborted to this point the output "Average
fuel consumption (per 100 km): " should be given.
Then the average fuel consumption should be calculated, if no km > 0
has been entered to this point the value should be 0. The average fuel
consumption should then be printed and followed by a double
linebreak.

This is the console output from the screenshot as plain text:


Enter kilometers driven (-1 to quit): 100
Enter liters used: 10
Fuel consumption this tankful per 100 km: 10.00
Average fuel consumption (per 100 km): 10.00
Enter kilometers driven (-1 to quit): 200
Enter liters used: 19
Fuel consumption this tankful per 100 km: 9.50
Average fuel consumption (per 100 km): 9.67

Enter kilometers driven (-1 to quit): 0


No distance travelled.
Average fuel consumption (per 100 km): 9.67
Enter kilometers driven (-1 to quit): 10
Enter liters used: -19
No negative liter.
Average fuel consumption (per 100 km): 9.67
Enter kilometers driven (-1 to quit): -1

Please make sure that you write the string describing what input the
user should provide and the output statements exactly as shown in the
screen shots. In particular, make sure that these strings includes all
necessary white space, e.g., blanks. Due to how the tests are
performed, you will receive 0 points if there is a mismatch.
The following cout will be helpful:

cout<<"Fuel consumption this tankful per 100 km: ";


cout<<"Average fuel consumption (per 100 km): ";
cout<<"No distance travelled.";
cout<<"Enter kilometers driven (-1 to quit): ";

Problem Set 10
Write a function to calculate the rate of inflation for the past year. The
function receives the current price of some item and its associated
price one year ago as inputs. The item could be a kilogram of sugar or
flour, for example. The inflation rate is calculated as the price
difference divided by the previous year's price multiplied by 100. The
estimated inflation rate should be returned as a value of type double
giving the rate in percent, for example, 5.3, if the inflation was
estimated to be 5.3 percent. Here is an example: product price
(previous year in EUR): 1.35; product price (current year in EUR): 1.40;
inflation rate (in percent): (1.40 - 1.35)/1.35*100 = 3.7

The function prototype is


double estimateInflationRate(double new_price, double old_price);
Part 2

We now also want to predict what the new price would be in one year
from the time of calculation given a current price and the estimated
inflation rate. The increase in cost over one year follows from the
estimated inflation rate and the price at the start of the year. Here is an
example: estimated inflation rate (in percent): 3.7; price (current year
in EUR): 1.40; price (next year in EUR): 1.40 + 3.7/100*1.40 = 1.45.

The function prototype is


double estimateNewPriceOneYear(double price, double inflation_rate);
Next, we want to calculate the price after two years. You may want to
think about how to reuse the function above, but you do not have to
and can, for example, use the formula provided in the third part below.

The function prototype is


double estimateNewPriceTwoYears(double price, double
inflation_rate);
Part 3

Finally, given the current price, pp, and the inflation rate (in
percent), rr, we want to calculate the price, pnpn, after nn years. The
formula for that is
pn=(1+r/100)n∗ppn=(1+r/100)n∗p
The function prototype is
double estimateNewPrice(double price, double inflation_rate, double
years);
You can also use this formula for the problems above.
AssessmentPart1.cpp
#include "AssessmentPart1.h"
#include <cmath>

double estimateInflationRate(double new_price, double old_price)


{
//implement your solution for Programmieraufgabe 2 Teil 1 here
}

double estimateNewPriceOneYear(double price, double inflation_rate)


{
//implement your solution for Programmieraufgabe 2 Teil 2 here
}

double estimateNewPriceTwoYears(double price, double inflation_rate)


{
//implement your solution for Programmieraufgabe 2 Teil 2 here
}

double estimateNewPrice(double price, double inflation_rate, double


years)
{
//implement your solution for Programmieraufgabe 2 Teil 3 here
}
AssessmentPart1.h
double estimateInflationRate(double new_price, double old_price);
double estimateNewPriceOneYear(double price, double inflation_rate);
double estimateNewPriceTwoYears(double price, double
inflation_rate);
double estimateNewPrice(double price, double inflation_rate, double
years);

Problem Set 11:


Task 2:
a) Implement the function:
float distance( float p1_x, float p1_y, float p2_x, float p2_y )
Their task is to determine the distance between two 2-D
points p1=(p1_x,p1_y)p1=(p1_x,p1_y) and p2=(p2_x,p2_y)p2=(p2_x,p2
_y). The distance, d, between two 2-D points is calculated as follows

d=(p2_x−p1_x)2+(p2_y−p1_y)2−−−−−−−−−−−−−−−−−−−−−−−−−
−√d=(p2_x−p1_x)2+(p2_y−p1_y)2

b) Define the function:


bool inside_circle(float c_x, float c_y, float r, float p_x, float p_y)
This function checks if a point p=(p_x,p_y)p=(p_x,p_y) is inside a circle
described by its centre point c=(c_x,c_y)c=(c_x,c_y) and its radius r. We
assume here that a point is inside a circle if its distance from the center
is less than or equal to the radius r.

c) Implement the function:


bool circles_intersect(float c1_x, float c1_y, float r1, float c2_x, float
c2_y, float r2)
This function checks whether two circles intersect. The centers of the
two circles
are c1=(c1_x,c1_y)c1=(c1_x,c1_y) and c2=(c2_x,c2_y)c2=(c2_x,c2_y).
The associated radii are designated r1 and r2. If two circles touch each
other, we assume that they still intersect, but only in one point.
AssessmentPart2.cpp
//Add nessessary headers here
#include "AssessmentPart2.h"
#include <cmath>

//Task 2a)
float distance( float p1_x, float p1_y, float p2_x, float p2_y )
{
//add your code for task 1a) here
}
//Task 2b)
bool inside_circle(float c_x, float c_y, float r, float p_x, float p_y)
{
//add your code for task 1b) here
}
//Task 2c)
bool circles_intersect(float c1_x, float c1_y, float r1, float c2_x, float
c2_y, float r2)
{
//add your code for task 1c) here
}
AssessmentPart2.h
//a)
float distance( float p1_x, float p1_y, float p2_x, float p2_y );
//b)
bool inside_circle(float c_x, float c_y, float r, float p_x, float p_y);
//c)
bool circles_intersect(float c1_x, float c1_y, float r1, float c2_x, float
c2_y, float r2);

You might also like