Вы находитесь на странице: 1из 4

# Ders 5.

Problemler
5.8 . Show the value of x after each of the following statements is performed:
a) x = fabs( 7.5 );
ANS: 7.5

b) x = floor( 7.5 );
ANS: 7.0

c) x = fabs( 0.0 );
ANS: 0.0

d) x = ceil( 0.0 );
ANS: 0.0

e) x = fabs( -6.4 );
ANS: 6.4

f) x = ceil( -6.4 );
ANS: -6.0

## g) x = ceil( -fabs( -8 + floor( -5.5 ) ) );

ANS: -14.0

5.9 (Parking Charges) A parking garage charges a \$2.00 minimum fee to park for up to three
hours and an additional \$0.50 per hour for each hour or part thereof over three hours. The maximum
charge for any given 24-hour period is \$10.00. Assume that no car parks for longer than 24 hours
at a time. Write a program that will calculate and print the parking charges for each of three customers
who parked their cars in this garage yesterday. You should enter the hours parked for each
customer. Your program should print the results in a neat tabular format, and should calculate and
print the total of yesterday's receipts. The program should use the function calculateCharges to
determine the charge for each customer. Your outputs should appear in the following format:
ANS:
Car
Hours
1
4.0
3
24.0
TOTAL 29.5

Charge
2.50
10.00
14.50

ANS:
/* Exercise 5.9 Solution */
#include <stdio.h>
#include <math.h>
double calculateCharges( double hours ); /* function prototype */
int main()
{
double h; /* number of hours for current car */
double currentCharge; /* parking charge for current car */
double totalCharges = 0.0; /* total charges */
double totalHours = 0.0; /* total number of hours */
int i; /* loop counter */
int first = 1; /* flag for printing table headers */
printf( "Enter the hours parked for 3 cars: " );
/* loop 3 times for 3 cars */
for ( i = 1; i <= 3; i++ ) {
scanf( "%lf", &h );
totalHours += h; /* add current hours to total hours */
/* if first time through loop, display headers */
if ( first ) {
printf( "%5s%15s%15s\n", "Car", "Hours", "Charge" );
/* set flag to false to prevent from printing again */
first = 0;
} /* end if */
/* calculate current car's charge and update total */
totalCharges += ( currentCharge = calculateCharges( h ) );
/* display row data for current car */
printf( "%5d%15.1f%15.2f\n", i, h, currentCharge );
} /* end for */
/* display row data for totals */
printf( "%5s%15.1f%15.2f\n", "TOTAL", totalHours, totalCharges );
return 0; /* indicate successful termination */
} /* end main */
/* calculateCharges returns charge according to number of hours */
double calculateCharges( double hours )
{
double charge; /* calculated charge */

/* \$2 for up to 3 hours */
if ( hours < 3.0 ) {
charge = 2.0;
} /* end if */
/* \$.50 for each hour or part thereof in excess of 3 hours */
else if ( hours < 19.0 ) {
charge = 2.0 + .5 * ceil( hours - 3.0 );
} /* end else if */
else { /* maximum charge \$10 */
charge = 10.0;
} /* end else */
return charge; /* return calculated charge */
} /* end function calculateCharges */

5.11 (Rounding Numbers) Function floor may be used to round a number to a specific decimal
place. The statement
y = floor( x * 10 + .5 ) / 10;

rounds x to the tenths position (the first position to the right of the decimal point). The statement
y = floor( x * 100 + .5 ) / 100;
rounds x to the hundredths

position (the second position to the right of the decimal point). Write
a program that defines four functions to round a number x in various ways
a) roundToInteger( number )
b) roundToTenths( number )
c) roundToHundreths( number )
d) roundToThousandths( number )
For each value read, your program should print the original value, the number rounded to the
nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hundredth,
and the number rounded to the nearest thousandth.
ANS:
/* Exercise 5.11 Solution */
#include <stdio.h>
#include <math.h>
double roundToInteger( double n ); /* function prototype */
double roundToTenths( double n ); /* function prototype */
double roundToHundredths( double n ); /* function prototype */
double roundToThousandths( double n ); /* function prototype */
int main()
{
int i; /* loop counter */
int count; /* number of values to process */
double number; /* current input */
printf( "How many numbers do you want to process? " );
scanf( "%d", &count );
/* loop for inputs */
for ( i = 0; i < count; i++ ) {
printf( "Enter number: " );
scanf( "%lf", &number );
/* display number rounded to nearest integer */
printf( "%f rounded to an integer is %f\n",
number, roundToInteger( number ) );
/* display number rounded to nearest tenth */
printf( "%f rounded to the nearest tenth is %f\n",
number, roundToTenths( number ) );
/* display number rounded to nearest hundredth */
printf( "%f rounded to the nearest hundredth is %f\n",
number, roundToHundredths( number ) );
/* display number rounded to nearest thousandth */
printf( "%f rounded to the nearest thousandth is %f\n\n",
number, roundToThousandths( number ) );
} /* end for */
return 0; /* indicate successful termination */
} /* end main */
/* roundToInteger rounds n to nearest integer */
double roundToInteger( double n )
{
return floor( n + .5 );
} /* end function roundToInteger */
/* roundToTenths rounds n to nearest tenth */
double roundToTenths( double n )
{
return floor( n * 10 + .5 ) / 10;
} /* end function roundToTenths */

