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

C programming assignment. instructions are given below and please edit this code only. also include screenshot...
C programming assignment. instructions are given below and please edit this code only. also include screenshot of the output //In this assignment, we write code to convert decimal integers into hexadecimal numbers //We pratice using arrays in this assignment #include <stdio.h> #include <stdlib.h> #include <assert.h> //convert the decimal integer d to hexadecimal, the result is stored in hex[] void dec_hex(int d, char hex[]) {    char digits[] ={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',   ...
Please follow the instructions carefully and complete the code given below. Language: Java Instructions from your...
Please follow the instructions carefully and complete the code given below. Language: Java Instructions from your teacher: (This is a version of an interview question I once saw.) In this problem, we will write a program that, given an integer k, an integer n, and a list of integers, outputs the number of pairs in in the list that add to k. To receive full credit for design, your algorithm must have a runtime in O(n) , where n is...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT