0
#include <iostream>
using namespace std;

int main()
{
    int z=0, x=0, c=0;
    float res=0, a[9]={0,0,0,0,0,0,0,0,0};
    cout << "How many numbers do you want to divide: " << endl;
    cin >> z;
    for (x=0;x<z;x++)
    {
        c = x + 1;
        cout << "Enter the " << c << " number: " << endl;
        cin >> a[x];
    }
    for(x;x>0;x--)
    {
        res = a[x]/a[x-1];
        cout << a[x] << "\n";
    }
    cout << "Result = " << res << endl << "\n";
    return 0;
}

I am trying to get the user to input as many numbers as they want and divide those numbers, for example 8/2/2=2, but the result for a[0] is always 0 and I am not sure how to get the math right, all I could find is for dividing two numbers only, excuse my lack of knowledge I am new to this.

expected :

how many numbers do you want to divide: 
3
enter the 1 number:
8
enter the 2 number: 
2
enter the 3 number: 
2  
8
2
2
result = 2
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 1
    Please give your exact input, exact output, and expected output. – JohnFilleau Mar 01 '20 at 05:07
  • In the second for you're printing from values of array `a` from index z+1 to 1, and a[z+1] is always 0 because you don't read this position. Is that what you mean with a[0] is always 0? – Eduardo Pascual Aseff Mar 01 '20 at 05:09
  • This might be a good opportunity for you to practice [debugging small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – JaMiT Mar 01 '20 at 05:26

1 Answers1

0

First of all, if you want to ask the user how many numbers he wants, ask first and just then create the array

#include <iostream>
using namespace std;

int main(){
    int z = 0, x = 0;

    cout<<"How many numbers do you want to divide: "<<endl;
    cin>>z;

    float res = 0;
    float a[z];           //now you can reserve the correct amount of space

    for (x = 0; x < z; x++){
        cout<<"Enter the "<<(x + 1)<<" number: "<<endl;
        cin>>a[x];
    }

From what I understood, you want the user to write, for example [120, 5, 2, 3] that would result in (((120 / 5) / 2) / 3) = 4, so you store your division in res but you never use it again, this is wrong. Also you wrote for(x;x>0;x--), which doesn't make sense, it should be for(x = 1; x < z; x++):

    res = a[0];
    for(x = 1; x < z; x++){  //start from position 1!
        res = res /a[x];     //here you always store the result in res 
    }
    cout<<"Result = "<<res<<endl;
    return 0;
}

Just put both parts of the code together and it should work.

You can check the code here with any input you want. Try 3 8 2 2 and see that it outputs 2. The explanation for the input is you enter 3 to say that 3 numbers will be read. Then you enter 8 2 2 to have your 8/2/2 = 2.

Daniel
  • 7,357
  • 7
  • 32
  • 84
  • 3
    [Variable length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) are not part of the standard, so might be unsupported by the OP's compiler. – JaMiT Mar 01 '20 at 05:18
  • thank you for your answer , it still outputs result = 0 – Omar Mahmoud Mar 01 '20 at 05:34
  • Here the output is correct. Check [here](http://www.cpp.sh/5byp4t) and try the input `3 8 2 2`. It outputs `2`. – Daniel Mar 01 '20 at 05:58
  • 1
    Instead of promoting the usage of variable length arrays, which are **non-standard extensions**, use a `std::vector`, which is standard and portable. – L. F. Mar 01 '20 at 06:24