Question

Mini-Recitation #2 – Due 02Dec (Monday) It is simpler than a full recitation and therefore is...

Mini-Recitation #2 – Due 02Dec (Monday)
It is simpler than a full recitation and therefore is worth only half as much (50 points instead of 100). It is due today but if it is presented at the beginning of Wednesday’s class, it will still be accepted for a slightly lower grade. This is a partner exercise so be prepared to work together with your partner.
This program will test your knowledge of single-dimension arrays and their search. Your program will create an array of 100 integers, fill it with 100 random values and then search for the third-largest and the third smallest values in the array.
Definition of “third-largest” value: find the largest value in the array and call it x. Then find the largest value that is smaller than x and call it y. find the largest value that is smaller than y and that is the third-largest value.
In this program you will use the random number generator to fill the array with 2-digit values ( 0 to 99). Then you will find the third-largest value in the array and the number of array elements containing that number. Find the third-smallest value in the array and the number of array elements containing that value. Display the values and their counts in an appropriate message.

C++ please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

-------------------- I used Visual Studio 2013, C++ Language, Console Application --------------------

-------------------- OUTPUT --------------------

-------------------- CODE --------------------

#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;

int thirdLargestNumber(int arr[]);
int thirdSmallestNumber(int arr[]);
int howManyContains(int arr[], int number);
const int arraySize = 100;

int main()
{
   srand(time(NULL));
   int arr[arraySize];

   for (int i = 0; i < arraySize; i++)
   {
       int random = rand() % 100;
       arr[i] = random;
   }
   int thirdLargestNum = thirdLargestNumber(arr);
   int thirdSmallestNum = thirdSmallestNumber(arr);
   cout << "Third Largest Number : " << thirdLargestNum << endl;
   cout << "Third Smallest Number : " << thirdSmallestNum << endl;
   int noOfThirdLargestNumber = howManyContains(arr, thirdLargestNum);
   int noOfThirdSmallestNumber = howManyContains(arr, thirdSmallestNum);
   cout << "No of Third Largest Number present in array : " << noOfThirdLargestNumber << endl;
   cout << "No of Third Smallest Number present in array : " << noOfThirdSmallestNumber << endl;
   system("pause");
   return 0;
}

int thirdLargestNumber(int arr[])
{
   // Initialize first, second and third Largest element
   int first = arr[0], second = 0, third = 0;

   // Loop through the array starting at index 1
   for (int i = 1; i < arraySize; i++)
   {
       /* If current element is greater than first,
       then update first, second and third */
       if (arr[i] > first)
       {
           third = second;
           second = first;
           first = arr[i];
       }

       /* If arr[i] is in between first and second */
       else if (arr[i] > second)
       {
           third = second;
           second = arr[i];
       }

       /* If arr[i] is in between second and third */
       else if (arr[i] > third)
           third = arr[i];
   }

   return third;
}

int thirdSmallestNumber(int arr[])
{
   int first = arr[0], second = 0, third = 0;
   for (int i = 0; i < arraySize; i++)
   {
       /* Check if current element is less than
       first, then update first, second and
       third */
       if (arr[i] < first)
       {
           third = second;
           second = first;
           first = arr[i];
       }

       /* Check if current element is less than
       second then update second and third */
       else if (arr[i] < second)
       {
           third = second;
           second = arr[i];
       }

       /* Check if current element is less than
       third then update third */
       else if (arr[i] < third)
           third = arr[i];
   }

   return third;
}

int howManyContains(int arr[], int number)
{
   int count = 0;
   for (int i = 0; i < arraySize; i++)
   {
       /*If number is equal with array element,
       increment the count*/
       if (arr[i] == number)
       {
           count++;
       }
   }
   return count;
}

Add a comment
Know the answer?
Add Answer to:
Mini-Recitation #2 – Due 02Dec (Monday) It is simpler than a full recitation and therefore is...
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++ program that will create an array with a given number of elements. Use...

    Write a C++ program that will create an array with a given number of elements. Use size = 10 for demonstration purposes, but write the program where the size of the array can be changed by changing the value of one constant. Fill the array with random numbers between 0 and 100. Write the program to perform the following operations: 1. Find and display the largest value in the array. 2. Find and display the smallest value in the array....

  • 7eatng that function Write a C++ program that does the following: Fill an array of 123...

    7eatng that function Write a C++ program that does the following: Fill an array of 123 elements using srand) and rand) with random numbers between 150 and 667. Fill an array of 123 elements with random numbers between 150 and 667. Using a loop. Fill an array of 123 elements with random numbers between 150 and 667. Using a for loop Use a void function to fill an array of 123 elements with random numbers between 150 and 667, Possible...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • COMP1410 - Lab Exercises #3 (Due at the end of the lab period or beginning of...

    COMP1410 - Lab Exercises #3 (Due at the end of the lab period or beginning of the next) Start Date: Oct. 1st, 2019 Objectives: Practice dealing with 2D arrays. Create a two dimensional array (e.g. int A2D[M] [N] ;) of size MXN to store integer values. Use #define M 4 and N 3 to start. (Using symbolic constants instead of hard coding the array sizes improves scalability). Create an interactive menu within main() for this program (call it Lab3.c) with...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays,...

    Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • for this assignment you will be creating a series of functions for manipulating an array of...

    for this assignment you will be creating a series of functions for manipulating an array of data. The data will consist of randomly generated doubles. You should make the following functions: generate() - This function fills an array with random doubles in a specified range. This function takes four inputs: an array of doubles, an integer representing the size of the array, and two doubles representing the lower an upper bounds of the range random values. For example, the function...

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