Algorithms and Programming Lesson 1 Lab Questions

Algorithms and Programming Lesson
Çoşar Gözükırmızı (E1-13)
cosargozukirmizi@beykent.edu.tr
For this lesson we use this book.

Lab lesson 1 Questions and Answers

Question 1:
Write a short program that declares and initialize double variables one,two,three,four and five to the values 1.000,1.414, 1.732,2.000 ,2.236, respectively. Then write output statement 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 space away.Many editors and most word processors will have adjustable tab stops. Our outputs does not.

The output should be:

N    Square root
1      1.000
2      1.414
3      1.732
4      2.000
5      2.236

codes are here:
#include <iostream>
#include <iomanip>

using namespace std;
int main() {
double one = 1.000;
double two = 1.414;
double three = 1.732;
double four = 2.000;
double five = 2.236;
cout << "N\tSquare Root";
cout <<"\n";
cout << "1\t"<< fixed<<setprecision(3)<< one <<"\n";
cout << "2\t"<< two <<"\n";
cout << "3\t"<< three <<"\n";
    cout << "4\t"<< fixed<<setprecision(3)<< four <<"\n";
    cout << "5\t"<< five <<"\n";

return 0;
}

Important: What is the setprecision in c++?
Sets the decimal precision to be used to format floating-point values on output operations. Behaves as if member precision were called with n as argument on the stream on which it is inserted/extracted as a manipulator.



Comments

Popular Posts