Hello! I am totally stumped! I looked at the other response for these questions
ID: 3546848 • Letter: H
Question
Hello! I am totally stumped! I looked at the other response for these questions and it isn't the same.
1.
Calculate your daily pay for 31 days assuming you start work of a penny a day and double your salary each day. You will be in the millions on day 31.
It's supposed to look like this:
2.
Print a table of windchills. Have the wind speed go from 5 to 50 and the temp go from 5 to 50. You can use nested for loops.
3. I have most of the code for this one, but I can't figure out how to add the commas. It seems like everyone has a different answer.
#include "StdAfx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>
using namespace std;
double fact(int n)
{
if(n==1) return 1;
else return n*fact(n-1);
}
int main()
{
cout <<(" Table of Factorials ");
for(int f=1; f<=20; f++)
cout << " " << f << " " <<right << setw(30) <<setprecision(20) << fact(f) << endl;
system("PAUSE");
return 0;
}
The code above is very similar to this output, just without the commas.
Thank you so much to anyone willing to put forth the time to help me with this.
Calculate your daily pay for 31 days assuming you start work of a penny a day and double your salary each day. You will be in the millions on day 31. Print a table of windchills. Have the wind speed go from 5 to 50 and the temp go from 5 to 50. You can use nested for loops. I have most of the code for this one, but I can't figure out how to add the commas. It seems like everyone has a different answer.Explanation / Answer
Now i'm not sure if you're allowed to add the commas for the numbers as I did, if not you'll have to figure out some method to do so. Everything is printed as shown on here tho. Enjoy
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <string>
using namespace std;
double fact(int n){
if(n==1) return 1;
else return n*fact(n-1);
}
double windchill(int temp, int wind){
return 35.74 + 0.6215*temp - 35.75*(pow(wind, 0.16)) + 0.4275*temp*(pow(wind, 0.16));
}
int main()
{
int day, length, comma;
double pay = 0.01;
string str1;
ostringstream os;
//part 1
for( day = 1; day <= 31; day++){
//convert the double into a string
os << fixed << setprecision(2) << pay;
str1 = os.str();
//clears stream for next double
os.str("");
length = str1.length() - 3; //minus 3 to ignore doesn't count the decimals
//finding where the first comma is
comma = length%3;
//deal with the strings that aren't multiples of 3
if(comma != 0 && length > 3){
while(comma < length){
str1.insert(comma,",");
//set up for next spot
comma+= 4;
length++; //add 1 more since the comma increase the length
}
}
else if(comma == 0 && length > 3){
while(comma+3 < length){
str1.insert(comma+3, ",");
comma+= 4;
length++;
}
}
cout << setw(7) << right << day << setw(23) << str1 << endl;
pay *= 2;
}
//Part 2
//display table
string label = "WIND SPEED"; //used to print the side table label
cout << setw(47) << right << "TEMPERATURE" << endl << setw(7) << " ";
//display the top row of integers
for(int i = 5; i <= 50; i+=5){
cout << setw(6) << right << i;
}
cout << endl << endl;
for(int wind = 5; wind <= 50; wind+=5){
cout << label[wind/5 - 1];
for(int temp = 0; temp <= 50; temp+=5){
if(temp == 0){
cout << setw(6) << right << wind;
continue;
}
cout << setw(6) << right << fixed << setprecision(0) << windchill(temp,wind);
}
cout << endl;
}
//Part 3
cout <<(" Table of Factorials ");
for(int f=1; f<=20; f++){
//convert the double into a string
os << setprecision(0) << fact(f);
str1 = os.str();
//clears stream for next double
os.str("");
length = str1.length();
//finding where the first comma is
comma = length%3;
//deal with the strings that aren't multiplies of 3
if(comma != 0 && length > 3){
while(comma < length){
str1.insert(comma,",");
//set up for next spot
comma+= 4;
length++;
}
}
else if(comma == 0 && length > 3){
while(comma+3 < length){
str1.insert(comma+3, ",");
comma+= 4;
length++;
}
}
cout << setw(3) << right << f << setw(28) << right << str1 << endl;
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.