Я изучаю Objective-C, выполнил простую программу и получил неожиданный результат.Эта программа - просто тест таблицы умножения ... Пользователь вводит количество итераций (тестовых вопросов), а затем вводит ответы.Это после того, как программа отображает количество правильных и неправильных ответов, процент и принятый / неудачный результат.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv []) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Welcome to multiplication table test");
int rightAnswers; //the sum of the right answers
int wrongAnswers; //the sum of wrong answers
int combinations; //the number of combinations@
NSLog(@"Please, input the number of test combinations");
scanf("%d",&combinations);
for(int i=0; i<combinations; ++i)
{
int firstInt=rand()%8+1;
int secondInt=rand()%8+1;
int result=firstInt*secondInt;
int answer;
NSLog(@"%d*%d=",firstInt,secondInt);
scanf("%d",&answer);
if(answer==result)
{
NSLog(@"Ok");
rightAnswers++;
}
else
{
NSLog(@"Error");
wrongAnswers++;
}
}
int percent=(100/combinations)*rightAnswers;
NSLog(@"Combinations passed: %d",combinations);
NSLog(@"Answered right: %d times",rightAnswers);
NSLog(@"Answered wrong: %d times",wrongAnswers);
NSLog(@"Completed %d percent",percent);
if(percent>=70)NSLog(@"accepted");
else
NSLog(@"failed");
[pool drain];
return 0;
}
Проблема (странный результат)
Когда я ввожу 3 итерации и отвечаю правильно, я не получаю 100% правильно.Получаю только 99%.То же количество я пробовал на своем калькуляторе iPhone.
100/3 = 33,3333333 ... процент за один правильный ответ (программа отображает 33%. Цифры после обрезания мантиссы)
33.3333333... * 3 = 100%
Может кто-нибудь объяснить мне, где я ошибся?Thanx.