Question

In: Computer Science

Use C language Write a program that reads in a series of lines of input character...

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.

Solutions

Expert Solution

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


Related Solutions

2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains an A for arrival or a D for departure, which is terminated by a :, and a license plate number, which is terminated by a :. The program should print a message each time a car arrives or departs. When a car arrives, the message should specify when the garage is full. If there is no room for a car, the car simply leaves....
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt and determines the following: The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following...
In C++, The following program reads one character from the keyboard and will display the character...
In C++, The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit. Modify the program below such that if one of the whitespaces is entered, it displays a message and tells what the character was. // This program reads one character from the keyboard and will //...
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT