cout
) the following three lines
New York New Jersey Connecticutand then produces the value
EXIT_SUCCESS
as its exit status.
i
,
of data type int
,
born holding the value 10
.
Then output (to cout
) the value of this variable,
followed by one newline character.
int i {5}; int j {2}; cout << i/j << "\n"; double d {5.0}; double e {2.0}; cout << d/e << "\n";
ncans
,
of data type int
,
containing the number of cans of beer you have.
Do not create this variable: assume it already exists.
Now create a variable named
nsixpacks
,
of type int
,
whose value is the number of complete sixpacks you can make
out of ncans
of beer.
(A complete sixpack contains six cans).
And make another variable named
leftovers
,
of type
int
,
whose value is the number of loose cans left over after you make all the
possible sixpacks out of
ncans
of beer.
i = 20 * j + k;
=
, *
, +
=
, +
, *
*
, =
, +
*
, +
, =
+
, =
, *
+
, *
, =
void f(); int main() { f(); int k {20}; return EXIT_SUCCESS; } void f() { for (int i {0}; i < 10; ++i) { cout << j << "\n"; } int j {10}; }
i
, j
, k
i
, k
, j
j
, i
, k
j
, k
, i
k
, i
, j
k
, j
, i
0 7 14 21 28 35 42 49 56 63 70Extra credit. Right justify the column of numbers, because that’s the way human beings like it:
0 7 14 21 28 35 42 49 56 63 70
x
,
of type double
, containing some value.
Do not create this variable: assume it already exists.
Output one of the three messages
“positive”,
“negative”,
or
“zero”,
followed by one newline,
to indicate whether the value of x
is positive, negative, or zero.
int
s,
named
source
and
dest
,
of equal length.
Do not create these arrays: assume they already exist.
Write C++ code that will copy all the values from the source
array into the dest
array.
int
s named a
.
Do not create this array: assume it already exists.
Write C++ code that will output (to cout
)
the
sum of all the positive values in the aray.
If the array contains no positive values,
your code should output 0.
int
s named a
.
Do not create this array: assume it already exists.
Write C++ code that will output (to cout
)
the smallest value in the array.
But if the array contains no values (i.e., if the array is of length zero),
your code should output (to cerr
) a message
saying that there is no smallest value in the array.
weatherReport
.
A variable of this new data type will contain three values inside of it:
int
named fahrenheit
,
giving the temperature in fahrenheit.
double
named millibars
,
giving the air pressure in millibars.int
named humidity
,
giving the humidity in percent.today
,
of data type weatherReport
,
born holding the three values
57
,
996.4
,
and 90
.
Output (to cout
) the three values in
today
on three separate lines.
a
,
containing 5 of the
weatherReport
s
of the previous examples.
You can make up the values for the three fields in each
weatherReport
.
Then write C++ code that will output (to cout
) the subscript
of the
weatherReport
with the highest humidity in the array.
(This subscript will be an integer in the range 0 to 4 inclusive,
since the array contains 5
weatherReport
s.)
If two or more weatherReport
s
have the same humidity,
output the subscript of the last one that has that humidity.
If the array contains no
weatherReport
s
(i.e., if the array is of length zero),
your code should output (to
cerr
)
a message saying that there is no smallest humidity in the array.
This will not happen, since your array contains 5
weatherReport
s.
But put it in anyway, just in case someone else changes the array.
double
,
named
a
,
b
,
c
,
d
,
e
,
and
f
.
Do not create these variables: assume they already exist.
Assume that the following code is in the main
function.
Change the code to compute the three averages by calling a
new function named average
.
This function average
should take two formal arguments,
of data type double
,
named
d1
and
d2
.
The function average
should return a value of data type double
,
giving the average of the two formal arguments.
Please write the definition of the function average
.
Don’t bother to write the declaration for the function,
since the declaration is almost exactly the same as the first line of the
definition.
Make sure that the output of the program remains exactly the same
when you make these changes.
cout << "The average of " << a << and " << b << is " << (a+b)/2.0 << ".\n"; cout << "The average of " << c << " and " << d << " is " << (c+d)/2.0 << ".\n"; cout << "The average of " << e << " and " << f << " is " << (e+f)/2.0 << ".\n";Extra credit. Create a new data type, named
pair
,
that holds a pair of double
s.
Put the three pairs of double
s
(a
and b
,
c
and d
,
e
and f
)
into an array of three pair
s.
Then loop through the array
instead of writing almost the same cout
statement over and over.
adjust
whose formal argument is a
“reference to an int
”
named n
.
If the value of this argument is greater than 100,
the function will change the value of this argument to 100.
asterisks
,
that takes one formal argument, named n
,
of data type int
.
The function will output (to cout
)
a row of n
asterisks.
The function will use recursion instead of a loop,
and will do the following things:
n
is negative,
the function will output (to cerr
) an error message
and terminate the program with exit(EXIT_FAILURE)
.
n
is zero,
the function will do nothing.
n
is positive,
the function will output (to cout
)
one asterisk, and will then
call itself
to output the remaining n-1
asterisks.
Just write the function definition, because the function declaration is almost the same as the first line of the function definition.