Get Fast C++ Assignment Help
Get Fast C++ Assignment Help
Recall from lecture that after you tell the preprocessor to use
functions from the standard input/output library (#include
<iostream>), you need to tell the compiler that the functions you’ll
be using are defined in the “standard namespace” (a language
feature we won’t go into in detail), which you do with “using
namespace std;”. Another way to access the functions in the
standard namespace is to prefix each one with the name of the
namespace. For instance, you can skip the using statement if you
write std::cout and std::cin every time you want to print or input
text.
Problem 1
Look at the following program and try to guess what it does
without running it.
#include <iostream>
int main()
{ int x = 5;
int y = 7;
std::cout << "\n";
std::cout << x + y << " " << x * y;
std::cout << "\n";
return 0;
}
Problem 2
#include <iostream>
int main()
{
cout < "Hello World\n;
return 0
}
Problem 3
Write a program that writes “I love C++” to the screen.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
for this data type – i.e. the equivalent for strings of int, double, etc.
– is char *. You do not need to know how this syntax works; all you
need to know is that when you want to declare a variable to store a
string, you type something like: char *loveCpp = "I love C++!";.
(The * is usually placed directly before the variable name, but the
name of this variable is just loveCpp.) The escape codes we talked
about in lecture are all for encoding special characters as chars or
within char *’s.
Problem 4
Problem 5
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
cpphomeworkhelp.com
reach us at : - https://www.cpphomeworkhelp.com/
Expressions:
An expression in C++ is any series of tokens that, when it is
evaluated and all commands contained within it are run, is
equivalent to a value. For instance, a + b is an expression, as are
35902 and "Hello world!". An expression is said to “evaluate to” its
value. This is in contrast to a statement, such as x = a + b;, which
gives an instruction to the processor. We will see other examples of
statements, such as conditional statements and loops, in the next
lecture. (Even x = a + b, an assignment statement, can be treated as
an expression, whose value is whatever x is equal to after the
assignment is completed, but this is not commonly used.)
Expressions that you type directly into your code – integers, decimal
numbers, true, false, etc. – are constants. 35 and "This is a string"
are constants, but 24 / 3 and myVariable are not, since they are
calculated at run-time.
There are two different types of expressions: L-values and R-values.
An L-value appears on the left side of an assignment statement (“L”
for “left”); it is the thing that is assigned to. An Rvalue is the
expression whose value is evaluated, and possibly assigned to an L-
value. An identifier – a name that you gave to something, such the
name of a variable – can be used as an expression in either of these
ways. For example, in the statement x = y + 5;, x and y are both
identifiers; x is an L-value; and y, 5, and y + 5 are all R-values.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Problem 6
a) 23.5
b) double a = 23.5;
c) 24 * 3.2
d) 24 / 32
e) 24 / 32.0
f) return 0;
g) x // Assume the line before says int x = 4;
h) 'x‘
Problem 7
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Problem 8
Named constants:
We talked in lecture about naming conventions for constants: a
name
in all-caps usually indicates a constant. C++ allows you to instruct
the compiler to insist that a variable stay constant throughout the
program. You can do this by putting the keyword const before the
variable declaration. For example, const double PI = 3.14159;
declares a variable named PI that can never be changed after its
initial assignment to 3.14159. To attempt to change the value of
such a variable – a named constant – is a syntax error. Effectively,
we’ve simply defined another name for the constant value 3.14159,
which of course we aren’t allowed to change.
Operator shorthands:
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
C++ provides a number of shortcuts for simple arithmetic
expressions. Often you’ll want to modify the value of a variable by
doing something to its current value – adding 1, multiplying by 42,
etc. We could express this as something like x = x + 2;. Because
this is
There is an even shorter shortcut for saying x += 1;: We can use the
pre-increment operator (syntax: ++variable) to add 1 to a variable
before evaluating it, and the post-increment operator (syntax:
variable++) to add 1 after evaluating it. For instance, if y is set to 3,
x = y++; will set x to 3 and y to 4, while x = ++y; will first set y to
4, then x to 4, as well.
Problem 9
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
cpphomeworkhelp.com
reach us at : - https://www.cpphomeworkhelp.com/
SOLUTIONS SET
Problem 1
OUTPUT:
12 35
Problem 2
Errors in code: missing second < after cout; missing final quote
after \n, missing semicolon after return statement. Corrected code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Problem 3
#include <iostream>
using namespace std;
int main()
{
Problem 4
a) (short) integer
b) double or float
c) (long) integer
d) double or float
e) char * (NOT char – char is only a single character; only a char
* can store a whole string)
f) boolean (bool)
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Problem 5
Problem 6
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Problem 7
#include<iostream>
using namespace std;
int main()
{
int a, b;
float c;
cout << "Enter an integer:";
cin >> a;
return 0;
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Problem 8
#include <iostream>
using namespace std;
int main()
{
int initialMiles, finalMiles, milesTraveled;
cout << "Enter the miles on your car's odometer at the start of your
journey \n";
cin >> initialMiles;
cout << "Enter the fuel level in your tank at the start of your
journey \n";
cin >> initialTank;
cout << "Enter the miles on your car's odometer at the end of your
journey \n";
cin >> finalMiles;
cout << "Enter the fuel level in your tank at the end of your journey
\n";
cin >> finalTank;
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
milesTraveled = finalMiles - initialMiles;
fuelUsed = initialTank - finalTank;
double milesPerGal = milesTraveled / fuelUsed;
cout << "You traveled " << milesTraveled << " miles using “
<< fuelUsed << " gallons of fuel \n";
cout << "Your fuel consumption was " << milesPerGal << "
miles per gallon \n";
return 0;
}
Problem 9
#include <iostream>
using namespace std;
int main()
{
const int NUMBER_OF_VARIABLES = 2;
int x = 100;
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
++x %= NUMBER_OF_VARIABLES;
x += 2;
cout << x;
return 0;
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/