same question of C++ program

C++

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1574 Views - Last Post: 06 March 2010 - 07:10 AM Rate Topic: -----

#1 Chris0083  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-March 10

same question of C++ program

Posted 02 March 2010 - 08:50 PM

#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
using namespace std;
void main()
{
     char next;
     int mark, question_no, f, s;
     float t, a;
     next = 'y';
     char chOptn[5] ="+-*/"; 
     while (next == 'y')
     {
         mark = 0;
         question_no = 1;
         while (question_no <= 10)
         {
             t = 0.00;
             srand((unsigned)time(NULL));
             f = rand()%100 + 1;
             s = rand()%100 + 1;
             srand((unsigned)time(NULL));
             int iIndex = rand()%4;
             cout << "Question " << question_no <<endl;
             cout << f << " " << chOptn[iIndex] << " " << s << " = ";
             cin >> a;
             if (iIndex==0)
                 t = f + s;
             if (iIndex==1)
                 t=f-s;
             if (iIndex==2)
                 t=f*s;
             if (iIndex==3)
                 t=f/s;
             if (a != t)
{
                 mark = mark - 5;
                 cout << " ";
                 cout << "Wrong! - 5marks" << endl << endl;
             }
             else
             {
                 mark = mark + 10;
                 cout << " ";
                 cout << "Correct !! + 10 marks" << endl << endl;
             }
             question_no = question_no + 1;
         }
         cout << "You have scored " << mark << " ."<<endl<< endl <<endl;
         cout << " do you want to keep playing?" << endl;
         cout << "type \'y\'for yes and \'n\' for no: ";
         cin >> next;
     }
     cout << endl << endl;
     cout << "game over !"<<endl;
     cout << "please press any key to exit...";
     getchar();
}



this is a program of my question i ask last night.
but what is mean
char chOptn[5] ="+-*/"; 
?
and also have problem: 1/3=0.333333333333......
but if i input 0.33 or 0.333 or 0.3333 is all wrong, why? my lecturer asked whatever input is 0.3 or 0.33 or 0.333 or 0.3333, it should be right.

and she also say we can use the function "fabs"? why and how to use in this program ?

This post has been edited by Chris0083: 02 March 2010 - 08:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: same question of C++ program

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: same question of C++ program

Posted 02 March 2010 - 09:10 PM

>>char chOptn[5] ="+-*/";

