Question: SAMPLE OF PROBLEMS: Program 9-16: Duplicates Arrays // This program uses a function to duplicate // an int array of any size. #include using namespace

 SAMPLE OF PROBLEMS: Program 9-16: Duplicates Arrays // This program usesa function to duplicate // an int array of any size. #include

SAMPLE OF PROBLEMS:

Program 9-16: Duplicates Arrays

// This program uses a function to duplicate

// an int array of any size.

#include

using namespace std;

// Function prototype

int *duplicateArray(const int *, int);

void displayArray(const int[], int);

int main()

{

// Define constants for the array sizes.

const int SIZE1 = 5, SIZE2 = 7, SIZE3 = 10;

// Define three arrays of different sizes.

int array1[SIZE1] = { 100, 200, 300, 400, 500 };

int array2[SIZE2] = { 10, 20, 30, 40, 50, 60, 70 };

int array3[SIZE3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Define three pointers for the duplicate arrays.

int *dup1 = nullptr, *dup2 = nullptr, *dup3 = nullptr;

Program 9-16 Cont.

// Duplicate the arrays.

dup1 = duplicateArray(array1, SIZE1);

dup2 = duplicateArray(array2, SIZE2);

dup3 = duplicateArray(array3, SIZE3);

// Display the original arrays.

cout

displayArray(array1, SIZE1);

displayArray(array2, SIZE2);

displayArray(array3, SIZE3);

// Display the new arrays.

cout

displayArray(dup1, SIZE1);

displayArray(dup2, SIZE2);

displayArray(dup3, SIZE3);

// Free the dynamically allocated memory and

// set the pointers to 0.

delete [] dup1;

delete [] dup2;

delete [] dup3;

dup1 = nullptr;

dup2 = nullptr;

dup3 = nullptr;

return 0;

}

ANOTHER EXAMPLE:

Program 9-16: Duplicates Arrays

// This program uses a function to duplicate

// an int array of any size.

#include

using namespace std;

// Function prototype

int *duplicateArray(const int *, int);

void displayArray(const int[], int);

int main()

{

// Define constants for the array sizes.

const int SIZE1 = 5, SIZE2 = 7, SIZE3 = 10;

// Define three arrays of different sizes.

int array1[SIZE1] = { 100, 200, 300, 400, 500 };

int array2[SIZE2] = { 10, 20, 30, 40, 50, 60, 70 };

int array3[SIZE3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Define three pointers for the duplicate arrays.

int *dup1 = nullptr, *dup2 = nullptr, *dup3 = nullptr;

Program 9-16 Cont.

// Duplicate the arrays.

dup1 = duplicateArray(array1, SIZE1);

dup2 = duplicateArray(array2, SIZE2);

dup3 = duplicateArray(array3, SIZE3);

// Display the original arrays.

cout

displayArray(array1, SIZE1);

displayArray(array2, SIZE2);

displayArray(array3, SIZE3);

// Display the new arrays.

cout

displayArray(dup1, SIZE1);

displayArray(dup2, SIZE2);

displayArray(dup3, SIZE3);

// Free the dynamically allocated memory and

// set the pointers to 0.

delete [] dup1;

delete [] dup2;

delete [] dup3;

dup1 = nullptr;

dup2 = nullptr;

dup3 = nullptr;

return 0;

ANOTHER EXAMPLE:

Program 9-16: displayArray Function

//**************************************************

// The displayArray function accepts an int array *

// and its size as arguments and displays the

*

// contents of the array.

*

//**************************************************

void displayArray(const int arr[], int size)

{

for (int index = 0; index

cout

cout

}

Program 9-12

// This program demonstrates that a pointer may be used as a parameter to accept the address of an array.

#include

#include

using namespace std;

// Function prototypes

void getSales(double *, int);

double totalSales(double *, int);

int main()

{

const int QTRS = 4;

double sales[QTRS];

// Get the sales data for all quarters.

getSales(sales, QTRS);

// Set the numeric output formatting.

cout

// Display the total sales for the year.

cout

cout

return 0;

}

Program 9-12

Functions

// Definition of getSales. This function uses a pointer to accept the address of an array of doubles.

// The function asks the user to enter sales figures and stores them in the array.

//*****************************************************************

void getSales(double *arr, int size)

{

for (int count = 0; count

{

cout

cout

cin >> arr[count];

}

}

//*****************************************************************

// Definition of totalSales. This function uses a pointer to accept the address of an array.

// The function returns the total of the elements in the array.

double totalSales(double *arr, int size)

{

double sum = 0.0;

for (int count = 0; count

{

sum += *arr;

arr++;

}

return sum;

}

PLEASE DO BOTH LAB9A AND LAB 9B

Laboratory 09: Duplicate and Reverse an Array Using Pointers Write a program that accepts, as input, a file of int, ("Gradelist.txt"), and stores it as an array. Use 50 for the Size of the resulting array Display the original array. Write a pointer function that accepts the int array, as well as the array's size as arguments. The function should create a copy of the array, except that the element values should be in reverse order in the copy. The function should return a pointer to the new array. Finally, display the new array showing that it is indeed in reverse order. Note: You will likely find Program 9-16 very helpful in the execution of this laboratory

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!