In: Computer Science
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems.
• stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you do not need nor should you use an array to solve this problem.
Enter integers: 2 4 1 3 Sum: 10.0
Average: 2.5 Sum of Squares: 30.0 Variance: 1.25
minmax.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the largest and smallest values. Remember, you do not need nor should you use an array to solve this problem.
Enter integers: 1 9 3 7 5 6 4 8 2 10 Min: 1
Max: 10
numbers.cpp: Write a program that prints out the binary, octal, decimal, and hexadecimal representation (respectively) of all unsigned shorts (in order). If the number is divisible by 3, then also print “Go”. If the number is divisible by 5, then also print “GOGOGO”. Otherwise, “S” should also be printed. Below is a sample of some of the entries near the middle:
0b1010'0000'0000'0000 0120000 40960 0xa000 GOGOGO 0b1010'0000'0000'0001 0120001 40961 0xa001 S 0b1010'0000'0000'0010 0120002 40962 0xa002 Go 0b1010'0000'0000'0011 0120003 40963 0xa003 S 0b1010'0000'0000'0100 0120004 40964 0xa004 S 0b1010'0000'0000'0101 0120005 40965 0xa005 GOGOGOGO
degrees.cpp: Write a program to read a floating point number representing degrees Celsius, and print the equivalent temperature in degrees Fahrenheit. Print your results in a form such that the numbers are represented up to two decimal places.
Enter in a temperature in Celsius: 100 100.00 degrees Celsius converts to 212.00 degrees Fahrenheit.
time.cpp: Given as input an integer number of seconds, print as output the equivalent time in hours, minutes and seconds.
Enter in the number of seconds as an integer: 7322 7322 seconds is equivalent to 2 hours 2 minutes 2 seconds.
Screenshots of the code:
1)
Sample output:
Code to copy:
//stat.cpp
#include"stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int n, i, num;
double squareSum = 0, sum = 0, avg, variance;
cout << "Enter the number you want to enter: ";
cin >> n;
cout << "Enter integers: ";
for (i = 0; i<n; i++)
{
cin >> num;
sum = sum + num;
squareSum = squareSum + (num*num);
}//end of main function
avg = sum / n;
variance = (squareSum) / n - avg*avg;
cout << "Sum: " << sum << endl;
cout << "Average: " << avg << endl;
cout << "Sum of squares: " << squareSum << endl;
cout << "Variance: " << variance << endl;
system("pause");
return 0;
}//end of main function
2)
Sample Output:
Code to copy:
//minmax.cpp
#include"stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int n, minimum = 99999, maximum = -1, i, num;
cout << "Enter the number you want to enter: ";
cin >> n;
cout << "Enter integers: ";
for (i = 0; i<n; i++)
{
cin >> num;
if (num<minimum)
minimum = num;
if (num>maximum)
maximum = num;
}
cout << "Minimum: " << minimum << endl;
cout << "Maximum: " << maximum << endl;
system("pause");
return 0;
}//end of main function
3)
Sample Output:
Code to copy:
//numbers.cpp
#include"stdafx.h"
#include<bitset>
#include<iostream>
using namespace std;
int main()
{
unsigned int number;
for (number = 0; number <= 65535; number++)
{
cout << "0b" << bitset<16>(number) << "\t";
cout << std::oct << number << "\t";
cout << std::dec << number << "\t";
cout << std::hex << std::showbase << number << "\t";
if (number % 3 != 0 && number % 5 != 0)
{
cout << "S";
}
else
{
if (number % 5 == 0)
cout << "GOGOGO";
if (number % 3 == 0)
cout << "GO";
}
cout << endl;
}
system("pause");
return 0;
}
4)
Sample Output:
Code to copy:
//degrees.cpp
#include"stdafx.h"
// degree to fahrenheit
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float cel, fahren;
cout << "Enter the temperature in celcius: ";
cin >> cel;
fahren = (9.0 / 5)*cel + 32;
cout << fixed << setprecision(2) << cel << " degrees Celcius converts to " << fixed << setprecision(2) << fahren << " degrees Fahrenheit";
system("pause");
return 0;
}//end of main function
5)
Sample Output:
Code to copy:
5)
//time.cpp
#include"stdafx.h"
// seconds into hours
#include<iostream>
using namespace std;
int main()
{
int sec, hr, minute, d;
cout << "Enter the number of seconds: ";
cin >> sec;
d = sec;
hr = sec / 3600;
sec = sec % 3600;
minute = sec / 60;
sec = sec % 60;
cout << d << " seconds is equivalent to " << hr << " hours " << minute << " minutes " << sec << " seconds.";
system("pause");
return 0;
}//end of main function