Question

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 two dimensional array.

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

  • 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 in 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 row and column elements. Have the function that prompt the user for the name of a file to output as a text file, the randomly generated values between the numbers 0 and 1 such as 0.036757, 0.45425676, etc (inclusively - means to include 0 and 1). 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:

  • 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 read in as a 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.

Sort the two dimensional array based on the column the user specifies, and output the sorted two dimensional array to the output filename.

(5) 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 data type.

Load in the data for the two dimensional array.

Prompt the user to enter the column they wish to search by, 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).

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

Here is the required code;

#include<iostream>
#include<fstream>
#include<time.h>
using namespace std;
int main()
{
srand(time(0));
int c;
do{
cout<<"\nMenu";
cout<<"\n1. write user data into a file ";
cout<<"\n2. write random data into a file";
cout<<"\n3. read data from the file";
cout<<"\n4. perform sort by on 2d array";
cout<<"\n5. find an element in sorted 2d array";
cout<<"\n6. Quit\n";
cin>>c;
switch(c)
{
case 1:{
string file;
cout<<"\n enter the file name: ";
cin>>file;
ofstream f;
f.open(file);
int row,col;
  
cout<<"\n enter the row and column of the array: ";
cin>>row>>col;
f<<row<<" "<<col<<"\n";
double ar[row][col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>ar[i][j];
f<<ar[i][j]<<" ";
}
f<<"\n";
}
break;}
case 2:{
string file;
cout<<"\n enter the file name: ";
cin>>file;
ofstream f;
f.open(file);
int row,col;
  
cout<<"\n enter the row and column of the array: ";
cin>>row>>col;
f<<row<<" "<<col<<"\n";
  
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
double ab=rand()%100000;
double ba=rand()%100000;
if(ab<ba)
f<<ab/ba<<" ";
else
f<<ba/ab<<" ";
}
f<<"\n";
}
break;}
case 3:{
string file;
cout<<"\n enter the file name: ";
cin>>file;
ifstream f;
f.open(file);
int row,col;
f>>row>>col;
cout<<"\n data is\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
double ab;
f>>ab;
cout<<ab<<" ";
}
cout<<"\n";
}
break;}
case 4:{
string file1,file2;
cout<<"\nenter the file name from which data to be taken: ";
cin>>file1;
cout<<"\n enter the file name in which sorted data to be stored: ";
cin>>file2;
ifstream f;
f.open(file1);
int row,col;
f>>row>>col;
double ar[row][col];
int i,j;
for(i=0;i<row;i++)
{
for( j=0;j<col;j++)
{
f>>ar[i][j];
  
}
  
}
int colsort;
cout<<"\nenter the column no.( starting from 0 to "<<col-1<<" )";
cin>>colsort;

for (i = 0; i < row-1; i++)   
  
// Last i elements are already in place
for (j = 0; j < row-i-1; j++)
if (ar[j][colsort] > ar[j+1][colsort])
{
for(int k=0;k<col;k++)
{
swap(ar[j][k],ar[j+1][k]);
}
}
f.close();
ofstream ff;
ff.open(file2);
ff<<row<<" "<<col<<"\n";
for(i=0;i<row;i++)
{
for( j=0;j<col;j++)
{
  
ff<<ar[i][j]<<" ";
}
ff<<"\n";
}
break;}
case 5:{
string file;
cout<<"\n enter the name of the file in which data to be find: ";
cin>>file;
ifstream f;
f.open(file);
int colno;
cout<<"\n enter the column no. in which data to be find: ";
cin>>colno;
int element;
cout<<"\n enter the element to be found: ";
cin>>element;
int row,col;
f>>row>>col;
double ar[row][col];
int i,j;
for(i=0;i<row;i++)
{
for( j=0;j<col;j++)
{
f>>ar[i][j];
  
}
  
}
int l=0,r=row-1;
int flag=1;
while (l <= r) {
int m = l + (r - l) / 2;
  
// Check if element is present at mid
if (ar[m][colno] == element){
cout<<"\nelement found at row :"<<m;
flag=0;
}
  
// If element greater, ignore left half
if (ar[m][colno] < element)
l = m + 1;
  
// If element is smaller, ignore right half
else
r = m - 1;
}
if(flag)
cout<<"\nelement not found\n";
break;}
case 6:break;
default:{
cout<<"\nenter write choice please\n";
break;
}
}
}while(c!=6);
}

here is the ss of the output

PS D:\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin> ./a.exe Menu 1. write user data into a file 2. write random da

I hope this will help you so please give positive ratings ::)))

sorry due to lack of time i've not posted complete output but you can generate your output

Add a comment
Know the answer?
Add Answer to:
Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...
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
  • 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...

  • 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 the program in C The following statistics for cities of the Vege country are stored...

    Write the program in C The following statistics for cities of the Vege country are stored in a file: Name: a string of characters less than 15 characters long. Population: an integer value. Area: square miles of type double Write a program that: asks and gets the name of the input file from the user. Read the contents of the file into an array length 10 of structures -ask the user for the name of a city (less than 15...

  • 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...

  • C++ Problem: Power Plant Data. The data file power1.dat contains a power plant output in megawatts...

    C++ Problem: Power Plant Data. The data file power1.dat contains a power plant output in megawatts over a period of 10 weeks. Each row of data contains 7 floating-point numbers that represent 1 week's data. In developing the following programs, use symbolic constants NROWS and NCOLS to represent the number of rows and columns in the array used to store the data. Write a function to compute and return the average value in a two-dimensional vector of type double. Assume...

  • Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in...

    Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in a file. Your program should prompt the user for the name of the file to use and then request values be entered until the user is done. This data should be written to two files: a text file, user_file_name.txt and a comma separated value file, user_file_name.csv. To help you with decision making, we suggest that you ask the user to input data representing students’...

  • Can you help us!! Thank you! C++ Write a program that can be used by a...

    Can you help us!! Thank you! C++ Write a program that can be used by a small theater to sell tickets for performances. The program should display a screen that shows which seats are available and which are taken. Here is a list of tasks this program must perform • The theater's auditorium has 15 rows, with 30 seats in each row. A two dimensional array can be used to represent the seats. The seats array can be initialized with...

  • Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a...

    Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns,...

  • this language is JAVA 2. Design a class named Location for locating a maximum value and...

    this language is JAVA 2. Design a class named Location for locating a maximum value and its location in a two-dimensional array. The class contains public data fields row, column, and maxValue that store the maximal value and its indices in a two-dimensional array with row and column as int types and maxValue as a double type. Write the following method that returns the location of the largest element in a two-dimensional array: Public static Location locateLargest(double [][] a) Create...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

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