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
-------------------- 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;
}
Mini-Recitation #2 – Due 02Dec (Monday) It is simpler than a full recitation and therefore is...
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 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 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 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 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 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...
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...
#include <stdio.h>
// Define other functions here to process the filled array: average, max, min, sort, search
// Define computeAvg here
// Define findMax here
// Define findMin here
// Define selectionSort here ( copy from zyBooks 11.6.1 )
// Define binarySearch here
int main(void) {
// Declare variables
FILE* inFile = NULL; // File pointer
int singleNum; // Data value read from file
int valuesRead; // Number of data values read in by fscanf
int counter=0; // Counter of...
2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...