Grade received: 95%
Problem C1
This program converts temperature in degrees Celsius to degrees Fahrenheit.
*/
#include<iostream>
#include<iomanip>
using namespace std;
int main (void)
{
double celsius, fahrenheit;
cout << "Enter a temperature in Celsius: ";
cin >> celsius;
fahrenheit = celsius * 9/5 + 32; I lost points for not making this a 'const' declaration
cout << setprecision(1) << fixed;
cout << "Celsius: " << setw(8) << celsius << endl;
cout << "Fahrenheit: " << fahrenheit << endl;
return 0;
}
/* Execution results:
Enter a temperature in Celsius: 37.778
Celsius: 37.8
Fahrenheit: 100.0
Process returned 0 (0x0) execution time : 5.456 s
Press any key to continue.
Enter a temperature in Celsius: -40
Celsius: -40.0
Fahrenheit: -40.0
Process returned 0 (0x0) execution time : 5.069 s
Press any key to continue.
Enter a temperature in Celsius: 0
Celsius: 0.0
Fahrenheit: 32.0
Process returned 0 (0x0) execution time : 1.332 s
Press any key to continue.
*/
Problem C1
This program converts temperature in degrees Celsius to degrees Fahrenheit.
*/
#include<iostream>
#include<iomanip>
using namespace std;
int main (void)
{
double celsius, fahrenheit;
cout << "Enter a temperature in Celsius: ";
cin >> celsius;
fahrenheit = celsius * 9/5 + 32; I lost points for not making this a 'const' declaration
cout << setprecision(1) << fixed;
cout << "Celsius: " << setw(8) << celsius << endl;
cout << "Fahrenheit: " << fahrenheit << endl;
return 0;
}
/* Execution results:
Enter a temperature in Celsius: 37.778
Celsius: 37.8
Fahrenheit: 100.0
Process returned 0 (0x0) execution time : 5.456 s
Press any key to continue.
Enter a temperature in Celsius: -40
Celsius: -40.0
Fahrenheit: -40.0
Process returned 0 (0x0) execution time : 5.069 s
Press any key to continue.
Enter a temperature in Celsius: 0
Celsius: 0.0
Fahrenheit: 32.0
Process returned 0 (0x0) execution time : 1.332 s
Press any key to continue.
*/
Problem C2
This program demonstrates using the getline function to read character data into a string object.
*/
#include<iostream>
#include<string>
using namespace std;
int main (void)
{
string name;
cout << "Please enter your name: ";
getline(cin, name);
return 0;
}
/* Execution results:
Please enter your name: George Washington
Process returned 0 (0x0) execution time : 51.463 s
Press any key to continue.
Please enter your name: Franklin Delano Roosevelt
Process returned 0 (0x0) execution time : 12.250 s
Press any key to continue.
*/
Problem C3
This program prints your initials after inputting your first and last name.
*/
#include<iostream>
#include<string>
using namespace std;
int main (void)
{
char firstInitial, lastInitial;
string firstname, lastname;
cout << "Please enter your first and last name: ";
getline(cin, firstname, ' ');
firstInitial = firstname[0];
getline(cin, lastname, '\n');
lastInitial = lastname[0];
cout << firstInitial << lastInitial << endl;
return 0;
}
/* Execution results:
Please enter your first and last name: Harry Truman
HT
Process returned 0 (0x0) execution time : 3.474 s
Press any key to continue.
*/
Comments
Post a Comment