Question

A function that takes three parameters: an int array, an int n that is the size...

A function that takes three parameters: an int array, an int n that is the size of the array and an int pointer max that is to assign the maximum value in the array. The function will find and return the index of the first occurrence of the maximum value in the array and assign the maximum value to the pointer. For example, in the array {1, 2, 6, 5, 6, 4, 3, 6, 4}, the maximum value is 6 and the index of the first occurrence of the maximum is 2. (3 b) A function that takes two parameters: an int array, an int n that is the size of the array, and an int pointer max that is to assign the maximum value in the array. The function will find and return the index of the last occurrence of the maximum value in the array and assign the maximum value to the pointer max. For example, in the array {1, 2, 6, 5, 6, 4, 3, 6, 4} the index of the last occurrence of the maximum value 6 is 7. in c

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

Please up-vote and in case of doubt you can comment ....

The code snippet :

Code snippet for two functions:

Calling the two functions in main:

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Code:

#include <stdio.h>

int first_function(int array[], int n,int *max_value_first) { // the first function
   int index=0;
   *max_value_first = array[0]; // initializing the values
   int i;
   for(i=1;i<n;i++)
   {
       if (array[i] > (*max_value_first)) // checking if the current elemnent is greater than the previous max
       {
           *max_value_first =array[i]; //updating the index and max value
           index=i;
       }
   }
   return index;
}

int second_function(int array[], int n,int *max_value_second) { // the second function
   int index=0;
   *max_value_second = array[0]; // initializing the values
   int i;
   for(i=1;i<n;i++)
   {
       if (array[i] >= (*max_value_second)) // checking if the current elemnent is greater than equal to the previous max
       {
           *max_value_second =array[i]; //updating the index and max value
           index=i;
       }
   }
   return index;
}

int main() {
  
   int n; // size of the array
   printf("enter the size of array");
   printf("\n");
   scanf("%d",&n);                   // taking the input from user
   int array[n]; // initializing a n size array
   printf("enter the elements of array");
   printf("\n");
   int i;
   for ( i=0 ;i <n ; i++)
   {
       scanf("%d",&array[i]); // taking the input from user and storing it into the array
         
   }
   printf("\n");
  
   int max_value_first;
   int index_of_first;
   index_of_first=first_function(array,n,&max_value_first); // calling the first function
   printf("The max value for first_function is %d \n",max_value_first);   
   printf("The first occurence of max value is at index %d \n",index_of_first); // printing the outputs   
  
   
   int max_value_second;
   int index_of_last;
   index_of_last=second_function(array,n,&max_value_second); // calling the second function
   printf("The max value for second_function is %d \n",max_value_second);
   printf("The last occurence of max value is at index %d \n",index_of_last); // printing the outputs
  
   return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------

Output :

Add a comment
Know the answer?
Add Answer to:
A function that takes three parameters: an int array, an int n that is the size...
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
  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

  • C language 3. (10 pts) Write a function readSegment that takes three parameters, a float array...

    C language 3. (10 pts) Write a function readSegment that takes three parameters, a float array as a pointer p as the first parameter, and two int indices startldx and endldx. The function readSegment reads the content of the array between startIdx and endldx, without using additional local variables. 4. (10 pts) Write a function called doubleOdds that takes two parameters, an array of double pointed to by pointer p and endldx of int type. The function doubleOdds doubles (multiply...

  • Create a function that takes two parameters : pointer to the array and the size of...

    Create a function that takes two parameters : pointer to the array and the size of the array. Print out the array's values and their memory addresses inside the function. Then create another function that takes two parameters : pointer to the array. Inside the function, multiply each of the array's values by two ad print the modified values out in the main function. With C programming language

  • Must be C++ 11 Write a function named insertinArray that takes the following parameters: list: an...

    Must be C++ 11 Write a function named insertinArray that takes the following parameters: list: an integer array index: index at which to insert the new item numitems: number of items currently in the array arrayCapacity: capacity of the array newVal: value to insert into the array If the array is at capacity then the function should return-1. Otherwise, insert newVal at index and return O for success int insertInArray(int listl 1, int index, int numItems, int arrayCapacity, int newVal)...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • -Create a function output() in C that takes the pointer to the array and its size...

    -Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (malloc format) (function prototype : void output(int *arrayPtr, int size); -Create a function check() in C that takes the pointer to the array and a number and checks if the number is in the array. If the number is in the array, returns the index of the element of the array holding the number. Otherwise, returns -1.

  • The function takes three parameters: string array, int arraySize, and string word The function will loop...

    The function takes three parameters: string array, int arraySize, and string word The function will loop through the string array. If word is found in the array, return the matched element’s position (index) in the array. Otherwise, return -1. When a match is found in the array, the loop should be terminated at the time --- no need to continue the loop. If no match is found, the function should display all elements in the array, as well as the...

  • 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: "...

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