|
-
Nov 10th, 2001, 04:57 PM
#1
Thread Starter
Fanatic Member
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
-
Nov 10th, 2001, 05:05 PM
#2
Fanatic Member
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.
-
Nov 10th, 2001, 05:07 PM
#3
transcendental analytic
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.
-
Nov 10th, 2001, 08:31 PM
#4
Hyperactive Member
And to add, sin and cos function take in radians, not degrees.
You may have to convert degrees to radians before using them.
-
Nov 11th, 2001, 06:17 AM
#5
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.
-
Nov 11th, 2001, 03:41 PM
#6
Fanatic Member
Alcohol & calculus don't mix.
Never drink & derive.
-
Nov 12th, 2001, 09:50 AM
#7
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.
-
Nov 12th, 2001, 07:07 PM
#8
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|