I have quite straight forward question. The following code prints out celsius and fahrenheit.
My question is though about number of times it iterate. For a small number e.g. start 0, stop at 10, with a step of 1.1. After the loop is finished it will print out the correct number of iterations it made.
But for large number 0-11000000, with step 1.1 it will print out wrong number of iteration. Why is that happening? Since 1100000/1.1 should be around 1000001, but I get 990293.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float start, stop, step;
int count = 0;
cout << "start temperature: ";
cin >> start;
cout << "stop temperature: ";
cin >> stop;
cout << "step temperature: ";
cin >> step;
cout << setw(10) << "celsius" << setw(15) << "fahrenheit" << endl;
cout << setw(25) << "celsius" << setw(15) << "fahrenheit" << endl;
while(start <= stop)
{
count++;
float c, f;
c = (5.0/9)*(start-32);
f = 32+(9.0/5)*start;
cout << setw(10) << fixed << setprecision(2) << c << setw(15) << start << setw(15) << fixed << setprecision(2) << f << " count: " << count << endl;
start = start + step;
}
cout << "The program loop made " << count << " iterations." << endl;
return 0;
}
Floating-point rounding error. Essentially, floats are not a 100% accurate representation, there are errors in every calculation you make, and as you repeatedly add to them, you will be adding more and more error. What you should do is compute the number of steps once, store it in an integer, and then loop that many times.
For the record, a cleaned-up version would look like:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double start, stop, step;
cout << "start temperature: ";
cin >> start;
cout << "stop temperature: ";
cin >> stop;
cout << "step temperature: ";
cin >> step;
cout << setw(10) << "celsius" << setw(15) << "fahrenheit" << endl;
unsigned steps = (stop - start) / step;
for(unsigned i = 0; i < steps; ++i)
{
double temp = start + i * step;
double c = (5.0 / 9.0) * (temp - 32.0);
double f = 32.0 + (9.0 / 5.0) * temp;
// This is a real good example of why people hate <iostream> formatting.
// If you want formatting that's quite different from the default, it
// gets too verbose too fast. Using C stdio:
//printf("%10.2f%15.2f\n", c, f);
cout << setw(10) << fixed << setprecision(2) << c
<< setw(15) << fixed << setprecision(2) << f << endl;
}
cout << "The program loop made " << steps << " iterations." << endl;
return 0;
}
The main benefit of this style of loop is that each iteration is (except for the output) order-independent, so it could be unrolled and (theoretically) parallelized.
Related
I was reading the chapter on structures in my book, and it got me re-modifying a program I already made, but this time using structures which I have never used before; however, after finishing the program, there's one issue I'm not understanding. The output of the program only displays once. It's in a for loop, and yet even though it asks me to input my information three times, it only outputs the first information.
I'm probably just not understanding how arrays in structures work.
An example of my issue is the following.
I have my output on the following loop
for(int counter = 0; counter <size; counter++)
The size is 3, which would mean I'll get the output printed three times; however the answer I'm getting is the same as if I was asking for the following.
Listofnames[0].F_name
When what I actually want is
Listofnames[0].F_name Listofnames[1].F_name Listofnames[2].F_name
However, I don't want to have to write it three times, I did to test it and it actually worked, but is that the only way to do it? Or did I miss something in my program?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Names
{
string F_name; //Creating structure called Names.
string L_name;
char Mi;
};
struct Payrate
{
double rate;
double hoursworked; //Creating structure called Payrate.
double gross;
double net;
};
int main()
{
double stateTax = 0, federalTax = 0, unionFees = 0, timeHalf = 1.5; //Initializing variables.
const int size = 2; //Array size.
Payrate employee[size]; //Structure variables
Names Listofnames[size];
for (int counter = 0; counter < size; counter++) //Initializing for loop.
{
cout << "What's your first name?: " << endl;
cin >> Listofnames[counter].F_name;
cout << "What's your last name?: " << endl; //Displaying names, and hours worked, rate.
cin >> Listofnames[counter].L_name;
cout << "What is your middle initial?: " << endl;
cin >> Listofnames[counter].Mi;
cout << "How many hours did you work? Please enter a number between 1-50: " << endl;
cin >> employee[counter].hoursworked;
cout << "What is your hourly rate? Please enter a number between 1-50: " << endl;
cin >> employee[counter].rate;
if (employee[counter].hoursworked < 0 || employee[counter].hoursworked >50) //Initializing conditional statements.
{
cout << "Sorry you entered a erong entry. Pc shutting off " << endl; //Displays what happens is user inputs a number under 0 or over 50.
}
if (employee[counter].rate < 0 || employee[counter].rate > 50) //Initializing conditional statements.
{
cout << "Sorry you entered a erong entry. Pc shutting off " << endl; //Displays what happens is user inputs a number under 0 or over 50.
}
if (employee[counter].hoursworked <= 40) //Initializing conditional statements.
{
employee[counter].gross = employee[counter].hoursworked * employee[counter].rate; //Calculating gross.
}
else if (employee[counter].hoursworked > 40) //Initializing conditional statements.
{
employee[counter].gross = employee[counter].hoursworked * (employee[counter].rate * timeHalf); //Calculating gross.
}
stateTax = employee[counter].gross * 0.06;
federalTax = employee[counter].gross * 0.12; //Calculates all the tax fees, and net.
unionFees = employee[counter].gross * 0.02;
employee[counter].net = employee[counter].gross - (stateTax + federalTax + unionFees);
}
cout << "FirstN " << "MI " << "LastName " << "\t" << "Rate " << "HoursWorked " << "TimeHalf " << "StateTax " << "FederalTax " << "UnionFees " << "Gross " << " " << "Net " << endl; //Displays header of output.
cout << "==================================================================================================================" << endl;
for (int counter = 0; counter <= size; counter++)
{
//Output.
cout << Listofnames[counter].F_name << "\t" << fixed << setprecision(2) << Listofnames[counter].Mi << " " << Listofnames[counter].L_name << "\t" << employee[counter].rate << "\t" << employee[counter].hoursworked << "\t" << setw(7) << timeHalf << "\t" << setw(8) << stateTax << setw(12) << federalTax << "\t" << unionFees << "\t" << employee[counter].gross << "\t" << employee[counter].net << endl;
system("pause");
}
}
P.s If you had to re modify this program again, what would you use to simplify it. Asking so I can keep re-modifying, and learn more advanced stuff. Vectors, pointers? Thanks in advance.
You have an array with 3 indexes but your loop is only going upto 2 indexes. Change your for loop to this.
for (int counter = 0; counter <= size; counter++)
Now, this loop will print the all the indexes.
Instead of using a static value you can also use this.
for (int counter = 0; counter < sizeof(Listofnames)/sizeof(Listofnames[0]); counter++)
sizeof(Listofnames)/sizeof(Listofnames[0]) This will give you the total size of your array.
Ideone Link
So this program is supposed to collect weather temperatures over 7 days using a for loop and then basically just print them back out to the user with an average temperature and the highest recorded temperature. Keep in mind, the following piece of code is a part of a much larger program. Anyway, the problem seems to be the "highest_temp1" float variable. When I run the program it produces some sort of error code instead of the highest temperature. This piece of code was tested in a separate source file and it works no problem.
switch (choice)
{
case 3:
int n;
float temperatures [7];
float lastweektemp [7] = {12.56,8.65,7.5,10,7.9,5,8};
float highest_temp1, highest_temp2;
float accumulated_temp1, accumulated_temp2;
system("CLS");
cout << "____________Weather Data____________" << endl << endl;
for (n = 0; n<7; n++)
{
cout << "What is the temperature for Day " << n+1 << " ?" << endl;
cin >> temperatures[n];
if (highest_temp1 < temperatures [n])
{
highest_temp1 = temperatures [n];
}
if (highest_temp2 < lastweektemp [n])
{
highest_temp2 = lastweektemp [n];
}
accumulated_temp1 = accumulated_temp1 + temperatures[n];
accumulated_temp2 = accumulated_temp2 + lastweektemp [n];
}
cout << endl << " Day This Week Last Week" << endl;
for (n=0; n<7; n++)
{
cout << n+1 << temperatures[n] << lastweektemp[n] << endl;
}
system("CLS");
cout << " Weather Report" << endl;
cout << " --------------" << endl << endl;
cout << "Current Week: " << endl;
cout << "-------------" << endl;
for (n=0; n<7; n++)
{
cout << "Day " << n+1 << ": " << temperatures[n] << endl;
}
cout << endl << " Average: " << accumulated_temp1 / 7 << endl;
cout << " Highest Temperature: " << highest_temp1 << endl;
cout << "Last Week: " << endl;
cout << "----------" << endl;
for (n=0; n<7; n++)
{
cout << "Day " << n+1 << ": " << lastweektemp[n] << endl;
}
cout << endl << " Average: " << accumulated_temp2 / 7 << endl;
cout << " Highest Temperature: " << highest_temp2 << endl;
system("PAUSE");
}
The highest temperature in current week is 24 but it is printing "Highest Temperature: 3.45857e+032"
This exact 'error-code' is appearing every time I run the program it doesn't change.
I am a newbie hence why I can't upload a photo.
Any help would be appreciated. I'm doing a small assignment in college. This is my first question so go easy !!
You have not assigned any value to teh variable highest_temp1 and you are comparing it with another value.
Basically you will need to assign it a value first before you compare..
highest_temp1 = 10.00
(or anything that it is supposed to contain)
You have not initialised highest_temp1 (or highest_temp1 for that matter: after that I stopped looking).
Same for accumulated_temp, which gets not initialised. can be done via
float accumulated_temp1(0);
Initialize variables before using them
float highest_temp1(-FLT_MAX); // -FLT_MAX insures results of first compare
float highest_temp2(-FLT_MAX); // Could use -1.0/0.0 of -INFINITY instead
float accumulated_temp1(0.0);
float accumulated_temp2(0.0);
For float number condition use if statements switch is not able to work in case of float number, switch only work for integer number.
I'm learning C++, and to do that I have created myself a problem which is to convert Celsius to Fahrenheit three times in the console. The user will input the Celsius degree.
I also want the output to be displayed like this:
Celsius: Fahrenheit:
cel1 fahr1
cel2 fahr2
cel3 fahr3
The code I have tried so far is:
double cel1, cel2, cel3;
double fahr1, fahr2, fahr3;
cout << "Celsius degree one: ";
cin >> cel1;
cout << "Celsius degree two: ";
cin >> cel2;
cout << "Celsius degree three: ";
cin >> cel3;
fahr1 = (cel1 * 9) / 5 + 32;
fahr2 = (cel2 * 9) / 5 + 32;
fahr3 = (cel3 * 9) / 5 + 32;
// messy like this to display like I want to
cout << endl <<
"Celsius: " << "Fahrenheit:" << endl <<
cel1 << " " << fahr1 << endl <<
cel2 << " " << fahr2 << endl <<
cel3 << " " << fahr3 << endl << endl;
which will display like I want to, but I feel this could have been achieved in a simpler way, so I tried something like this with a loop, but I couldn't figure out how to do it properly:
double celsius;
for (int times = 0; times != 3; ++times){
cout << "Celsius degree: ";
cin >> celsius;
double fahrenheit = (celsius * 9) / 5 + 32;
cout << "Fahrenheit degree: " << fahrenheit << endl;
cin.clear();
}
This code is less then the previous one, gives the correct answer and will convert three times, but I couldn't figure out how to display it like I want to.
My question is what is the best way to do this?
I suggest to split the code into smaller functions:
The one to compute the conversion
double celsius_to_fahrenheit(double celsius)
{
return (celsius * 9.0) / 5.0 + 32.0;
}
The one to get the input, I choose to use std::vector as container.
you may choose std::array<double, 3> since the array have fixed size,
but std::vector is a good default choice.
std::vector<double> get_input_celsius(std::size_t size)
{
std::vector<double> celsius(size);
for (std::size_t i = 0; i != celsius.size(); ++i) {
std::cout << "Celsius degree " << (i + 1) << ": ";
std::cin >> celsius[i];
}
return celsius;
}
The method to display the result. I choose to not store the conversion in a new std::vector since it is not used afterward:
void display_celsius_and_fahrenheit(const std::vector<double>& celsius)
{
std::cout << std::endl << "Celsius: " << "Fahrenheit:" << std::endl;
for (auto c : celsius) { // for range since C++11
std::cout << c << " " << celsius_to_fahrenheit(c) << std::endl;
}
}
And finally the main function:
int main()
{
std::vector<double> celsius = get_input_celsius(3);
display_celsius_and_fahrenheit(celsius);
return 0;
}
Live example
Create arrays to store the temperatues.
Instead of
double cel1, cel2, cel3;
double fahr1, fahr2, fahr3;
use
double celsius[3];
double fahrenheit[3];
Change the input gathering loop to use the arrays.
for (int times = 0; times != 3; ++times){
cout << "Celsius degree: ";
cin >> celsius[times];
fahrenheit[times] = (celsius[times] * 9) / 5 + 32;
cin.clear();
}
Use a loop to create the output:
cout << endl << "Celsius: " << "Fahrenheit:" << endl <<
for (int times = 0; times != 3; ++times){
cout << celsius[times] << " " << fahrenheit[times] << endl;
}
You could get away with not creating the array fahrenheit too if you compute it only during output.
In that case, change the input gathering loop to:
for (int times = 0; times != 3; ++times){
cout << "Celsius degree: ";
cin >> celsius[times];
cin.clear();
}
Change the output loop to:
cout << endl << "Celsius: " << "Fahrenheit:" << endl <<
for (int times = 0; times != 3; ++times){
double fahrenheit = (celsius[times] * 9) / 5 + 32;
cout << celsius[times] << " " << fahrenheit << endl;
}
I'm attempting to create a program to figure out Delta-V for my Kerbal Space Program game, and C++ (being run in the Eclipse IDE) does not allow for use of log() without assuming I'm trying to call a function. Thank you so much for help! It's really nice of you.`
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Hello. Welcome to the Kerbal Space Program Delta V Calculator. \n";
cout << " \n";
cout << "Note that each stage must use the same engine for this calculator.";
cout << "\n";
cout << "\nHow many stages make up your rocket? :";
int stageNumber;
cin >> stageNumber;
//cout << "Your rocket has " << stageNumber << " stages.\n";
cout << "\n\nStart from the bottom stage, please. ";
//ACTUAL DELTA V CALCULATIONS
for(int currentStage = 1; currentStage <= stageNumber; currentStage = currentStage + 1){
cout << "What is the total mass of this stage? :";
int totalMass;
cin >> totalMass;
cout << "What is the fuel mass of this stage? :";
int fuelMass;
cin >> fuelMass;
cout << "\n";
int dryMass;
dryMass = totalMass - fuelMass;
cout << "Your dry mass is" << dryMass << "\n";
cout << "\n";
cout << "Give the specific impulse of this stage's engine. \n";
int iSP;
cin >> iSP;
cout << "Here is the Delta V of your rocket.\n";
int deltaMass;
deltaMass = totalMass/dryMass;
int deltaV;
deltaV = iSP * log(deltaMass);
cout << deltaMass
exit(0);
}
}
`
log() is a function in the C standard library that takes the natural logarithm of a number. The name is effectively reserved — pick something else.
So I've seen the few other posts here and elsewhere regarding increasing decimal precision, but for some odd reason I cannot for the life of me get it to work. Here's what I've got:
//Euler's Method Approximation
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int n;
int i=1;
double h;
double x_n;
double y_n;
cout << "Enter n: " ;
cin >> n;
cout << endl << "Enter h: " ;
cin >> h;
cout << endl << "Enter x_0: ";
cin >> x_n;
cout << endl << "Enter y_0: ";
cin >> y_n;
cout << "n=" << i << " x=" << x_n << " y=" << y_n << endl;
++i;
while(i<n+1)
{
y_n = y_n + 3*x_n*h*y_n;
x_n = x_n + h;
cout << "n=" << i << " x=" << x_n << " y=" << setprecision(6) << y_n << endl;
++i;
}
return(0);
}
I apologize for the mess. Just threw this down under a time crunch, but now I've got more time to look at it. What could I be doing wrong? The mathematics definitely generates decimals to arbitrary precision depending on input, and all the variable types are as they should be. Even have the iomanip library in there. Any pointers? ( Still only prints out to the millionths )
Put std::fixed at the end of cout << "n=" << i << " x=" << x_n << " y=" << setprecision(6) << y_n << fixed << endl; as suggested by #40two in comments.
For ofstream you can use the precision method, http://www.cplusplus.com/reference/ios/ios_base/precision/