Question

In C ++ How to find the Index of the Largest value in array such as...

In C ++

How to find the Index of the Largest value in array

such as we have 5 values which are 21 34 24 123

so how to find the index of 123

Homework Answers

Answer #1
#include<iostream>

using namespace std;

int getMaxIndex(int* arr, int size){
   int maxIndex = 0;
   for(int i = 0;i<size;i++){
      if(*(arr+maxIndex) < *(arr+i)){
         maxIndex = i;
      }
   }
   return maxIndex;
}

int main() {
   int arr[] = {21,34,24,123};
   int size = 4;
   int index = getMaxIndex(arr, 4);
   cout<<"Max index = "<<index<<endl;
   return 0;
}

​​​​​​​

Know the answer?
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
Similar Questions
How to use C++ figure this question? Collection and Sorted Collection An array is great for...
How to use C++ figure this question? Collection and Sorted Collection An array is great for storing a list of values. However, one challenge when working with arrays is they have a fixed size. If you declare an array of size 10 and later need to actually store 11 elements you have to create a new array and copy everything over. C++ (and other langauges) create classes that handle this and other operations behind the scenes. In this assignment, we...
Question 5: Recommend / Explain a C++ program which uses an array of 20 integers whose...
Question 5: Recommend / Explain a C++ program which uses an array of 20 integers whose input is taken by user, the array is passed to a functions named i.e. smallest(int A[20) and largest(int B[20]) to determine minimum and maximum values respectively. Also create a function named modify(int *p) which modifies the value at the index given by user.
Write a C program to find the sum of contiguous subarray within an int array which...
Write a C program to find the sum of contiguous subarray within an int array which has the largest sum. A subarray may have a length from 1 to the length of the array. For example, if the array is [1, -3, 2, 9, -2, 10], then the subarray with the largest sum is [2, 9, -2, 10], so your program should print the sum of the subarray which is 19. Hint: Your function should call sumOfSubarray().
Write a method name maxElement, which returns the largest value in an array that is passed...
Write a method name maxElement, which returns the largest value in an array that is passed as an argument. The method must use recursion to find the largest element. Demonstrate the method in a program. Write the JAVA CODE on the paper that is provided. (Put your name on every piece of paper) Use the following array to test the method. Int [] numbers = {2,12,1999,99,100,4,7,300} PROGRAMMING LANGUAGE- JAVA. Please answer the question ASAP. Thanks in advance!
C++ Write a recursive routine that will have a character array and an index as parameters...
C++ Write a recursive routine that will have a character array and an index as parameters and will return the count of all vowels (assume lowercase). You may assume that the index starts out at the END of the array.
What are the legal array index values? What is a bounds error? What is an instance...
What are the legal array index values? What is a bounds error? What is an instance method, and how does it differ from a static method? How many constructors can a class have? Can you have a class with no constructors? If a class has more than one constructor, which of them gets called?
USE C++ Write a sum() function that is used to find the sum of the array...
USE C++ Write a sum() function that is used to find the sum of the array through pointers. In this program we make use of * operator. The * (asterisk) operator denotes the value of variable. Input: array = 2, 4, -6, 5, 8, -1 Output: sum = 12
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
The array a[1..n] contains arbitrary integers. Write C++ function reduce(a,n) that reduces the array a[1..n] by...
The array a[1..n] contains arbitrary integers. Write C++ function reduce(a,n) that reduces the array a[1..n] by eliminating from it all values that are equal to three largest different odd integers. For example, if a[ ]={9,1,1,6,7,1,2,3,3,5,6,6,6,6,7,9} then three largest different odd integers are 5,7,9 and after reduction the reduced array will be a[ ]={1,1,6,1,2,3,3,6,6,6,6}, and n=11. If you have less than 3 largest different odd integers, eliminate only those that you found.
IN JAVA please Given a sorted array and a target value, return the index if the...
IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT