You want to spice up your resume before the career fair, so you decide to update your GPA using the program below. What is the GPA that you will have on display for future employers?
#include <iostream>
using namespace std;
int main() {
double GPA = 3.52;
int updatedGPA = int(GPA);
cout << "GPA: " << updatedGPA;
}
3.0
Its correct to think that your GPA will be rounded down, but what else happens when you convert from int to double?
3
Converting to an int always rounds down to the nearest integer, so I do not recommend using type conversions to build your resumeβ¦ especially if youβre close to 4.0.
4.0
Converting to an int will round your GPA, but not in the direction that you were hoping for⦠what else happens when you convert from int to double?
4
Converting to an int will round yor GPA, but not in the direction that you were hoping for.
Error!
No errors here! Type conversions are perfectly legal in C++!
#include <iostream>
using namespace std;
int main() {
int x = acos(-1);
}
3.14159265358979323846
If x were a double, C++ would automatically round the value of pi to 15 decimal places.
3.142
If x were a double, C++ would automatically round the value of pi to 15 decimal places.
3.0
Automatic type conversion will round the value of pi down to the nearest integer, but what else happens when we convert a double to an int?
3
The value of x should be 3, since automatic type conversion will round the value of pi down to the nearest integer. Are you sure this program compiles?
Error!
Whenever we use math functions, we must include the <cmath> header file.
#include <iostream>
using namespace std;
int main() {
int a = 1.5;
double b = a + 1.5;
double c = 2.4;
double d = 1/5;
int e = c * c;
double f = int(c);
}
a
C++ performs automatic type conversion to round 1.5 down to the nearest integer.
b
Since a = 1, we know that b = 2.5, which is a non-zero decimal.
c
c is a double and has a non-zero decimal.
d
C++ performs integer division to round 1/5 down to the nearest integer. The value will be stored as 0, not 0.2.
e
c squared may have a non-zero decimal, but automatic type conversion will round it down to the nearest integer before storing the value in e.
f
int(c) rounds c down to the nearest integer before storing the value in f.
Rachel and Monica are best friends. They write a function called bestFriends so that they announce this fact to the rest of their friends. What is printed when they run the code below? Are there any errors?
#include <iostream>
using namespace std;
void bestFriends(string a, string b) {
cout << a << " is best friends with " << b;
}
int main() {
string a = "Rachel";
string b = "Monica";
bestFriends(b, a);
}
"Monica is best friends with Rachel"
Correct! Although the function definition has a << " is best friends with " << b, we call the function with variable b as argument a and variable a as argument b.
"Rachel is best friends with Monica"
You seem to be confusing your arguments and parameters!
a is best friends with b
The function couts the values of the variables, not their names!
b is best friends with a
The function couts the values of the variables, not their names!
#include <iostream>
using namespace std;
void greeting(string name) {
cout << "hello, " << name << "!";
}
void goodbye(string name) {
greeting(name);
cout << "!!";
}
int main() {
string hannah = "Hannah";
string anna = "Anna";
string louise = hannah;
hannah = anna;
anna = louise;
goodbye(anna);
}
hello, Hannah!!!
Correct! The string βHannahβ is assigned to the variable louise, then the value of louise is assigned to the variable anna. When goodbye(anna) runs, anna has the value βHannahβ.
hello, anna!!!
The function couts the value of the variable anna not the variable name!
hello, Anna!!!
Is "Anna" still the value of anna?
hello, Louise!
The goodbye function adds extra exclamation points.
hello, Louise!!!
We assigned the value of louise to anna. Is "Louise" the value of louise?
#include <iostream>
using namespace std;
void orderFood(string food, int quantity) {
cout << "I'll have " << quantity << " " << food;
}
int main() {
string a = "wings";
string b = "sliders";
int c = 3;
double d = 8.64;
char e = 'p';
}
orderFood(a, c);
Correct! a is a string and c is an int.
orderFood(b, d);
Correct! Automatic type conversion will convert d to an int.
orderFood(e, c);
e has a character value, and this function takes a string.
orderfood(a, d);
Correct! Automatic type conversion will convert d to an int.
orderFood(c, a);
You have to input your arguments in the correct order.