The syntax to declaire a variable in a function.

This is a discussion on The syntax to declaire a variable in a function. within the C Programming forums, part of the General Programming Boards category; Hello, i'm trying to use the rand() function and i would like to know how to make the random number ...

  1. #1
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    The syntax to assign a variable to a function.

    Hello, i'm trying to use the rand() function and i would like to know how to make the random number a variable.
    I've tryed:
    Code:
    rand(int c);
    My intention is to compare two variable's in a statement, one random. I hope you all understand.
    Thank's.
    Last edited by loopy; 05-25-2002 at 06:47 AM.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    rand("%d", number);
    number will hold a number, provided you "assigned" something to number somewhere else in your program

    Simple example:
    Code:
    int main()
    {
    	int number = 3;
    	printf("%d", number);
    	return 0;
    }
    or:
    Code:
    int main()
    {
    	go(5);
    	return 0;
    }
    
    int go(int number)
    {
    	printf("%d", number);
    	return 0;
    }
    The world is waiting. I must leave you now.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    hmm
    are you trying to generate a random number also?
    remember to use srand(time(NULL));
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    Thank's for the reply.

    Ok i understand how to assign the variable to the rand() function.
    Code:
    int curb()
    {
      int z, c=5;
      rand("%d", c); 
      sleep(1);
      clrscr();
      printf("Were coming up to a waxed curb, what do you want to do, make it fast\n");
      printf("1. do an olie over it.\n");
      printf("2. do a flip kick onto it.\n");
      printf("3. do a big jump over it.\n");
      printf("4. do a switch onto it.\n");
      printf("5. exit\n");
      scanf("%d", &z);
      if (z == c) 
      b += OLIE;
      else if (z == c)
      b += FLIP_KICK;
      else if (z == c)
      b += BIG_JUMP;
      else if (z == c)
      b += SWITCH;
      else if (z == c)
      exit();
      else 
      printf("You fell\n");
      printf("%d", b);
      printf(" point's\n");
      return z; 
    }
    I do want to make the vaiable c a random number between 1 and 6. I don't want other's to write my code for me but where do i go from here with srand(time(NULL))? Just hint's, i want to learn not to have other's do my thinking for me.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I do want to make the vaiable c a random number between 1 and 6. I don't want other's to write my code for me but where do i go from here with srand(time(NULL))? Just hint's, i want to learn not to have other's do my thinking for me.
    Oh, I donno... How about you read the FAQ for this web site? HMMMMM?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    You only need call srand once in your program, so place it after your variable declarations in main.
    Your if else ladder wont work properly as your using the same condition for every test. A switch statement would be much better here.
    to get a number from 1 to 6 you need to use the '%' operator
    Code:
    c = rand() % 6;
    This will give you a number from 0 to 5, so you need to add 1 to c or change your code for the range 0 to 5.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  7. #7
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    Explaination and thank's.

    In reply to quzah...
    Oh, I donno... How about you read the FAQ for this web site? HMMMMM?
    I read the faq many of time's, but after reading the part on random number's i still didn't understand how to incorperate it into my program.
    Thank's for the hint's C_coder.(everything's a hint at my level of programming knowledge...)

    Your if else ladder wont work properly as your using the same condition for every test
    I don't know what i'm going to do with that yet, when i get my random number's going i can trash the whole program and start over .
    Last edited by loopy; 05-25-2002 at 04:09 AM.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  8. #8
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    Problem solved

    this work's for me:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
      srand(time(NULL));
      int c = rand() % 7;
      printf("%d", c);
      return 0;
    }
    Last edited by loopy; 05-25-2002 at 06:35 AM.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  9. #9
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    int c = rand() % 7;
    bear in mind your getting 0 to 6 here, not 1 to 6
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  10. #10
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    What i was after...

    Was a 1 and a 0.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

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