Intro C++ : Write the code ===> Problem 1: Given an integer variable i and a flo
ID: 3662745 • Letter: I
Question
Intro C++ : Write the code
===> Problem 1:
Given an integer variable i and a floating-point variable f, write a statement that writes both of their values to standard output in the following format: i=value -of-i f=value -of-f
Thus, if i has the value 25 and f has the value 12.34, the output would be:
i=25 f=12.34
But if i has the value 187 and f has the value 24.06, the output would be:
i=187 f=24.06
====> Problem 2:
Assume that x is a double variable that has been initialized . Write a statement that prints it out, guaranteed to have a decimal point, but without forcing scientific (also known as exponential or e-notation).
=====> Problem 3:
Assume that x is a double variable that has been given a value . Write a statement that prints it out with exactly three digits to the right of the decimal point no matter what how big or miniscule its value is.
====> Problem 4:
Declare k, d, and s so that they can store an integer , a real number, and a small word (under 10 characters ).
Use these variables to first read in an integer , a real number, and a small word and print them out in reverse order (i.e., the word, the real, and then the integer ) all on the same line, separated by EXACTLY one space from each other.
Then, on a second line, print them out in the original order (the integer , the real, and the word), separated again by EXACTLY one space from each other.
Explanation / Answer
//Problem1-solution
#include<iostream>
using namespace std;
int main()
{
//declare i as integer
int i;
//declare f as float
float f;
cout<<"Enter i-value : ";
//read integer i value
cin>>i;
cout<<"Enter f-value : ";
//read float f value
cin>>f;
//print i and f values
cout<<"i="<<i<<" f="<<f<<endl;
//pause the program output on console
system("pause");
return 0;
}
Output:
Enter i-value : 25
Enter f-value : 12.34
i=25 f=12.34
----------------------------------------------------------------------------
//Problem2-solution
//C++ program that prints the decimal values
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//declare x asdouble
double x;
cout<<"Enter x-value : ";
//read integer i value
cin>>x;
//print x value
cout<<fixed<<setprecision(2)
<<"x="<<x<<endl;
//pause the program output on console
system("pause");
return 0;
}
Output:
Enter x-value : 50
x=50.00
-----------------------------------------------------------------------------------------
//Problem3 solution:
//C++ program that prints the decimal values
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//declare x asdouble
double x=50;
//print x value upto three decimal places
ios::right;
//set width =6 and set precision 3 digits
cout<<setw(6)<<fixed<<setprecision(3)<<
"x="<<x<<endl;
//pause the program output on console
system("pause");
return 0;
}
Output:
x=50.000
--------------------------------------------------------------------
//Program 4 solution
#include<iostream>
using namespace std;
int main()
{
int k;//integer variable
double d;//real number
char s[10];//char array of size=10
cout<<"Enter k-value: ";
cin>>k;
cout<<"Enter d-value: ";
cin>>d;
cout<<"Enter s-word: ";
cin>>s;
cout<<"Reverse order"<<endl;
cout<<s<<" "<<d<<" "<<k<<endl;
cout<<"Original order"<<endl;
cout<<k<<" "<<d<<" "<<s<<endl;
//pause the program output on console
system("pause");
return 0;
}
Output:
Enter k-value: 5
Enter d-value: 10.5
Enter s-word: hello
Reverse order
hello 10.5 5
Original order
5 10.5 hello
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.