Thread: arrays of random integers & time

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    i am new i have two questions about functions

    1.I need to know how to test my program with a loop of 25 repetitions

    2. generate an attack location with rand() function. and pass the variable row and col by refernces

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What have you tried so far?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    i dont have anything in the main(),i am so stumped i jus need a kick in the right direction

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well a loop would be
    for ( int i = 0 ; i < 25 ; i++ )

    Picking random numbers is in the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Passing by reference,
    Code:
    void foo ( int &a ) {
    }
    int main ( ) {
        int bar;
        foo( bar );
    }

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    thank u,but this doesnt help me pass it thru columns and rows

    THANk U,and how long did it take u to understand this

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but this doesnt help me pass it thru columns and rows
    Sure it does. Change a to col and add another parameter exactly like it except named row:
    Code:
    void f(int& col, int& row);
    >and how long did it take u to understand this
    It doesn't matter. Everyone learns differently.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    this is what i got i am still confused:

    int main ()
    {
    srand(time(NULL));
    int a = rand();
    int row,col;
    funcrand(row,col);
    for (int i= 0 ; i< 25 ; i++ )
    row= rand();
    return 0;

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <iostream>
    void foo ( int &a ) {
        a = 2
    }
    int main ( ) {
        int bar = 1;
        std::cout << bar;
        foo( bar );
        std::cout << bar << std::endl;
    }

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    doesnt it have to be random and its output is suppose to be in rows and columns and run 25 times

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >doesnt it have to be random and its output is suppose to be in rows and columns and run 25 times
    That's your job. We're helping you, not doing it for you.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    Quote Originally Posted by Prelude
    >doesnt it have to be random and its output is suppose to be in rows and columns and run 25 times
    That's your job. We're helping you, not doing it for you.

    i have it running numbers,but i cant get it to run 25 times look at what i have wuts wrong:

    srand( (unsigned)time( NULL) );
    for(int times=0;times==25;times++)
    int a = 1;
    int b = 2;
    for (int a = 0; a<25; a++)
    cout<<1 + rand() % 10 <<endl;
    funcrand( a,b );
    return 0;

    but right now i cant get the other column
    Last edited by krazykid18; 12-11-2004 at 06:34 PM.

  12. #12
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Quote Originally Posted by krazykid18
    i have it running numbers,but i cant get it to run 25 times look at what i have wuts wrong:

    srand( (unsigned)time( NULL) );
    for(int times=0;times==25;times++)
    int a = 1;
    int b = 2;
    for (int a = 0; a<25; a++)
    cout<<1 + rand() % 10 <<endl;
    funcrand( a,b );
    return 0;

    but right now i cant get the other column
    for(int times=0;times<25;times++)
    ^-------The second term (times<25) is like a boolean. You want it to run as often as that returns true.
    To code is divine

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Unless you only want to loop through one line, you need to put curly braces ( { } 's ) around the code you want to execute 25 times. You'd also have a problem if you had a semicolon after the for statement.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    this what i got
    int main ( )
    {
    srand( (unsigned)time( NULL) );
    for (int times=0;times<25;times++)
    int a;
    int b;
    int grid[25][25];
    int random_integer;
    for (int a = 0; a<25; a++){
    for (int b = 1; b<25; b++){
    {random_integer=rand();
    cout<<1 + rand() % 10 << a << 1+ rand() % 10 << b <<endl;}
    funcrand(a,b);
    return 0;
    }

    but when i run it says unexpected end of file as its error,wut is the problem

  15. #15
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    > for (int times=0;times<25;times++) I'm going to guess that.
    To code is divine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  3. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  4. Adding arrays of integers in a node struct
    By emilyashu in forum C Programming
    Replies: 7
    Last Post: 04-26-2003, 06:19 PM
  5. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM