Question

Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

Write a menu based program implementing the following functions:

(0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following:

  • a do/while loop with the displayMenu function call inside the loop body
  • switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu.
  • the do/while loop should always continue as long as the user does not choose to quit the program.

(1) Write a function that prompts 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.

Have the program prompt the user to enter the number of rows and the number of columns for the two dimensional array.

Have the program prompt the user to enter the values for each row and column element in the two dimensional array.

Write the two dimensional array out as a text file. You will want the file format to be as follows:

  • First line contain the string: "UNSORTED"
  • The first two values stored in the file is the number of rows and the number of columns.
  • The remaining data is to be the two dimensional array being stored as long double typed values.

(2) Write a 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 a minimum of 2 columns, and a maximum of 10 rows and a maximum of 10 columns). Have the function that prompt the user for the name of a file to output as a text file the randomly generated values of type long double between the numbers 0 and 1, including fractional values in between such as (0.54734824682, etc).

Hint: you may want to have your program generate two random numbers and divide the smaller of the two random numbers into the larger random number.

You will want the file format to be as follows:

  • First line contain the string: "UNSORTED"
  • The first two values stored in the file is the number of rows and the number of columns.
  • The remaining data is to be the two dimensional array being stored as long double typed values.

(3) Write a function to 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.

Output the contents of the two dimensional array to the screen.

(4) Write a function to prompt the user for the name of a file to input as a text file to read in the two dimensional array of the long double type and another filename for the name of the output file that will be outputting the two dimensional array after it has been sorted.

Load in the data from the original text file and ask the user which column would they like to sort by utilizing bubble sort.

Sort the two dimensional array based on the column the user specifies and by the sort order the user would like to sort (prompt the user, asking if they would like to sort the column from smallest to largest, or largest to smallest), and output the sorted two dimensional array to the output filename.

You will want the file format of the output sorted file to be as follows:

  • First line contain the string: "SORTED"
  • The first two values stored in the file is the number of rows and the number of columns.
  • Sorted Column (this is the column number of the sorted column)
  • Sort Order (this is a flag for what order is the sorted column sorted by)
  • The remaining data is to be the two dimensional array being stored as long double typed values.

(5) Write a function to prompt the user for the name of a text file to read in the two dimensional array of long double data type.

Load in the data for the two dimensional array

Prompt the user to enter the column they wish to search by (check to make sure the file is "marked" as "SORTED" - if the file is marked as "UNSORTED" do not allow a binary search to be done, but rather in this case, utilize linear search), and the value to find. Implement the binary search algorithm to find the value (remember, the column they specify must be sorted or binary search may not always work). You will have to come up with some technique to be able to determine if the user has the column sorted in smallest to largest, or largest to smallest sort order in order to use binary search correctly (the algorithm in class only works for sorted data that is sorted from smallest to largest)

c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

All the explanation is in the code comments. Hope this helps!

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 << ", " << j << ") cell: ";
cin >> val;
fout << std::setprecision(15) << val << " "; // write value to file
}
fout << endl;
}
  
// close file
fout.close();
}

// (2) function
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
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;
  
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";
}

// (4) function
void function4()
{
// required variables
string f1, f2, temp;
int rows, cols, colNumber, sortOrder;
// declare a matrix
long double arr[10][10];
  
// prompt the user for the name of a file to input as a text file
// to read in the two dimensional array of the long double type
cout << "Enter name of input file: ";
cin >> f1;
// another filename for the name of the output file
// that will be outputting the two dimensional array after it has been sorted
cout << "Enter name of output file: ";
cin >> f2;
  
// open input file
ifstream fin(f1);
if (!fin.is_open())
{
cout << "Unable to open file";
return;
}
  
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 >> arr[i][j];
}
// close file
fin.close();
  
// ask the user which column would they like to sort by utilizing bubble sort
cout << "Which column would you like to sort: ";
cin >> colNumber;
  
// prompt the user asking the order of soring
cout << "Enter 0 to sort the column from smallest to largest or 1 for largest to smallest: ";
cin >> sortOrder;
  
// now sort the column
if(sortOrder) // descending
{
for (int i = 0; i < rows-1; i++)
{
// Last i elements are already in place
for (int j = 0; j < rows-i-1; j++)
if (arr[j][colNumber] < arr[j+1][colNumber])
{
// swap values
long double tmp = arr[j][colNumber];
arr[j][colNumber] = arr[j+1][colNumber];
arr[j+1][colNumber] = tmp;
}
}
}
else
{
for (int i = 0; i < rows-1; i++)
{
// Last i elements are already in place
for (int j = 0; j < rows-i-1; j++)
if (arr[j][colNumber] > arr[j+1][colNumber])
{
long double tmp = arr[j][colNumber];
arr[j][colNumber] = arr[j+1][colNumber];
arr[j+1][colNumber] = tmp;
}
}
}
  
// open file
ofstream fout;
fout.open (f2);
  
fout << "SORTED" << endl; // 1st line in file
  
fout << rows << " " << cols << endl; // 2nd line
  
fout << colNumber << endl; // 3rd line - Sorted Column
  
fout << sortOrder << endl; // 4th line - Sort Order (0 - ascending, 1 - descending)
  
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
fout << std::setprecision(15) << arr[i][j] << " "; // write value to file
}
fout << endl;
}
  
// close file
fout.close();
  
}

