SlideShare a Scribd company logo
1 of 15
Get Fast C++ Assignment Help
For any Assignment related queries, Call us at : - +1 678 648 4277
You can mail us at :- info@cpphomeworkhelp.com or
reach us at :- https://www.cpphomeworkhelp.com/
cpphomeworkhelp.com
PROBLEM SET
Standard namespace:
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;
}
cpphomeworkhelp.com
Problem 2
Put the following program in a file called buggy.cpp and compile it. (“.cpp” is the
standard suffix for C++ code files.) What errors do you receive? How would you fix
them? (You may need to correct and recompile several times before all the problems are
fixed.)
#include <iostream>
int main()
{
cout < "Hello Worldn;
return 0
}
Problem 3
Write a program that writes “I love C++” to the screen.
char * data type:
One data type that we discussed only implicitly in lecture is strings. Strings are just
sequences of characters, which are denoted by quotation marks – e.g. "Hello world!" is a
string, as are "n", "The Noble Duke of York", and so on. (Note that strings use double
quotes, whereas lone characters, such as 'n', use single quotes.) The official C++ name
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
What would be the correct variable type in which to store the following information?
a. Your age
b. The area of your backyard
c. The number of stars in the galaxy
d. The average rainfall for the month of January
e. Your name
f. A status value corresponding to failure or success
Problem 5
Suggest a good variable name for each piece of information you described in Problem
4, and provide a sample declaration/assignment statement for the variable.
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.
The compiler will often convert numbers between types automatically for you to make your
expressions work. For example, if you write 3 + x where x is a double, the compiler
converts the 3 to a double for you.
cpphomeworkhelp.com
Problem 6
State whether each of the following is an expression or a statement. If it is an expression,
state its data type, what type of expression it is (if it is a constant or an identifier), and the
value it evaluates to. (Decimal numbers are doubles by default. You need not consider
assignment statements to be expressions.)
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
Write a program that prompts the user to enter two integer values and a float value,
and then prints out the three numbers that are entered with a suitable message.
cpphomeworkhelp.com
Problem 8
Write a program to evaluate the fuel consumption of a car. Have the user input the miles
on the car’s odometer at the start and end of the journey, and also the fuel level in the tank
(in gallons) at the start and end of the journey. Calculate fuel used, miles travelled, and the
overall fuel consumption in miles travelled per gallon of fuel. Print these values,
accompanied by appropriate messages indicating what they are.
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:
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
cpphomeworkhelp.com
such a common need, though, C++ allows us to simply write x += 2;. The same syntax
works for other mathematical operators: -=, *=, /=, %= are all valid operators.
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
Write a program that sets the constant integer NUMBER_OF_VARIABLES to 2 and the
integer variable x to 100. The program should then increment x, set it to the remainder of
its current value divided by NUMBER_OF_VARIABLES, and add 2 to it. Use the operator
shorthands as much as you can.
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 Worldn";
return 0;
}
cpphomeworkhelp.com
Problem 3
#include <iostream>
using namespace std;
int main()
{
cout << "I love C++";
return 0;
}
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)
cpphomeworkhelp.com
Problem 5
a) int myAge = 18;
b) double yardArea = 20.5;
c) long numOfStars = 100000000;
d) double avgRain = 15.3;
e) char *myName = "Tanmay";
f) bool success = false;
Problem 6
a. expression, double, constant
b. statement
c. expression, double, neither, 76.8
d. expression, double, neither, 0
e. expression, double, neither, 0.75
f. statement
g. expression, int (or long int), identifier, 4
h. expression , char, constant
cpphomeworkhelp.com
Problem 7
#include<iostream>
using namespace std;
int main()
{
int a, b;
float c;
cout << "Enter an integer:";
cin >> a;
cout << "Enter another integer:";
cin >> b;
cout << "Enter a number with decimal:";
cin >> c;
cout << "You entered " << a << ", " << b << ", and " << c;
return 0;
}
cpphomeworkhelp.com
Problem 8
#include <iostream>
using namespace std;
int main()
{
int initialMiles, finalMiles, milesTraveled;
float initialTank, finalTank, fuelUsed, fuelConsumed;
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;
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;
cpphomeworkhelp.com
++x %= NUMBER_OF_VARIABLES;
x += 2;
cout << x;
return 0;
}
cpphomeworkhelp.com

More Related Content

Similar to Get Fast C++ Homework Help

C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2Tekendra Nath Yogi
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variablesTony Apreku
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance levelsajjad ali khan
 
C programming language
C programming languageC programming language
C programming languageAbin Rimal
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework HelpC++ Homework Help
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Rasan Samarasinghe
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxLikhil181
 

Similar to Get Fast C++ Homework Help (20)

C++ Function
C++ FunctionC++ Function
C++ Function
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variables
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
What is c
What is cWhat is c
What is c
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
C programming language
C programming languageC programming language
C programming language
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
Tut1
Tut1Tut1
Tut1
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
C Programming
C ProgrammingC Programming
C Programming
 
Visual c sharp
Visual c sharpVisual c sharp
Visual c sharp
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
 
C important questions
C important questionsC important questions
C important questions
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 

More from C++ Homework Help (17)

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 

Recently uploaded

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Get Fast C++ Homework Help

  • 1. Get Fast C++ Assignment Help For any Assignment related queries, Call us at : - +1 678 648 4277 You can mail us at :- info@cpphomeworkhelp.com or reach us at :- https://www.cpphomeworkhelp.com/ cpphomeworkhelp.com
  • 2. PROBLEM SET Standard namespace: 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; } cpphomeworkhelp.com
  • 3. Problem 2 Put the following program in a file called buggy.cpp and compile it. (“.cpp” is the standard suffix for C++ code files.) What errors do you receive? How would you fix them? (You may need to correct and recompile several times before all the problems are fixed.) #include <iostream> int main() { cout < "Hello Worldn; return 0 } Problem 3 Write a program that writes “I love C++” to the screen. char * data type: One data type that we discussed only implicitly in lecture is strings. Strings are just sequences of characters, which are denoted by quotation marks – e.g. "Hello world!" is a string, as are "n", "The Noble Duke of York", and so on. (Note that strings use double quotes, whereas lone characters, such as 'n', use single quotes.) The official C++ name cpphomeworkhelp.com
  • 4. 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 What would be the correct variable type in which to store the following information? a. Your age b. The area of your backyard c. The number of stars in the galaxy d. The average rainfall for the month of January e. Your name f. A status value corresponding to failure or success Problem 5 Suggest a good variable name for each piece of information you described in Problem 4, and provide a sample declaration/assignment statement for the variable. cpphomeworkhelp.com
  • 5. 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. The compiler will often convert numbers between types automatically for you to make your expressions work. For example, if you write 3 + x where x is a double, the compiler converts the 3 to a double for you. cpphomeworkhelp.com
  • 6. Problem 6 State whether each of the following is an expression or a statement. If it is an expression, state its data type, what type of expression it is (if it is a constant or an identifier), and the value it evaluates to. (Decimal numbers are doubles by default. You need not consider assignment statements to be expressions.) 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 Write a program that prompts the user to enter two integer values and a float value, and then prints out the three numbers that are entered with a suitable message. cpphomeworkhelp.com
  • 7. Problem 8 Write a program to evaluate the fuel consumption of a car. Have the user input the miles on the car’s odometer at the start and end of the journey, and also the fuel level in the tank (in gallons) at the start and end of the journey. Calculate fuel used, miles travelled, and the overall fuel consumption in miles travelled per gallon of fuel. Print these values, accompanied by appropriate messages indicating what they are. 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: 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 cpphomeworkhelp.com
  • 8. such a common need, though, C++ allows us to simply write x += 2;. The same syntax works for other mathematical operators: -=, *=, /=, %= are all valid operators. 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 Write a program that sets the constant integer NUMBER_OF_VARIABLES to 2 and the integer variable x to 100. The program should then increment x, set it to the remainder of its current value divided by NUMBER_OF_VARIABLES, and add 2 to it. Use the operator shorthands as much as you can. cpphomeworkhelp.com
  • 9. 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 Worldn"; return 0; } cpphomeworkhelp.com
  • 10. Problem 3 #include <iostream> using namespace std; int main() { cout << "I love C++"; return 0; } 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) cpphomeworkhelp.com
  • 11. Problem 5 a) int myAge = 18; b) double yardArea = 20.5; c) long numOfStars = 100000000; d) double avgRain = 15.3; e) char *myName = "Tanmay"; f) bool success = false; Problem 6 a. expression, double, constant b. statement c. expression, double, neither, 76.8 d. expression, double, neither, 0 e. expression, double, neither, 0.75 f. statement g. expression, int (or long int), identifier, 4 h. expression , char, constant cpphomeworkhelp.com
  • 12. Problem 7 #include<iostream> using namespace std; int main() { int a, b; float c; cout << "Enter an integer:"; cin >> a; cout << "Enter another integer:"; cin >> b; cout << "Enter a number with decimal:"; cin >> c; cout << "You entered " << a << ", " << b << ", and " << c; return 0; } cpphomeworkhelp.com
  • 13. Problem 8 #include <iostream> using namespace std; int main() { int initialMiles, finalMiles, milesTraveled; float initialTank, finalTank, fuelUsed, fuelConsumed; 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; cpphomeworkhelp.com
  • 14. 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; cpphomeworkhelp.com
  • 15. ++x %= NUMBER_OF_VARIABLES; x += 2; cout << x; return 0; } cpphomeworkhelp.com