C++ Bài tập Pointer - Phân tích array

Threads: C++

GOAL

  • Write a program that reads a variable number of integers (from 1 to 15 integers) from user input, into an array in main(), and then passes that array to a function analyzeValues().
  • Your analyzeValues() function will return statistics about the numbers entered, via pass-by-pointer arguments you will pass to the analyzeValues() function from main().
  • In main(), print out the results of the analysis as shown below in the sample output section below, using the values that are returned from your function via their pointers.
  • All variables passed to your function must be declared in main().

DETAILS

  • Your function must match the following prototype:
void analyzeValues(int nums[], int count, int *minVal, int *maxVal, bool* isLastEven, char* hexChar,  double *ave); 
  • An analyzeValues() function prototype is defined for you above main(). Implement the function below main to match the prototype provided.
  • As you can see from the analyzeValues() prototype, you will pass an int array, along with pointers to variables that you must declare in main().
  • Note that analyzeValues() does not return a value.

analyzeValues() arguments are:

  • INPUT: int nums[]: a collection of up to 15 values (but may be less)
  • INPUT: int count: an integer that indicates how many integers are stored in nums
  • OUTPUT: int * minVal: a pointer to the smallest value you find in nums[]
  • OUTPUT: int * maxVal: a pointer to the largest value you find in nums[]
  • OUTPUT: bool * isLastEven: a pointer to a boolean value indicating if the last value entered was even or odd (not including the negative termination value)
  • OUTPUT: char * hexChar: a pointer to a lower-case character that represents the value of count in its hex (base 16) equivalent: '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', or 'f'. (from 1 to 15).

REQUIREMENTS

  • In main(), the input loop that collects integer values from the user must "break" when it receives an input value that is less than zero, so that it can call the analyzeValues() function.
  • You must print your results from main() after returning from your analyzeValues() function. Your function must NOT print any results.
  • You should not include the negative terminating integer in your collection of numbers that are sent to the function.
  • Your output text must exactly match the prompts shown below to help your unit tests pass.
  • Your computed average must be the correct double-type average (integer division will yield an incorrect result).
  • You should test for zero entered user values (that is, when only a negative value is entered) and print an error message as shown below (see sample output), rather than calling your function. (You should not attempt to print statistics from your function call). Do not "exit" the program, as that will prevent unit tests from running).

HINTS

  • main() must pass the address of each argument by using the address-of operator &, when you call your function.
  • Your function must use the dereference operator * to access and change values, based on basic statistical analysis it will perform.
  • Use the string prompt variable provided to ensure your unit tests will pass
  • https://en.cppreference.com/w/cpp/language/ascii will provide you with ascii values to help with your hex conversion.
  • The function argument count points to an integer, and function argument hexChar points to a character.
  • The ASCII value for '0' (zero) is not zero, it is 48.
  • You cannot compute a correct double-type average by dividing two integer values.
  • Match the prompts and output format shown in the examples below to ensure your unit tests will pass

SUBMISSION

Test your results using the Unit Test provided. Submit your assignment via this repl class when you are satisfied with your results.

SAMPLE OUTPUT

Example 1

Enter up to positive 15 numbers, separated by spaces, ending with a negative value to stop):  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -1
Min value is 1
Max value is 15
The last value is odd
The hex character for 15 is f
The average value is 8

Example 2

Enter up to positive 15 numbers, separated by spaces, ending with a negative value to stop):  1 2 3 4 -1
Min value is 1
Max value is 4
The last value is even
The hex character for 4 is 4
The average value is 2.5

copied!

Example 3

Enter up to positive 15 numbers, separated by spaces, ending with a negative value to stop): -1
Error: No values were entered

CONCEPTS COVERED

  • functions
  • pointers
  • pass-by-pointer
  • address operator
  • dereference operator
  • modulo
  • ASCII codes
  • 1D arrays
  • function prototypes
  • cin / cout
  • passing arrays to functions
  • double division vs integer division

Please login or register to see more...

Tags: C++

Reviews

Please login or register to comment