How do you print a part of a large array?

Viewed 70

I'm attempting to use a pointer to memory allocated on the heap, print part of a large array, and call a function in a loop all in the same project on C++ for a school assignment, but I can't seem to print the elements inside of specific indexes.

I created an array with a max size of 200 and a pointer pointing to the array, so pointed to heap memory complete. As for the loop, I created a For loop that picks a random number between 1 and 400 (2 times the array's size, which in this instance is 200) and add it to the indexes of the array. When I cout the array itself, I get 200 random variables between 1 and 400, which is good, but when I cout specific indexes, I get a logic error that just prints whatever number is in the index, but 200 times. I only need it printed once, and I need to be able to print several indexes. For this I tried an if statement inside the loop a la "if(i = 10 || 11 || 12...) cout << myArray[i] << endl;" but that didn't seem to do anything different than just attempting to cout << myArray[10] << endl; by itself, which would just print the contents of index 10 200x.

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
      srand(time(NULL));

      const unsigned int sizeOfArray = 200;
      int *myArray = new int[sizeOfArray];

      for(int i = 0; i < sizeOfArray; i++)
      {
           myArray[i] = rand() % 400;
           cout << myArray[10] << endl;

      }

}
0 Answers
Related