Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ program from the following Array and Structs prototype: (It's a bit long but please help is much appreciated) For this homework, you

Write a C++ program from the following Array and Structs prototype: (It's a bit long but please help is much appreciated)

For this homework, you will write a C++ program with several functions that use arrays and structs. Write a main function that convinces you that your functions are working properly (you decide what goes in your main). Please use exactly the prototypes given in this assignment.

************************

void show( const unsigned a[], unsigned elements );

Output the # of elements in brackets, and the values separated by commas.

For instance,

unsigned a[] = {2, 4, 6, 8};

show( a, 4);

should output

[4]: 2, 4, 6, 8

bool isPermutation( const unsigned a[], unsigned elements );

Output whether the set of values is a permutation (that is, just a reordering) of the set of subscripts.

For instance,

unsigned a[] = {3, 0, 2, 1};

bool boo = isPermutation( a, 4 );

would set boo to true because the set of subscripts is {0, 1, 2, 3}, and the values are just a reordering of those values. On the other hand,

unsigned a[] = {3, 0, 2, 3};

bool boo = isPermutation( a, 4 );

would set boo to false because the set of subscripts is {0, 1, 2, 3}, but theres no value thats equal to 1.

void inverse( unsigned a[], const unsigned b[], unsigned elements );

Array a is parallel to array b. Make sure that b is a permutation by calling isPermutation. (If it isnt, then complain and

die (by which I mean output an error message and terminate the program by calling the exit function).) Set array a so

that any time b[i] is j, a[j] will be i.

For instance, if b were {3, 0, 2, 4}, then inverse( a, b, 4 ) would complain and die (not a permutation).

For instance, if b were {3, 0, 2, 1}, then inverse( a, b, 4) would set a to {1, 3, 2, 0}.

bool sorted( const unsigned a[], unsigned elements );

Return whether (when I say whether, I mean for the answer to be true or false) the values in a are in increasing order.

For instance, if a were {1, 5, 5, 7}, then the function would return true. (Duplicate values are ok)

For instance, if a were {1, 5, 3, 7}, then the function would return false. (3 should come before 5, not after)

void merge( unsigned combo[], const unsigned a[], unsigned aElements, const unsigned b[], unsigned bElements );

a is supposed to be sorted, according to the sorted function. If it isnt, then complain and die.

b is supposed to be sorted, according to the sorted function. If it isnt, then complain and die.

The number of elements in the combo array is aElements + bElements. merges job is to go through the a and b arrays, copying the elements from a and b into combo in such a way that combo ends up being sorted.

For instance, if a were {3, 5, 7, 7, 9} and b were {2, 5, 8, 1234} (so combo must have 9 elements), then this function will set combo to {2, 3, 5, 5, 7, 7, 8, 9, 1234}. Do this the efficient way: when you put values into combo, always put them in the right location; dont put the number in haphazardly and rearrange them later.

unsigned long long sum( const unsigned a[], unsigned elements, unsigned f( unsigned n ) );

The third parameter, f, represents a function specified by the caller. sums job is to apply the function f to each element of a, add up the return values, and return that sum.

For instance, suppose we had a function cube:

unsigned cube( unsigned n ){

return n * n * n; // Lets not worry about overflow here

}

Then if the main program said

unsigned a[] = {3, 5, 2, 1};

cout <

the program would output 161, because 33+53+23+13 = 161

(The return type is unsigned long long because if we add a bunch of unsigneds together, the answer might be too big to be an unsigned.)

Use the following struct type definition for the remaining functions:

struct ABC{

unsigned n;

char c;

double a[3]; // Is this ok, to have an array inside a struct? Absolutely!!

};

void show( const ABC & x );

Output the data in x.

For instance, if x held the data {18, W, {1.1, 2.2, 3.3}}, then show would output

{18, W, {1.1, 2.2, 3.3}}

It doesn't matter about how many digits are displayed after the decimal point. Dont worry about the c field containing a weird character that doesnt display prettily. Your function will output the braces, commas, and quotes so that the data looks like the above.

Youll notice that you already wrote a show function for this homework; theres no conflict with this show function, thanks to the magic of function overloading, because the functions have different argument types.

void show( const ABC arr[], unsigned elements );

Now we have a 3rd show function, but the arguments still distinguish them, so were ok. This functions job is to output all the structs in the array, like the previous show function, with each element being output per line.

For instance, if elements were 4, then wed have 4 ABCs to output, and the output might look like

{123, 'A', {1.1, 1.2, 1.3}}

{234, 'B', {2.1, 2.2, 2.3}}

{345, 'C', {3.1, 3.2, 3.3}}

{456, 'D', {4.1, 4.2, 4.3}}

Naturally, I expect that youll write this show function the easy way, which is to repeatedly call the previous show function.

double max( const ABC & x );

Return the largest double stored in the a array in x.

For instance, if x held the data {18, W, {1.1, 2.2, 3.3}}, then the function would return 3.3.

double max( const ABC arr[], unsigned elements );

return the largest double stored in any as of the array arr.

For instance, if arr held

{123, A, {1.1, 1.2, 1.3}}

{234, B, {2.1, 2.2, 2.3}}

{345, C, {3.1, 3.2, 3.3}}

{456, D, {4.1, 4.2, 4.3}}

then this function would return 4.3.

Naturally, I expect that youll write this max function the easy way, which is to repeatedly call the previous max function.

If elements == 0, complain and die. (If there arent any numbers, I dont know how we can sensibly talk about the largest number.)

void set( ABC & x, unsigned n, char c, const double a[3] );

sets job is to copy the data from the 2nd through 4th args into x.

For instance, if the arg n were 1234, the arg c were ?, and the arg a held {10.0, 20.0, 30.0}, then this function would set x to hold {1234, ?, {10.0, 20.0, 30.0}} (replacing whatever x might have had in it before the function was entered).

I happened to give the args after x the same names as the corresponding struct fields. Thats ok, and it also would have been ok if Id given them different names.

void get( unsigned & n, char & c, double a[3], const ABC & x );

More or less the opposite of the set function. gets job is to copy the data from the 3 fields of x into the 1st through 3rd args.

For instance, if x were {1234, ?, {10.0, 20.0, 30.0}}, then get would set n to 1234, set c to ?, and set a to {10.0, 20.0, 30.0}.

void set( ABC x[], const unsigned n[], const char c[], const double a[][3], unsigned elements );

This does the same thing as the other set function, but it does it with arrays of values instead of single values. x, n, c, and a are parallel arrays: x, n and c each have elements elements; a has elements rows. Each element of x is set according to the corresponding elements of n, c, and a. For instance, if n[17] were 1234, c[17] were ?, and a[17] were {10.0, 20,0, 30.0}, then set would set x[17] to {1234, ?, {10.0, 20.0, 30.0}} (assuming, of course, that elements > 17).

Naturally, I expect that youll write this function the easy way, which is to repeatedly call the other set function.

void get( unsigned n[], char c[], double a[][3], const ABC x[], unsigned elements );

Just like the last set function extends the first set function work with arrays of values, this get function extends the last get function to work with arrays of values. The arrays are parallel; each element of n is gotten from the corresponding element of x, each element of c is gotten from the corresponding element of x, and each row of a is gotten from the corresponding element of x. Naturally, I expect that youll write this function the easy way, which is to repeatedly call the other get function.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Understand some techniques for evaluating the HRM function

Answered: 1 week ago