//Created by: Noor Hannani Syamimi and Thuvaaritha
//Program: Assignment 3 & 4 

//program library 
#include <fstream>
#include <iostream>
using namespace std;

//function prototype 
int *expandArray(int[], int); 
void showArray(int[], int);

int main()
{
	const int SIZE = 50; //constant variable for size
	int values[SIZE]; //declaring an array called values
	int n; // declaring integer n
    
    /************************************************************/
	/*reads an interger n from standard input                   */
	/*this n input is to specify the size of the 'values' array */
	/************************************************************/
    cout << "input data: ";
	cin >> n;
	if (n<0 || n>=SIZE)
		return 0;
	
	/********************************************/
	/*reads an interger n from file named data  */
	/********************************************/
	ifstream inFile; // declaring file name 
	
	inFile.open("data.txt"); //opening a file
	
	cout << "The original array: " << endl; //displaying string
	
	//using for loop to store array of integer from the file
	for (int k=0; k < n; k++)
	{
		inFile >> values[k]; //storing the element from the vile to 'values' array
		cout << values[k] << " " << endl; //displaying the elements
	}
	
	inFile.close(); //closing the file

	//Make an expanded copy of the array.
	int *arrCopy = expandArray(values, n);

	// Display the contents of the new array.
	showArray(arrCopy, n*2);
    
	return 0;
}

// ********************************************************
// The expandArray function accepts an int array and an   *
// int indicating the array's size. The function returns  *
// a pointer to an array that is twice the size of the    *
// array that was passed as an argument. The elements of  *
// the argument array are copied to the new array, and    *
// the remaining elements are set to 0.                   *
// ********************************************************
int *expandArray(int arr[], int size)
{
    // Make sure the size is positive and
    // non-zero.
    // COMPLETE THIS (2 Marks)
	if (size <= 0)
	{
		exit(0); //program will exit if the size is wrong
	}

    // Allocate an array twice the size
    // of the array that was passed as
    // an argument.
    // COMPLETE THIS (2 Marks)
    
	int *newArray = new int [size * 2]; //allocate new size of array to newArray 
    
    // Copy arr's elements to the new array.
     // COMPLETE THIS (4 Marks)
    for (int i = 0; i < (size * 2); i++)
    {
    	newArray[i] = arr[i];
	}
    
    // Set the remaining elements to 0.
    // COMPLETE THIS (4 Marks)
    for (int i = size; i < (size * 2); i++)
    {
    	newArray[i] = 0;
	}
	

    // Return a pointer to the new array.
    // COMPLETE THIS (1 Marks)
    return newArray;
    
    //deallocate the memory after done with the execution
    delete[] newArray;
    newArray = NULL; //now the array is null (array not pointing to anything)
}

// ********************************************************
// The showArray function displays the contents of an int *
// array.                                                 *
// ********************************************************
void showArray(int arr[], int size)
{
    // Display elements of new array. 
    // COMPLETE THIS (4 Marks)
    cout << "\n\nThe new array: " << endl;
	for (int i = 0; i < size; i++)
    {
    	cout << arr[i] << " " << endl;
	}
}

