SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
C++ Basics
Exercise:
Self-test Exercises
Question:14 | ISBN:9780321531346 | Edition: 7

Question

Write a short program that declares and initializes double variables one,two, three, four, and five to the values 1.000, 1.414, 1.732, 2.000, and 2.236, respectively. Then write output statements to generate the following legend and table. Use the tab escape sequence \t to line up the columns. If you are unfamiliar with the tab character, you should experiment with it while doing this exercise. A tab works like a mechanical stop on a typewriter.

A tab causes output to begin in a next column, usually a multiple of eight spaces away. Many editors and most word processors will have adjustable tab stops. Our output does not.

The output should be:

N Square Root

1 1.000

2 1.414

3 1.732

4 2.000

5 2.236

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program Code:

#include <iostream>
using namespace std;
int main ()
{
    // Declares and initializes double variables one,two, three, four,
    // and five to the values 1.000, 1.414, 1.732, 2.000, and 2.236
    double one(1.000);
    double two(1.414);
    double three(1.732);
    double four(2.000);
    double five(2.236);
    //Display  output statements to generate the following 
    //legend and table by using a tab causes '\t' used for 
    //output to begin in a next column, usually a multiple 
    //of eight spaces away.
    cout << "\tN\tSquare Root\n";
    cout << "\t1\t" << one << endl
        <<"\t2\t"<<two<<endl
        <<"\t3\t"<<three<<endl
        <<"\t4\t"<<four<<endl
        <<"\t5\t"<<five<<endl;
    return 0;
}

Output of the program code:

       N       Square Root                                                                                                            
        1       1                                                                                                                      
        2       1.414                                                                                                                  
        3       1.732                                                                                                                  
        4       2.000                                                                                                                      
        5       2.236  

 

1 0

Discussions

Post the discussion to improve the above solution.