Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6125225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:13:23+00:00 2026-05-23T16:13:23+00:00

I am learning Objective-C and have completed a simple program and got an unexpected

  • 0

I am learning Objective-C and have completed a simple program and got an unexpected result. This program is just a multiplication table test… User inputs the number of iterations(test questions), then inputs answers. That after program displays the number of right and wrong answers, percentage and accepted/failed result.

#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;

}

Problem (strange result)

When I input 3 iterations and answer ’em right, i am not getting of 100% right. Getting only
99%. The same count I tried on my iPhone calculator.

100 / 3 = 33.3333333… percentage for one right answer (program displays 33%. The digits after mantissa getting cut off)

33.3333333… * 3=100%

Can someone explain me where I went wrong? Thanx.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:13:24+00:00Added an answer on May 23, 2026 at 4:13 pm

    This is a result of integer division. When you perform division between two integer types, the result is automatically rounded towards 0 to form an integer. So, integer division of (100 / 3) gives a result of 33, not 33.33…. When you multiply that by 3, you get 99. To fix this, you can force floating point division by changing 100 to 100.0. The .0 tells the compiler that it should use a floating point type instead of an integer, forcing floating point division. As a result, rounding will not occur after the division. However, 33.33… cannot be represented exactly by binary numbers. Because of this, you may still see incorrect results at times. Since you store the result as an integer, rounding down will still occur after the multiplication, which will make it more obvious. If you want to use an integer type, you should use the round function on the result:

    int percent = round((100.0 / combinations) * rightAnswers);
    

    This will cause the number to be rounded to the closest integer before converting it to an integer type. Alternately, you could use a floating point storage type and specify a certain number of decimal places to display:

    float percent = (100.0 / combinations) * rightAnswers;
    NSLog(@"Completed %.1f percent",percent); // Display result with 1 decimal place
    

    Finally, since floating point math will still cause rounding for numbers that can’t be represented in binary, I would suggest multiplying by rightAnswers before dividing by combinations. This will increase the chances that the result is representable. For example, 100/3=33.33… is not representable and will be rounded. If you multiply by 3 first, you get 300/3=100, which is representable and will not be rounded.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning objective c. I have this little problems and questions, hope
I am just learning objective-C, and have a book with code like this: NSArray
I'm learning objective-C and Cocoa and have come across this statement: The Cocoa frameworks
I have just started learning Objective-C, I am reading Programming in Objective-C 3rd Edition
I just started learning Objective-C and OOP ... so I have very stupid question
I'm still learning Objective-C so forgive me if this is a simple amateur mistake,
I'm just learning Objective-C and have a what I'm sure is a pretty basic
I have just started learning objective-C and the iphone sdk and I have a
I'm learning Objective-C at the moment and have come across optional methods in Protocols.
I'm learning Objective-C, and have a C/C++ background. In object-oriented C++, you always need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.