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

program in C++ Please create a menu-driven program that offers the user a chance

ID: 3715978 • Letter: P

Question

program in C++

Please create a menu-driven program that offers the user a chance to play with a list of numbers:

The Numbers List Game

1. Add one number to the end of the list

2. Display the list

3. Display the list backwards

4. Display the sum of all the numbers in the list

5. Exit the Numbers List Game

Here are the rules:

General Rules: When the program starts, the list is empty. Keep the menu looping around until the user wishes to stop.

Menu Option 1: All numbers must be positive integers. Allow no more than 20 numbers to be added to the list in any given run of the program. Don't allow the program to crash - keep a counter.

Menu Option 2: Display the list. Either put a space between each number or put one number per line so that it's easy to read. Make all of the output beautiful.

Menu Option 3: Display the list backwards. Either put a space between each number or put one number per line so that it's easy to read. Make all of the output beautiful.

Menu Option 4: Add up all the numbers in the list and display that number.

Programming Rules: You must use an array to hold the list of numbers. That array must be declared in main() and then passed around to the various functions. Your code must be divided into logical functions - use a separate function to implement each menu option. No global variables. Global constants are okay. All good programming practices should be in place.

Explanation / Answer

#include<iostream>

#include<cstdlib>

#include<list>

using namespace std;

void function1(list<int> & l)

{

if(l.size()<20)

{

int num;

cout<<" Enter the number=";

cin>>num;

l.push_back(num);

}

else

cout<<" List is full";

}

void function2(list<int> &l)

{

if(l.empty()==true)

{

cout<<" List is empty.";

return;

}

list<int>::iterator itr;

for(itr=l.begin();itr!=l.end();itr++)

cout<<*itr<<" ";

cout<<endl;

}

void function3(list<int> &l)

{

if(l.empty()==true)

{

cout<<" List is empty.";

return;

}

l.reverse();

list<int>::iterator itr;

for(itr=l.begin();itr!=l.end();itr++)

cout<<*itr<<" ";

cout<<endl;

l.reverse();

}

void function4(list<int> &l)

{

if(l.empty()==true)

{

cout<<" List is empty.";

return;

}

int sum=0;

list<int>::iterator itr;

for(itr=l.begin();itr!=l.end();itr++)

sum=sum+ *itr;

cout<<" Sum of numbers="<<sum<<endl;

}

int main()

{

list<int> l;

cout<<" ----Number list game menu------------------------------------------";

cout<<" Select option 1 for Add one number to the end of the list";

cout<<" Select option 2 for Display the list";

cout<<" Select option 3 for Display list backward";

cout<<" Select option 4 for Display the sum of all the numbers in the list";

cout<<" Select option 5 for Exit the Numbers List Game";

cout<<" -------------------------------------------------------------------";

l:

int ch;

cout<<" Enter Your choice=";

cin>>ch;

switch(ch)

{

case 1:

function1(l);

break;

case 2:

function2(l);

break;

case 3:

function3(l);

break;

case 4:

function4(l);

break;

case 5:

exit(0);

break;

default:

cout<<" Invalid option---" ;

  

}

goto l;

return 0;

}

output:


----Number list game menu------------------------------------------
Select option 1 for Add one number to the end of the list
Select option 2 for Display the list
Select option 3 for Display list backward
Select option 4 for Display the sum of all the numbers in the list
Select option 5 for Exit the Numbers List Game
-------------------------------------------------------------------
Enter Your choice=1

Enter the number=2

Enter Your choice=1

Enter the number=4

Enter Your choice=1

Enter the number=5

Enter Your choice=2
2 4 5

Enter Your choice=3
5 4 2

Enter Your choice=4

Sum of numbers=11

Enter Your choice=5

--------------------------------
Process exited after 30.84 seconds with return value 0
Press any key to continue . . .