Question
c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled

7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write
1 6 2 -16 -16 2 -5 200 Index of maximum: 7 Number to Insert: 888 AFTER 1 6 2 -16 -16 2 -5 888 208 SECOND EXAMPLE BEFORE 3 -4
#include <iostream> using namespace std; 14 PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE 71 END OF FUNCTIONS void printArray(int
0 0
Add a comment Improve this question Transcribed image text
Answer #1

We can solve the given problem in C++ using following concepts/procedures

  1. The concept of call by reference. Call by reference is a way of passing the address of a variable to a function and therefore the original value gets modified, if changes are made inside that function. In the given problem we have to pass the address of our array and count variable, so that changes made inside insertNumber() function is reflected in the main() function
  2. For inserting the new element, we have to move the remaining elements by 1 step. For this, we start from the end of the array upto the index where we need to insert the new element and keep shifting elements by 1 position to the right.

Given below is the entire C++ file with the required functions and main():

Solution:

using namespace std;
#include <iostream>


int findMaxIndex(int array[], int count) {
  
// assume the maximum element is at index 0
int maxIndex = 0;
  
// loop from index 1 upto count-1
for(int i=1; i<count; i++) {
  
// check if any element is greater than element at index "maxIndex"
if(array[i] > array[maxIndex]) {
  
// if yes, over write maxIndex value with that element's index
maxIndex = i;
}
}
  
return maxIndex;
}

// we use * and & in the function parameter which indicates we pass arguments to this function by reference
// * is used when we need to pass array by reference
// & is used when we need to pass int by reference
void insertNumber(int *array, int& count, int number) {
  
// find the max element's index using the findMaxIndex() function
int maxIndex = findMaxIndex(array, count);
int i;
  
// loop from end of array upto the maximum element's index
for(i=count-1; i>= maxIndex; i--) {
// shift each element by 1 position to right
array[i+1] = array[i];
}
  
// insert the new element in correct index
array[++i] = number;
  
// update the count of the elements in given array
count++;
}

void printArray(int array[], int count) {
cout << endl << "---------------------" << endl;
for(int i=1; i <=count; i++) {
if(i%3 == 0)
cout << " " << array[i-1] << endl;
else
cout << " " << array[i-1];
}
  
cout << endl << "---------------------" << endl;
}

int main()
{
int index, number;
  
int array[200] = {1, 140, 32, 3, 140, 21, 3};
int count = 7;
  
cout << "BEFORE";
printArray(array, count);
  
index = findMaxIndex(array, count);
cout << "Index of maximum: " << index << endl;
cout << "Number to insert: ";
cin >> number;
  
insertNumber(array, count, number);
  
cout << "AFTER";
printArray(array, count);
return 0;
}

Given below is the screenshot of the complete code and sample output

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

CODE

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

using namespace std; #include <iostream> 5. int findMaxIndex(int array[], int count) { // assume the maximum element is at in24 / we use * and & in the function parameter which indicates we pass arguments to this function by reference 25 // * is used

58 int main() 59-{ int index, number; int array[200] = {1, 140, 32, 3, 140, 21, 3}; int count = 7; cout << BEFORE; printArr

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

OUTPUT

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

BEFORE 1 140 32 3 140 21 - - - - - - - - - - - - - - - - - - - - - Index of maximum: 1 Number to insert: 333 AFTER 1 333 140

Add a comment
Know the answer?
Add Answer to:
c++, we have to write functions for the code given below and other instructions for it...
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
  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

  • C++ Code Pass By Reference Practice Write the following functions for basic array operations based on...

    C++ Code Pass By Reference Practice Write the following functions for basic array operations based on the function descriptions given below. Write the following functions for basic array operations based on the function descriptions given below. FindMinArray: This function will search an array of integers (passed to the function as a parameter) for its minimum value, then return that value. SumArray: This function will sum an array of integers (passed to the function through a parameter) then return that sum....

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • 17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int...

    17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int array1[20] = {3, 18, 1, 25, 4, 7, 30, 9, 80, 16, 17}; int numElements = 11; cout << "Part 1" << endl; // Part 1 // Enter the statement to print the numbers in index 4 and index 9 // put a space in between the two numbers    cout << endl; // Enter the statement to print the numbers 3 and 80...

  • What's wrong with my code? : I'm trying to use recursive functions to display and count...

    What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • 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()...

  • Need done in C++ Can not get my code to properly display output. Instructions below. The...

    Need done in C++ Can not get my code to properly display output. Instructions below. The code compiles but output is very off. Write a program that scores the following data about a soccer player in a structure:            Player’s Name            Player’s Number            Points Scored by Player      The program should keep an array of 12 of these structures. Each element is for a different player on a team. When the program runs, it should ask the user...

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
ADVERTISEMENT