quotient = numerator / denominator; remainder = numerator % denominator;
user inputs | expected output | ||
numerator | denominator | quotient | remainder |
17 | 5 | 3 | 2 |
11 | 13 | 0 | 11 |
12 | 4 | 3 | 0 |
std::cout << std::setw(ColWidth) << numerator << std::endl; std::cout << std::setw(ColWidth) << denominator << std::endl; std::cout << std::setw(ColWidth) << userInitial << std::endl; std::cout << std::setw(ColWidth) << "And this!" << std::endl;
std::cout << std::setiosflags(std::ios::fixed); std::cout << std::setprecision(5);
double third = 1/3.0;
std::cout << Pi << std::endl; std::cout << third << std::endl;
double xVal, yVal; // two values to be supplied by the user double answer; // location where we'll store the result of our call std::cout << "Enter two numeric values:" << std::endl; std::cin >> xVal >> yVal; answer = rootOfSumSquares(xVal, yVal); std::cout << "For inputs " << xVal << " and " << yVal; std::cout << ", the root of the sum of their squares is "; std::cout << answer << std::endl;
double rootOfSumSquares(double x, double y) { double sumSqs; // will hold the sum of the two squares double root; // will hold the root of the sum of the squares (our final answer) sumSqs = x*x + y*y; root = sqrt(sumSqs); return root; }
// sample structure for a program with its own functions foo and blah #includes and constants // prototypes int foo(double a, int b, float c); double blah(int m) // main routine int main() { ... main's code ... } // full versions int foo(double a, int b, float c); { ... foo's code ... } double blah(int m) { ... blah's code ... } |
Welcome to the StairCounter! We calculate your total stair climbing for your Math/CS courses based on how often you go from building to building and from floor to floor. At the moment it just handles travel between buildings 210 (2nd floor), 315 (floors 1 and 2), and 460 (3rd floor). How many times did you travel from building 210 to the ground floor of building 315? 4 Within building 315, how many times did you travel from floor 1 up to floor 2? 2 How many times did you travel from the upper floor of building 315 to the third floor of building 460? 3 Based on the information you provided you climbed 578 total stairs, or roughly the equivalent of 41 flights of stairs Thanks for using StairCounter! |
// #include the libraries we need // declare constants for the stair counts for each route int main() { // declare variables for the number of times each trip is made // a variable for the total calculated stair count // a variable for the total calculated number of flights // prompt (cout) the user for the number of times they traveled 210-315 // read (cin) their response // prompt the user for the number of times they traveled 315 floor one to floor two // read (cin) their response // prompt the user for the number of times they traveled 315-460 // read (cin) their response // calculate the sum of the total distances and store in the final result variable // (# times each trip was made * the stair count for that route) // calculate how many flights of stairs this represents // (the total number of stairs / our constant for stairs per flight) // display (cout) the final results } |
void displayResults(int stairCount, int numFlights) { std::cout << "Based on the information you provided, you climbed " << stairCount; std::cout << " total stairs" << std::endl; std::cout << "or roughly the equivalent of " << numFlights; std::cout << " flights of stairs" << std::endl << std::endl; std::cout << "Thanks for using Stair Counter!" << std::endl << std::endl; }
int calcFlights(int stairCount) { int flights; flights = stairCount / StairsPerFlight; return flights; }(assuming our constant up top for the number of stairs in a flight had been declared using the name StairsPerFlight)