14 Replies - 10457 Views - Last Post: 07 March 2010 - 05:36 PM Rate Topic: -----

#1 Guest_spcy*


Reputation:

how to use fabs?

Posted 05 March 2010 - 06:56 PM

Hi everyone!

I'm a noob c++ student and also new here, so do let me know if I'm doing anything wrong!

Anyway, I really need help on a problem that has been plaguing me for awhile.
I've created a program for an assignment that randomly generates 10 arithmetic questions to be answered, and it all worked out fine except for the division part. I can't seem to get the correct answer all the time!
We are supposed to also incorporate something new into the program that our lecturer hasn't taught us, so it really is headache-inducing for me!

My lecturer told us to use the fabs function, but after much searching I could not understand how to incorporate it into my program!
Help, please?

Here's my program (sorry if it is messy; it took me more than 24 hours just to generate this! I don't know how you guys do it!):

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main() {
    int a, b, answer, sum, count;
    char next = 'y';
    srand((unsigned)time(NULL));

    cout << "\n  Welcome to ~ Arithmetic Game ~!!";
    cout << "\nThere will be 10 questiond per session.";
    cout << "\nEach question carries 10 marks.";
    cout << "\nLet's see how good you are at maths.";
    cout << "\nLet the game begin!\n\n";

    while (next == 'Y' || next == 'y') {
        sum = 0;
        count = 1;
        while (count <= 10) {
            do {
                int temp = rand() % 4 + 1;
                a = rand() % 100 + 1;
                b = rand() % 100 + 1;

                if (count <= 10)
                    cout << "\nQuestion " << count << "\n";

                switch( temp ) {
                case 1:
                    cout << a << "+" << b << "= ";
                    cin >> answer;
                    if(answer == ( a + b )) {
                        cout << "Correct! +10 marks!\n\n";
                        sum = sum + 10;
                    } else {
                        cout << "Wrong! -5 marks!\n\n";
                        sum = sum - 5;
                    }
                    break;

                case 2:
                    cout << a << "-" << b << "= ";
                    cin >> answer;
                    if (answer == (a - B)/>) {
                        cout << "Correct! +10 marks!\n\n";
                        sum = sum + 10;
                    } else {
                        cout << "Wrong! -5 marks!\n\n";
                        sum = sum - 5;
                    }
                    break;

                case 3:
                    cout << a << "*" << b << "= ";
                    cin >> answer;
                    if (answer == (a * B)/>) {
                        cout << "Correct! +10 marks!\n\n";
                        sum = sum + 10;
                    } else {
                        cout << "Wrong! -5 marks!\n\n";
                        sum = sum - 5;
                    }
                    break;

                case 4:
                    cout << a << "/" << b << "= ";
                    cin >> answer;
                    if (answer == (a / B)/>) {
                        cout << "Correct! +10 marks!\n\n";
                        sum = sum + 10;
                    } else {
                        cout << "Wrong! -5 marks!\n\n";
                        sum = sum - 5;
                    }
                    break;
                }
                count = count + 1;
            }

            while (count <= 10);
            cout << "\nYou have scored " << sum << "marks.";
        }
        cout << "\n\nDo you want to keep playing?\n";
        cout << "Type 'y' for yes and 'n' for no:";
        cin >> next;
    }

    if (( next != 'Y' ) || ( next != 'y' ) )
        cout << "\nGame Over!\n";
    cout << "Please press any key to exit...";

    getch();
}


Thank you very much for your time; hope to learn something new today! :)

This post has been edited by NickDMax: 05 March 2010 - 08:44 PM
Reason for edit:: Added Code tags: [code]paste code here[/code]


Is This A Good Question/Topic? 0

Replies To: how to use fabs?

#2 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: how to use fabs?

Posted 05 March 2010 - 08:12 PM

Quote

My lecturer told us to use the fabs function, but after much searching I could not understand how to incorporate it into my program!
Help, please?


You are paying to be taught like this???

Tap your professor upside the head with a board or something because this code it definitely not standard.
Enough with the rant.


You have just become a victim of integer division.

Integer division is like this:
3/2 = 1.5 but since you are dividing integers the answer truncates to 1. Make your variable types (including sum) a double and your problems will be gone.

I really don't know why you need fabs in this program but you could incorporate it in some silly way. (i.e. Take a random variable and take the absolute value of it for no reason)

Also when posting code please use code tags:
[ code ]Post code here...[ /code ]
Take the spaces out of the code tags though.

Yet another edit:
Here is is great tutorial for learning C++ the new and improved way with "int main()"
http://cplusplus.com/doc/tutorial/

This post has been edited by eker676: 05 March 2010 - 08:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: how to use fabs?

Posted 05 March 2010 - 08:19 PM

And you have stdio and conio included... why?
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: how to use fabs?

Posted 05 March 2010 - 11:57 PM

<stdio.h> is not needed, but he needs <conio.h> for getch()
Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: how to use fabs?

