Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 a. To generate 100 random numbers between 1-100 in a randomData.txt file b. To read the 100 random numbers from randomData.txt and store them in an array c. Print the data in the array d. Find the smallest and the largest of the random numbers and their array position e. Insert an element of value100 in the 51 th position of the array f. Delete all the elements of the array having values between 50-80 and print the residual array g. Sort the data in the final array(residual) in an ascending order and print the final array.

here is my code, i keep getting an error saying that windows cant find the file specified, can anyone help its likely an easy fix, i am just not good at computer science at all.

#include #include #include #include #include

using namespace std;

void writeToFile() { int num, i, min = 1, max = 100; ofstream outfile; outfile.open("randomData.txt"); srand(time(NULL));

for (i = 0; i < 100; i++) { num = min + rand() % (max - min + 1); outfile << num << " ";

} outfile.close(); }

void readFromFile(int* arr) { int num, i = 0; ifstream infile; infile.open("randomData.txt"); while (infile) { infile >> num; // Console is reading number by number. arr[i++] = num; } infile.close(); }

void printArray(int* arr, int length) { for (int i = 0; i < length; i++) cout << arr[i] << " "; cout << endl; }

void find_largest(int* arr, int length) { int first, second, third; first = second = third = INT_MIN; for (int i = 0; i < length; i++) { if (arr[i] > first) { third = second; second = first; first = arr[i]; } else if (arr[i] > second) { third = second; second = arr[i]; } else if (arr[i] > third) { third = arr[i]; } } cout << "First Largest Number: " << first << endl; cout << "Second Largest Number: " << second << endl; cout << "Third Largest Number: " << third << endl; }

int* delete2040(int* arr, int& length) { // The temp_array is used because after deletion there will be a size reduction. int i, j, * temp_arr; i = -1; for (j = 0; j < length; j++) { if (arr[j] < 20 || arr[j] > 40) { arr[++i] = arr[j]; } } // This checks if there is no element < 20 and > 40. if (i != -1) { length = i + 1; temp_arr = new int[length]; for (int i = 0; i < length; i++) temp_arr[i] = arr[i]; return temp_arr; // The temp_arr is returned if elements are deleted. } return arr; // The standard arr is returned if no elements are deleted. }

void sort_array(int* arr, int length) { int element, i, j; for (i = 1; i < length; i++) { j = i - 1; element = arr[i]; while (j >= 0 && element < arr[j]) { arr[j + 1] = arr[j]; arr[j] = element; j--; } } }

int* sorted_insert(int* arr, int& length, int elem) { int j; length++; // length increased by 1. int* temp_arr = new int[length]; for (int i = 0; i < length - 1; i++) temp_arr[i] = arr[i]; arr[length - 1] = elem; // Puts new elements at the end. j = length - 2; // Checks if there is any previous element greater than last elements. If yes, move that element to the right. while (j >= 0 && elem < arr[j]) { temp_arr[j + 1] = temp_arr[j]; temp_arr[j] = elem; j--; } for (int i = 0; i < length; i++) { arr[i] = temp_arr[i]; } return arr; }

int main() { int length = 100; int* arr = new int[length]; writeToFile(); readFromFile(arr); cout << "Initial array pulled from the file: "; printArray(arr, length); cout << " ------------------------ " << "Finding 1st, 2nd and 3rd largest numbers: "; find_largest(arr, length); cout << " ------------------------ " << "After deleting all numbers from 20 to 40: "; arr = delete2040(arr, length); printArray(arr, length); sort_array(arr, length); cout << " ------------------------ " << "Sorted Array: "; printArray(arr, length); cout << " ------------------------ " << "After inserting 50, array becomes: "; arr = sorted_insert(arr, length, 50); printArray(arr, length); cout << " ------------------------ " << "After inserting 80, array becomes: "; arr = sorted_insert(arr, length, 80); printArray(arr, length);

return 0;

}

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_2

Step: 3

blur-text-image_3

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

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions

Question

6. List and explain important trends in compensation management.

Answered: 1 week ago

Question

What are our strategic aims?

Answered: 1 week ago