Results 1 to 5 of 5

Thread: Problem in random number subtraction

  1. #1
    Join Date
    Nov 2009
    Posts
    57

    Problem in random number subtraction

    Hello to all,
    I am having problem in random number subtraction in following code. I have create one program that generate random number between 1 to 5 and after that it shows total of it. In the following code 2's and 3's are equal to zero. In this code I have done addition in following ways.
    Code:
    int sums;
    	sums = rolls1 + rolls2 + rolls3 + rolls4 + rolls5;
    In above code I want to add up the 2's and 3's. Please help me to fix this problem.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Problem in random number subtraction

    You have to test each rolls to check that it is equal to 2 or 3. If anyone of the rolls are equals are to 2 or 3 then set it to zero. To do this you have to use "*" to "+"or "/" in yo7ur code. You have to use If statement in your code to check for each rolls number. After this you have to use for loop to get five number form the user and using if statement you have to check these numbers.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Problem in random number subtraction

    Assume that your random generator program give you numbers like 1, 4, 3, 2, 2 and you want 2's and 3's to be zeros, so the your answer will be zero. To fix this problem you have to use switch statement in your code to fix this problem.
    Code:
    int sums =  rolls1 + rolls2 + rolls3 + roll s4 + rolsl5;
    cout << sums << endl;
    
     
    switch (rolls1){
         case 2:
              rolls1 = 0;
              break;
         case 3:
              rollss1 = 0;
              break;
         default:
         break;
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Problem in random number subtraction

    You have to use following code to fix this problem. In the following code I have use If statement to check for zero. In If statement I check value of rolls1 and rolls2 and if there values are same then I have set value of rolls1 to zero.
    Code:
    if(rolls1==2 || rolls2==3)
    {
        rolls1=0;
    }
    //It will repeat for all 5 rolls

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Problem in random number subtraction

    You have written wrong code and that's why you are getting problem in random number subtraction. To fix this problem you have to use array in your code. I have written following code for you. Just try to understand it. In the following code I have use For loop to check for five values.
    Code:
    for(int k = 0;k<5;++k)
    {
         if(rolls[k] == 2 || rolls[k] == 3||)
         {
              rolls[k] = 0;
         }
    }
    sums=rolls[0]+rolls[1]+rolls[2]+rolls[3]+rolls[4];

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. How to use "Math.random()" to generate a random number in JavaScript?
    By Silent~Kid in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 05:06 AM
  3. Problem of subtraction in C
    By Camryn in forum Software Development
    Replies: 5
    Last Post: 30-12-2009, 07:07 PM
  4. Problem in random number
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 01:30 PM
  5. Generate random number using VB
    By Bambina in forum Software Development
    Replies: 2
    Last Post: 02-09-2009, 10:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,135,654.29513 seconds with 16 queries