+ 3
How to do this code in c++?
four input values add the three minors together and divide them into the largest
12 Answers
+ 2
*EDIT*
Updated code to include the remainder in the answers.
Best of luck to you!
https://code.sololearn.com/cqiNO3gtyVAs/#cpp
#include <iostream>
using namespace std;
int main() {
// We'll use array to store all of our numbers, easier to work with
int numArray[4] = {0,0,0,0};
// This will hold our largest number
int max = 0;
// This will hold the index that the largest number was at
int pos = -1;
int sum = 0;
// Don't forget to enter all 4 inputs at runtime.
// EXAMPLE:
// 12
// 35
// 78
// 5
cout<<"Please enter 4 numbers, separated by new line (SoloLearn): "<<endl;
cin>>numArray[0];
cin>>numArray[1];
cin>>numArray[2];
cin>>numArray[3];
// get largest num. Check if current number is greater than max, if it is then store it in max.
// at the end we'll have largest number stored in max, and its index position stored in pos.
for(int i = 0; i < 4; ++i) {
if(numArray[i] > max){
max = numArray[i];
pos = i;
}
}
// Display the largest number and its index
cout<<"Largest number is: "<<max<<" (Index "<<pos<<")"<<endl;
// Lets loop through our numbers again, and use pos to skip over our largest number.
// If it isn't the largest number, add together so we can divide after loop.
for(int i = 0; i < 4; ++i){
if(i != pos){
sum += numArray[i];
}
}
cout<<numArray[pos]<<" divided by "<<sum<<" is "<<(numArray[pos]/sum)<<" (Remainder: "<<(numArray[pos]%sum)<<")"<<endl;
return 0;
}
::::::::::OUTPUT::::::::::::
Please enter 4 numbers, separated by new line (SoloLearn):
Largest number is: 78 (Index 2)
78 divided by 12 is 6 (Remainder: 6)
78 divided by 35 is 2 (Remainder: 8)
78 divided by 5 is 15 (Remainder: 3)
+ 1
@GiltDrake Read through my code and you should be able to reconfigure it easily to do exactly what you're asking at this point. Unfortunately, you didn't give any of those specifications in your original post, so I did exactly what you asked for. I don't have time to redo it because you decided on something new after the work was already done.
However, this is a great lesson about the corporate world. As a programmer, even though you told me what you wanted, I should have assumed you didn't tell me exactly what you wanted and I should have given you an interview prior to starting so that all specifications are known. This happens all of the time, and as the programmer, I should have made sure you actually gave me the information required for what you were wanting in your head. I suppose a lesson on your end is to ask for exactly what you want instead of being vague.
Either way, best of luck to you!
*EDIT*
I'm feeling nice, so I updated my code to include adding the smaller numbers together prior to dividing against the largest number. It'll be up to you to convert my loops into IF/ELSE statements though.
+ 1
Updated code. In future, list all of your specifications so you don't waste other's time needlessly.
https://code.sololearn.com/cpOMFxa97Gvv/#cpp
int main() {
// We'll use array to store all of our numbers, easier to work with
int numArray[4] = {0,0,0,0};
// This will hold our largest number
int max = 0;
// get index pos of max
int pos = -1;
// Smaller numbers added together before division
int sum = 0;
// Don't forget to enter all 4 inputs at runtime.
// EXAMPLE:
// 12
// 35
// 78
// 5
cout<<"Please enter 4 numbers, separated by new line (SoloLearn): "<<endl;
cin>>numArray[0];
cin>>numArray[1];
cin>>numArray[2];
cin>>numArray[3];
// Check each element in array against the max. If it's greater,
// store it as the new max. While you're at it, grab the position
// of the max element so we can use in calculations later
if(numArray[0] > max){
max = numArray[0];
pos = 0;
}
if(numArray[1] > max){
max = numArray[1];
pos = 1;
}
if(numArray[2] > max){
max = numArray[2];
pos = 2;
}
if(numArray[3] > max){
max = numArray[3];
pos = 3;
}
cout<<"Largest number is "<<max<<". (Index "<<pos<<")"<<endl;
// Check if current element is the pos of the max element.
// If it is, skip it and only add the other numbers together.
if(numArray[0] != numArray[pos]){
sum += numArray[0];
}
if(numArray[1] != numArray[pos]){
sum += numArray[1];
}
if(numArray[2] != numArray[pos]){
sum += numArray[2];
}
if(numArray[3] != numArray[pos]){
sum += numArray[3];
}
// Divide largest against the sum of the smaller numbers, print remainder with it.
cout<<numArray[pos]<<" divided by "<<sum<<" is "<<(numArray[pos]/sum)<<". (Remainder: "<<(numArray[pos]%sum)<<")"<<endl;
return 0;
}
0
wrong new line(( sorry. but code is true
0
ops
0
you need reverse my code. I thing the largest divide into 3 smaller
0
reverse denominator on numerator in cout
0
sorry i don't understand
0
I correct my code
- 1
int x,x1,x2,y;
cin>>x;
cin>>y;
cin>>x1;
cin>>x2;
if (y>x)
{
if (y>x1)
{
if(y>x2)
cout<<x1/y<<x/y<<x2/y
else
cout<<x1/x2<<x/x2<<y/x2;
}
else
cout<<y/x1<<x2/x1<<x/x1;
}
else
cout<<x1/x<<y/x<<x2/x;
}
- 1
four input values add the three minors together and divide them into the largest
- 1
Only with the if command