Name: ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________
// Function prototypes - each function what the name says it does
int abs(int j); // returns "absolute value" of j. defined in library cstdlib
double round(double x); // returns x rounded to nearest integer. defined in library cmath
string to_string(int val); // returns string representing val. defined in library string
// Note: to_string works only if you run g++ with option -std=c++11
// Variable definitions
int k = 4;
double x = 3.8;
string s = "The number of cookies I need: ";
| expression | type | value |
k + abs(-4) | ||
x + abs(-4) | ||
round(x) + k | ||
k = round(x) | ||
s + to_string(k + 5) | ||
to_string(int(round(x))) | ||
(3 + 10)/round(x - 1) | ||
k++ < x | ||
++k < x | ||
k++ < x && k > x |
istream objects have a function "get()" that can be useful.
We have not yet needed it, but you call it like cin.get() or fin.get() when fin is an ifstream.
The function returns the next character in the stream, even if it is a whitespace character.
Remember that our istreams have always helped us by skipping whitespace. But sometimes you might need to know what spaces are there!
It has the following prototype:
int get();
Note the function returns an int. Given this, write below the output of
the following programs when the users inputs the letter U.
|
|
[Assume the users inputs U] Output: |
[Assume the users inputs U] Output: |
firstfactor:
#include <iostream>
// PROTOTYPE: IMPLEMENT THIS FUNCTION BELOW main!!
int firstfactor(int);
int main() {
// Get integer n, n > 1, from user.
int n;
cout << "Enter an integer larger than 1: ";
cin >> n;
// Print out the factorization.
cout << "The factorization of " << n << " is ";
while( n > 1 ) {
// Get & print the next prime factor.
int f = firstfactor(n);
cout << '(' << f << ')';
n = n / f;
}
cout << endl;
return 0;
}
As you see, the program is missing the definition of the function
firstfactor. Complete the program by defining the function (a
description of what the function is supposed to do is given in the source
code's comments). When your program is working correctly, a typical run might
look like this:
Enter an integer larger than 1: 60 The factorization of 60 is (2)(2)(3)(5)Turn in a printout of this cover sheet with your answers to the questions, your source code, and a screen capture of your program running the input 21978.