Posted 06 March 2010 - 12:02 AM

...which is pointless when he can use cin for the same purpose.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Guest*


Reputation:

Re: how to use fabs?

Posted 06 March 2010 - 05:41 PM

Quote

You are paying to be taught like this???

Tap your professor upside the head with a board or something because this code it definitely not standard.

LOL, which explains why I cannot find the proper way to incorporate the code!

Quote

And you have stdio and conio included... why?

I forgot to remove it! Thanks for bringing it up, PlasticineGuy! <conio.h> is there because it became a habit; our lecturer taught us about using it and getch() to ensure the the program executes! Didn't know I could just leave it out since I can use cin. So thank you for teaching me something new!

I managed to fix division problems like 3/2 = 1.5, but I can't seem to get problems like 2/6 = 0.33333 (infinity) or 9/7 = 1.285714286! Any idea why? Is there something I can add to the program that takes in answers between one to four decimal places?

And finally, is my code okay now?

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

void main()

{
  double a, b, answer, sum;
  int count;
  char next = 'y';
  srand((unsigned)time(NULL));

  cout << "\n  Welcome to ~ Arithmetic Game ~!!";
  cout << "\nThere will be 10 questiond per session.";
  cout << "\nEach question carries 10 marks.";
  cout << "\nLet's see how good you are at maths.";
  cout << "\nLet the game begin!\n\n";

while (next == 'Y' || next == 'y')
 {
   sum = 0;
   count = 1;
   while (count <= 10)
   {
    do
    { int temp = rand() % 4 + 1;
      a = rand() % 100 + 1;
      b = rand() % 100 + 1;

      if (count <= 10)
      cout << "\nQuestion " << count << "\n";

        switch( temp )
        { case 1:
          cout << a << "+" << b << "= ";
          cin >> answer;
          if(answer == ( a + b ))
          {
          cout << "Correct! +10 marks!\n\n";
          sum = sum + 10;
          }
          else
          {
          cout << "Wrong! -5 marks!\n\n";
          sum = sum -5;
          }
          break;

          case 2:
          cout << a << "-" << b << "= ";
          cin >> answer;
          if (answer == (a - B)/>)
          {
          cout << "Correct! +10 marks!\n\n";
          sum = sum + 10;
          }
          else
          {
          cout << "Wrong! -5 marks!\n\n";
          sum = sum - 5;
          }
          break;

          case 3:
          cout << a << "*" << b << "= ";
          cin >> answer;
          if (answer == (a * B)/>)
          {
          cout << "Correct! +10 marks!\n\n";
          sum = sum +10;
          }
          else
          {
          cout << "Wrong! -5 marks!\n\n";
          sum = sum - 5;
          }
          break;

          case 4:
          cout << a << "/" << b << "= ";
          cin >> answer;
          if (answer == (a / B)/>)
          {
          cout << "Correct! +10 marks!\n\n";
          sum = sum + 10;
          }
          else
          {
          cout << "Wrong! -5 marks!\n\n";
          sum = sum - 5;
          }
          break;
        }
      count = count + 1;
    }

    while (count <= 10);
    cout << "\nYou have scored " << sum << "marks.";
 }
    cout << "\n\nDo you want to keep playing?\n";
    cout << "Type 'y' for yes and 'n' for no:";
    cin >> next;
  }

    if (( next != 'Y' ) || ( next != 'y' ) )
    cout << "\nGame Over!\n";
    cout << "Please press any key to exit...";
}


Do let me know if there are any improvements that I can make! Thank you for the link, and thank you so much for the help; I really appreciate it!
:D
Was This Post Helpful? 0

#7 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: how to use fabs?

Posted 06 March 2010 - 06:02 PM

1. iostream.h doesn't (or really shouldn't) exist. It's just iostream.
2. In C++, stdlib.h and time.h shouldn't really exist; use cstdlib and ctime. The same applies for all C standard libraries.
3. void main() is illegal. main MUST return an int to specify the result. 0 means success, so return 0 at the end of your program.
4. Your indenting is a bit dodgy.

Which IDE/compiler are you using?

This post has been edited by PlasticineGuy: 06 March 2010 - 06:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_spcy*


Reputation:

Re: how to use fabs?

Posted 06 March 2010 - 06:17 PM

I'm using Borland C++ Builder 6!
Okay, now I'm confused :whatsthat:
Was This Post Helpful? 0

#9 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: how to use fabs?

Posted 06 March 2010 - 06:22 PM

What output do you get for infinitely recurring answers?
Was This Post Helpful? 0
  • +
  • -

#10 Guest_spcy*


Reputation:

Re: how to use fabs?

Posted 06 March 2010 - 06:32 PM

I just get "Wrong! -5 marks".

I tried entering an answer with one decimal point, wrong.
Two decimal points, wrong.
Three decimal points, wrong.
Four decimal points, also wrong!

