arrays

Write your question here.
My code doesn't work. I'm not sure how to assign as many random numbers as specified by the user.
The Question:
Asks user how many numbers to fill (the length of the partially filled array), with
random numbers 0-99. First fill all 100 elements with zeroes then insert as many random
numbers as specified by the user. Validate the user entered number (1-100).
My Code:
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
int length=100;
int array[length];
int input;
int number_of_random_numbers=0;
srand(((unsigned)time(0)));
int randomNum= 1 + rand() % 100;
cout << "Enter input" << endl;
cin >> input;
if(input==0){
cout << "Enter the length of the array:" << endl;
cin >> number_of_random_numbers;
for (int i = 0; i< number_of_random_numbers; i++)
array[i] =randomNum ;
}
}
Hello cash,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Read the comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include<cstdlib>
#include<ctime>

using namespace std;  // <--- Not the best idea to use this.
int main()
{
	int length = 100;  // <--- Should be "const int length" variable.
	int array[length]{}; // <--- Initializes all elements to zero.
	int input;  // <--- Should be initialized to zero.
	int number_of_random_numbers = 0;

	srand(((unsigned)time(0))); // <--- You could use size_t (another name for unsigned int) or "unsigned int", but not just unsigned

	int randomNum = 1 + rand() % 100;

	cout << "Enter input ";  // <--- Added space at the end and removed "endl"
	cin >> input;

	if (input == 0)  // <--- if statement not needed here.
	{
		cout << "Enter the length of the array:" << endl;  // <--- Better prompt heeded. This could allow the user to enter a number larger than 100.
		cin >> number_of_random_numbers;

		// for loop puts the same number into each element of the array. This is where you should use "rand()".
		for (int i = 0; i< number_of_random_numbers; i++)
			array[i] = randomNum;
	}
}


My initial thoughts. I will have more shortly.

Hope that helps for now,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include<cstdlib>
#include<ctime>

int main()
{
    const int ARRAY_SZ = 100; // note: const
    int array[ARRAY_SZ] = {0} ; // initialise to all zeroes

    std::srand( std::time(nullptr) );

    int cnt = 0 ;
    std::cout << "Enter the count of random numbers: " ;
    std::cin >> cnt ;
    if( cnt > ARRAY_SZ ) cnt = ARRAY_SZ ;
    
    // note: generate a fresh random number for each element in the array
    for( int i = 0; i < cnt ; ++i ) array[i] = 1 + std::rand() % 100; 

    std::cout << "the random numbers are: " ;
    for( int i = 0; i < cnt ; ++i ) std::cout << array[i] << ' ' ;
    std::cout << '\n' ;
}
Hello cash,

Keeping with what you started with this is my revised version. Again read the comments, Some have changed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include<cstdlib>
#include<ctime>

//using namespace std;  // <--- Not the best idea to use this.

int main()
{
	constexpr int length = 100;  // <--- Should be "const int length" variable.
	int array[length]{}; // <--- {} Initializes all elements to zero.
	int input{};  // <--- Should be initialized to zero. Defined, but never used.
	int number_of_random_numbers = 0;

	srand(size_t(time(0))); // <--- You could use size_t (another name for unsigned int) or "unsigned int", but not just unsigned

	int randomNum = 1 + rand() % 100;  // <---  Defined, but never used.

	std::cout << "\n Enter input. Do not know what this is for: ";  // <--- Beginning prompt vague and "input" unused.
	std::cin >> input;

	std::cout << "\n Enter how many numbers do you want: ";  // <--- Better prompt heeded. This could allow the user to enter a number larger than 100. Added space and removed "endl"
	std::cin >> number_of_random_numbers;

	// Code to validate "number_of_random_numbers". Should be greater than zero and less than 101.
	// You could use a while loop here.

	// for loop puts the same number into each element of the array. This is where you should use "rand()".
	for (int i = 0; i < number_of_random_numbers; i++)
		array[i] = rand() % 100 + 1; // <--- Same as above, but more conmanly written this way.

	// Used for testing the array.
	for (size_t lc = 0; lc < number_of_random_numbers; lc++)
	{
		std::cout << " " << array[lc] << " ";
	}

	std::cout << std::endl;

	return 0;  // <--- Not needed, but good programming
}


JLBorges has an even more updated and shorter version that is worth looking at. I would even keep that for future reference.

Hope that helps,

Andy
Thanks, a lot, I feel like I understand it much better now.
Topic archived. No new replies allowed.