ATTN: Without changing any of the code already written, how do i write the rest
ID: 3631550 • Letter: A
Question
ATTN: Without changing any of the code already written, how do i write the rest of the code within the first 2 functions? comment please, thanks!
// The first function takes in an array as input and returns the three largest numbers. The second function populates a new array that is one smaller than the original array where every position is the difference of the two neighboring numbers. For example newArray[x] = oldArray[x + 1] – oldArray[x].
#include <iostream>
using namespace std;
// This function returns the three
// largest numbers in the array
void getThreeLargestNumbers(int inputArray[], int size, int & largest, int & secondLargest, int &thirdLargest)
{
}
// This function populates a new array
// with the difference of consecutive
// elements in the array
void generateDifferenceArray(int inputArray[], int diffArray[], int sizeOfOriginal)
{
}
int main()
{
const int SIZE = 15;
int counter = 0;
int originalArray[SIZE] = {0};
// Populating array
for (counter = 0; counter < SIZE; counter++)
originalArray[counter] = rand()%200 - rand()%200;
// Printing out the array
for (counter = 0; counter < SIZE; counter++)
cout << originalArray[counter] << " ";
cout << " ";
int largest = 0, secondLargest = 0, thirdLargest = 0;
getThreeLargestNumbers(originalArray, SIZE, largest, secondLargest, thirdLargest);
cout << "Printing the three largest numbers: ";
cout << "1: " << largest << " ";
cout << "2: " << secondLargest << " ";
cout << "3: " << thirdLargest << " ";
// Populating difference array
int differenceArray[SIZE - 1] = {0};
generateDifferenceArray(originalArray, differenceArray, SIZE);
// Printing out the difference array
cout << "The difference array is : ";
for (counter = 0; counter < SIZE - 1; counter++)
cout << differenceArray[counter] << " ";
cout << " ";
return 0;
}
Explanation / Answer
#include
using namespace std;
// This function returns the three
// largest numbers in the array
void getThreeLargestNumbers(int inputArray[], int size, int largest, int secondLargest, int thirdLargest)
{
largest=0;
secondLargest=0; // initialised all value o if your array has no negative number with smallest number
thirdLargest=0;
for(int i=0;i<size;i++) // scan through your array
{
if(inputArray[i]>largest) //check if it is larger than largest is yes than it became lagest
{
thirdLargest=secondLargest; //so second largest became third largest
secondLargest=largest; //largset became second largest
largest=inputArray[i]; //the new element became largest
// the sequence is importent other wise you loss some value
}
else //if the current element of array is not lager than largest
if(inputArray[i]>secondLargest) //it may be larger than second largest
{
thirdLargest=secondLargest; // so second largest became third largest
secondLargest=inputArray[i]; // and current element became second largest
}
else //if the current element is not larger than secondLargest
if(inputArray[i]>thirdLargest) // it may be lager than third largest if yes
thirdLargest=inputArray[i];// than it became third largest
}
// Here you can print the value of all three numbers
}
// This function populates a new array
// with the difference of consecutive
// elements in the array
void generateDifferenceArray(int inputArray[], int diffArray[], int sizeOfOriginal)
{
for(int i=0;i<sizeOfOriginal-1;i++) // whene value of i became equal to sizeOfOriginal than
{ //than inputArray[i+1] will go out side of the Array so i should be less than the sizeOfOriginal -1
diffArray[i]=inputArray[i+1]-inputArray[i]; //here we update the diffArray with value
}
}
// rest are your own code
int main()
{
const int SIZE = 15;
int counter = 0;
int originalArray[SIZE] = {0};
// Populating array
for (counter = 0; counter < SIZE; counter++)
originalArray[counter] = rand()%200 - rand()%200;
// Printing out the array
for (counter = 0; counter < SIZE; counter++)
cout << originalArray[counter] << " ";
cout << " ";
int largest = 0, secondLargest = 0, thirdLargest = 0;
getThreeLargestNumbers(originalArray, SIZE, largest, secondLargest, thirdLargest);
cout << "Printing the three largest numbers: ";
cout << "1: " << largest << " ";
cout << "2: " << secondLargest << " ";
cout << "3: " << thirdLargest << " ";
// Populating difference array
int differenceArray[SIZE - 1] = {0};
generateDifferenceArray(originalArray, differenceArray, SIZE);
// Printing out the difference array
cout << "The difference array is : ";
for (counter = 0; counter < SIZE - 1; counter++)
cout << differenceArray[counter] << " ";
cout << " ";
return 0;
}
Note :There is error in your main function .Whene you are calling getThreeLargestNumber() you are passing the largest, secondLargest and thirdLargest by value, and printing the value in main so the modified value of these variables are on avialable here .
Solution is :
1)pass these variable by refernce or
or (2) print the values from getThreeLargestNumber() function
So modiffy the code according to your choice.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.