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:
srand(((unsigned)time(0)));
int randomNum= 1 + rand() % 100;
if(input==0){
cout << "Enter the length of the array:" << endl;
for (int i = 0; i<100; i++)
array[i] =randomNum ;
}
cout << "Enter the length of the array:" << endl;
You're not asking the user for the length of the array. You already know the length of the array. 100.
You're asking the user how many random numbers to put in it.
Get number_of_random_numbers from user. Use cin for this. make sure the number is between 1 and 100.
Loop over array:
1 2 3
for (int i = 0 ; i < number_of_random_numbers; +i)
{
...
// 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).
#include <cstdlib>
#include <ctime>
#include <iostream>
//using namespace std; <-- dangerous!! You introduce the entire std namespace
// in your program. Later you call a variable
// 'array'... Have you ever heard about std:array?
// http://en.cppreference.com/w/cpp/container/arrayint main()
{
// (a loop would be great from here...) <--------------//
// -------------------------------------------- //
// * 1) Asks user how many numbers to fill * //
// -------------------------------------------- //
std::cout << "How many random numbers do you want? "; //
int input = 0; // initialize every variable //
std::cin >> input; //
//
// --------------------------------------------------- //
// * 2) Validate the user entered number (1-100): * //
// --------------------------------------------------- //
if( /* ??? */) { //
// ??? //
// exit loop //
} else { //
// exit program (or... //
// ...restart loop) //
} //
// (to here...) <-------------//
// If we are here, it means you're sure user input is between 1 and 100:
int length = 100;
int array[length]; // why not simply "int array[100];"?
// --------------------------------------------------
// * 3) First fill all 100 elements with zeroes: *
// --------------------------------------------------
// ??? suggestion: a for loop would be fine
// ---------------------------------------------------------------------
// * 4) then insert as many random numbers as specified by the user *
// ---------------------------------------------------------------------
srand((unsigned)time(0));
// The logic is:
// a) start loop
// b) has the loop already created all the needed numbers?
// yes --> exit loop // no --> continue loop
// c) create a random number
// d) assign it to a proper position inside the array
// e) restart loop
// I'm afraid the following lines are wrong or in the wrong place.
int number_of_random_numbers = 0; // <-- You've already got this information
int randomNum = 1 + rand() % 100;
if(input==0){
cin >> number_of_random_numbers;
for (int i = 0; i< number_of_random_numbers; i++)
array[i] = randomNum ;
}
}
The problem is that you think, since you assigned a random value to randomNum, that whenever you assign randomNum to something else, that something is also going to be random - which is not true.
Observe the difference between the two:
Doesn't work:
1 2 3 4 5 6 7
int x[100]
int randomNum = 1 + rand() % 100; //randomNum is now let's say: 47 (happens only once)
for (int i=0; i<100; ++i)
x[i] = randomNum;
for (int i=0; i<100; ++i)
std::cout << x[i];
//now every number in x[] is 47.
Works:
1 2 3 4 5 6 7 8
int x[100]
for (int i=0; i<100; ++i) {
int randomNum = 1 + rand() % 100; //randomNum gets randomized every time
x[i] = randomNum;
}
for (int i=0; i<100; ++i)
std::cout << x[i];
//now every number in x[] is random within [1..100]
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace 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;
// ^- This should be in the for loop bellow before array[i]=randomNum
cout << "Enter input" << endl;
cin >> input;
if(input==0){ //<- This makes no sense - it should be (input!=0) instead.
cin >> number_of_random_numbers;
for (int i = 0; i< number_of_random_numbers; i++)
array[i] =randomNum;
}
}