How can produce division

This is part of what I'm working...

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
31
32
33
34
35
36
37
38
39
40
41
void functiondivide(){

  int size = 50;

     float iarray[size];

     for (int i=0; i<size; i++){
         iarray[i]=0.0;
         }


     float number = 0.0;

     float total = 0.0;

     for(int i=0; i<size; i++){

    cout<< "Enter a number to divide, Enter -1 to get total.\n";
     cin >> number;

    if(number!=-1){

     iarray[i]= number;
     }
     else
     break;
     }

     for(int i=0; i<1 ; i++){

         total = iarray[i];

         for(int s = 1; s<size; s++)

                  total /= iarray[s];
                  }


                  cout <<"\nThe total is : " <<total<<"\n\n\n"<<endl;

     }


I can't get the correct answer, instead it gives me this.

The total is : 1.#INF

3
4
5
int size = 50;

float iarray[size];


That isn't even valid C++
Last edited on
I had it, as an "int" it was producing "1.#INF". So I changed it into float thinking it will work, but it does the same thing.. either way.
I mean the fact that you created an array with the size being a non-const variable.
Ohh right, this yeah I know about that.. I was thinking you said you can't make arrays of float, because I always thought you can make array of anything..

AnyWays though.
Last edited on
You are dividing by 0
Yeah I should throw and exception if the user enters in zero... But even when I don't enter zero it still gives me "1.#INF"
Indent your code.
What is the correct answer? (ie what are you trying to do?)
for(int i=0; i<1 ; i++) This is not a loop. It will be only executed once.
1
2
3
4
5
for (int i=0; i<size; i++)
   iarray[i]=0.0;
//...
for(int s = 1; s<size; s++)
    total /= iarray[s]; //divided by zero 


What is the purpose of having functions if you put all the code in just one?
Last edited on
I am trying to put, total = iarray[i]; so I don't divide by zero. Yeah I guess I don't need the for statement.

Nah Im just testing this out, Im not really making it offical. Yeah I make a class with member functions for all that,

but I need to get this division thing working first.
Last edited on
a /= b; //equivalent to a = a/b
Yes I know.
If you know that you are dividing by 0, then what do you mean by this?
Analyzer wrote:
I am trying to put, total = iarray[i]; so I don't divide by zero.
Because, total was given the value 0.0 at initilization, so in order for me to divide properly, I gave total the first value that the user entered so that can be divided by the second value later on in the loop.
Last edited on
iarray might have zeros in the end since you initialize it with zeros but the user may not enter all 50 numbers. You need to keep track of how many numbers were entered or make a check to stop when you get to a 0.
Last edited on
I tell the user, if he wants to get the total.. to enter -1.. Thats something I need to fix.

And if the user enters zero fixed up my code to throw an exception, which would prompt him about it and restart the function over again.
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
void functiondivide(){

  long int num1, num2, total;

  int remainder;

    cout<< "Enter a number to divide.\n";

     cin >> num1;

     cout<<"\nEnter another number to divide.\n";

    cin>>num2;

     if((num1 == 0) || (num2 == 0)){
     cout <<"\n\nYou cant divide by zero! \n\n";
     functiondivide();
     }
    else

    total = num1 /num2;

    remainder = num1 % num2;

                  cout <<"\nThe total is: " <<total<< ". The remainder is: "<<remainder<<"\n\n\n"<<endl;

     }


Im deciding to go with this because, it works better, and its simplier.
Last edited on
Topic archived. No new replies allowed.