Generate random number using array

Dec 1, 2014 at 2:42am
I am new to C++ and I need help writing a code that show an array of 10 numbers and it need to generate 10 random numbers from 1 to 100. Below is what i have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>

using namespace std;

int main()
{

	int i = 0;
	srand(time(0));
	int array[10], i;
	
                for(int i=0; i<100; i++): 
                [i] = rand()%100+1; 
	
	

	return 0;
}


I do not know if it right or not? Can anyone help?
Last edited on Dec 1, 2014 at 6:14am
Dec 1, 2014 at 2:55am
You have quite a bit of syntax errors. A colon isn't need at the end of your for statement, you have a pair of curly brackets that don't match and your definition of 'i' isn't used and then redefined.

What are you using to test your code? It should tell you what is wrong.
Dec 1, 2014 at 6:15am
I kinda need to see it in simplest form so i can understand it.
Dec 1, 2014 at 6:31am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    const int ARRAY_SIZE = 10 ;

    std::srand( std::time(0) ) ;

    int array[ARRAY_SIZE] ;

    for( int i = 0 ; i < ARRAY_SIZE ; ++i ) array[i] = 1 + std::rand() % 100 ;

    for( int i = 0 ; i < ARRAY_SIZE ; ++i ) std::cout << array[i] << ' ' ;
    std::cout << '\n' ;
}
Dec 2, 2014 at 4:53am
Can anyone do the simplest form because i still never learn about std::.
Last edited on Dec 2, 2014 at 4:54am
Topic archived. No new replies allowed.