Skip to content

Instantly share code, notes, and snippets.

@chrisscherer
Created May 4, 2014 02:08
Show Gist options
  • Save chrisscherer/178eb8a78cae5980392b to your computer and use it in GitHub Desktop.
Save chrisscherer/178eb8a78cae5980392b to your computer and use it in GitHub Desktop.
//In this challenge, write a program that takes in three arguments, a start temperature (in Celsius), an end temperature (in Celsius) and a step size.
//Print out a table that goes from the start temperature to the end temperature, in steps of the step size; you do not actually need to print the final end temperature if the step size does not exactly match.
//You should perform input validation: do not accept start temperatures less than a lower limit (which your code should specify as a constant) or higher than an upper limit (which your code should also specify).
//You should not allow a step size greater than the difference in temperatures. (This exercise was based on a problem from C Programming Language).
//Ask user for start temp (in celsius)
//Ask user for end temp (in celsius)
//ask user for step size
#include <iostream>
using namespace std;
int main ()
{
//initialize values
int start_temp;
int end_temp;
int step_size;
bool valid = false;
//validate user input, repeat until all are correct
while(!valid)
{
cout << "Please enter a starting temperature (>0) in celsius: ";
cin >> start_temp;
cout << "Please enter an ending temperature (<150) in celsius: ";
cin >> end_temp;
cout << "finally, pick a step size (can't be greater than the difference in temperatures): ";
cin >> step_size;
//if user doesn't violate rules, continue program
if(start_temp > 0 && end_temp < 150 && step_size < (end_temp - start_temp)){
valid = true;
}
else
{
cout << "one of your inputs was invalid, please enter valid integers"
}
}
//calculate how many intervals between starting and ending temp, based on step size
int num_steps = end_temp / step_size;
//this makes sure that the program never exceeds the ending temp
if(end_temp % step_size == 0)
{
num_steps--;
}
//initialize temperatures in farenheit
int farenheit_temps[num_steps];
cout << "Celsius Farenheit" << endl;
cout << "------- ---------" << endl;
//calculate and print both temperatures
for(int i=0;i<num_steps;i++)
{
farenheit_temps[i] = ((start_temp + (i * step_size))* 9 / 5) + 32;
cout << (start_temp + (i * step_size)) << " " << farenheit_temps[i] << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment