SlideShare ist ein Scribd-Unternehmen logo
1 von 19
For any Homework 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/
Ternary operator:
The conditional operator ? : is also another way to evaluate conditions. It operates
on three operands, and is thus known as a ternary operator. Here is an example of
its usage:
a = x < y ? x : y;
In this example, if the test expression evaluates to true – i.e. if x < y – then the
variable a will be assigned the value that the variable x contains. Otherwise, else it
will be assigned the value of the variable y.
The whole expression containing this operator (the right side of the assignment
statement) is called a conditional expression, and the sub-expression before the
question mark is called the test expression. If this test expression is true, the entire
expression takes on the value of the expression immediately after the question
mark. Otherwise, it takes on the value of the expression following the colon.
Usually you only want to use this construct for choosing between two simple
expressions for a particular value (both choices must be the same data type). Doing
otherwise could run afoul of the syntactic rules of the operator and lead to syntax
errors. You cannot, for instance, make the two options two different return
statements.
cpphomeworkhelp.com
1. Without running, explain what the following set of statements will do:
bool input;
int a;
cin >> input >> a;
char *choice = ( input ? "Hello World!" : "I love C++!" );
if( ( input ? a > 1 : a < 1 ) ) {
cout << "Why did you pick "" << choice << ""?" << endl;
}
else {
cout << "Yay, you picked " << choice;
}
2. Convert this for loop to a do-while loop.
int sum = 0;
for( int i = 0; i < 5; i++ )
sum += i;
cout << sum;
cpphomeworkhelp.com
break and continue:
Two keywords often used with loops are break and continue.
The break keyword causes the entire construct to exit, and control passes to
the statement immediately after the body of the construct.
For example:
for( n = 10; n > 0; n-- )
{
cout << n << ", ";
if( n == 7 )
break;
}
In this example, only the first few values of n (the numbers 10, 9, 8) will be
executed. As soon as the value of n becomes 7, the loop will exit.
The continue statement causes the program to skip the rest of the loop in the
current iteration as if the end of the statement block had been reached,
causing it to jump to the start of the next iteration:
cpphomeworkhelp.com
for( int n = 10; n > 0; n-- )
{
if ( n == 7 )
continue;
cout << n << ", ";
}
In this example, 10, 9, and 8 will again be displayed, but as soon as the value of
n becomes 7, control will be passed to the beginning of the loop body. The rest
of that iteration is skipped, and the rest of the countdown till 1, will be displayed
(so the final output will be “10, 9, 8, 6, 5, 4, 3, 2, 1”). When you place a continue
command within a for loop, the increase statement (in this case, n--) is executed
before the loop iterates again.
3. Write a program to take in a number from a user, find its reciprocal and add it
to a running sum (the running sum will be 0, when the loop initially starts). The
program should repeat this procedure 10 times. However, if the user enters 0,
the loop should exit, and if the user enters 1, nothing should be added. Print the
final sum at the end of the program.
cpphomeworkhelp.com
switch statements:
In lecture, the main type of conditional construct discussed was if-else. There is
another type of construct, which is also sometimes used, called the switch-case
construct. In this construct, the value of a variable is compared to a set of
constants, and when a corresponding match is found, the statements
associated with that case are executed. It is like an if-else construct that can
only check for equality
For example:
switch(number)
{ case 3:
cout << "Red"; break;
case 2:
cout << "Orange"; break;
case 7:
cout << "Black"; break;
default:
cout << "None of the above";
}
cpphomeworkhelp.com
In this switch statement, the value of the variable numberis compared with 3, 2
and 7, and if a match is formed, the corresponding color is printed. If no match
is found then “None of the above” is printed. (The break keyword is required to
end each caseblock except the last.)
4.Using the switch-case construct write a program to input two numbers,
display the following menu, and then print according to the user’s choice:
1.Difference of two numbers
2.Quotient of two numbers
3. Remainder of two numbers
For example, if the user inputs 1, then the difference of the two numbers should
be printed.
5.Find the sum of the first n terms of the following series, where n is a number
entered by the user:
6. Print the following pattern, using horizontal tabs to separate numbers in the
same line. Let the user decide how many lines to print (i.e. what number to
start at).
cpphomeworkhelp.com
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Series:
As specified in lecture, nested loops are used when for one repetition of a
process, many repetitions of another process are needed. Similar to patterns,
nested loops can be used to print the sums of nested series. For example the
sum of the series 1+(1+2) +(1+2+3) +... Can
befoundbyaddingonetoarunningsumforeach execution of the inner loop, instead
of printing them as you would for a pattern. As with patterns, the number of
times the inner loop runs in this case depends on the value of the outer loop.
7. Write a program that inputs two numbers x and n, and find the sum of the
first n terms of the following series: x +(x + x2)+(x + x2 + x3) + ....
cpphomeworkhelp.com
8. An Angstrom number is one whose digits, when cubed, add up to the number
itself. For instance, 153 is an Angstrom number, since 13 + 53 + 33 = 153 .
Write a program to input a number, and determine whether it is an Angstrom
number or not.
Hint: The modulus operator (%) will be useful here.
cpphomeworkhelp.com
Problem 1:
i. The program takes 2 inputs from the user, and stored it in variables input
and a.
ii. If the user has inputted the value of input as 1
a. the string choice will be assigned value “Hello World”.
b. In the if-else construct, the condition returned to the if statement will be
a>1. Now, if this condition is true (i.e if a>1), then
Why did you pick "Hello World"?
will be displayed.
c. If this returned condition is false, (i.e if a<1), then
Yay you picked Hello World
will be displayed.
iii. If the user has inputted the value of input as 0
a. the string choice will be assigned value “I love C++”.
b. In the if-else construct, the condition returned to the if statement will be
a<1. Now, if this condition is true (i.e if a<1), then
cpphomeworkhelp.com
Why did you pick "I love C++"?
will be displayed.
c. If this returned condition is false, (i.e if a>1), then
Yay you picked I love C++
will be displayed.
Problem 2:
int sum = 0, i = 0;
do
{
sum += i++;
} while ( i < 5 );
Problem 3:
#include <iostream>
int main()
{
float a, sum=0;
cpphomeworkhelp.com
for(int i=1;i<=10;i++)
{
cout << "Enter the number whose reciprocal has to be added to the
series:";
cin >> a;
if( a == 0 )
break;
else if ( a == 1)
continue;
else
sum += 1 / a;
}
Problem 4:
#include<iostream>
using namespace std;
int main()
{
int choice, a, b;
cout << "Enter two numbers:";
cin >> a >> b; cout << "1. Difference of two numbers" << endl
cpphomeworkhelp.com
<< "2. Quotient of two numbers" << endl
<< "3. Remainder of two numbers" << endl
<< "Enter your choice:";
cin >> choice;
switch(choice)
{
case 1:
if ( a > b ) // Optional check
cout << "Difference is " << a - b << endl;
else
cout << "Difference is " << b - a << endl;
break;
case 2:
if ( a > b ) // Optional check
cout << "Quotient is " << a / b << endl;
else
cout << "Quotient is " << b / a << endl;
break;
case 3:
if ( a > b ) // Optional check
cout << "Remainder is " << a % b << endl;
cpphomeworkhelp.com
else
cout << "Remainder is " << b % a << endl;
break;
default:
cout << "Not an option!" << endl;
}
return 0;
}
Problem 5:
#include <iostream>
// For the second possibility below:
#include <cmath>
using namespace std;
int main()
{
int n;
float sum = 0, denom = 1;
cout << "Enter the number of terms of the series:" << endl;
cpphomeworkhelp.com
cin >> n;
for (int i = 1; i<=n; i++, denom += 2)
{
if( i % 2 == 0 )
sum += 1 / (denom * denom );
else
sum -= 1 / (denom * denom );
}
// or sum += pow(-1.0, i) * 1/( term * term)
// Also, instead of adding to denom each time, the denominator can be
// calculated as (2 * i – 1)
cout<<"The sum is:"<<sum<<endl;
return 0;
}
Problem 6:
#include <iostream>
using namespace std;
int main()
{
cpphomeworkhelp.com
int n;
float sum=0,term=1;
cout << "Enter the number of rows of the pattern:";
cin>>n; for (int i=n; i > 0; i--) {
for(int j=n; j>=i; j--)
cout << j << 't'; cout << endl;
}
return 0;
}
Problem 7:
#include<iostream>
// For the second possibility below:
#include <cmath>
using namespace std;
int main()
{
cpphomeworkhelp.com
int n,x, sum = 0;
cout << "Enter the value for term 'x': ";
cin >> x;
cout << "Enter the sub-series of the main series: ";
cin >> n;
for(int i=1; i<=n; i++)
{
int term=1;
for(int j=1; j<=i; j++)
{
term *= x;
sum += term;
}
}
/*
Alternate possibility:
for(int i=1; i<=n; i++)
{
for(int j=1; j<=i; j++)
{
sum += pow(x, b);
}
} cpphomeworkhelp.com
*/
cout << "The sum is:" << sum << endl;
return 0;
}
Problem 8:
#include <iostream>
using namespace std;
int main()
{
int num;
float sum=0, term=1;
cout<<"Enter the number:";
cin>>num;
for(int n=num; n > 0; n /= 10 )
{
int digit = n % 10;
sum += digit * digit * digit;
}
cpphomeworkhelp.com
if( sum == num )
cout << "This is an Angstrom number" << endl;
else
cout << "This is not an Angstrom number" << endl;
return 0;
}
cpphomeworkhelp.com

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
7 functions
7  functions7  functions
7 functions
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
C++ theory
C++ theoryC++ theory
C++ theory
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 

Ähnlich wie CPP Homework Help

Ähnlich wie CPP Homework Help (20)

CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C++ loop
C++ loop C++ loop
C++ loop
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Python programing
Python programingPython programing
Python programing
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C important questions
C important questionsC important questions
C important questions
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
C code examples
C code examplesC code examples
C code examples
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Ch03
Ch03Ch03
Ch03
 
Ecs 10 programming assignment 4 loopapalooza
Ecs 10 programming assignment 4   loopapaloozaEcs 10 programming assignment 4   loopapalooza
Ecs 10 programming assignment 4 loopapalooza
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 

Mehr von C++ Homework Help (16)

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
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming 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 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
 

Kürzlich hochgeladen

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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
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
 
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
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
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
 
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
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Kürzlich hochgeladen (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
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 ...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
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
 
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
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).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
 
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
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

CPP Homework Help

  • 1. For any Homework 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/
  • 2. Ternary operator: The conditional operator ? : is also another way to evaluate conditions. It operates on three operands, and is thus known as a ternary operator. Here is an example of its usage: a = x < y ? x : y; In this example, if the test expression evaluates to true – i.e. if x < y – then the variable a will be assigned the value that the variable x contains. Otherwise, else it will be assigned the value of the variable y. The whole expression containing this operator (the right side of the assignment statement) is called a conditional expression, and the sub-expression before the question mark is called the test expression. If this test expression is true, the entire expression takes on the value of the expression immediately after the question mark. Otherwise, it takes on the value of the expression following the colon. Usually you only want to use this construct for choosing between two simple expressions for a particular value (both choices must be the same data type). Doing otherwise could run afoul of the syntactic rules of the operator and lead to syntax errors. You cannot, for instance, make the two options two different return statements. cpphomeworkhelp.com
  • 3. 1. Without running, explain what the following set of statements will do: bool input; int a; cin >> input >> a; char *choice = ( input ? "Hello World!" : "I love C++!" ); if( ( input ? a > 1 : a < 1 ) ) { cout << "Why did you pick "" << choice << ""?" << endl; } else { cout << "Yay, you picked " << choice; } 2. Convert this for loop to a do-while loop. int sum = 0; for( int i = 0; i < 5; i++ ) sum += i; cout << sum; cpphomeworkhelp.com
  • 4. break and continue: Two keywords often used with loops are break and continue. The break keyword causes the entire construct to exit, and control passes to the statement immediately after the body of the construct. For example: for( n = 10; n > 0; n-- ) { cout << n << ", "; if( n == 7 ) break; } In this example, only the first few values of n (the numbers 10, 9, 8) will be executed. As soon as the value of n becomes 7, the loop will exit. The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the next iteration: cpphomeworkhelp.com
  • 5. for( int n = 10; n > 0; n-- ) { if ( n == 7 ) continue; cout << n << ", "; } In this example, 10, 9, and 8 will again be displayed, but as soon as the value of n becomes 7, control will be passed to the beginning of the loop body. The rest of that iteration is skipped, and the rest of the countdown till 1, will be displayed (so the final output will be “10, 9, 8, 6, 5, 4, 3, 2, 1”). When you place a continue command within a for loop, the increase statement (in this case, n--) is executed before the loop iterates again. 3. Write a program to take in a number from a user, find its reciprocal and add it to a running sum (the running sum will be 0, when the loop initially starts). The program should repeat this procedure 10 times. However, if the user enters 0, the loop should exit, and if the user enters 1, nothing should be added. Print the final sum at the end of the program. cpphomeworkhelp.com
  • 6. switch statements: In lecture, the main type of conditional construct discussed was if-else. There is another type of construct, which is also sometimes used, called the switch-case construct. In this construct, the value of a variable is compared to a set of constants, and when a corresponding match is found, the statements associated with that case are executed. It is like an if-else construct that can only check for equality For example: switch(number) { case 3: cout << "Red"; break; case 2: cout << "Orange"; break; case 7: cout << "Black"; break; default: cout << "None of the above"; } cpphomeworkhelp.com
  • 7. In this switch statement, the value of the variable numberis compared with 3, 2 and 7, and if a match is formed, the corresponding color is printed. If no match is found then “None of the above” is printed. (The break keyword is required to end each caseblock except the last.) 4.Using the switch-case construct write a program to input two numbers, display the following menu, and then print according to the user’s choice: 1.Difference of two numbers 2.Quotient of two numbers 3. Remainder of two numbers For example, if the user inputs 1, then the difference of the two numbers should be printed. 5.Find the sum of the first n terms of the following series, where n is a number entered by the user: 6. Print the following pattern, using horizontal tabs to separate numbers in the same line. Let the user decide how many lines to print (i.e. what number to start at). cpphomeworkhelp.com
  • 8. 5 5 4 5 4 3 5 4 3 2 5 4 3 2 1 Series: As specified in lecture, nested loops are used when for one repetition of a process, many repetitions of another process are needed. Similar to patterns, nested loops can be used to print the sums of nested series. For example the sum of the series 1+(1+2) +(1+2+3) +... Can befoundbyaddingonetoarunningsumforeach execution of the inner loop, instead of printing them as you would for a pattern. As with patterns, the number of times the inner loop runs in this case depends on the value of the outer loop. 7. Write a program that inputs two numbers x and n, and find the sum of the first n terms of the following series: x +(x + x2)+(x + x2 + x3) + .... cpphomeworkhelp.com
  • 9. 8. An Angstrom number is one whose digits, when cubed, add up to the number itself. For instance, 153 is an Angstrom number, since 13 + 53 + 33 = 153 . Write a program to input a number, and determine whether it is an Angstrom number or not. Hint: The modulus operator (%) will be useful here. cpphomeworkhelp.com
  • 10. Problem 1: i. The program takes 2 inputs from the user, and stored it in variables input and a. ii. If the user has inputted the value of input as 1 a. the string choice will be assigned value “Hello World”. b. In the if-else construct, the condition returned to the if statement will be a>1. Now, if this condition is true (i.e if a>1), then Why did you pick "Hello World"? will be displayed. c. If this returned condition is false, (i.e if a<1), then Yay you picked Hello World will be displayed. iii. If the user has inputted the value of input as 0 a. the string choice will be assigned value “I love C++”. b. In the if-else construct, the condition returned to the if statement will be a<1. Now, if this condition is true (i.e if a<1), then cpphomeworkhelp.com
  • 11. Why did you pick "I love C++"? will be displayed. c. If this returned condition is false, (i.e if a>1), then Yay you picked I love C++ will be displayed. Problem 2: int sum = 0, i = 0; do { sum += i++; } while ( i < 5 ); Problem 3: #include <iostream> int main() { float a, sum=0; cpphomeworkhelp.com
  • 12. for(int i=1;i<=10;i++) { cout << "Enter the number whose reciprocal has to be added to the series:"; cin >> a; if( a == 0 ) break; else if ( a == 1) continue; else sum += 1 / a; } Problem 4: #include<iostream> using namespace std; int main() { int choice, a, b; cout << "Enter two numbers:"; cin >> a >> b; cout << "1. Difference of two numbers" << endl cpphomeworkhelp.com
  • 13. << "2. Quotient of two numbers" << endl << "3. Remainder of two numbers" << endl << "Enter your choice:"; cin >> choice; switch(choice) { case 1: if ( a > b ) // Optional check cout << "Difference is " << a - b << endl; else cout << "Difference is " << b - a << endl; break; case 2: if ( a > b ) // Optional check cout << "Quotient is " << a / b << endl; else cout << "Quotient is " << b / a << endl; break; case 3: if ( a > b ) // Optional check cout << "Remainder is " << a % b << endl; cpphomeworkhelp.com
  • 14. else cout << "Remainder is " << b % a << endl; break; default: cout << "Not an option!" << endl; } return 0; } Problem 5: #include <iostream> // For the second possibility below: #include <cmath> using namespace std; int main() { int n; float sum = 0, denom = 1; cout << "Enter the number of terms of the series:" << endl; cpphomeworkhelp.com
  • 15. cin >> n; for (int i = 1; i<=n; i++, denom += 2) { if( i % 2 == 0 ) sum += 1 / (denom * denom ); else sum -= 1 / (denom * denom ); } // or sum += pow(-1.0, i) * 1/( term * term) // Also, instead of adding to denom each time, the denominator can be // calculated as (2 * i – 1) cout<<"The sum is:"<<sum<<endl; return 0; } Problem 6: #include <iostream> using namespace std; int main() { cpphomeworkhelp.com
  • 16. int n; float sum=0,term=1; cout << "Enter the number of rows of the pattern:"; cin>>n; for (int i=n; i > 0; i--) { for(int j=n; j>=i; j--) cout << j << 't'; cout << endl; } return 0; } Problem 7: #include<iostream> // For the second possibility below: #include <cmath> using namespace std; int main() { cpphomeworkhelp.com
  • 17. int n,x, sum = 0; cout << "Enter the value for term 'x': "; cin >> x; cout << "Enter the sub-series of the main series: "; cin >> n; for(int i=1; i<=n; i++) { int term=1; for(int j=1; j<=i; j++) { term *= x; sum += term; } } /* Alternate possibility: for(int i=1; i<=n; i++) { for(int j=1; j<=i; j++) { sum += pow(x, b); } } cpphomeworkhelp.com
  • 18. */ cout << "The sum is:" << sum << endl; return 0; } Problem 8: #include <iostream> using namespace std; int main() { int num; float sum=0, term=1; cout<<"Enter the number:"; cin>>num; for(int n=num; n > 0; n /= 10 ) { int digit = n % 10; sum += digit * digit * digit; } cpphomeworkhelp.com
  • 19. if( sum == num ) cout << "This is an Angstrom number" << endl; else cout << "This is not an Angstrom number" << endl; return 0; } cpphomeworkhelp.com