int i {10}; int j {i}; int k {20}; int& r {k}; ++j; ++r; cout << i << "\n"; cout << k << "\n";
d
,
of data type double
,
born holding the value 3.14159
.
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";
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.
asterisks
,
whose formal argument is an
int
named n
.
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
asterisks
will output (to cerr
) an error message
and terminate the program by passing the argument
EXIT_FAILURE
to the C++ function
exit
.
n
is positive,
the function
asterisks
will output (to cout
)
one asterisk, and will then
call itself
to output the remaining n-1
asterisks.
n
is zero,
the function
asterisks
will do nothing, because no work remains to be done.
nshoes
,
of data type int
,
containing the number of horseshoes you have.
Do not create this variable: assume it already exists.
Now create a variable named
npacks
,
of type int
,
whose value is the number of complete packs you can make
out of nshoes
horseshoes.
And create another variable named
nleftovers
,
of type
int
,
whose value is the number of loose horseshoes left over after you make all the
possible packs out of
nshoes
horseshoes.
(The value of nleftovers
will be an int
in the range 0 to 3 inclusive.)
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.
void f(); int main() { f(); int k {20}; return EXIT_SUCCESS; } void f() { int j {10}; for (int i {0}; i < 10; ++i) { cout << j << "\n"; } }
i
, j
, k
i
, k
, j
j
, i
, k
j
, k
, i
k
, i
, j
k
, j
, i
clamp
whose formal argument,
named n
,
is of data type
“reference to int
”.
If the value of the argument is less than 0,
the function will change the value of the argument to 0.
If the value of the argument is greater than 100,
the function will change the value of this argument to 100.
cout
) the following three lines
New York New Jersey Connecticutand then produces the value
EXIT_SUCCESS
as its exit status.
n
,
of type int
, containing some value.
Do not create this variable: assume it already exists.
Write C++ code that will
output the first message (and only the first message)
that is applicable in the following list.
n
is greater than 1,000,000n
is greater than 100,000n
is greater than 10,000n
is greater than 1,000n
is less than or equal to 1,000if (hour >= 12) { if (hour < 12 + 6) { cout << "Good afternoon.\n"; } else if (hour < 12 + 9) { cout << "Good evening.\n"; } else { cout << "Good night.\n"; } } else { cout << "Good morning.\n"; }
double
s named a
.
Do not create this array: assume it already exists.
Write C++ code that will output (to cout
)
the
average of all the positive values in the array.
If the array contains no positive values,
your code should output (to cerr
) a message
saying there are no positive values in the array.
0 8 16 24 32 40 48 56 64 72Extra credit. Right justify the column of numbers, because that’s the way human beings like to see it. Is there any file you would need to
#include
in order to right justify?
0 8 16 24 32 40 48 56 64 72
person
.
A variable of this new data type will contain three values inside of it:
string
named lastName
,
giving the last name of the person
.
string
named firstName
,
giving the first name of the
person
.
int
named age
,
giving the age in years of the
person
.
founder
,
of data type person
,
born holding the three values
"Ignatius"
,
"Loyola"
,
and 64
.
Then write code that gets the three values from
founder
and outputs them
(to cout
)
on three separate lines.
a
,
containing 5 of the
person
s
of the previous problem.
You can make up any values for the three fields in each
person
.
Then write C++ code that will output (to cout
) the first and
last names
of the
person
with the highest age in the array.
If two or more person
s
have the highest age,
output the first and last names of the first person
in the array
that has that age.
If the array contains no
person
s
(i.e., if the array is of length zero),
your code should output (to
cerr
)
a message saying that there is no highest age in the array.
This will not happen, since your array contains 5
person
s.
But put it in anyway, just in case someone else tampers with the array.
i = 20 * j + k;
=
, *
, +
=
, +
, *
*
, =
, +
*
, +
, =
+
, =
, *
+
, *
, =
mpg
,
that
return
s a value of type double
telling you how many miles per gallon of gas you’re getting.
The two formal arguments of this function should be double
s
named
miles
and
gallons
.
gallons
is less than or equal to 0,
the function
should output (to cerr
)
an error message saying that the number of gallons must be positive,
and should terminate the program by passing the argument
EXIT_FAILURE
to the C++ function
exit
.
gallons
is positive,
the function mpg
should return the number of miles per gallon that you’re getting.
int
s.
Why would it be more natural and useful in this language
to output the int
s from 0 to 9 inclusive,
rather than the int
s from 1 to 10 inclusive?