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;
}
elsebreak;
}
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.
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..
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?
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.
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.
void functiondivide(){
longint 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.