Question

I have to a C++ program I need help with. Can you break it into the...

I have to a C++ program I need help with. Can you break it into the two steps provided below.

Suppose MAX_SIZE is a global constant int variable that has been declared and initialized to an appropriate positive value.

1. int maxValue(const array<int, MAX_SIZE>&, int); is the prototype for a function that returns the value of the largest element in the array parameter. The array may be only partially filled. The int parameter indicates how many items are actually in the array, consecutively located beginning at index 0. For example, suppose the array contains the following 7 elements, with the remainder of the array unused and occupied by zeros:

1 10 3 5 10 4 9 0 0 ...

Under these circumstances, the maxValue() function should return the value 10.

Write the complete function definition, including function header, for the maxValue()function.

HINT: Assume the first element in the array is the largest. Then compare all other elements to largest, updating largest each time a bigger element is found.

2. void showNums(const array<int, MAX_SIZE>&, int); is the prototype for a function that displays all the elements in the array parameter, where the parameters of this function are the same as described in question 1. This function will display the elements of the array horizontally, each right-justified in a 6-character wide field, with at most 5 elements per line. Given the example array in question 1, the output would look like the following, where the yellow ruler line is only for reference and not part of the output:

123456123456123456123456123456 
     1    10     3     5    10
     4     9

Write the complete function definition, including function header, for the showNums()function.

Hint: Use cout << endl; whenever 5 elements have been displayed. Use % on the for-loop counter to determine when it's time to do this.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The solution to the above question is given below. With the screenshot of output attached.

=====================================================

I kept the logic simple and i have tested all outputs.

If there is anything else do let me know in comments.

=====================================================

============ CODE TO COPY ===============================

main.cpp

#include <iostream>
#include <iomanip>
using namespace std;

//  Global constant variable 
//  initialized to positive value 
#define MAX_SIZE 12

//  returns the value of the largest element in the  
//  array parameter. 
int maxValue(const int *array, int size); 

//   print array right justified six char wide
void showNums(const int *array, int size); 

int main() {
  
  int array[MAX_SIZE] = {1,5,4,7,8,9,6,4,11,21};

  cout << "Max value is : " << maxValue(array,10) << endl;
  cout << "Array is : "     << endl;
  showNums( array , 10 );
  cout << endl;
  return 0;
}

int maxValue(const int *array, int size){
  int max  = array[0];// assuming first element is largest
  for ( int i = 0 ; i < size ; i++)
    if ( array[i] > max )
      max = array[i];
  return max;
}

void showNums(const int *array, int size){
  for ( int i = 0 ; i < size ; i++)
  {
    cout << right << setw(6) << array[i];
    if ( ( (i+1) % 5 )  == 0  )
      cout << endl;
  }
}

=====================================================

Output :

Add a comment
Know the answer?
Add Answer to:
I have to a C++ program I need help with. Can you break it into the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • please help me fill out the blanks to the prewritten code for c++ 1. OBLJ UI...

    please help me fill out the blanks to the prewritten code for c++ 1. OBLJ UI SuuenLD, JUN, UML function find MultiMax that finds both the value ane handle ties, the find MultiMax function she are and assumed to be initialized alues should be as the returned values show 4. (30 pts) Use the space provided to write the function find Multima location of the maximum element of an array. To handle ties, the store the index locations in an...

  • PLEASE ANSWER ALL OF THEM, I REALLY APPRECIATE IT PLEASE IGNORE THE SELECTED ANSWERS Question 9...

    PLEASE ANSWER ALL OF THEM, I REALLY APPRECIATE IT PLEASE IGNORE THE SELECTED ANSWERS Question 9 3 pts Which best describes the meaning of a 1 (true) being output? Assume v is a large array of ints. int i; bool si for (i = 0; i < N_SIZE; Hi) { if (v[i] < 0) { S-true; else S = false; } cout << si O first value is negative all values are negative some value other than the last is...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • This is C++ Question 1 (12 points) (2 points each) Matching (match each answer only once)...

    This is C++ Question 1 (12 points) (2 points each) Matching (match each answer only once) match function type to function. Function header with a reference parameter 1. myFun(x); Function prototype with an array parameter 2. int myFun(int x) Function header that returns a value 3. void myFun(int &x) Function call 4. void myFun(int, int =0); 5. void myFun(int x) Function prototype with a default value 6. void myFun(int[], int); Function header with no return value

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • c++, need help thank you Given an array of ints, return the sum of all elements...

    c++, need help thank you Given an array of ints, return the sum of all elements from the array that come before the first element that equals number 4 in the array. The array will contain at least one 4. Function prototype: int pre4int array ( ], int size); Hint: to find the array size, use: int size = sizeof(array) / sizeof( array[0]); Sample runs: int array1[ ] = {1, 2, 4, 1); pre4(array1, 4); // returns 3 int array2...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT