Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Declare and initialize arrays/variables Display menu 1. create a dynamic array o

ID: 3537059 • Letter: D

Question

Declare and initialize arrays/variables
Display menu
1. create a dynamic array of random elements (array size indicated by the user) and
write it into a file indicated by the user
2. load a dynamic array of elements from a file indicated by the user
3. sort the array
4. display the array
5. exit


If menu choice 1
Call a function to populate the array:
Ask the user for the number of elements (size of the array)
Ask the user for a seed (a seed is any positive integer)
Generate a dynamic array of random numbers from the set
{5, 10, 15, 20, %u2026, 95, 100}
Ask the user for file name
Call a function to write the content of the array into the specified file
Display a prompt for successful operation
Else if menu choice 2
Ask the user for the file name
Call a function to load the content of the file into a dynamic array of elements
Display a prompt for successful operation
Else if menu choice 3
Call a selection sort function to sort the current array
Else if menu choice 4
Call a function to display the content of the current array on a single line with elements separated by | (Important: no | after the last element)
Else if menu choice 5
Display an exit message
Else
Display an error on input prompt
If not an exit condition
Display the menu again

This is what i have


#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;


void populate();

void display();

void writeFile(ofstream& ,int[],int);


int main()

{

int choice;

cout << "Please Enter (1) if you wish to create a new set of randomly generated numbers ";

cout << "Enter (2) if you wish to display the numbers that were generated ";

cout << "Enter (3) if you wish to reverse the array";

cin >> choice;

if(choice == 1)

{

populate();

}

else if (choice == 2)

{

display();

}

else

void reverse();


return 0;

}


void populate()


{int values[]={5, 10, 15, 20, 25, 30, 35, 95, 100};

int elements,seed,i;

int *array;

char filename[30];

ofstream out;

cout<<"Enter number of elements: ";

cin>>elements;

cout<<"Enter random number generator seed: ";

cin>>seed;

srand(seed);

array=new int[elements];

for(i=0;i<elements;i++)

array[i]=values[rand()%7];

cout<<"Enter file name: ";

cin>>filename;

out.open(filename);

writeFile(out,array,elements);

cout<<"Operation successfully completed ";

}


void display()

{

char filename[50];

ifstream my;


cin.getline(filename, 50);

cin >> filename;

my.open(filename);

if(!my.is_open()){

exit(EXIT_FAILURE);

}

while(my.good())

{

int num;

cout << num << " ";

my >> num;;

}

}



void writeFile(ofstream& out ,int a[],int m)

{int i;

for(i=0;i<m;i++)

out<<a[i]<<endl;

}

Explanation / Answer



#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int populate();
void display(int *a,int no_of_elements = 0);
void writeFile(ofstream& ,int[],int);
void select_sort(int *a,int no_of_elements)
{
int i, index, max, temp;
for (index = no_of_elements; index > 1; index--)
{
for (i = 0; i < index; i++)
max = a[i] > a[max] ? i : max;
//now swap here
temp = a[max];
a[max] = a[index - 1];
a[index - 1] = temp;
}
}
void display_on_single_line(int *a,int no_of_elements)
{
for(int i=0; i< no_of_elements; i++)
{
if(i == no_of_elements-1)
cout << a[i] << endl;
else
cout << a[i] << "|";
}
}
int main()
{
int choice;
int *a;
int no_of_elements;
do
{
cout << "Please Enter (1) if you wish to create a new set of randomly generated numbers ";
cout << "Enter (2) if you wish to display the numbers that were generated ";
cout << "Enter (3) if you wish to apply selection sort. ";
cout << "Enter (4) if you wish to display current array on single line with elements separated by |. ";
cout << "Enter (5) if you wish to exit ";
cin >> choice;
switch(choice)
{
case 1: no_of_elements= populate(); a = new int[no_of_elements]; break;
case 2: display(a, no_of_elements); break;
case 3: select_sort(a, no_of_elements); break;
case 4: display_on_single_line(a, no_of_elements); break;
case 5: cout << " Exiting from program " << endl; return 0;
}
}while(choice>=1 && choice<=5);
return 0;
}
int populate()
{int values[]={5, 10, 15, 20, 25, 30, 35, 95, 100};
int elements,seed,i;
int *array;
char filename[30];
ofstream out;
cout<<"Enter number of elements: ";
cin>>elements;
cout<<"Enter random number generator seed: ";
cin>>seed;
srand(seed);
array=new int[elements];
for(i=0;i<elements;i++)
    array[i]=values[rand()%7];
cout<<"Enter file name: ";
cin>>filename;
out.open(filename);
writeFile(out,array,elements);
cout<<"Operation successfully completed ";
out.close();
return elements;
}
void display(int *a,int no_of_elements)
{
char filename[50];
ifstream my;
cin.getline(filename, 50);

cout << " enter file name you want to read from ";
cin >> filename;
my.open(filename);
if(!my.is_open()){
exit(EXIT_FAILURE);
}
int num;
int i=0;
while(my >> num)
{
a[i++] = num;
cout << num << " ";
}
}

void writeFile(ofstream& out ,int a[],int m)
{
int i;
for(i=0;i<m;i++)
    out<<a[i]<<endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote