C Learning
How can you determine how fast your own computer really operates? Write a program with a while loop that counts from 1 to 1,000,000,000 by 1. Every time the count reaches a multiple of 200,000,000, print that number on the screen. Use the function clock() to time how long 1 billion repetitions of the loop take. Write a program that reads three nonzero real numbers and determines and if they can be used to as the dimensions of a right-angled triangle. The program could interact as follows: Enter three integers: 3 4 5 The three integers are the sides of a right triangle Enter three integers: 9 4 1 The three integers are not the sides of a right triangle The factorial of a nonnegative integer n is written as n! n! can be defined as follows: n! = n(n − 1)(n − 2) · · · 1 for values of n greater than or equal to 1 and n! = 1 for n = 0. a) Write a program that reads a nonnegative integer, and computes and prints the factorial of the nonnegative integer. b) Write a program that estimates the value of the mathematical constant e by using the formula: e = 1 + (1/1!) + (1/2!) + (1/3!) + ... The program should display a result for the user. [Hints: If e represents a variable of data type double, printf(“%f ”, e) can be used to print the value of e. What does the following programming segment do? Redevelop the following using the ‘while’ – loop.void main()
{
for (int i = 1; i <= 5; ++i)
{
for (int j = 1; j <= 3; ++j)
{
for (int k = 1; k <= 4; ++k)
{
printf("*");
}
printf("\n");
}
printf("\n");
}
}
(refer to lab script for the pattern)
void main()
{
for (int i = 1; i <= 5; ++i)
{
for (int j = 1; j <= 3; ++j)
{
for (int k = 1; k <= 4; ++k)
{
printf("*");
}
printf("\n");
}
printf("\n");
}
}
(refer to lab script for the pattern)
This is an example of the checksum:
Data : 25 11 12 7 13 4 8, where the last integer, 8, performs the checksum.
Sum of the data: 72 = (25+11+12+7+13+4)
The remainder when dividing the sum by 16 is 8 which is equal to the checksum. Hence this
data is correct.
This is another example of the checksum:
Data : 24 11 12 7 13 4 8, where the last integer, 8, performs the checksum.
Sum of the data: 71 = (24+11+12+7+13+4)
The remainder when dividing the sum by 16 is 7 which is not equal to the checksum. Hence
this data is not correct.
The program provides a generic example of the mechanics of defining and
using an array of pointers to functions. We define three functions - function1, function2 and
function3 - that each take an integer argument and return nothing. We store pointers to these
three functions in array f, which is defined in line 12. The definition is read beginning in the
leftmost set of parentheses, “f is an array of 3 pointers to functions that each takes an int as
an argument and return void.” The array is initialized with the names of the three functions.
When the user enters a value between 0 and 2, the value is used as the subscript of the
array of function pointers. In the function (line 24), f[choice] selects the pointer at location
choice in the array. The pointer is dereferenced to call the function, and choice is passed as
the argument to the function. Each function prints its argument's value and its function name to
demonstrate that the function is called correctly.
Enter number of applicants (0 . . 50)
> 5
Enter names of applicants on separate lines:
In the order in which they applied
SADDLER, MARGARET
INGRAM, RICHARD,
FAATZ, SUSAN
GONZALES, LORI
KEITH, CHARLES
Alphabetical Order:
FAATZ, SUSAN
GONZALES, LORI
INGRAM, RICHARD
KEITH, CHARLES
SADDLER, MARGARET
In the program, we need to create a structure type namely battery_t, to represent the battery variables
- Battery voltage
- Amount of energy that the battery is capable of storing
- Amount of energy currently storing (in joules) in the battery. In the program, we need to define three functions namely power_device, max_time, and recharge.
power_device attempts to determine the following:
- power_device determines whether the battery’s energy reserve is adequate to power the device for the prescribed time.
- If the energy reserve is adequate, power_device updates the battery’s energy reserve by subtracting the energy consumed and informs the users by returning a true message.
- If the energy reserve is adequate, power_device leaves the energy reserve unchanged and informs the users by returning a false message.
max_time uses the battery variables represented by battery_t and the current of an electrical device as the function input. It computes the number of seconds that the battery can operate the device before it is fully discharged. max_time does not attempt to change any battery variables.
recharge attempts to reset the amount of energy currently stored in the battery to be the amount of energy that the battery is capable of storing.
To develop this program, we may use the following equations:
p = v * i w = p * t p = power (W) v = voltage (V) i = current in amps (A) w = energy (J) t = time (s)
After developed the program, we can perform the simulation. We neglect any loss of energy in the transfer from battery to device. We consider the following parameters in the battery and the electrical device.
For the battery, we use:
- a 12-V automobile battery
- the maximum energy storage of 5106 J in the battery
- Amount of energy currently storing in the battery is 4106 J
The electrical device is a lighting system which needs to be operated with the following requirements:
- operation current is 4 A
- operation time is 15 minutes
After connected with this light system for 15 minutes, we connect the battery to an 8-A device. Compute how long the battery can power this 8-A device. After recharging the battery, recalculate how long it can operate on this 8-A device.