Question: write a function, createArray, that takes an integer parameter and returns a pointer to an array whose size is specified by the function's parameter. Its
write a function, createArray, that takes an integer parameter and returns a pointer to an array whose size is specified by the function's parameter. Its signature is int* createArray(int n).
write a function, resizeArray, that takes a array of one size and changes its size to another size. Its signature is void resizeArray(int * &array, int old_size, int new_size).
write and test a function, generateSquares, that generates an array of the squares of the integers from from 1 to n where n is a positive integer. Its signature is int* generateSquares(int n);
write a function, reverseArray, that reverses the order of the elements of an array. It signature is void reverseArray(int * array, int n);
write a function, partitionSquares that takes an array of squares and distributes the squares among two other arrays so that the sum of the contents of the two arrays equal or as close to equal as possible. Its signature is void partitionSquare(int n, int original[], int * &part1, int * & part2, int & size1, int& size2, int&sum1, int& sum2), where part1 and part2 are arrays of squares, size1 and size2 are the sizes of the squares, and sum1 and sum2 are sums of the contents of the array.
Write a function, showPartition, that displays the partition of the squares. The function's signature is void showPartition(int part1[], int part2[], int size1, int size2, int sum1, int sum2);
Write a program that reads a number n from an input device, partitions the squares of the numbers from 1 to n, and displays the partitions. This is what I have! Can someone write this in a different way with an output (screenshot)? (Data Structure C++ language)
Source.cpp
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
default_random_engine dre(0);
uniform_int_distribution
void randomFill(int *ia, int size)
{
for (int i = 0; i < size; i++)
ia[i] = uid(dre);
}
void showArray(int a[], int size)
{
for (int i = 0; i < size; i++)
cout << a[i] << ' ';
}
int* createArray(int n)
{
int *ia = nullptr;
if (n > 0) ia = new int[n];
return ia;
}
void resizeArray(int *&old_array, int old_size, int new_size)
{
int *new_array = createArray(new_size);
int size = min(old_size, new_size);
for (int idx = 0; idx < size; ++idx)
new_array[idx] = old_array[idx];
delete [] old_array;
old_array = new_array;
}
int * generateSquares(int n)
{
int *ia = createArray(n);
for (int i = 1; i <= n; i++)
ia[i - 1] = i * i;
return ia;
}
void reverseArray(int * array, int n)
{
for (int i = 0; i < n / 2; ++i)
{
int temp = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
void partitionSquares(int n, int orig[], int * &part1, int * & part2, int & size1, int& size2, int&sum1, int& sum2)
{
part1 = createArray(n);
part2 = createArray(n);
size1 = 0;
size2 = 0;
sum1 = 0;
sum2 = 0;
for(int i = 0; i < n; i++)
if (sum1 < sum2)
{
sum1 += orig[i];
part1[size1] = orig[i];
size1++;
}
else
{
sum2 += orig[i];
part2[size2] = orig[i];
size2++;
}
}
void showPartition(int part1[], int part2[], int size1, int size2, int sum1, int sum2)
{
cout << sum1 << ' ' << sum2 << endl;
cout << size1 << ' ' << size2 << endl;
for (int idx = 0; idx < size1; idx++)
cout << part1[idx] << ' ' << part2[idx] << endl;
}
int main()
{
int *pi = createArray(5);
randomFill(pi, 5);
showArray(pi, 5); cout << endl;
resizeArray(pi, 5, 10);
showArray(pi, 10); cout << endl;
resizeArray(pi, 10, 5);
showArray(pi, 5); cout << endl;
delete[] pi;
pi = generateSquares(8);
showArray(pi, 8); cout << endl;
reverseArray(pi, 8);
showArray(pi, 8); cout << endl;
int *squares = generateSquares(32);
int *partition1, *partition2;
int size1, size2, sum1, sum2;
partitionSquares(16, squares, partition1, partition2, size1, size2, sum1, sum2);
showPartition(partition1, partition2, size1, size2, sum1, sum2);
return 0;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
