Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello friends! I'm having issues finishing my C++ homework assignment. This is the prompt: Problem 1: Write C++ program in .cpp file and algorithm in

Hello friends! I'm having issues finishing my C++ homework assignment. This is the prompt:

Problem 1: Write C++ program in .cpp file and algorithm in .doc file to create seven subroutines (functions) described in the following and call them in the main function

  1. To generate 100 random numbers between 1-100 in a randomData.txt file
  2. To read the 100 random numbers from randomData.txt and store them in an array
  3. Print the data in the array
  4. Find the largest, second and third largest of the random numbers and their array position
  5. Delete all the elements of the array having values between 20-40 and print the residual array
  6. Sort the residual data in the array in an ascending order and print
  7. Insert elements 50 and 80 in the sorted array at their respective positions and print the final array

My code (so far):

#include #include #include #include

using namespace std;

//steps I'm currently stuck on/having difficulties with completing:

//** 4. Find the largest, second and third largest of the random numbers and their array position. //currently, this code finds the smallest and the largest of the random numbers & their array position.

//** 5. Delete all the elements of the array having values between 20-40 and print the residual array //currently, this code deletes all elements of the array having values between 50-80 and prints the residual array.

//** 7. Insert elements 50 and 80 in the sorted array at their respective positions and print the final array. //code currently insert an element of value100 in the 51st position of the array.

//Part [1] Wirte in File void writeInFile(int length) {

int min = 0, i, num;

int max = length;

ofstream outfile; outfile.open("Output.txt");

srand(time(NULL));

for (i = 1; i <= length; i++) { num = (min + (rand() % (int)(max - min + 1))); outfile << num << endl; }

outfile.close(); }

//Part [2] Read from File int *readFromFile(int length) {

int *r = new int[length];

int i = 0, num;

ifstream infile;

infile.open("Output.txt");

while (infile) {

infile >> num; // delim defaults to ' ' r[i++] = num; }

return r; }

// Part [3] Print Array void printArray(int *p, int length) {

for (int i = 0; i < length; i++) { cout << *(p + i) << " "; }

cout << endl; }

//Paryt[4] find Small and Large Value void findSmallLarge(int *p, int length) {

int largeIndex3 = 0;

int largeValue3 = *(p + 0); // p[0]

int largeIndex2 = 0;

int largeValue2 = *(p + 0);

int largeIndex1 = 0;

int largeValue1 = *(p + 0);

for (int i = 1; i < length; i++) {

if (largeValue1 < *(p + i)) { largeValue1 = *(p + i); largeIndex1 = i; } } for (int i = 1; i < length; i++) { if ((largeValue2 < *(p + i)) && (*(p + i) != largeValue1)) { largeValue2 = *(p + i); largeIndex2 = i; } } for (int i = 1; i < length; i++) { if ((largeValue3 < *(p + i)) && (*(p + i) != largeValue2) && (*(p + i) != largeValue1)) { largeValue3 = *(p + i); largeIndex3 = i; } }

cout << "Largest Value1 is : " << largeValue1 << " at index : " << largeIndex1 << endl; cout << "Largest Value2 is : " << largeValue2 << " at index : " << largeIndex2 << endl; cout << "Largest Value3 is : " << largeValue3 << " at index : " << largeIndex3 << endl; }

//Part[5] Insert Value int *insertvalue100(int *p, int length) { *(p + 50) = length; // just copy this and use 80 for the other half of this problem. return p; }

int *deletevalue2040(int *p, int &length) // Deletes values between 20 and 40 { int *temparr = new int[length]; int templength = length;

for (int i = 0; i < length; i++) { temparr[i] = *(p + i); }

/* Alternatively instead of overwriting the values, keep the temp array empty and only copy values over to the new array. You can use templength to count the number of items copied if you start at one and copy to index - 1 -- temparr[templength-1]

*/

for (int i = 0; i < length; i++) { if (temparr[i] >= 20 && temparr[i] <= 40) { cout << "Deleting: " << temparr[i] << endl; for (int j = i; j < templength - 1; j++) // Leapfrogs the next value onto the current index in the array. { if(j != length) { temparr[j-1] = temparr[j+1]; } } templength--; } }

printArray(temparr, length);

return p; // Replace the old array with the temp array and then delete the old array before returning. }

int *arraySort(int *p, int length) { // You can use Bubble sort for this, you can find the sorting algorithms in the book, // feel free to use better algorithms than bubble sort (ie: merge sort, etc) if you can. return p; }

int main() { int *randomarray; int mylength = 100; // this defines the size of you array overall when the program starts.

writeInFile(mylength);

randomarray = readFromFile(mylength);

printArray(randomarray, mylength);

//findSmallLarge(randomarray);

deletevalue2040(randomarray, mylength);

//randomarray = insertvalue100(randomarray);

//printArray(randomarray);

return 0; }

It would be greatly appreciated for any assistance. Some of the bottom comments are were from a helper, but I'm lost from where he left me.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

2. What potential barriers would you encourage Samuel to avoid?

Answered: 1 week ago

Question

6. How would you design your ideal position?

Answered: 1 week ago