That is declaring a character array and initializing it to the string "+-*/". It could have also been declared like this (without specifying the string's length: char chOptn[] ="+-*/";

>>but if i input 0.33 or 0.333 or 0.3333 is all wrong, why?
Don't know -- what is the value of the variable after you enter 0.33 etc.?\


>>we can use the function "fabs"? why and how to use in this program ?
fabs. I don't know why you should use it because you didn't post the problem.
Was This Post Helpful? 0
  • +
  • -

#3 Chris0083  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-March 10

Re: same question of C++ program

Posted 02 March 2010 - 09:36 PM

Write a program to test students on arithmetic operations involving addition (+), subtraction
(-), multiplication (*) and division (/) among integers in the range of 1 to 100. The program
should randomly generate the arithmetic questions for the user to solve. The program
should also keep track of the number of questions solved correctly to calculate the total
marks. Each arithmetic question carries 10 marks. The deduction for each wrong answer
will be 50% of the allotted marks, i.e. 5 marks will be deducted for each wrong answer. The
total number of arithmetic questions that the program should pose to the user is 10
questions. You may use the function rand()%100 + 1 to generate a random number
between 1 and 100. The seed for this random number generator is given as
srand((unsigned)time(NULL)). This seed will allow the random number generator to
generate a different set of random numbers every time the program is executed. Use a
random number to determine randomly the operations for the arithmetic questions. An
example of the input/output screen is shown in the Appendix.

the same question i post last night, i think fabs is to solve the 0.3 problem.
isn't it?

This post has been edited by Chris0083: 02 March 2010 - 09:38 PM

Was This Post Helpful? 0
  • +
  • -

#4 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: same question of C++ program

Posted 02 March 2010 - 09:46 PM

fabs only returns the absolute value of a number. If the number is negative then it will return the positive value. If the number of positive then fabs will do nothing but return the same value. (Had you bothered to read the first link that I posted you would have found that out.)

fabs() will not solve the 0.33 problem. You still did not say exactly what the proble you are having with 0.33.

Even after reading the program requirements I don't see how fabs() would help, unless your instructor wants you to enter only positive values. In that case you would have to reset to variable to the return value of fabs. a = fabs(a);

This post has been edited by Ancient Dragon: 02 March 2010 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 Chris0083  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-March 10

Re: same question of C++ program

Posted 02 March 2010 - 10:02 PM

sorry, im not good at english, i mean that 1/3=0.3333... right? but we can input the unlimited number, so we just can input 0.3 or0.333, but the problem is in this program, 1/3 whatever i input 0.3 or 0.33 all wrong, it should be right, so how to solve this problem?
Was This Post Helpful? 0
  • +
  • -

#6 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: same question of C++ program

Posted 02 March 2010 - 10:07 PM

The result of 1 divided by 3 can not be represented exactly in floating point because 1/3 is an infinite series of 3's.

One way to truncate all those 3's is to multiply the variable by 100 then divide by 100, like this:
int main(int argc, char *argv)
{
    float x = 1.0F / 3.0F;
    cout << x << '\n';
    x = (int)(x * 100)/100.0F;
    cout << x << '\n';

}



Was This Post Helpful? 1
  • +
  • -

#7 Chris0083  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-March 10

Re: same question of C++ program

Posted 03 March 2010 - 02:06 AM

no, now problem is , if the answer unlimited, whatever you input , it's all wrong.
why?
Was This Post Helpful? 0
  • +
  • -

#8 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: same question of C++ program

Posted 03 March 2010 - 02:21 AM

because, for a computer, 0.3 is not equal to 0.333333333,
and 0.3333 is not equal to 0.333333333
Because 0.3 will be stored as 0.300000000 and 0.3333 as 0.3333000000
and cleary those are not the same as 0.33333333333
So you'll have to find a way to change 0.33333333 into 0.30000000 or 0.333300000 before comparing it with the user's input.
Ancient Dragon gave an example on how to do that.
Was This Post Helpful? 0
  • +
  • -

#9 Chris0083  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-March 10

Re: same question of C++ program

Posted 03 March 2010 - 02:35 AM

good,so where is the example?
Was This Post Helpful? 0
  • +
  • -

#10 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: same question of C++ program

Posted 03 March 2010 - 02:40 AM

View PostAncient Dragon, on 03 March 2010 - 05:07 AM, said:

The result of 1 divided by 3 can not be represented exactly in floating point because 1/3 is an infinite series of 3's.

One way to truncate all those 3's is to multiply the variable by 100 then divide by 100, like this:
int main(int argc, char *argv)
{
    float x = 1.0F / 3.0F;
    cout << x << '\n';
    x = (int)(x * 100)/100.0F;
    cout << x << '\n';

}



3 posts up.
Was This Post Helpful? 0
  • +
  • -

#11 Chris0083  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-March 10

Re: same question of C++ program

Posted 03 March 2010 - 02:47 AM

no, it not right, i try to change the " float" to " bouble", is it helpful?
Was This Post Helpful? 0
  • +
  • -

#12 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: same question of C++ program

Posted 03 March 2010 - 03:05 AM

For me, Ancient Dragon's code gives me the following output:
0.333333
0.33

so it actually cuts off the last 4 threes.
changing float to double gives me the same output, so that's apparently not the problem.

Here is an idea for generating division problems:
Generate a random integer divisor, and a random integer result (quotient).
multiply divisor and result to get the dividend. If the dividend <= 100, you have a valid problem, if not, try again.
This way you eliminate the remainder problem...
Was This Post Helpful? 1
  • +
  • -

#13 Chris0083  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-March 10

Re: same question of C++ program

Posted 03 March 2010 - 03:16 AM

e^^^^^ can you give me a example? because my english is not good enough to truly understand what you say.
Was This Post Helpful? 0
  • +
  • -

#14 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: same question of C++ program

Posted 03 March 2010 - 04:19 AM

problem will be: what is a / b? ... answer = c.

int a, b, c;

do
{
   // generate c (== answer or quotient)
   int c = rand()%100 + 1;

   // generate b  (divisor):
   int b = rand()%100 + 1;

   // calculate a (dividend)
   a = b*c;
} while (a>100); // a is bigger than 100, so it is not allowed.

cout << "what is " << a << " divided by " << b << " ? ... answer = " <<  c;



Was This Post Helpful? 0
  • +
  • -

#15 wana901109  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-March 10

Re: same question of C++ program

Posted 06 March 2010 - 06:12 AM

can anyone tell me how to generate arithmetic questions that involv multiplying,divion,subtraction and addition.can somebody help me..plssssssssss i would very very appreciate it.this is urgent
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2