Get the quotient with decimal using for loop

May 26, 2022 at 11:18am
this has been bugging me for a quite a while now, i'm confused how to get the proper formula to get the correct output

here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  int quotient()
{
	cout << endl;
	cout << "======================= QUOTIENT =======================\n";
    double n, num, quo=1.000;
    cout<<"How many numbers you want to input? ";
    cin>>n;
    for(int i=0; i<n; i++)
    {
    	cout << i+1 << ". ";
        cin>>num;
        quo = quo/num;
    }
    cout<<"\nThe quotient of the "<<n<<" numbers you entered is "<<quo;
    cout<<endl;
    return 0;
}

May 26, 2022 at 11:26am
??? The quotient of 2 numbers is the whole number of times the numerator can be divided by the denominator. eg for 7 / 3, the quotient is 2.

So how does this fit with trying to find the quotient of an arbitrary number of decimal point numbers??

Perhaps if you explained more what you're trying to do.
May 26, 2022 at 11:29am
i'm getting a wrong numbers when i trying to calculate something
here's the output


======================= QUOTIENT =======================
How many numbers you want to input? 3
1. 90
2. 90
3. 90

The quotient of the 3 numbers you entered is 1.37174e-06
May 26, 2022 at 11:33am
But what output are you expecting? What definition of 'quotient' are you using to apply to multiple numbers?
May 26, 2022 at 11:35am
the exact calculations of the user-input numbers
May 26, 2022 at 11:40am
Yes. But what is the 'exact' calculation you are expecting? What answer are you expecting from the given numbers? Why is 1.37174e-06 not considered 'correct'?
May 26, 2022 at 11:47am
I mean

this is my expectations

======================= QUOTIENT =======================
How many numbers you want to input? 2
1. 90
2. 9


The quotient of the 3 numbers you entered is 10



======================= QUOTIENT =======================
How many numbers you want to input? 3
1. 90
2. 9
3. 3


The quotient of the 3 numbers you entered is 3.333
May 26, 2022 at 11:51am
Apparently, quo needs to be initialised as the FIRST number, not as 1.0

Presumably your last example implies that you want 90 / 9 / 3. But it's a daft problem and the "quotient" really only corresponds to an outcome of a single division operation.
May 26, 2022 at 12:12pm
Do you mean this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

void quotient() {
	cout << "\n======================= QUOTIENT =======================\n";

	double quo {1.0};
	unsigned n {};

	cout << "How many numbers you want to input? ";
	cin >> n;

	for (unsigned i {}; i < n; ++i) {
		double num {};

		cout << i + 1 << ". ";

		if (i) {
			cin >> num;
			quo /= num;
		} else
			cin >> quo;
	}

	cout << "\nThe quotient of the " << n << " numbers you entered is " << quo << '\n';
}

int main() {
	quotient();
}



======================= QUOTIENT =======================
How many numbers you want to input? 3
1. 90
2. 9
3. 3

The quotient of the 3 numbers you entered is 3.33333

Last edited on May 26, 2022 at 12:19pm
May 26, 2022 at 1:07pm
quo was initialized as 1.0 first. So when you input numbers. For example,
1. 90
2. 9
3. 3
It actually executed like this: 1.0 / 90 / 9 / 3 . That's not what you expected.
May 26, 2022 at 1:46pm
ohhh i just realized that, thank you! i solved my problem thanks ningfan666
Topic archived. No new replies allowed.