ardym ardym - 3 months ago 7
C++ Question

c++ , if else statement doesn't work , where is my mistalke?

in the following code
it should be printed how many times is every element appears in an array
but it seems that i have a mistake in my code, that i couldn't recognize
the numbers 0.3 and 0.6 are counted as 0.0
i would be grateful if i get some help.

void getTotalA(int n, float a[], int b[]);
int main()
{
float a[]={0.0,1.0,0.0,0.0,0.6,0.3,1.0,0.3,0.6};
int b[4]={0,0,0,0};
getTotalA(9,a,b);
return 0;
}
void getTotalA(int n, float a[], int b[]) {
for(int i=0; i < numOfStudents; i++){
if(a[i]== 1.0){
b[3]++;
}
else if(a[i]== 0.6){
b[2]++;
}
else if(a[i]== 0.3){
b[1]++;
}
else{
b[0]++;
}
}
cout << "Printing the array : " << endl;
for (int i = 0;i < 4 ; i++)
{
cout << b[i] << endl ;
}
}

Answer

0.3 is not float. It has double type.

0.3 != 0.3f

http://cpp.sh/9ewib

Comments