Question

Write in C.Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In this question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times. In the first sample, the two largest values are 9 and 8 (even though the value 9 appears twice). In the second sample, all the values are equal and the program recognizes this by printing a special message Array data 1 957283 19 4 Maximum value:9 second largest value: 8 Array data Maximum value: 2 All the values are equal

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
   
void maxNextmax(int arr[], int size){
   int max = -1, nextMax = -1, i;
   if(size > 0){
      max = arr[i];
      nextMax = -1;
   }
   for(i = 1;i<size;i++){
      if(max == arr[i]){
         continue;
      }
      else if(max < arr[i]){
         if(nextMax < max){
            max = nextMax;
         }
         max = arr[i];
      }
      else{
         if(nextMax < arr[i]){
            nextMax = arr[i];
         }
      }
   }
   printf("\nMaximum value: %d\n",max);
   if(nextMax == -1){
      printf("All the values are equal");
   }
   else{
      printf("Second largest value: %d\n",nextMax);
   }
}

int main()
{
   int arr[10];
   int i, size = 10;
   printf("Array data\n");
   for(i = 0;i<size;i++){
      scanf("%d",&arr[i]);
   }
   maxNextmax(arr,size);
    return 0;

}

\color{blue}Please\; up\;vote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
Write in C. Write a function that takes an array of integers and its size as...
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, 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...

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

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • (C++) Write a function, remove, that takes three parameters: an array of integers, the number of...

    (C++) Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the user to enter 10 digits...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • In C, write a method that takes in an array of integers and returns its size....

    In C, write a method that takes in an array of integers and returns its size. Q9]Write a method that takes in an array of integers and returns it's sizel10 points

  • Write a MIPS program that reads (with an appropriate prompt) a sequence of 20 integers and...

    Write a MIPS program that reads (with an appropriate prompt) a sequence of 20 integers and stores them in an array, and then calls the following two functions and prints the results in a readable format. The two functions are: smallestLargest: computes the smallest and the largest values in the array. oddEven: computes the number of even integers and the number of odd integers.

  • Write array methods that carry out the following tasks for an array of integers by completing...

    Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . } a. Swap the first and last elements in the array. b. Shift all elements to the right by one...

  • The purpose of this lab is to manipulate an array of integers. The assignment is to...

    The purpose of this lab is to manipulate an array of integers. The assignment is to write a program that: 1. Declares an array of integers of some maximum size 20. 2. Implements the following functions: ü A function that prompts the user to initialize the array with up to 20 non-negative whole numbers and mark the end of the input with a negative number. The entire array does not need to be initialized. The user may enter only as...

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