This is driving me crazy because it is the only problem I have with the program before I can finalize everything and submit my assignment.

Please :helpsmilie:!
Was This Post Helpful? 0

#11 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: how to use fabs?

Posted 06 March 2010 - 07:11 PM

Here is a really dirty fix but it works.

When calculating the real answer do this:

Divide the numbers
Multiply the answer by 1000
Round the answer to the nearest whole number
Divide by 1000 and you get a number accurate to 3 decimal places.

Then compare the entered answer against the calculated one.

Here is a function that will do the rounding for you: ( I just wrote it )

double roundNumber(double num)
{
  int t = num; // truncate the number;
  double decimal = num - t; // give you only the decimal places

  if(decimal < .5)
    decimal = 0;
  else
   decimal = 1;

  return (double)(t + decimal);
}

This post has been edited by eker676: 06 March 2010 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

#12 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: how to use fabs?

Posted 06 March 2010 - 07:14 PM

You need to round the decimal to a number of places. Here's a function that will do that (thanks Google):
double roundto(double x, int places) { return floor(x * pow((double)10, (double)places)) / pow((double)10, (double)places); }
That requires cmath included.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_spcy*


Reputation:

Re: how to use fabs?

Posted 07 March 2010 - 07:06 AM

Thank you all so much for the help and codes!

It all worked, but I finally managed to figure out how to use the fabs function with the help of my friends.
Here's the code if you all are curious:

#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

void main()

{
  float a, b, ans, answer;
  int sum, count;
  char next = 'y';
  srand((unsigned)time(NULL));

  cout << "\n  Welcome to ~ Arithmetic Game ~!!";
  cout << "\nThere will be 10 questiond per session.";
  cout << "\nEach question carries 10 marks.";
  cout << "\nLet's see how good you are at maths.";
  cout << "\nLet the game begin!\n\n";

while (next == 'Y' || next == 'y')
 {
   sum = 0;
   count = 1;
   while (count <= 10)
   {
    do
    { int temp = rand() % 4 + 1;
      a = rand() % 100 + 1;
      b = rand() % 100 + 1;

      if (count <= 10)
      cout << "\nQuestion " << count << "\n";

        switch(temp)
        { case 1:
          cout << a << "+" << b << "= ";
          ans = a +b;
          cin >> answer;
          if(ans == answer)
          {
          cout << "Correct! +10 marks\n\n";
          sum = sum + 10;
          }
          else
          {
          cout << "Wrong! -5 marks\n";
          cout << "The correct answer is " << ans << "\n";
          sum = sum - 5;
          }
          break;

          case 2:
          cout << a << "-" << b << "= ";
          ans = a - b;
          cin >> answer;
          if (ans == answer)
          {
          cout << "Correct! +10 marks\n\n";
          sum = sum + 10;
          }
          else
          {
          cout << "Wrong! -5 marks\n";
          cout << "The correct answer is " << ans << "\n";
          sum = sum - 5;
          }
          break;

          case 3:
          cout << a << "*" << b << "= ";
          ans = a * b;
          cin >> answer;
          if (ans == answer)
          {
          cout << "Correct! +10 marks\n\n";
          sum = sum +10;
          }
          else
          {
          cout << "Wrong! -5 marks\n";
          cout << "The correct answer is " << ans << "\n";
          sum = sum - 5;
          }
          break;

          case 4:
          cout << a << "/" << b << "= ";
          ans = a / b;
          cin >> answer;
          if (fabs(ans - answer) < 0.005)
          {
          cout << "Correct! +10 marks\n\n";
          sum = sum + 10;
          }
          else
          {
          cout << "Wrong! -5 marks\n";
          cout << "The correct answer is " << ans << "\n";
          sum = sum - 5;
          }
          break;
        }
      count = count + 1;
    }

    while (count <= 10);
    cout << "\nYou have scored " << sum << "marks.\n";

    if (sum < 0)
    cout << "You are terrible at this, use a calculator!\n";

    if (sum < 100)
    cout << "You failed! Better luck next time!\n";

    if (sum == 100)
    cout << "EXCELLENT! PERFECT SCORE!\n";
 }
    cout << "\n\nDo you want to keep playing?\n";
    cout << "Type 'y' for yes and 'n' for no :";
    cin >> next;

  }

    if ((next != 'Y') || (next != 'y'))
    cout << "\nGame Over!\n";
    cout << "Please press any key to exit...";
}


It works perfectly, but I'm curious to know if this is actually legal?
Was This Post Helpful? 0

#14 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: how to use fabs?

Posted 07 March 2010 - 07:09 AM

I'd like you to use "int main()" instead of "void main()": http://www.gidnetwork.com/b-66.html
And there's no such thing as "illegal program" in C/C++. If it works, it's legal :P
Was This Post Helpful? 0
  • +
  • -

#15 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: how to use fabs?

Posted 07 March 2010 - 05:36 PM

No. void main is illegal. It's just supported by many compilers for some reason.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1