Array Expander Function Using Pointers
Oct 7, 2014 at 10:38pm UTC
Still working on this same problem. My assignment is to create a function that accepts an array as an argument and creates an array that is twice the size.
The full problem: "Write a function that accepts an int array and the array ’s size as arguments . The function should create a new array that is twice the size of the argument array . The function should copy the contents of the argument array to the new array , and initialize the unused elements of the second array with 0. The function should return a pointer to the new array . Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an array ."
I've been trying to get this to work for a few days now. I am completely stuck, hopeless, lost. I don't know what is wrong. One error I keep seeing is invalid conversion from int* to int.
Any help would be appreciated. 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
#include <iostream>
#include <fstream>
using namespace std;
// Prototypes
void *expand(int [], int );
void showArray(int [], int );
int main()
{
const int SIZE = 50;
int values[SIZE] = {1, 2, 3, 4, 5};
int *arrayPtr = 0; // To point to an array
int n;
cin >> n;
if (n<0 || n>=SIZE)
return 0;
ifstream file;
file.open("data.txt" );
// Allocate an array to hold the test scores.
arrayPtr = new int [n];
showArray(values, n);
expand(arrayPtr, n);
file.close();
return 0;
}
void expand(int * arr[], int size)
{
int *copy = new int [size*2];
for (int index = 0; index < size; index++)
{
copy[index] = arr[index];
}
for (int index = size; index < size * 2; index++)
{
copy[index] = 0;
}
return copy;
}
void showArray(int values[], int size)
{
int *nums = values;
cout << "The elements of the array are: \n" ;
cout << *nums << " " ;
while (nums < &values[size - 1])
{
nums++;
cout << *nums << " " ;
}
}
Last edited on Oct 7, 2014 at 11:08pm UTC
Oct 7, 2014 at 11:24pm UTC
hope this helps
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int * expand(int [], int );
int main()
{
int n;
cout<<"Enter array size: " ;
cin>>n;
srand(static_cast <unsigned >(time(NULL)));
int randomArray[n];
int *expanded = new int [n*2];
for (int i = 0; i < n; i++) //fill randomArray
randomArray[i] = rand()%20;
expanded = expand(randomArray,n); //expand array
cout<<"Original array\n" ;
for (int i = 0; i < n; i++)
cout<<randomArray[i]<<" " ;
cout<<endl<<endl;
cout<<"Expanded array\n" ;
for (int i = 0; i < n*2; i++)
cout<<expanded[i]<<" " ;
cout<<endl;
system("pause" );
return 0;
}
int *expand(int a[], int n)
{
int *temp = new int [n*2];
//new fill the array with 0.
for (int i = 0; i < n; i++)
temp[i] = a[i];
return temp;
}
Oct 7, 2014 at 11:31pm UTC
About your updated question,
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
//read the first N numbers from a file
#include <fstream>
using namspace std;
ifstream input("data.txt" );
int N;
cout<<"Enter size: " ;
cin>>N;
if (N < 0 || N > 50)
{
cout<<"Invalid number" <<endl;
return -1; //not successful
}
int array[N];
int count = 0;
while (count <= N)
{
input>>array[count];
count++;
}
Now, continue to expand the array as in the code above.
Oct 8, 2014 at 12:59am UTC
You are a lifesaver. Thanks so much I was really struggling with this one.
Here is the final 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 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
#include <iostream>
#include <fstream>
using namespace std;
//Prototype
int * expand(int [], int );
int main()
{
int n;
cout<<"Enter array size to generate a random array: " ;
cin>>n;
if (n < 0 || n > 50)
{
cout << "Invalid number." << endl;
return 0;
}
int array[n];
int count = 0;
while (count <= n)
{
n >> array[count];
count++;
}
ifstream file;
file.open("data.txt" );
int *originalArray = new int [n];
int *expandedArray = new int [n*2];
expandedArray = expand(originalArray,n);
cout<<"Original array\n" ;
for (int i = 0; i < n; i++)
cout<<originalArray[i]<<" " ;
cout<<endl<<endl;
cout<<"Expanded array\n" ;
for (int i = 0; i < n*2; i++)
cout<<expandedArray[i]<<" " ;
cout<<endl;
//system("PAUSE");
file.close();
delete [] originalArray;
originalArray = 0;
delete [] expandedArray;
expandedArray = 0;
return 0;
}
// This function expands the array by doubling the array size.
int *expand(int arr[], int n)
{
int *temp = new int [n*2];
for (int i = 0; i < n; i++)
temp[i] = arr[i];
return temp;
}
Last edited on Oct 8, 2014 at 1:12am UTC
Oct 8, 2014 at 1:36am UTC
No. You got it all mixed up.
Follow this keenly.
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
#include <iostream>
#include <cstdlib>
#include <fstream> //input file
using namespace std;
int * expand(int [], int );
int main()
{
ifstream input("data.txt" ); //input file
int N;
cout<<"Enter size: " ;
cin>>N;
if (N < 0 || N > 50)
{
cout<<"Invalid number" <<endl;
return -1; //not successful
}
int array[N]; //original array
int *expanded = new int [N*2]; //new expanded array
int count = 0;
while (count <= N)
{
input>>array[count]; //read input to array
count++;
}
expanded = expand(array,N); //expand array
cout<<"Original array\n" ; //print original array
for (int i = 0; i < N; i++)
cout<<array[i]<<" " ;
cout<<endl<<endl;
cout<<"Expanded array\n" ; //print expanded array
for (int i = 0; i < N*2; i++)
cout<<expanded[i]<<" " ;
cout<<endl;
system("pause" );
return 0;
}
int *expand(int a[], int n)
{
int *temp = new int [n*2]; //create temporary array
for (int i = 0; i < n*2; i++) //fill array with 0
temp[i] = 0;
for (int i = 0; i < n; i++) //copy old array to new array
temp[i] = a[i];
return temp; //return new array
}
Oct 8, 2014 at 1:54am UTC
This works even better, thanks again!
Don't I have to delete the pointers to the array and initialize them to zero?
Topic archived. No new replies allowed.