decreasing variable

This is a discussion on decreasing variable within the C Programming forums, part of the General Programming Boards category; hi there i just wrote the following code. Code: void case3() { int i; puts("here are your lottery numbers: "); ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    decreasing variable

    hi there i just wrote the following code.
    Code:
    void case3()
    {
        int i;
        puts("here are your lottery numbers: ");
        for(i=0;i<6;i++)
        {	
            int a=rando();
            if (a == a && a == 0)
                i=i-1;
            else
                printf("%d ", a);
        }
        printf("\nhere goes your additional number: %d\n", rando());
    }
    with this for statement i want to start a loop that generates 6 random and different numbers.
    i tried to kill the same numbers with that if statement whats supposed to proof if the same value is given or 0, if so it should decrease i by one and start from the beginning.
    but actually it doesnt do that, it goes on printing zeros and the same values.
    whats wrong?

    help would be greatly appreciated

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How about finding a way to "remember" the numbers you've already generated...

    Also, you'll want to get numbers between 1 and 50 (or whatever), I guess, so different method of getting random numbers is needed. That way, you won't need to check for 0.

    [edit]
    Also, there's various examples of this on the forums here, try a search and see what you can find.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    the rando() function returns a value from 0 to 49.
    i was just wondering why it didnt work that way. can you give me
    an example how it would work my way?

    thanks

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Oopps, sorry, I didn't see it was rando(), I thought it was rand(), hence my comments.

    If your own version (rando) gives you 0 - 49, and you don't want 0, why not fix it so it gives you 1 - 49.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >with this for statement i want to start a loop that generates 6 random and different numbers.
    Your loop is a bit unusual:

    >if (a == a && a == 0)
    Of course a is going to be equal to a, you can remove that and just test for 0. Though since you want all different numbers simply testing for 0 doesn't help you much.

    Perhaps if you kept a record of the numbers already produced and didn't increment the counter until a new number is found:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    static int is_saved ( int val, int *saved, int size )
    {
      int i;
    
      for ( i = 0; i < size; i++) {
        if ( saved[i] == val ) {
          return 1;
        }
      }
    
      return 0;
    }
    
    int main ( void )
    {
      int a, i = 0;
      int saved[6];
    
      srand ( (unsigned)time ( NULL ) );
    
      while ( i < 6 ) {
        a = rand() % 50;
    
        if ( !is_saved ( a, saved, 6 ) ) {
          saved[i++] = a;
        }
      }
    
      printf ( "Your numbers are: " );
    
      for ( i = 0; i < 6; i++ ) {
        printf ( "%d ", saved[i] );
      }
    
      printf ( "Your bonus number is: %d\n", rand() % 50 );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks for your help.
    my program is finished soon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04:31 PM
  3. Replies: 2
    Last Post: 04-12-2004, 02:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 02:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21