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:
(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:
(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:
(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:
(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++
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:
a.txt file
b.txt file
c.txt file
Code screenshots:
Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...
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 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 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 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...
==============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 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 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, 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. 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...
Assignment: Write a program with each of the following methods. You can assume this program is saved in a file called multiArrays.java. A sample main method and sample output is provided at the end of this section. All arrays created are of integer-type. Write a method to declare, initialize, and populate a two-dimensional array. Using the following header: public static int[ ][ ] declare2D(Scanner scnr) The user will indicate the size of the array. Write a method to declare,...