This is a fill in the code type:
// FILL IN THE CODE - Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order.
#include <iostream>
using namespace std;
int main() {
int num1; // holds 1st number
int num2; // holds 2nd number
int result; // holds result of multiplication
int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number
int *num2Ptr = nullptr; // int pointer which will be set to point to the 2nd number
cout << "Please input the 1st number" << endl;
cin >> num1;
cout << "Please input the 2nd number" << endl;
cin >> num2;
// Fill in code to make num1Ptr point to num1 (hold its address)
// Fill in code to make num2Ptr point to num2 (hold its address)
num1Ptr = &num1;
num2Ptr = &num2;
// Fill in code to find the result by using only the pointer variables
result = *num1Ptr * *num2Ptr;
cout << "The result is " << result << endl;
if (*num1Ptr > *num2Ptr)
cout << "The 1st number is greater than the 2nd number" << endl;
else if (*num2Ptr > *num1Ptr)
cout << "The 2nd number is greater than the 1st number" << endl;
else
cout << "The 2nd number and 1st number are the same" << endl;
return 0;
}
I have filled out all the ones that needs to be filled.
It says "Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order."
It's already printing out the results of the two numbers multiplied, but I'm not quite sure how to print put the original numbers in ascending order. Let's say, the user inputs 8 then 10. it should display 8, 10. Thank you.
#include <iostream> using namespace std; int main() { int num1; // holds 1st number int num2; // holds 2nd number int result; // holds result of multiplication int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number int *num2Ptr = nullptr; // int pointer which will be set to point to the 2nd number cout << "Please input the 1st number" << endl; cin >> num1; cout << "Please input the 2nd number" << endl; cin >> num2; // Fill in code to make num1Ptr point to num1 (hold its address) // Fill in code to make num2Ptr point to num2 (hold its address) num1Ptr = &num1; num2Ptr = &num2; // Fill in code to find the result by using only the pointer variables result = *num1Ptr * *num2Ptr; cout << "The result is " << result << endl; if (*num1Ptr > *num2Ptr) { cout << "The 1st number is greater than the 2nd number" << endl; cout << "Numbers in ascending order is " << *num2Ptr << ", " << *num1Ptr << endl; } else if (*num2Ptr > *num1Ptr) { cout << "The 2nd number is greater than the 1st number" << endl; cout << "Numbers in ascending order is " << *num1Ptr << ", " << *num2Ptr << endl; } else { cout << "The 2nd number and 1st number are the same" << endl; cout << "Numbers in ascending order is " << *num2Ptr << ", " << *num1Ptr << endl; } return 0; }
Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...
SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){ int num1,num2; cout << "Enter two numbers "<< endl; cout << "First :"; cin >> num1; cout << "Second :"; cin >>num2; int result=num1+num2; cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result; ...
So the assignment is to write a program that have the user entered 3 numbers and than it will sort it by ascending order. Here are the code I wrote, but it won't compiled and I kept on getting an error message "Using uninitiazed memory" for all the variables. Can someone please help take a look and see whats going on? #include <iostream> using namespace std; int main() { //variables int num1, num2, num3; int lowest, middle, highest; //user inputs...
#include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...
C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...
I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...
What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...
may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...
Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...
(C++) What is the best way to validate for the two user inputs in the followig for loop? Would like it to say "That value is not allowed. Enter another value." , if 1 ) numbers are not within range 2) a blank is entered 3) other characters (that are not numbers) are entered and 4) numbers with decimals or negatives are entered. int num1; int num2; for(int i = 1; i <= 4; i++) { cout << "Enter number...