In: Computer Science
Use C language
Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point value, with up to three digits before the decimal point, and up to three digits following the decimal point (but there is not necessarily a decimal point in each number; i.e., it may appear to be an integer, but the digits should be read by your program, and the number should be converted to a corresponding floating point number). For instance, suppose the following input:
5
3.125
20.25
0.875
921.50
31
The required output is:
Each of the input floating point values, printed on a separate line with three digits of precision, using printf();
On the last line of the output, the string “Total:” followed by the sum of the input values, printed with printf() to 3 digits of precision. For example, the total of the sample input given above is 976.750, so the required output for this input would be:
3.125
20.250
0.875
921.500
31.000
Total: 976.750
Do not concern yourself with small differences in the total due to rounding, as the grader will not deduct points for this.
Be sure to include a descriptive message for the user to input the values correctly. You can assume that the user will follow your directions (which should be consistent with the description above), so no exception handling for incorrect input is required. You cannot change the input specifications given above, however.
CONSTRAINTS:
You are not allowed to use arrays on this portion of the lab assignment.
You must use getchar() to read in the floating point values one character at a time (i.e. DO NOT USE scanf()).
-Only use printf() to output the floating point numbers and the total (Do not useputchar()).
Be sure your directions to the user are clear so they are sure to enter the input data correctly.
Complete Program:
Sample Output:
CODE TO COPY:
// Header files section
#include <stdio.h>
#include <stdlib.h>
// start main function
int main()
{
// declare the required variables
int size;
int i;
double num;
double decimal;
double total = 0;
char ch;
// read the number of remaining lines of
input
scanf("%d\n", &size);
// read each number
for (i = 0; i < size; i++)
{
num = 0;
decimal = 1.0;
// read each
character and make a number
while ((ch = getchar())
!= '\n')
{
if (ch == '.')
{
decimal = decimal / 10;
}
else if(ch >= '0' && ch <= '9')
{
if (decimal == 1.0)
{
num = (num * 10.0) + (ch - '0');
}
else
{
num = num + ((ch - '0') * decimal);
decimal = decimal / 10;
}
}
}
// print the
number
printf("%.3f\n",
num);
// add the number to
total
total = total +
num;
}
// print the sum of numbers
printf("Total: %.3f\n", total);
return 0;
} // end of main function