Possibly out of bounds? please help

So I have already turned this homework assignment in before knowing that it wouldn't work sometimes.

My problem with it is. When I type any number to generate random numbers it will print it out but sometimes it does not work.

I checked to make sure the size was being properly passed into the display array function and it is.

If somebody could help me with this probably that would be awesome and it would put my mind to rest.
-Thank 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <cstdlib>
#include <iostream>
using namespace std;

//Global Constants

//Function Prototypes
void introduction();
int getNumbers();
int *generateNumbers(int size);
void elementShifter(int numbers[], int size);
void displayNumbers(int numbers[], int size);

int main()
{
    int amount;
    
    int *numbers;
    
    introduction(); //Gives some info on the program.
    amount = getNumbers();
    
    cout << endl;
    cout << "Normal Array: ";
    numbers =  generateNumbers(amount);
    displayNumbers(numbers, amount);
    
    cout << endl;
    cout << "Shifted Array: ";
    elementShifter(numbers, amount); 
    
    cout << endl << endl << endl;
    system("pause");
    return 0;
}
//////////////////////////////////////////
//  This function is an intro giving    //  
//  information about what the (driver) //
//  program does.                       //
//////////////////////////////////////////

void introduction()
{
     cout << "Element Shifter" << endl
          << "---------------" << endl;
     cout << "An element shifter is when you have a random set of numbers" << endl
          << "and add a 0 infront of all the numbers in the array" << endl << endl;
     cout << "How many numbers would you like to use?" << endl; 
}

///////////////////////////////////////////
//     This function gets the amount     //
//     numbers that we will use.         //
///////////////////////////////////////////

int getNumbers()
{
    int getAmount;
    cout << "Amount: ";
    cin >> getAmount;
    
    return getAmount;
}

///////////////////////////////////////////
//   This function returns a pointer     //
//   to an array that is filled with     //
//   a certain amount of numbers.        //
//   Ranging in values of 1-20.           //
///////////////////////////////////////////
int *generateNumbers(int size)
{
    int *numbers;
    srand(time(0));
    
    numbers = new int(size);
    
    //Lets load a random number into each element of the array.
    for(int count = 0;count < size;count++)
        numbers[count] = rand() % 20 + 1;
    
    return numbers;
    
}

///////////////////////////////////////////
//   This function will shift the        //
//   original array up a size and        //
//   shift all of the numbers up a       //
//   subscript and put a 0 in the first  //
//   element.                            //
///////////////////////////////////////////

void elementShifter(int numbers[], int size)
{
    size +=1;
    int shiftedArray[size];
    int count = 0;
    
    //Initialize the first element to 0.
    shiftedArray[0] = 0;

    //Add the rest of the numbers from the 2nd element on...
    for(int x = 1;x < size;x++)
        shiftedArray[x] = numbers[count++];
        
    displayNumbers(shiftedArray, size);
}

void displayNumbers(int numbers[], int size)
{
     for(int count = 0;count < size;count++)
         cout << numbers[count] << " ";
}
Hi

First of all whenever you use the operator new, you have to free the allocated memory when your work is done with the varibale( pointers, memory addresses), you free it by
apllying delete [] variable-name(pointer name)

on line 76 you have numbers = new int(size); but I see no delete [] numbers at all-> and change numbers = new int(size) into
numbers = new int[size];

second what are you doing at line 97 int shiftedArray[size]; is size known at the compile time ? If not, that is the problem, instean use the operator new
, for example int* shiftedArray = new int[size];and free
the memory when your work is done, at the end of line 107, after calling displayNumbers, you
should add then delete [] shiftedArray

hope it helps
Last edited on
thank you very much that helped alot. and about line 97..the size is known at the compile time. It is passed in to the function.

It works flawlessly now :)
closed account (o1vk4iN6)
numbers = new int(size);

The reason you don't want to use that is you are creating one "int" and are initializing it to be the value of "size".
That is because there are parentheses around size right? i didnt even notice that i didnt use square braces
Topic archived. No new replies allowed.