#include <cstdlib>
#include <iostream>
usingnamespace 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 = newint(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] << " ";
}
First of all whenever you use the operatornew, you have to free the allocated memory when your work is done with the varibale( pointers, memory addresses), you free it by
apllyingdelete [] variable-name(pointer name)
on line 76 you have numbers = newint(size); but I see nodelete [] numbers at all-> and change numbers = newint(size) into numbers = newint[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 theoperatornew
, for example int* shiftedArray = newint[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