Question

#include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) // declaration of main...

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[]) // declaration of main function
{
int limit, // a user entered limit for a loop
step, // a user entered step value
x; // control variable for the loop
cout << "Counting up from 1 to 10\n";
// The control variable x takes on values from 1 to 10
for (x = 1; x <= 10; x++) // for loop to count from 1 to 10 values incremented by 1
cout << x << " "; // print the "x" value
cout << "\n\nCounting down from 10 to 1\n";
for (x = 10; x >= 1; x--) // for loop to count from 10 to 1 values decremented by 1
cout << x << " "; // prints x value
cout << "\n\nEnter a top counting value: ";
cin >> limit; // reads the limit value from user
cout << "\n\nCounting up from 1 to " << limit << "\n";
for (x = 1; x <= limit; x++) // for loop to count from 1 to given limit value incremented by 1
cout << x << " "; // prints x value
cout << "\n\nCounting up from 1 to " << limit << " by 2\n";
for (x = 1; x <= limit; x+=2) // for loop to count from 1 to given limit value incremented by 2
cout << x << " "; // prints x value
cout << "\n\nEnter a step value for counting: ";
cin >> step; // reads step value
cout << "\n\nCounting up from 1 to " << limit << " by "
<< step << "\n";
for (x = 1; x <= limit; x+=step) // for loop to count values from 1 to limit and incremented by the given step value
cout << x << " "; // prints x value
cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}

  1. What happens when a negative number is entered for the top counting value? Why?
  2. What happens when a negative number is entered for the step value? Why?
  3. Use x != limit for the Boolean expression in the last for statement. Sometimes this works and sometimes it doesn’t.
  4. Give values for limit and step when this works.

limit:                                              step:

  1. Give values for limit and step when it does not work.

limit:                                              step:

  1. What does the program do when it does not work? Why?

d. What does the above tell you about using a less than/greater than relation for Boolean expressions in loops vs. using an equality/inequality relation?

Homework Answers

Answer #1

Answer a

When a negative number is entered for the top counting value, for loop initial condition (1 <= limit) becomes false. So it does not print anything.

When a negative value is entered for the step value, for loop condition will always be true (x <= limit). it will become infinite loop. For example limit = 45 and step = -2 so every time x is reduced by 2 and it will make condition (x<=limit) as true. Infinite loop

Limit = 9 and step = 2 this will work.

Answer b

When Limit = 10 and step = 2 This will not work.

Answer c

This will go into infinite loop.

Answer d

LessThan/GreaterThan condition stops the execution of loop when it reaches the limit. But Equality/Inequality condition might skip the limit value and get program into infinite loop.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout...
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout << "Welcome to the Calorie Count-ulator!\n"; // Get user input: cout << "Enter your meal choice ([P]izza, [S]alad, [H]amburger)\n"; cin >> meal_choice; cout << "Enter the amount of servings (1-9):\n"; cin >> servings; // TODO: Use a switch statement to evaluate the user's meal choice // Handle error checking where appropriate // Exit the program: return 0;}
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
Language : C ++ #include #include #include #include #include using namespace std; void DisplayMenu() {   ...
Language : C ++ #include #include #include #include #include using namespace std; void DisplayMenu() {    cout << "1. E games\n";    cout << "2. T games\n";    cout << "3. M games\n";    cout << "4. Total Games\n";    cout << "5. Exit\n"; } double total(double egames, double tgames, double mgames) {    int totalgames = egames + tgames + mgames;    cout << "There are " << totalgames << " games\n";    return totalgames; } double Egames(double egames,...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...
What is value ofmyInt? #include <iostream> using namespace std; int main() {    int myInt;   ...
What is value ofmyInt? #include <iostream> using namespace std; int main() {    int myInt;    int* myScore;    int myVar;       myInt = 10;    myScore = &myInt;    myVar = 20;    myVar = *myScore;    *myScore = 30;    myVar = 40;    myVar = *myScore;    } Group of answer choices 30 20 10 0
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You must...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
If and else statements: 1a.) Given the following declaration: int temperature; Write an if statement that...
If and else statements: 1a.) Given the following declaration: int temperature; Write an if statement that prints the message "The number is valid" if the variable temperature is within the range of -50 trough 150 (both values excluded). 1b.) Given the following declarations int x; int y; Write an if statement that assigns 100 to x when y is equal to 0. 1c.) Given the following declaration: int grade; Write an if statement that prints the message "The number is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT