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
Printable View
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
2:3: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;
}
Code:#include <cmath>
using namespace std;
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
And to add, sin and cos function take in radians, not degrees.
You may have to convert degrees to radians before using them.
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.
Cmath works too.
k, you're right. But it is not mentioned in MSDN...
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 ?