SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente (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
 

Similar a CPP Homework Help

Similar a 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
 

Más de 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
 

Último

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 

Último (20)

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 

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