Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Answered the 1st 4 sub-parts of the question as per Chegg's answering guidelines. This will provide with almost everything required for the next 2 sub-parts. Hope this helps!
- All the explanations is in the code comments.
- Code:
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #include<time.h>
- #include <iomanip> // std::setprecision
- using namespace std;
- // (0) function does not take any parameters
- // returns an integer representing your user's menu choice
- int displayMenu()
- {
- int choice;
- while(true)
- {
- cout << "Menu ----------------------- " << endl;
- cout << "Choose 0 to exit" << endl;
- cout << "Choose a function from 1 to 5" << endl;
- cin >> choice;
- // return valid user input
- if(choice>=0 && choice<=5)
- return choice;
- }
- }
- // (1) function
- void function1()
- {
- // required variables
- string filename;
- int rows, cols;
- long double val;
- // prompt the user for the name of a file to output as a text file
- // that will hold a two dimensional array of the long double data type.
- cout << "Enter name of output file: ";
- cin >> filename;
- // open file
- ofstream fout;
- fout.open (filename);
- fout << "UNSORTED" << endl; // 1st line in file
- // prompt the user to enter the number of rows
- // and the number of columns for the two dimensional array.
- cout << "Enter number of rows: ";
- cin >> rows;
- cout << "Enter number of columns: ";
- cin >> cols;
- fout << rows << " " << cols << endl; // 2nd line
- // prompt the user to enter the values for each row
- // and column element in the two dimensional array.
- for(int i=0; i<rows; i++)
- {
- for(int j=0; j<cols; j++)
- {
- cout << "Enter value for (" << (i+1) << ", " << (j+1) << ") cell: ";
- cin >> val;
- fout << std::setprecision(15) << val << " "; // write value to file
- }
- fout << endl;
- }
- // close file
- fout.close();
- }
- // (2) function like function #1, except this time,
- // instead of asking the user for the values for each row and column element,
- // have the function create a randomly generated values for the number of rows and columns
- // (a minimum of 2 rows and 2 columns, and a maximum of 10 rows and 10 columns).
- void function2()
- {
- // required variables
- string filename;
- int rows, cols;
- long double val;
- // prompt the user for the name of a file to output as a text file
- // that will hold a two dimensional array of the long double data type.
- cout << "Enter name of output file: ";
- cin >> filename;
- // open file
- ofstream fout;
- fout.open (filename);
- fout << "UNSORTED" << endl; // 1st line in file
- // Use current time as seed for random generator
- srand(time(0));
- // generate the number of rows and the number of columns
- // for the two dimensional array. (between 2 and 10)
- rows = rand() % 9 + 2;
- cols = rand() % 9 + 2;
- fout << rows << " " << cols << endl; // 2nd line
- // prompt the user to enter the values for each row and column element in the two dimensional array.
- for(int i=0; i<rows; i++)
- {
- for(int j=0; j<cols; j++)
- {
- long double a = (double)rand() / (double)(RAND_MAX);
- long double b = (double)rand() / (double)(RAND_MAX);
- // divide the smaller number by larger number
- if(a<b)
- val = a/b;
- else
- val = b/a;
- // fprintf(fout, "%.15Lf\n", val);
- fout << std::setprecision(15) << val << " "; // write value to file
- }
- fout << endl;
- }
- // close file
- fout.close();
- }
- // (3) function
- void function3()
- {
- // required variables
- string filename, temp;
- int rows, cols;
- long double val;
- // prompt the user for the name of a file to input as a text file
- // to read in the two dimensional array of long double type.
- cout << "Enter name of input file: ";
- cin >> filename;
- // open file
- ifstream fin(filename);
- if (fin.is_open())
- {
- fin >> temp;
- // read rows and columns number
- fin >> rows >> cols;
- // read values
- for(int i=0; i<rows; i++)
- {
- for(int j=0; j<cols; j++)
- {
- fin >> val;
- cout << std::setprecision(15) << val << " "; // print contents
- }
- cout << endl;
- }
- // close file
- fin.close();
- }
- else cout << "Unable to open file";
- }
- // main function
- int main()
- {
- // sample run
- while(true)
- {
- // user's choice for menu
- int choice = displayMenu();
- if(choice == 0)
- {
- break;
- }
- else if(choice == 1)
- {
- function1();
- }
- else if(choice == 2)
- {
- function2();
- }
- else if(choice == 3)
- {
- function3();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement