Question

In: Computer Science

The code is bellow. follow this instructions to edit the code : lets assume that instead...

The code is bellow. follow this instructions to edit the code :

lets assume that instead of the maximum and minimum values, you want to find the 2nd largest/smallest. For example, in the numbers 1 to 10, this would return 2 and 9 .you will have to change the "max_min" function accordingly. this has a different puepose than "max_min", so you must give it another name.

code:

#include <stdio.h>
#define N 235


void max_min (int a[], int n, int *max, int *min);
int main()
{
int b[N],i,big,small,n;
printf("Enter the Number of elements in the array\n");
scanf("%d",&n);
printf("Enter %d Numbers: \n",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);

max_min(b,n,&big,&small);

printf("Largest %d\n",big);
printf("Smallest %d\n",small);
return 0;
}

void max_min(int a[], int n, int *max, int *min)
{
int i;
*min=*max=a[0];
for(i=1;i<n;i++)
{
if(a[i]>*max)
*max=a[i];
else if(a[i]<*min)
*min=a[i];
}
}

Solutions

Expert Solution

The modified code and the corresponding output are as follows:

#include <stdio.h>
#define N 235


void second_maxmin (int a[], int n, int *max2, int *min2);
int main()
{
        int b[N],i,n,big2,small2;
        printf("Enter the Number of elements in the array:\n");
        scanf("%d",&n);
        printf("Enter %d Numbers: \n",n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&b[i]);
    }
        
        second_maxmin(b,n,&big2,&small2);//function call
        
        printf("Second Largest: %d\n",big2);
        printf("Second Smallest: %d\n",small2);
        return 0;
}

void second_maxmin(int a[], int n,int *max2, int *min2)
{
        int i,j,temp;
    //sorting array in descending order
        for(i=0;i<n;i++)
        {
        for(j=i+1;j<n;j++)
                {
            if (a[i] < a[j])
            {
                temp=a[i];
                a[i] = a[j];
                a[j] = temp;
            }
                }
        }
    *min2=a[n-2];// last element a[n-1] will be smallest
        *max2=a[1];// first element a[0] will be largest

}

Output:


Related Solutions

As code is given below, follow the instructions: 1. Count the number of comparisons and find...
As code is given below, follow the instructions: 1. Count the number of comparisons and find where to put that counter in the code 2. Pick a random pivot, right pivot, left pivot, middle pivot for each smaller array /sub-array import java.util.*; import java.util.List; public class QuickSort { public static void main(String[] args) { int[] values = { 6, 5, 4, 3, 1, 7, 8 }; System.out.println("Original order: "); for (int element : values) System.out.print(element + " "); IntQuickSorter.quickSort(values); System.out.println("\nFirst...
Follow the steps bellow to construct a general solution to the equation: y'' + y =...
Follow the steps bellow to construct a general solution to the equation: y'' + y = 3sect -t2+1 a. find the general solution to the homogeneous version of the problem b. find the particular solution to y'' + y = 3sect c. find the particular solution to y'' + y = -t2+1 d. use part (a), (b), (c) to construc the general solution to the original given DE
use repil.it edit my code please i already did all part but need to edit more...
use repil.it edit my code please i already did all part but need to edit more its run for some not shwing all intro to C-programin be sure to edit on my code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input...
lets say i make a PYTHON code that lets a user guess a number between 1-1000,...
lets say i make a PYTHON code that lets a user guess a number between 1-1000, every failed attempt you get a hint (go lower or go higher) how can i penalize the user if they dont follow the hints, example ( go higher!... your next pick: a smaller number... 2 you loose $100 for not following hint) 2 and the user has unlimited attempts, until he guesses the number, this is made using random.randint(1,1000) function THE ONLY THING IM...
Writing python code: Can you do for me this. The question looks bellow Overview : As...
Writing python code: Can you do for me this. The question looks bellow Overview : As a programmer, you have be asked by a good friend to create a program that will allow them to keep track of their personal expenses. They want to be able to enter their expenses for the month, store them in a file, and then have the ability to retrieve them later. However, your friend only wants certain people to have the ability to view...
Java: Make 3 use cases for the code bellow. Example of Use case: Leave a Message...
Java: Make 3 use cases for the code bellow. Example of Use case: Leave a Message 1.Caller dials main number of voice mail system 2.System speaks prompt 3.User types extension number 4.System speaks 5.Caller speaks message Variation #1 3.1 In step 3, user enters invalid extension number 3.2 Voice mail system speaks 3.3 Continue with step 2. What the code Does: adds a new regular task, delete a task , show all tasks, and show regular tasks. my code: import...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard (int...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT