I desperately need help getting my program to work! 1. I need an If Else Menu dr
ID: 3622614 • Letter: I
Question
I desperately need help getting my program to work!1. I need an If Else Menu driven program to generate an array of 20 random numbers from a seed.
2. Call a function to write the content of the array to a file specified by a user.
3. Then be able to load a file into an array of 20 elements.
4. Call a selection sort.
5. And then display the array on a single line with elements separated by |(but no | after the last element).
If someone could please take a look at what I have so far I would greatly appreciate it. I think I'm working this too hard and i know I have mistakes but I'm not sure what they are.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
void create_array(int seed);
void read_array();
void sort_array(int a[]);
void display_array();
using namespace std;
int main()
{
char choice;
int final_array[20];
int a[20];
for(;;)
{
system("cls");
//menu choice
cout<<"Menu "
<<"1. Create the Array "
<<"2. Read the Array "
<<"3. Sort the Array "
<<"4. Display the Array "
<<"5. Exit "
<<" "
<<"Enter Option: ";
cin >> choice;
//process selection with if-else menu
while(choice >=1 && choice <=5)
if(choice=='1')
{
void create_array(int seed);
break;
}
else if (choice=='2')
{
read_array();
break;
}
else if (choice=='3')
{
sort_array(string &list)
break;
}
else if (choice=='4')
{
display_array();
break;
}
else if (choice=='5')
{
cout<<"You have choosen to exit the program. ";
cout<<"End of program. ";
return 0;
}
else
cout<<" Invalid Choice" << endl;
}
}
//function to generate random numbers from a seed
//and populate them into an array
void create_array(int seed)
{
using namespace std;
ifstream infile;
ofstream outfile;
char filename[16];
cout<<"Enter a Seed: ";
cin>>seed;
srand(seed);
for(int i=0;i<20;i++)
cout << " " << (rand()%20+1)*5<<"1";
cout<<"Enter a filename: ";
cin>>filename;
infile.open(filename);
//infile<<outfile;
cout<<"Array Created and written to " << filename << endl;
infile.close();
}
//function to load the content of a file into an arrary of 20 elements
void read_array()
{
using namespace std;
char filename[16]
ifstream infile;
ofstream outfile;
cout<<"Enter a filename: ";
cin>>filename;
infile.open(filename);
//how to send datat to array?
cout<<"File written to array successful" << endl;
infile.close();
}//selection sort function to sort the current array
void sort_array(string &list)
{
string temp;
int i,j;
for(i=0;i<list.size()-1;i++)
{
for(j=i+1;j<list.size();j++)
{
if(list[i].compare(list[j])>0)
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
}
//function to display the content of the current array
void display_array(int num)
{
int final_array[40];
cout<<"In sorted order the random numbers are: ";
for (int i=0;i< num;i++)
cout<<" | "<<final_array[i]<<endl;
}
Explanation / Answer
order of execution
first give option 1 and give file name to generate 20 random numbers and to save in file
next give option 2 to load file contents to array
next give option 3 to sort and
finally option 4 to disaply. 5 to exit.
remember order of execution is important.
file goes here.
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <string.h>
void create_array();
void read_array(int a[]);
void selectionSort(int a[],int );
void display_array(int a[],int);
int main()
{
int choice;
int final_array[20];
int a[20];
do
{
//menu choice
cout<<"Menu "
<<"1. Create the Array "
<<"2. Read the Array "
<<"3. Sort the Array "
<<"4. Display the Array "
<<"5. Exit "
<<" "
<<"Enter Option: ";
cin >> choice;
//process selection with if-else menu
switch(choice)
{
case 1:create_array();
break;
case 2:read_array(a);
break;
case 3:selectionSort(a,20);
break;
case 4:display_array(a,20);
break;
case 5: cout<<"You have choosen to exit the program. ";
cout<<"End of program. ";
return 0;
default: cout<<" Invalid Choice" << endl;
break;
}
// cout<< "Enter Your Choice again"<<endl;
// cin >> choice;
}while(choice>=1 && choice <=5);
return 0;
}
//function to generate random numbers from a seed
//and populate them into an array
void create_array()
{
int seed;
ofstream outfile;
char filename[16];
cout<<"Enter a Seed: ";
cin>>seed;
srand(seed);
cout<<"Enter a filename: ";
cin>>filename;
outfile.open(filename);
for(int i=0;i<20;i++)
outfile <<rand()%20+1<<endl;
cout<<"Array Created and written to " << filename << endl;
outfile.close();
}
//function to load the content of a file into an arrary of 20 elements
void read_array(int a[])
{
char filename[16];
char inp[20];
ifstream infile;
int i=0;
int k=0;
cout<<"Enter a filename: ";
cin>>filename;
infile.open(filename);
while(!infile.eof())
{
infile >> i;
a[k++] = i;
}
cout<<"File contents written to array successful" << endl;
infile.close();
}
//selection sort function to sort the current array
void selectionSort(int a[], int n) {
int i, j, minIndex, tmp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++)
if (a[j] < a[minIndex])
minIndex = j;
if (minIndex != i) {
tmp = a[i];
a[i] = a[minIndex];
a[minIndex] = tmp;
}
}
cout << "Sorting Has Been Completed Successfully"<<endl;
}
//function to display the content of the current array
void display_array(int a[],int num)
{
cout<<" sorted order the random numbers are: ";
for (int i=0;i< num;i++)
cout<<" | "<<a[i];
cout << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.