STARTING OUT WITH C++ MPL
9th Edition
ISBN: 9780136673989
Author: GADDIS
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 5, Problem 42RQE
Explanation of Solution
Purpose of the given code:
The purpose of the give code segment is to print “$9999” three times using two “for” loops.
Program:
The given code segment is highlighted below.
//Include required header files
#include <iostream>//Line 1
using namespace std; //Line 2
//Main function
int main()//Line 3
{//Line 4
//Loop from 1 through 5
for(int row =1; row <= 3; row++)//Line 5
{//Line 6
//Print the statement
cout<<"\n$";//Line 7
//Loop from 1 through 4
for(int digit = 1; digit <= 4;digit++)//Line 8
//Print the character '9'
cout<<'9';//Line 9
}//Line 10
//Return the statement
return 0; //Line 11
}//Line 12
Explanation:
- Include required header files on “Line 1” and “Line 2”.
- Define a function “main ()” on “Line 3”. Inside this function,
- “Line 5” loops from 1 through 5 using “for” loop.
- “Line 7” prints “\n$”.
- “Line 8” loops from 1 through 4 using “for” loop.
- “Line 9” prints “9”.
- “Line 11” returns the statement.
- “Line 5” loops from 1 through 5 using “for” loop.
Explanation of “for” loop:
- The outer “for” loop, loops from 1 through 3, during the first iteration, the value of “row = 1”...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Please answer it according to the code below
#include
#define MAX_EMP 5
struct Employee {
int id;
int age;
float salary;
};
int count = 0;
struct Employee emp[MAX_EMP];
void display() {
printf("\n---=== EMPLOYEE DATA ===---\n");
printf("EMP ID\tEMP AGE\tEMP SALARY\n");
printf("======\t=======\t==========\n");
for(int i = 0; i < count; i++) {
printf("%d\t%d\t%.2f\n", emp[i].id, emp[i].age, emp[i].salary);
}
}
void add() {
if(count == MAX_EMP) {
printf("\nERROR!!! Maximum Number of Employees Reached\n");
return;
}
printf("\nAdding Employee ===============\n");
printf("Enter Employee ID: ");
scanf("%d", &emp[count].id);
printf("Enter Employee Age: ");
scanf("%d", &emp[count].age);
printf("Enter Employee Salary: ");
scanf("%f", &emp[count].salary);
count++;
}
void update() {
int id, found = 0;
float newSalary;
printf("\nUpdate Employee Salary…
#ifndef lab5ExF_h
#define lab5ExF_h
typedef struct point
{
char label[10];
double x ; // x coordinate for point in a Cartesian coordinate system
double y; // y coordinate for point in a Cartesian coordinate system
double z; // z coordinate for point in a Cartesian coordinate system
}Point;
void reverse (Point *a, int n);
/* REQUIRES: Elements a[0] ... a[n-2], a[n-1] exists.
* PROMISES: places the existing Point objects in array a, in reverse order.
* The new a[0] value is the old a[n-1] value, the new a[1] is the
* old a[n-2], etc.
*/
int search(const Point* struct_array, const char* target, int n);
/* REQUIRES: Elements struct-array[0] ... struct_array[n-2], struct_array[n-1]
* exists. target points to string to be searched for.
* PROMISES: returns the index of the element in the array that contains an
* instance of point with a matching label. Otherwise, if there is
* no point in the array that its label matches the target-label,
* it should return -1.
* If there are more than…
Which XXX completes the following code?
public class MyGCDCalculator {
public static int findGCD(int inNum1, int inNum2) {
int tempVal;
if (inNum1
inNum2) {
tempVal = inNum1;
%3D
}
else {
if (inNum1 > inNum2) {
XXX
}
else {
tempVal
findGCD(inNum1, inNum2
inNum1);
%3D
}
}
return tempVal;
}
public static void main (String[] args) {
int num1
25;
int num2
15;
%3D
Chapter 5 Solutions
STARTING OUT WITH C++ MPL
Ch. 5.1 - How many lines will each of the following while...Ch. 5.1 - Prob. 5.2CPCh. 5.3 - What will each of the following program segments...Ch. 5.6 - In the following program segment, which variable...Ch. 5.6 - Find four errors in the following code that is...Ch. 5.6 - Write a sentinel-controlled while loop that...Ch. 5.7 - Prob. 5.7CPCh. 5.7 - Write a program segment with a do-while loop that...Ch. 5.7 - Revise your answer to Question 5.8 to use the...Ch. 5.8 - What three expressions appear inside the...
Ch. 5.8 - You want to write a for loop that displays I love...Ch. 5.8 - Prob. 5.12CPCh. 5.8 - Write a for loop that displays your name 10 times.Ch. 5.8 - Write a for loop that displays all of the odd...Ch. 5.8 - Write a for loop that displays every fifth number,...Ch. 5.8 - Write a for loop that sums up the squares of the...Ch. 5.8 - Write a for loop that sums up the squares of the...Ch. 5.8 - Write a for loop that repeats seven times, asking...Ch. 5.8 - Write a for loop that calculates the total of the...Ch. 5.11 - Which loop (while, do-while, or for) is best to...Ch. 5.11 - How many total stars will be displayed by each of...Ch. 5.11 - What will the following program segment display?...Ch. 5.12 - Prob. 5.24CPCh. 5.12 - What header file must be included in a program to...Ch. 5.12 - What five steps must be taken when a file is used...Ch. 5.12 - What is the difference between a text file and a...Ch. 5.12 - Prob. 5.28CPCh. 5.12 - What type of file stream object do you create if...Ch. 5.12 - What type of file stream object do you create if...Ch. 5.12 - If dataFi1e is an of stream object associated with...Ch. 5.12 - If dataFile is an ifstream object associated with...Ch. 5.12 - Assume you have an output file named numbers.txt...Ch. 5 - To _______ a value means to increase it by one.Ch. 5 - To _______ a value means to decrease it by one.Ch. 5 - Prob. 3RQECh. 5 - Prob. 4RQECh. 5 - The statement or block that is repeated is known...Ch. 5 - Each repetition of a loop is known as a(n)...Ch. 5 - A loop that evaluates its test expression before...Ch. 5 - A loop that evaluates its test expression after...Ch. 5 - A loop that does not have a way of stopping is...Ch. 5 - A(n) ______ is a variable that counts the number...Ch. 5 - Prob. 11RQECh. 5 - A(n) ________ is a variable that is initialized to...Ch. 5 - A(n) ______ is a special value that marks the end...Ch. 5 - The ________ loop is ideal for situations that...Ch. 5 - The _____ loop always iterates at least once.Ch. 5 - The _______and ______ loops will not iterate at...Ch. 5 - Inside the for loops parentheses, the first...Ch. 5 - A loop that is inside another is called a(n)...Ch. 5 - The _________ statement causes a loop to terminate...Ch. 5 - The _____ statement causes a loop to skip the...Ch. 5 - What header file do you need to include in a...Ch. 5 - What data type do you use when you want to create...Ch. 5 - What happens if you open an output file and the...Ch. 5 - What data type do you use when you want to create...Ch. 5 - What is a files read position? Where is the read...Ch. 5 - What should a program do when it is finished using...Ch. 5 - Write code that lets the user enter a number. The...Ch. 5 - Write a do-while loop that asks the user to enter...Ch. 5 - Write a for loop that displays the following set...Ch. 5 - Write a loop that asks the user to enter a number....Ch. 5 - Write a nested loop that displays the following...Ch. 5 - Write a nested loop that displays 10 rows of #...Ch. 5 - Rewrite the following code, converting the while...Ch. 5 - Rewrite the following code, replacing the do-while...Ch. 5 - Convert the following whi1e loop to a for loop:...Ch. 5 - Convert the following for loop to a while loop:...Ch. 5 - Complete the program segment below to write the...Ch. 5 - Complete the following program segment that reads...Ch. 5 - What will each of the following program segments...Ch. 5 - int x = 1 ; while (x 10) x++; cout x;Ch. 5 - Prob. 41RQECh. 5 - Prob. 42RQECh. 5 - Each of the program segments in this section has...Ch. 5 - A) // This code should use a loop to raise a...Ch. 5 - A) // This code should display the sum of two...Ch. 5 - Prob. 46RQECh. 5 - Characters for the ASCII Codes Write a program...Ch. 5 - Sum of Numbers Write a program that asks the user...Ch. 5 - Distance Traveled The distance a vehicle travels...Ch. 5 - Celsius to Fahrenheit Table In one of the Chapter...Ch. 5 - Speed Conversion Chart Write a program that...Ch. 5 - Ocean Levels Assuming the level of the Earths...Ch. 5 - Circle Areas The formula to compute the area of a...Ch. 5 - Pennies for Pay Write a program that calculates...Ch. 5 - Weight Loss If moderately active persons cut their...Ch. 5 - Calories Burned Running on a particular treadmill,...Ch. 5 - Membership Fees Increase A country club, which...Ch. 5 - Random Number Guessing Game Write a program that...Ch. 5 - Random Number Guessing Game Enhancement Enhance...Ch. 5 - The Greatest and Least of These Write a program...Ch. 5 - Student Line-Up A teacher has asked all her...Ch. 5 - Rate of Inflation The annual rate of inflation is...Ch. 5 - Population Write a program that will predict the...Ch. 5 - Math Tutor Version 3 This program started in...Ch. 5 - Hotel Suites Occupancy Write a program that...Ch. 5 - Rectangle Display Write a program that asks the...Ch. 5 - Diamond Display Write a program that uses nested...Ch. 5 - Triangle Display Write a program that uses nested...Ch. 5 - Arrowhead Display Write a program that uses nested...Ch. 5 - Sales Bar Chart Write a program that asks the user...Ch. 5 - Savings Account Balance Write a program that...Ch. 5 - Using FilesTotal and Average Rainfall Write a...Ch. 5 - Using FilesPopulation Bar Chart Write a program...Ch. 5 - Using FilesStudent Line Up Modify the Student...Ch. 5 - Using FilesSavings Account Balance Modification...
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Match the C-function on the left to the Intel assemble function on the right. W: cmpl $4 movl %edi , %edi jmp .L4(,%rdi,8) %edi .L3: movl $17, %eax ret .15: movl $3, %eax int A ( int x , int y) { int a ; if ( x == 0 ) else i f ( x == 1 ) a = 3 ; else i f ( x == 2 ) a = 2 0 ; else i f ( x == 3 ) a = 2 ; else i f ( x == 4 ) a = 1 ; ret .L6: a = 17; movl $20, %eax ret .L7: movl $2, %eax ret else a = 0; .L8: return a ; movl $1, %eax .L2: ret . section .rodata . L4: .quad .L3 .quad .L5 .quad .L6 .quad .L7 .quad .L8 X: testl %edi, %edi je cmpl je cmpl je стр1 je cmpl .L16 $1, %edi .L17 $2, %edi .L18 $3, %edi int B (int x, int y) { int a; switch (x) { .L19 $4, %edi %al movzbl %al, %eax case 0: a = 17; break; sete break; case 1: a = 3; case 2: a = 20; break; case 3: a = 2; break; case 4: a = 1; a = 0; } return a; ret .L16: break; movl $17, %eax ret .L17: movl $3, %eax } ret .L18: movl $20, %eax ret .L19: movl ret $2, %eaxarrow_forwardint FindSmallestVal() { int num = 0, min = 0; // reads num until the num > 0 while (num <= 0) { cin >> num; // finds the min value in the min,num min = num < min ? num : min; } // returns min return min; }arrow_forwardC# application that will allow users to enter values store all the expenses on that month in an array clothes, food, cool drinks, water, meatarrow_forward
- #include <stdio.h>#include<stdlib.h>struct Distance{int feet;float inch;} dist1, dist2, sum;int main(){struct Distance* distance1 = NULL; struct Distance* distance2 = NULL; struct Distance* distance3 = NULL; distance1 = (struct Distance*)malloc(sizeof(struct Distance*));distance2 = (struct Distance*)malloc(sizeof(struct Distance*));distance3 = (struct Distance*)malloc(sizeof(struct Distance*));printf("input feet for distance no.1:");scanf("%d", &distance1->feet); printf("input inch for distance no.1:");scanf("%f", &distance1->inch); printf("input feet for distance no.2:");scanf("%d", &distance2->feet); printf("input inch for distance no.2:");scanf("%f", &distance2->inch); printf("input feet for distance no.3:");scanf("%d", &distance3->feet); printf("input inch for distance no.3:");scanf("%f", &distance3->inch); printf("1st distance\n");printf("Enter feet: ");scanf("%d", &dist1.feet); printf("Enter inch: ");scanf("%f",…arrow_forwardint x1 = 66; int y1 = 39; int d; _asm { } mov EAX, X1; mov EBX, y1; push EAX; push EBX; pop ECX mov d, ECX; What is d in decimal format?arrow_forwardDice_Game.cpp #include <iostream>#include "Die.h" using namespace std; // a struct for game variablesstruct GameState { int turn = 1; int score = 0; int score_this_turn = 0; bool turn_over = false; bool game_over = false; Die die;}; // declare functionsvoid display_rules();void play_game(GameState&);void take_turn(GameState&);void roll_die(GameState&);void hold_turn(GameState&); int main() { display_rules(); GameState game; play_game(game);} // define functionsvoid display_rules() { cout << "Dice Game Rules:\n" << "\n" << "* See how many turns it takes you to get to 20.\n" << "* Turn ends when you hold or roll a 1.\n" << "* If you roll a 1, you lose all points for the turn.\n" << "* If you hold, you save all points for the turn.\n\n";} void play_game(GameState& game) { while (!game.game_over) { take_turn(game); } cout << "Game…arrow_forward
- Dice_Game.cpp #include <iostream>#include "Die.h" using namespace std; // a struct for game variablesstruct GameState { int turn = 1; int score = 0; int score_this_turn = 0; bool turn_over = false; bool game_over = false; Die die;}; // declare functionsvoid display_rules();void play_game(GameState&);void take_turn(GameState&);void roll_die(GameState&);void hold_turn(GameState&); int main() { display_rules(); GameState game; play_game(game);} // define functionsvoid display_rules() { cout << "Dice Game Rules:\n" << "\n" << "* See how many turns it takes you to get to 20.\n" << "* Turn ends when you hold or roll a 1.\n" << "* If you roll a 1, you lose all points for the turn.\n" << "* If you hold, you save all points for the turn.\n\n";} void play_game(GameState& game) { while (!game.game_over) { take_turn(game); } cout << "Game…arrow_forwardComplete the code: string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Honda"}; for(int i = 0; i < - ; i++) { cout << cars[i]< "\n"; } %3Darrow_forward#include <iostream>using namespace std;struct item{ int id; float price;} s[50];int size = 0;void addData() { cout << "Enter an item data " << endl; cout << "Enter id: "; cin >> s[size].id; cout << "Enter price: "; cin >> s[size].price; cout << "successfully added" << endl << endl; size++; for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { if (s[i].price > s[j].price) { struct item t = s[i]; s[i] = s[j]; s[j] = t; } } }}void retrivePrice() { int id; cout << "enter the element id:"; cin >> id; cout << endl; int i; for (i = 0; i < size; ++i) { if (id == s[i].id) { cout << "price for this item: " << s[i].price << endl << endl; break; } } if (i == size) cout…arrow_forward
- #include <iostream>using namespace std;struct item{ int id; float price;} s[50];int size=0; void addData(){ cout << "Enter an item data " << endl; cout << "Enter id: "; cin >> s[size].id; cout << "Enter price: "; cin >> s[size].price; cout<<"successfully added"<<endl<<endl; size++; for(int i=0;i<size;i++) { for(int j=i+1;j<size;j++) { if(s[i].price>s[j].price) { struct item t=s[i]; s[i]=s[j]; s[j]=t; } } }}void retrivePrice(){ int id; cout<<"enter the element id:"; cin>>id; cout<<endl; int i; for( i = 0; i < size; ++i) { if(id == s[i].id){ cout << "price for this item: " << s[i].price << endl<<endl;…arrow_forwardJSON Response example when code = BAHRAIN as follows: { "Country": "Bahrain", "Tenps"il ("day": "SUNDAY", "temp"i {"day": "HONDAY", "temp": 35), ("day": "TUESDAYr, "teng": 39), ("day": "WEDNESDAY", "tenp": 37),. ("day"i "THURSDAY", "tenp": 35), ("day"i "FRIDAV", "temp": 34), ("day": "SATURDAY", "teng": 37) 1. Implement find Templ ) function as follows: Sample Eample This function will be called whenever the user change the value of the pull-down menu. This function should contact Ve the Web Service API, sending the selected country code, to Co display the information (country, daily temperatures in the week and average weekly temperature) inside the div tag whose id -result as shown in the example on the right. Country ahraln In addition, the country name and the average temperatures should be saved as one JS object inside an array in a local storage. Whenever new request is made, the countrylaverage will be saved and added to the array inside the local storage (duplicates is allowed).…arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage