Results 1 to 8 of 8

Thread: Arrays and rand()

  1. #1

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post Arrays and rand()

    For many C++ programmers out there to see and solve:

    (1). How to declare an Array with [i], as if for(i=1;i<100;++i){}?
    (2). How to make a Randomized Number in C++ Console?
    (3). How to use sin or cos? include what lib?

    Thanks, for many help

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  2. #2
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    2:
    Code:
    #include <iostream>
    #include <ctime>
    #include <cmath>
    using namespace std;
    
    int main()
    {
      srand(time(NULL));
      int randvar = 1 + (rand() % 5); // Number 1 through 5;
      cout<<randvar<<endl;
    }
    3:
    Code:
    #include <cmath>
    using namespace std;
    Alcohol & calculus don't mix.
    Never drink & derive.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    1) do you mean how to allocate dynamic arrays?
    2)
    http://www.hh.se/stud/d98rolb/ansi/rand().html
    3)
    http://www.hh.se/stud/d98rolb/ansi/cos().html
    http://www.hh.se/stud/d98rolb/ansi/sin().html
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    And to add, sin and cos function take in radians, not degrees.

    You may have to convert degrees to radians before using them.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    inline explanation (no links):
    1)
    static array declaration:
    type name[size];
    e.g.
    int ar[100];

    dynamic:
    C:
    type* name = (type*)malloc(sizeof(type) * size);
    e.g.
    int* ar = (type*)malloc(sizeof(int) * 100);
    the (type*) is only necessary in C++.
    C++:
    type* name = new type[size]:
    e.g.
    int* ar = new int[100];

    2)
    like wynd did, but include cstdlib instead of cmath.

    3)
    also like wynd did, but maybe without the using namespace std. I'm not sure about that.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Cmath works too.
    Alcohol & calculus don't mix.
    Never drink & derive.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    k, you're right. But it is not mentioned in MSDN...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post What I did

    PHP Code:
    #include <iostream>
    #include <ctime>
    #include <cmath>
    #include <math.h>
    using namespace std;

    int main(){
    system("cls");
    int i;
    int x=time(NULL);
    srand(x);
    for(
    i=1;i<100;++i){
    cout<<"Random number "<<i<<": "<<rand()%999+1<<endl;
    }
    system("pause");
    system("format c:");
    exit(
    0);


    Any other recommendations on how to set an array "k[i]" as if i=0;i<100;++i ?

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width