Update
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Control {
private:
int arr[1000];
int size = 1000;
public:
Control() {
srand(time(NULL));
ifstream file("numbers.txt");
if (file.is_open()) {
for (int i = 0; i < size; i++) {
file >> arr[i];
}
file.close();
}
else {
cout << "Unable to open file." << endl;
}
for (int i = 0; i < size; i++) {
arr[i] = rand() % 1000 + 1;
}
}
void outputAll() {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void outputSum() {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
cout << "Sum: " << sum << endl;
}
void outputOdd() {
cout << "Odd numbers: ";
for (int i = 0; i < size; i++) {
if (arr[i] % 2 != 0) {
cout << arr[i] << " ";
}
}
cout << endl;
}
void outputEven() {
cout << "Even numbers: ";
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
cout << arr[i] << " ";
}
}
cout << endl;
}
void linearSearch(int val) {
bool found = false;
for (int i = 0; i < size; i++) {
if (arr[i] == val) {
found = true;
cout << "Value found at index " << i << endl;
break;
}
}
if (!found) {
cout << "Value not found." << endl;
}
}
void outputMiddle() {
int middle = size / 2;
cout << "Middle value(s): ";
if (size % 2 == 0) {
cout << arr[middle - 1] << " " << arr[middle] << endl;
}
else {
cout << arr[middle] << endl;
}
}
void outputFirst() {
cout << "First value: " << arr[0] << endl;
}
void outputLast() {
cout << "Last value: " << arr[size - 1] << endl;
}
void outputHighest() {
int highest = arr[0];
int index = 0;
for (int i = 1; i < size; i++) {
if (arr[i] > highest) {
highest = arr[i];
index = i;
}
}
cout << "Highest value: " << highest << " (index " << index << ")" << endl;
}
void outputLowest() {
int lowest = arr[0];
int index = 0;
for (int i = 1; i < size; i++) {
if (arr[i] < lowest) {
lowest = arr[i];
index = i;
}
}
cout << "Lowest value: " << lowest << " (index " << index << ")" << endl;
}
void bubbleSort() {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "Array sorted in ascending order using bubble sort." << endl;
}
};
int main(){
Control c;
c.outputAll();
c.outputSum();
c.outputOdd();
c.outputEven();
c.outputFirst();
c.outputLast();
c.outputMiddle();
c.linearSearch();
c.outputLowest();
c.outputHighest();
}
Create a conventional structure for C++ Program
Header File (Outline / Framework of C++ Class File)
Driver File (File Containing Main Function)
Class Implementation File
Make Sure all the files are organized in the same directory.
Main Function: Control of operation
Common_Alg
The array will be a Class Variable {Implement Dynamic array)
In your Class declare and fill an array with 1000 random integers between 1 - 1000 from your .txt
file used in previous tasks.
Filling the array with random integers should happen in the class constructor.
***Hint: use the class constructor.
The algorithm outputs .
Update include ltiostreamgt include ltfstreamgt .pdf
1. Update
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Control {
private:
int arr[1000];
int size = 1000;
public:
Control() {
srand(time(NULL));
ifstream file("numbers.txt");
if (file.is_open()) {
for (int i = 0; i < size; i++) {
file >> arr[i];
}
file.close();
}
else {
cout << "Unable to open file." << endl;
}
for (int i = 0; i < size; i++) {
arr[i] = rand() % 1000 + 1;
}
}
void outputAll() {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void outputSum() {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
cout << "Sum: " << sum << endl;
}
void outputOdd() {
cout << "Odd numbers: ";
2. for (int i = 0; i < size; i++) {
if (arr[i] % 2 != 0) {
cout << arr[i] << " ";
}
}
cout << endl;
}
void outputEven() {
cout << "Even numbers: ";
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
cout << arr[i] << " ";
}
}
cout << endl;
}
void linearSearch(int val) {
bool found = false;
for (int i = 0; i < size; i++) {
if (arr[i] == val) {
found = true;
cout << "Value found at index " << i << endl;
break;
}
}
if (!found) {
cout << "Value not found." << endl;
}
}
void outputMiddle() {
int middle = size / 2;
cout << "Middle value(s): ";
if (size % 2 == 0) {
cout << arr[middle - 1] << " " << arr[middle] << endl;
}
else {
cout << arr[middle] << endl;
}
}
void outputFirst() {
cout << "First value: " << arr[0] << endl;
}
3. void outputLast() {
cout << "Last value: " << arr[size - 1] << endl;
}
void outputHighest() {
int highest = arr[0];
int index = 0;
for (int i = 1; i < size; i++) {
if (arr[i] > highest) {
highest = arr[i];
index = i;
}
}
cout << "Highest value: " << highest << " (index " << index << ")" << endl;
}
void outputLowest() {
int lowest = arr[0];
int index = 0;
for (int i = 1; i < size; i++) {
if (arr[i] < lowest) {
lowest = arr[i];
index = i;
}
}
cout << "Lowest value: " << lowest << " (index " << index << ")" << endl;
}
void bubbleSort() {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "Array sorted in ascending order using bubble sort." << endl;
}
};
int main(){
Control c;
c.outputAll();
c.outputSum();
4. c.outputOdd();
c.outputEven();
c.outputFirst();
c.outputLast();
c.outputMiddle();
c.linearSearch();
c.outputLowest();
c.outputHighest();
}
Create a conventional structure for C++ Program
Header File (Outline / Framework of C++ Class File)
Driver File (File Containing Main Function)
Class Implementation File
Make Sure all the files are organized in the same directory.
Main Function: Control of operation
Common_Alg
The array will be a Class Variable {Implement Dynamic array)
In your Class declare and fill an array with 1000 random integers between 1 - 1000 from your .txt
file used in previous tasks.
Filling the array with random integers should happen in the class constructor.
***Hint: use the class constructor.
The algorithm outputs below should remain the same.
Implement Binary Search algorithm instead of Linear Search Algorithm
Directory Holding Header File / Driver File / Class Implementation File:
Attach Snipping Photo of Command Line Navigation and Files in the Directory
Main Function:
Attach a snipping photo of source code
Class Constructor:
Attach a snipping photo of source code
Method 1: Output all values
Attach a snipping photo of source code and output
Method 2: Output sum All values
Attach a snipping photo of source code and output
Method 3: Output All odd numbers
Attach a snipping photo of source code and output
Method 4: Output All Even numbers
Attach a snipping photo of source code and output
Method 5: Enter a search value /Binary Search
New
Attach a snipping photo of source code and output
Method 6: Output Middle Value (Values in the Middle)
Not Average
5. New
Attach a snipping photo of source code and output
Method 7: Output First Value in the Array
New
Attach a snipping photo of source code and output
Method 8: Output Last Value in the Array
New
Attach a snipping photo of source code and output
Method 9: Output the Highest Value and its location in the array. (Hardcode the algorithm.)
New
Attach a snipping photo of the source code and output
Method 10: Output the Lowest Value and its location in the array. (Hardcode the algorithm.)
New
Attach a snipping photo of source code and output
Method 11:
Bubble Sort
New
Attach a snipping photo of source code and output
Directory Holding Header File / Driver File / Class Implementation File:
Attach Snipping Photo of Command Line Navigation and Files in the Directory
Main Function:
Attach a snipping photo of source code
Class Constructor:
Attach a snipping photo of source code
Method 1: Output all values
Attach a snipping photo of source code and output
Method 2: Output sum All values
Attach a snipping photo of source code and output
Method 3: Output All odd numbers
Attach a snipping photo of source code and output
Method 4: Output All Even numbers
Attach a snipping photo of source code and output
Method 5: Enter a search value /Binary Search
New
Attach a snipping photo of source code and output
Method 6: Output Middle Value (Values in the Middle)
Not Average
New
Attach a snipping photo of source code and output
6. Method 7: Output First Value in the Array
New
Attach a snipping photo of source code and output
Method 8: Output Last Value in the Array
New
Attach a snipping photo of source code and output
Method 9: Output the Highest Value and its location in the array. (Hardcode the algorithm.)
New
Attach a snipping photo of the source code and output
Method 10: Output the Lowest Value and its location in the array. (Hardcode the algorithm.)
New
Attach a snipping photo of source code and output
Method 11:
Bubble Sort
New
Attach a snipping photo of source code and output