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
1. Type in the following program. Compile and run. #include <iostream> using namespace std; int main()...
1. Type in the following program. Compile and run. #include <iostream> using namespace std; int main() { ​cout << "Hello, world." << endl; } Open a Word Document and copy and paste the program and the output. 2. Create a new program: #include <iostream> using namespace std; int main() { ​float num1; ​float num2 ​float avg; ​cout << "Enter two numbers" << endl; ​cin >> num1 >> num2; ​avg = num1 + num2/2; ​cout << "The average is " <<...
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...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
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; }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT