Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment 3 Task 2 #include #include //header for File stream libarary #include #include using namespace std; class FileBuffer { private: int NumberArray[1000]; //array to hold

Assignment 3 Task 2

#include #include //header for File stream libarary #include #include using namespace std;

class FileBuffer { private: int NumberArray[1000]; //array to hold integer value int index=0; //number of values in array

public : FileBuffer(string FileName) //paramter constructor {

string line; //hold each line of file or each number //fin takes filapath as char array so Filename is a string c_str converts it into char array ifstream fin(FileName.c_str()); //open the file if (fin.fail()) { //if file not found or unable to open throw error and exit cerr << "File cannot be opened for reading." << endl; exit(1); // exit if failed to open the file } else {

while(fin)//read till last line { getline(fin,line); //read line by line from file //c_str converts string to char array than atoi converts char array to integer NumberArray[index++]=atoi(line.c_str()); //file numbers copied in array }

} }

void PrintNumber() { cout<<"Random numbers inside file are : "; for(int i=0;i cout< cout< }

int SumOfNumbers() //return sum of all numbers { int Total=0; for(int i=0;i { Total+=NumberArray[i]; } return Total; }

void PrintOdd() //prints all odd number stored in file { cout<<"Odd Numbers are : "; for(int i=0;i { if(NumberArray[i]%2!=0) cout< } cout< }

void PrintEven() //print all even numbers { cout<<"Even Numbers are : "; for(int i=0;i { if(NumberArray[i]%2==0) cout< } cout<

}

int FindNumber(int number) //return the index of numbers pass in function else return -1 { for(int i=0;i { if(NumberArray[i]==number) return i; } return -1; }

int MiddleValue() //find middle value of array not average value { int middle= index/2;

return NumberArray[middle];

}

int FirstValue() //first value of file { return NumberArray[0]; }

int LastValue() //last number of file { return NumberArray[index-1]; }

int MaxValue() //maximum number in array { int maximum=NumberArray[0];

for(int i=0;i { if(NumberArray[i]>maximum) maximum=NumberArray[i];

}

return maximum; }

int MinValue() //return minimum number of array { int minimum=NumberArray[0];

for(int i=0;i { if(NumberArray[i] minimum=NumberArray[i]; } return minimum; }

void bubbleSort() //sort the array using bubble sort { int i, j; for (i = 0; i < index-1; i++) for (j = 0; j < index-i-1; j++) if (NumberArray[j] > NumberArray[j+1]) //check present number is greater than next number { //so swap with next number int temp;

temp=NumberArray[j]; NumberArray[j]=NumberArray[j+1]; NumberArray[j+1]=temp; } cout<<"Sorted Array is: "; //print the sorted array for(int i=0;i cout<

cout<

}

void ExitProgram() //exit the program { cout<<"Program Exit"< exit(1); }

};

int main() { FileBuffer F("a.txt"); //pass file name to constructor F.PrintNumber(); //print all numbers cout<<"Sum is :"< F.PrintOdd(); //odd F.PrintEven(); //even cout<<"Index is : "< cout<<"Middle value is : "< cout<<"First Value is : "< cout<<"Last Value is : "< cout<<"Max Value is : "< cout<<"Min Value is : "< F.bubbleSort(); //sort and print F.ExitProgram(); //exit the program

}

Task 2: In C++

Update Assignment 3 Task 2:

Main Function: Read random 1000 random integers from a .txt file.

Class will contain the methods below and an array as the data structure.

Please use comments to identify constructors and methods in the program.

Method 5:

New

Call bubble method sorted

Enter a search value / Binary Search and remove Linear Search

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

More Books

Students also viewed these Databases questions

Question

List the different types of closed-end funds.

Answered: 1 week ago