Having problems with codes

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.
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
  #include <iostream>
#include <time.h>
using namespace std;

void PrintArray(int nArray[]){

	int length = sizeof(nArray);
	for (int i = 0; i < length; i++){
		cout << nArray[i] << " ";
	}
	cout << endl;
}

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;
} 
How come you didn't just set up your for loop inside your PrintArray Function like this if you wanted to print out all five elements of numArray?

1
2
3
4
5
for (int i = 0; i < 5; i++){
		cout << nArray[i] << " ";
	}
	cout << endl;
}
@JoanT

I don't understand your code, compared to what you say you're supposed to do.
I'm supposed to write a program to create an array of size 20 to store integers.

You then create an array of 5.
int numArray[5];

Then, at the end..
Then, the program should print the 4 elements from that location.

But each array location, will hold just one number. What 4 elements in the array?


Hi,

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
      const unsigned int 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
    const unsigned int 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[] , const unsigned int 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.

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 <time.h>
#include <ctime>

using namespace std;  // try not to use this Google to see why not, and what to do instead


void PrintArray(int nArray[],  const unsigned int 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[],  const unsigned int length) {

	int length = sizeof(nArray);
	for (int i = 0; i < length; i++){
		cout << nArray[i] << " ";
	}
	cout << endl;
}


Hope all goes well :+)
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?

I don't really understand either.
it's pretty clear to me, if the location was 10, then elements at 10, 11, 12 ,13 are printed.
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]?
Maybe this is the one youre expecting. I hope this would help you.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    int row = 20;
    int column = 4;

    int numArray[row][column];
    int ranNum;

    srand(time(NULL));

    for (int i = 0; i < row; ++i)
    {
        for(int j = 0; j < column; ++j)
        {
            ranNum = rand() % 7 + 1;
            numArray[i][j] = ranNum;
        }
    }

    //Print 4 number/element in a randomized row
    int i = rand() % 20;
    std::cout << "numArray[" << i << "] contains: ";
    for(int j = 0; j < column; ++j)
        std::cout << numArray[i][j] << " ";
    std::cout << std::endl;

    return 0;
}
Last edited on
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.

A sample run of the program is shown below:

Sample output #1:

Array: 2 7 4 3 1 5 7 2 3 6 2 7 1 3 2 4 5 3 2 6
Random position: 2
Subset: 4 3 1 5
Here's the modified version. Just refactor and/or arrange it according to your code preference. I hope this would help you.

Source code:
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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    int maxNumberOfElements = 20;
    int nextNumberOfElements = 4;

    int numArray[maxNumberOfElements];
    int randomPosition;
    int index;

    srand(time(NULL));

    //Set Values
    for (int i = 0; i < maxNumberOfElements; ++i)
        numArray[i] = rand() % 7 + 1;
    randomPosition = rand() % maxNumberOfElements;
    index = randomPosition;

    //Print
    std::cout << "Array: ";
    for (int i = 0; i < maxNumberOfElements; ++i)
        std::cout << numArray[i] << " ";
    std::cout << "\nRandom position: " << randomPosition;
    std::cout << "\nSubset: ";
    for(int counter = 0; counter < nextNumberOfElements; ++counter)
    {
        std::cout << numArray[index] << " ";
        (index != (maxNumberOfElements - 1)) ? ++index : index = 0;
    }
    std::cout << std::endl;

    return 0;
}


Program Output:

- 1st run
Array: 2 4 2 1 6 2 7 2 4 4 2 2 7 7 5 2 2 5 2 6
Random position: 2
Subset: 2 1 6 2


- 2nd run
Array: 6 7 7 5 1 3 1 1 5 5 7 1 2 6 2 3 7 7 7 4
Random position: 18
Subset: 7 4 6 7


- 3rd run
Array: 7 3 5 3 1 6 7 7 7 5 7 5 6 3 5 4 4 4 6 4
Random position: 4
Subset: 1 6 7 7
Last edited on
Topic archived. No new replies allowed.