Question

1. Create a new project. Type in the following program. Add a comment at the top...

1. Create a new project. Type in the following program. Add a comment at the top to include your name and the date.
2. Compile and Run with different values. What data values should you choose? Think about what we discussed in class.
3. Add code to compute the modulus. Hint: you will also have to declare a new variable.
//this program demonstrates the use of various operators
#include <iostream >
using namespace std;
int main()
{
int num1;
int num2;
int sum;
int diff;
int prod;
float quot;
cout << "Enter two integer values" << endl;
cin >> num1 >> num2;
sum = num1 + num2;
cout << num1 << " + " << num2 << " is " << sum << endl;
diff = num1 - num2;
cout << num1 << " - " << num2 << " is " << diff << endl;
prod = num1 * num2;
cout << num1 << " * " << num2 << " is " << prod << endl;
​quot = (float) num1 / num2;
cout << num1 << " / " << num2 << " is " << quot << endl;
return 0;
}
4. Answer the following questions:
• What data values should you choose to test the program? Think about what we discussed in class.
• What happens when num2 is 0? Why?
• What does this tell you about using division or modulus in your programs?

5. Add the following lines to correct the divide by zero problem. Compile and run with different values to ensure it works correctly.
if (num2 == 0)
{
​​ cout << “cannot divide by zero!” << endl;
}
else
{
​​…
}
6. Submit program, output and questions as one .doc file on Blackboard.

Homework Answers

Answer #1

Question No 3:

float mod;

mod = num1 % num2;
cout << num1 << " % " << num2 << " is " << mod << endl;

Question No 4:

When we put 0 in number 2 case the program halts it gives infinity to division operator and crashes for modulus.

Question No 6:

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP

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
Consider the following program: #include #include #include using namespace std; int main() { int num1; int...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int num2; cout << fixed << showpoint << setprecision(2); cout << "Enter two integers: "; cin >> num1 >> num2; cout << endl; if (num1 != 0 && num2 != 0) cout << sqrt(abs(num1 + num2) + 0.5) << endl; else if (num1 != 0) cout << floor(num1 + 0.5) << endl; else if (num2 != 0) cout << ceil(num2 + 0.5) << endl; else...
Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide...
Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide and clear. Test your calculator by writing a main program incorporating the test code below: Calculator mycalc; mycalc.clear(); mycalc.add(4.52); mycalc.add(3.789); mycalc.divide(2.6); mycalc.multiply(3.12); mycalc.subtract(2.678); cout << mycalc.display() << endl;       // prints out "7.2928" mycalc.clear(); mycalc.add(5.0); cout << mycalc.display() << endl;       // prints out "5" //advanced stuff #1: add a constructor Calculator calc1; cout << calc1.display() << endl;  //prints out 0 //advanced stuff #2: add a parameterized...
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
Consider the following C++ program: #include <iostream> #define PRIORITY 10 void main() {             int tableInd =...
Consider the following C++ program: #include <iostream> #define PRIORITY 10 void main() {             int tableInd = 20;             int arrayInd = 30;             cin >> tableInd;             if (tableInd > 50)                arrayInd = PRIORITY;             else                arrayInd = setindex(PRIORITY);             cout << arrayInd; } Answer the following questions: a) At what time is the keyword void bound to its meaning? Language design         Language implementation     Compile          Link    Load    Run b) At what time is the value of PRIORITY bound? Language design         Language implementation     Compile          Link    Load    Run c) At what...
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...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… The program should now determine the number of classes, and should do so by generating a unique, random number. Replace taking user input for the number of rooms with a computer generated number...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i is saved in the array ascend[], in order. Compile and run the program; it should print 0 4 5. In this exercise, you’ll try to translate the C++ program to MIPS. Some of the more tedious parts are already given in Assignment3.F19.s. You won’t have to write the data allocation sections, some of the initializations, and the output loop at the end. So the...
1) What do you think the following program will print if you type from the keyboard...
1) What do you think the following program will print if you type from the keyboard the numbers 3 for a and 8 for b ? #include < iostream> using namespace std; int main() { int a, b; cin >> a >> b; if (a > b && b > 7) { if(a < 7) cout << “ a less than 7: “ << a << ‘\n’; else cout << “Else branch executed\n”; return 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...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT