0

Hi All, I'm stuck on my C++ assignment. I have no idea how to finish it...I think I'm missing something in my code such as how to double the size of the argument array and copy it, then initialize the unused elements of the second array with 0. thanks in advance

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 in a complete program. Print the contents of the array inside the main function.

Here's my code

#include "stdafx.h"
#include <iostream>
using namespace std;

void showValues(int[], int);	// Function prototype


int main()
{
	const int SIZE = 4;
	int numbers[SIZE] = {1, 2, 3 ,4};
	showValues(numbers, SIZE);
	
	// Twice the size of argument array
	int * dynamic;
	dynamic = new int[SIZE * 2];

	// Copy to the content
	for(int i = 0; i < SIZE; i++)
		for(int j = SIZE; j < SIZE * 2; j++)
			cout << endl;
	return 0;
}

//***************************************************
// Definition of function showValue.				*
// This function accepts an array of integers and	*
// the array's size as its arguments. The contenets	*
// of the array are displayed.						*
//***************************************************

void showValues(int nums[], int size)
{
	for(int index = 0; index < size; index++)
		cout << nums[index] << " ";
	cout << endl;
		
}

Edited by Nick Evan: Added code-tags

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Lerner
0

Do you know how to use the new operator to allocate an array? If yes, then just allocate an array that is twice the size as the array argument, such as size * 2 You will need a function something like this:

int* expand(int nums[], int size)
{

}

Edited by Ancient Dragon: n/a

0

Please learn how to use code tags when posting code to this board.

Change this:

// Copy to the content
for(int i = 0;  i < SIZE;  i++)
for(int j = SIZE;  j < SIZE * 2;  j++)

to this:

for(int i = 0;  i < SIZE;  i++)
  //assign element from old array to new array;

for(remainder of elements in new array)
   //assign each remaining element the value of zero.

Edited by Lerner: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.