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 cout << "Please enter the first number:" << endl; cin >> num1; cout << "Please enter the second number:" << endl; cin >> num2; cout << "please enter the third number:" << endl; cin >> num3; //sorting the numbers if (num1 < num2 && num1 < num3) { lowest = num1; if (num2 > num3) { highest = num2; middle = num3; } } if (num1 < num2 && num3 < num1) { lowest = num1; if (num2 < num3) { middle = num2; highest = num3; } } if (num1 > num2 && num3 > num1) { middle = num1; if (num2 < num3) { lowest = num2; highest = num3; } } if (num1 < num2 && num3 <num1) { middle = num1; if (num2 > num3) { highest = num2; lowest = num3; } } if (num1 > num2 && num1>num3) { highest = num1; if (num3 > num2) { middle = num3; lowest = num2; } } if (num1 > num2 && num1 > num3) { highest = num1; if (num2 > num3) { middle = num2; lowest = num3; } } //output to user
cout << "The number you entered in ascending orders are: " << lowest << " , " << middle << " , and " << highest << endl;
return 0;
}
/*
* File Name : sort.cpp
*
* There is a chance for your program to get errors
* because when the input is 1,2,3
* in first if block lowest will be set and the remaining middle and
highest
* are not set and printing them will results in garbage
values.
*
* so i have changed your code little bit.
* consider the possibilities of sorting order of 3 variables
NUM1,NUM2,NUM3
NUM2,NUM1,NUM3
NUM3,NUM1,NUM2
NUM1,NUM3,NUM2
NUM2,NUM3,NUM1
NUM3,NUM2,NUM1
*
* Now first check num1 is both less than num2,num3
* if less than, lowest = num1,
* check if num2 < num3
* then middle =
num2, highest = num3.
* if not
* middle = num3,
highest = num2.
* Above block checks the two permuations -> NUM1,NUM2,NUM3,
NUM1,NUM3,NUM2
*
* if above block not satisfied check num2 is both less than
num1,num3
* if less than, lowest = num2,
* check if num1 < num3
* then middle =
num1, highest = num3.
* if not
* middle = num3,
highest = num1.
*
* Above block checks the two permuations -> NUM2,NUM1,NUM3,
NUM2,NUM3,NUM1
*
* if above block not satisfied check num3 is both less than
num1,num2
* if less than, lowest = num3,
* check if num1 < num2
* then middle =
num1, highest = num2.
* if not
* middle = num2,
highest = num1
* Above block checks the two permuations -> NUM3,NUM1,NUM2,
NUM3,NUM2,NUM1
* so all the 6 permutations are completed with 3 blocks.
* comment if you have any doubts.
*/
#include <iostream>
using namespace std;
int main()
{
//variables
int num1, num2, num3;
int lowest, middle, highest;
//user inputs
//just added for loop to display all the 6 possible
inputs
//if you wish to ask input 6 times remove comment of
statements
//start with --->
//--->for(int i = 0;i<6;i++){
//--->cout<<"\nPermutation Check -
"<<i+1<<endl<<endl;
cout << "Please enter the first number:"
<< endl;
cin >> num1;
cout << "Please enter the second number:"
<< endl;
cin >> num2; cout << "please enter the
third number:" << endl;
cin >> num3;
//sorting the numbers
/*
* use nested if else if.
* it removes confusion and on entering a block
* we will set 3 variables values, not to check other
blocks.
*/
if (num1 < num2 && num1 < num3)
{
lowest = num1;
if (num2 < num3)
{
highest =
num3;
middle =
num2;
}
//need an else statement to set
highest and middle
//if num2 <= num3
else{
highest = num2;
middle = num3;
}
}
else if (num2 < num1 && num2 <
num3)
{
lowest = num2;
if (num1 < num3)
{
middle =
num1;
highest =
num3;
}else{
middle =
num3;
highest =
num1;
}
}
else if (num3 < num2 && num3 <
num1){
lowest = num3;
if (num1 < num2)
{
middle =
num1;
highest =
num2;
}else{
middle =
num2;
highest =
num1;
}
}
//output to user
cout << "The number you entered in ascending
orders are: " << lowest << " , " << middle
<< " , and " << highest << endl;
//--->}
return 0;
}
//OUTPUT and screenshots of code.
So the assignment is to write a program that have the user entered 3 numbers and...
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...
#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 <<...
Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: "; cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2; 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; ...
(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...
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...
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...
So far your TestEqual functions have tested values but haven't made any changes... Q: If you changed one of the variable values in your function, would it change the value of the variable in main? Why? Q: What are the two options for passing parameters into a function? (by _________ and by __________) What's the difference? Write a new function, MakeEqual, that takes two integers and if they are not equal (tested using your TestEqual function), displays a message and...
Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...