// (5) function
void function5()
{
// required variables
string filename, isSorted;
int rows, cols, colNumber, sortOrder, colSearch;
// declare a matrix
long double arr[10][10], x;
  
// 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 input file
ifstream fin(filename);
if (!fin.is_open())
{
cout << "Unable to open file";
return;
}
  
fin >> isSorted;
  
// read rows and columns number
fin >> rows >> cols;
  
// if the file is sorted, then read 2 extra lines
if(isSorted == "SORTED")
{
fin >> colNumber;
fin >> sortOrder;
}
  
// read values
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
fin >> arr[i][j];
}
  
// close file
fin.close();
  
// Prompt the user to enter the column they wish to search by
cout << "Which column would you like to search in: ";
cin >> colSearch;
  
cout << "Enter value to find: ";
cin >> x;
  
// determine which search technique to use
if(isSorted == "SORTED" && colSearch == colNumber && sortOrder == 0)
{
// use binary search
cout << "Searching " << x << " using binary search" << endl;
  
int l = 0, r = rows-1, flag = 0;
  
while (l <= r) {
  
int m = l + (r - l) / 2;
  
// Check if x is present at mid
if (arr[m][colSearch] == x)
{
cout << x << " found at (" << m << ", " << colSearch << ")\n";
flag = 1;
break;
}
  
// If x greater, ignore left half
if (arr[m][colSearch] < x)
l = m + 1;
  
// If x is smaller, ignore right half
else
r = m - 1;
}
if(flag == 0)
cout << x << " not found\n";
}
else
{
// use linear search
cout << "Searching " << x << " using linear search" << endl;
  
int i;
for(i=0; i<rows; i++)
{
if(arr[i][colSearch] == x)
{
cout << x << " found at (" << i << ", " << colSearch << ")\n";
break;
}
}
if(i == rows)
cout << x << " not found\n";
}
}

// main function
int main()
{
// sample run
while(true)
{
// user's choice for menu
int choice = displayMenu();
  
if(choice == 0)
{
return 0;
}
else if(choice == 1)
{
function1();
}
else if(choice == 2)
{
function2();
}
else if(choice == 3)
{
function3();
}
else if(choice == 4)
{
function4();
}
else
{
function5();
}
}
  
return 0;
}

Sample run:

Menu Choose 0 to exit Choose a function from 1 to 5 Enter name of output file: a.txt Enter number of rows: 3 Enter number of

0.627808777639908 0.923046516973276 0.423755155146275 0.294131695502033 0.674942041359895 0.441604382634508 0.92709 724407542

a.txt file

1 UNSORTED 2 3 2 0.999 0.214 4 0.462898 0.6 0.01 0.35

b.txt file

UNSORTED 10 7 0.0928973545645574 0.3191052286518e3 0.0372961207647387 0.622522767617788 0.756494726054279 e.509002423022909 0

c.txt file

1 SORTED 2 3 2 4 0 5 0.01 0.214 6 0.462898 0.6 7 0.999 0.35 8.

Code screenshots:

#include <iostream> 2 #include <fstream> #include <stdlib.h> 4 #include<time.h> 5 #include <iomanip> // std::setprecision 7 u

// open file ofstream fout; 41 42 fout.open (filename); 43 44 fout <« UNSSORTED <« endl; // 1st line in file 45 46 // promp

// prompt the user for the name of a file to output as a text file cout <« Enter name of output file: ; cin >> filename; 81

fout.close(); 121 122 } 123 124 // (3) function void function3() 125 126- { // required variables string filename, temp; int

161 162 // (4) function void function4() 164 - { 163 // required variables string f1, f2, temp; int rows, cols, colNumber, so

201 // ask the user which column would they like to sort by utilizing bubble sort cout <« Which column would you like to sor

// open file ofstream fout; 241 242 fout.open (f2); 243 244 fout <« SORTED << endl; // 1st line in file 245 246 fout <« row

// open input file ifstream fin(filename); if (!fin.is_open()) { cout <« Unable to open file; return; 281 282 283 284- 285

// use binary search cout <« Searching 321 using binary search << endl; <<х << 322 323 int 1 = 0, r = rows-1, flag 324 0; 3

361 362 if(i rows) 363 cout << x << not found\n 364 365 366 } 367 368 // main function int main() 370 - { 369 // sample ru

401 402 return 0; 403 404

Add a comment
Know the answer?
Add Answer to:
Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts 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. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a program that uses a two-dimensional array to store daily minutes walked and protein intake...

    Write a program that uses a two-dimensional array to store daily minutes walked and protein intake for a week. Prompt the user for 7 days of minutes exercise and protein intake; store in the array. Write a bubble sort to report out the sorted minutes walked values -lowest to highest. Write another to report out the sorted protein intake - highest to lowest. Your program should output all the values stored in the array and then output the 2 sorted...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • Write a modularized, menu-driven program to read a file with unknown number of records.

    ==============C++ or java================Write a modularized, menu-driven program to read a file with unknown number of records.Create a class Records to store the following data: first and last name, GPA , an Id number, and an emailInput file has unknown number of records; one record per line in the following order: first and last names, GPA , an Id number, and emailAll fields in the input file are separated by a tab (‘\t’) or a blank space (up to you)No error...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • IN JAVA Write a program that uses a two-dimensional array to store daily minutes walked and...

    IN JAVA Write a program that uses a two-dimensional array to store daily minutes walked and carb intake for a week. Prompt the user for 7 days of minutes walked and carb intake; store in the array.   Write a sort ( your choice which sort ) to report out the sorted minute values -lowest to highest. Write another to report out the sorted carb intake - highest to lowest. These methods MUST be your original code.   Your program should output...

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT