I'm supposed to write a program to create an array of size 20 to store integers. Then, the program should generate and insert random numbers between 1 and 7, inclusive into the array. Next, the program should print the array as output. The program will then generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the program should print the 4 elements from that location.
This are the codes I did so far but im not sure how to proceed to the next step.
You might benefit from writing some pseudo code. Start by writing comments that are general, then go back and add refinements until you are happy to convert them into code. I will start by using text from the assignment:
1 2 3 4 5
// create an array of size 20 to store integers
// generate and insert random numbers between 1 and 7, inclusive into the array.
// print the array as output.
// generate a random number between 0 and 19, which represents the location in the array
// print the 4 elements from that location.
Now the next step:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// create an array of size 20 to store integers
constunsignedint ArraySize = 20; // use a variable not magic numbers throughout code
int NumberArray[ArraySize];
// generate and insert random numbers between 1 and 7, inclusive into the array.
// function InitilaseArray - make this function
constunsignedint MaxArrayValue = 7;
// your code lines 17 - 20 Ok, but use ArraySize , not 5.
// print the array as output.
// function PrintArray - pass the size of it as a parameter
void PrintArray(int nArray[] , constunsignedint length){
// call the function using ArraySize as a parameter
// generate a random number between 0 and 19, which represents the location in the array
// you know how to do this, make use of ArraySize
// print the 4 elements from that location.
// const unsigned int SubSequenceLength = 4;
// do some error checking, print a warning if number is less than 4 from the end
// print the numbers and warning if necessary
When one writes pseudo code, it helps to keep things organised, avoids missing details (like array size 20 not 5), identify what code should be in a function, where loops should be.
Also it is good form to put function declarations before main(), and the function definitions after main(), that way we don't have to go looking for where main() is, it's always near the top. Having function declarations makes it easier for the compiler to identify problems.
#include <iostream>
#include <time.h>
#include <ctime>
usingnamespace std; // try not to use this Google to see why not, and what to do instead
void PrintArray(int nArray[], constunsignedint length);
int main() {
int numArray[5];
srand(time(NULL));
for (int i = 0; i < 5; i++){
int ranNum = rand() % 7 + 1;
numArray[i] = ranNum;
}
PrintArray(numArray);
return 0;
}
void PrintArray(int nArray[], constunsignedint length) {
int length = sizeof(nArray);for (int i = 0; i < length; i++){
cout << nArray[i] << " ";
}
cout << endl;
}
I think he means size 20 as in 20 bytes in total which on his computer is maybe 4 bytes. Knowing that he needs to store integers, he creates 5 elements storing 4 bytes each adding up to 20 bytes.
But as for:
But each array location, will hold just one number. What 4 elements in the array?
Well he only has 5 elements in the array which he created and initialized. Correct me if I'm wrong, but if he only created an array with 5 elements, how will he be able to access the values outside of the range like numArray[5,6,7,8,9 ... 18,19]?
Thanks for the help. Sorry.. I think I didn't explain the question well. I will copy paste the question here.
Write a program to create an array of size 20 to store integers. Then, the program should generate and insert random numbers between 1 and 7, inclusive into the array. Next, the program should print the array as output.
A simple subset is part of an array that consists of a set of 4 elements next to each other. The program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the program should print the 4 elements from that location. The program should take into consideration the boundaries of the array. There is no user input for this program.
Your program must include, at least, the following methods:
• insertNumbers, which will take as input one integer array and store the random numbers in it.
• computeLocation, which will generate the location random number and return it.