## /* roundToHundredths rounds n to nearest hundredth */

double roundToHundredths( double n )
{
return floor( n * 100 + .5 ) / 100;
} /* end function roundToHundredths */
/* roundToThousandths rounds n to nearest thousandth */
double roundToThousandths( double n )
{
return floor( n * 1000 + .5 ) / 1000;
} /* end function roundToThousandths */

5.13 Write statements that assign random integers to the variable n in the following ranges:
a) 1 n 2
ANS: n = 1 + rand() % 2;

b) 1 n 100
ANS: n = 1 + rand() % 100;

c) 0 n 9
ANS: n = rand() % 10;

d) 1000 n 1112
ANS: n = 1000 + rand() % 113;

e) 1 n 1
ANS: n = -1 + rand() % 3;

f) 3 n 11
ANS: n = -3 + rand() % 15;

5.14 For each of the following sets of integers, write a single statement that will print a number
at random from the set.
a) 2, 4, 6, 8, 10.
ANS: printf( %d\n, 2 * ( 1 + rand() % 5 ) );

b) 3, 5, 7, 9, 11.
ANS: printf( %d\n, 1 + 2 * ( 1 + rand() % 5 ) );

## c) 6, 10, 14, 18, 22.

ANS: printf( %d\n, 6 + 4 * ( rand() % 5 ) );

5.15 (Hypotenuse Calculations) Define a function called hypotenuse that calculates the length
of the hypotenuse of a right triangle when the other two sides are given. Use this function in a program
to determine the length of the hypotenuse for each of the following triangles. The function
should take two arguments of type double and return the hypotenuse as a double. Test your program
with the side values (3.0 and 4.0 ; 5.0 and 12.0; 8.0 and 15.0)
ANS:

## gen Side 1 Side 2

/* Exercise 5.15 Solution */
#include <stdio.h>
#include <math.h>
double hypotenuse( double s1, double s2 ); /* function prototype */
int main()
{
int i; /* loop counter */
double side1; /* value for first side */
double side2; /* value for second side */
/* loop 3 times */
for ( i = 1; i <= 3; i++ ) {
printf( "Enter the sides of the triangle: " );
scanf( "%lf%lf", &side1, &side2 );
/* calculate and display hypotenuse value */
printf( "Hypotenuse: %.1f\n\n", hypotenuse( side1, side2 ) );
} /* end for */
return 0; /* indicate successful termination */
} /* end main */
/* hypotenuse calculates value of hypotenuse of
a right triangle given two side values */
double hypotenuse( double s1, double s2 )
{
return sqrt( pow( s1, 2 ) + pow( s2, 2 ) );
} /* end function hypotenuse */
Enter the sides of the triangle: 3.0 4.0
Hypotenuse: 5.0
Enter the sides of the triangle: 5.0 12.0
Hypotenuse: 13.0
Enter the sides of the triangle: 8.0 15.0
Hypotenuse: 17.0

5.16 (Exponentiation) Write a function integerPower(base, exponent) that returns the value of
baseexponent

## For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero

integer, and base is an integer. Function integerPower should use for to control the calculation.
Do not use any math library functions.
ANS:
/* Exercise 5.16 Solution */
#include <stdio.h>
int integerPower( int b, int e );
int main()
{
int exp; /* integer exponent */
int base; /* integer base */
printf( "Enter integer base and exponent: " );
scanf( "%d%d", &base, &exp );
printf( "%d to the power %d is: %d\n",
base, exp, integerPower( base, exp ) );
return 0; /* indicate successful termination */
} /* end main */
/* integerPower calculates and returns b raised to the e power */
int integerPower( int b, int e )
{
int product = 1; /* resulting product */
int i; /* loop counter */
/* multiply product times b (e repetitions) */
for ( i = 1; i <= e; i++ ) {
product *= b;
} /* end for */
return product; /* return resulting product */
} /* end function integerPower */

5.17 (Multiples) Write a function multiple that determines for a pair of integers whether the second
integer is a multiple of the first. The function should take two integer arguments and return 1
(true) if the second is a multiple of the first, and 0 (false) otherwise. Use this function in a program
that inputs a series of pairs of integers
5.18 (Even or Odd) Write a program that inputs a series of integers and passes them one at a time
to function even, which uses the remainder operator to determine if an integer is even. The function
should take an integer argument and return 1 if the integer is even and 0 otherwise.