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...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for e.g., 1980), except that it is not a leap year if it is also divisible by 100 (for e.g., 1900); however, it is a leap year if it is further divisible by 400 (for e.g., 2000). Write a program that prompts the...
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
C Code Edit this code to make it output all unique letters IN LOWERCASE of an...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an input file. For example, with this input: Raspberry Grapefruit Straw berry raspBERRY Blue$$berry apple Pine_apple raspberry The output should be: raspberry grapefruit straw berry blue berry apple pine NOTE: Words with different capitlization, like raspberry and raspBERRY count as 1 unique word, so raspberry should only be seen once in the output. #include <stdio.h> #include <stdlib.h> int main() {     //Pointer to access the...
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...
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...
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:...
Change or edit the following code so that it has 20 questions, there's an image for...
Change or edit the following code so that it has 20 questions, there's an image for each question and once the user enters an incorrect answer, there's a button that the user can click on that displays the correct answer. The questions and answer choices should also be random each time you start the quiz. <!DOCTYPE html> <html lang="en" > <head> <meta charset="utf-8" /> <title>World Cup Quiz</title> </head> <body> <style> #flashcards001 { background-color: white; border: 4px solid black; width: 400px;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT