Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

It remains to fix the part of the while that I don't know why when I enter option 3 the program does not end. It

It remains to fix the part of the "while" that I don't know why when I enter option 3 the program does not end. It does not exit the program, it keeps repeating its self.

Please help.

/*

* CECS 2203, Computer Programming I Lab

* Topic: Team Project

* Instructions:

* This is a team project. Your team will be required to present

* your work on Wednesday, February 1.

* Complete the code with the required instructions and execute

* the program.

*/

#include

using namespace std;

// Method prototypes

int menu();

bool execute(int, string[], const int, int[], const int);

string getLuckyColor(string[], const int);

int getLuckyNumber(int[], const int);

void randomAssign(int[], const int);

void printNumbers(int[], const int);

void orderNumbers(int[], const int);

int main() {

// declare a string array named colors which contains the following values:

// Red, Green, Blue, Purple, Yellow, Black

string colors[] = { "Red", "Green", "Blue", "Purple", "Yellow", "Black"};

// declare an integer named colorsCount and assign the size of colors

int colorsCount = 6;

// declare an integer constant named NUMBERS and initialize to 10

const int NUMBERS = 10;

// declare an integer array named numbers using NUMBERS as the size, and

// initialize all elements to 0

int numbers[NUMBERS] = { 0 };

// call the randomAssign method to populate the numbers array

randomAssign(numbers, NUMBERS);

// call the printNumbers method to print the values in the numbers array

printNumbers(numbers, NUMBERS);

// call the orderNumbers method to order the values in numbers from high to low

orderNumbers(numbers, NUMBERS);

// call the printNumbers method to print the values in the numbers array

printNumbers(numbers, NUMBERS);

// DO NOT DECLARE ANY OTHER VARIABLES IN main!

// develop a while iteration control structure which uses the return value

// of the execute method as sentinel. The execute method uses the return value of

// the menu method as parameter, along with the names and sizes for the two arrays.

while (1) {

execute(menu(), colors, colorsCount, numbers, NUMBERS);

}

cout << endl;

// print the phrase "Program developed by [team member 1 name], ID#[team member 1 ID NUMBER HERE],

// and [team member 2 name], ID#[team member 2 ID NUMBER HERE]"

// where the square brackets and the text within must be replaced with your information

cout << "Program developed by Esdras Cordero Feliciano, ID# 136668, and [team member 2 name], ID#[team member 2 ID NUMBER HERE]";

cout << endl;

system("pause"); // For Visual Studio only!

return 0;

}

// The menu method prints a menu with the list of options.

// The text to be printed is:

// Select one of the following options:

// 1. Find my lucky number

// 2. Find my lucky color

// 3. Exit the program

// [blank line]

// Your selection:

// [blank line]

// The method declares a local variable named option, which must be

// initialized to 0, where the selected option value is stored.

// The method returns the value of the selected option.

int menu() {

int option = 0;

cout << "Select one of the following options:" << endl;

cout << "1. Find my lucky number" << endl;

cout << "2. Find my lucky color" << endl;

cout << "3. Exit the program" << endl << endl;

cout << "Your selection: ";

cin >> option;

cout<< endl << endl;

return option;

}

// The execute method receives the option value, the two arrays,

// and their respective sizes as parameters. It implmenets a switch

// selection control structure to call the methods to find either the

// lucky number or lucky color.

// The method returns true unless the value of the option is 3, in this

// case it returns false. Make sure to include the default case which

// prints the error message " \tERROR! Incorrect option value! "

// NO VARIABLES ARE DECLARED IN THIS METHOD

bool execute(int option, string colors[], const int colorsCount, int numbers[], const int NUMBERS) {

switch (option)

{

case 1:

cout << "Your lucky number is " << getLuckyNumber(numbers, NUMBERS) << endl << endl;

break;

case 2:

cout << "Your lucky color is " << getLuckyColor(colors, colorsCount) << endl << endl;

break;

case 3:

return false;

default:

cout << " \tERROR! Incorrect option value! ";

break;

}

return true;

}

// The getLuckyColor method receives an array of strings and its size

// as parameters. It prompts the user for the lucky number using the

// phrase "Enter your lucky number: ", and returns the corresponding color.

// Declare an integer variable named number and initialize it to 0.

// The method uses the value of the number, modulus the size of the array, to select the

// correct index number from the array. You must ensure that the value

// entered is a positive value.

string getLuckyColor(string colors[], const int colorsCount) {

int number = 0;

cout << "Enter your lucky number: ";

cin >> number;

while (number <= 0) {

cout << "Enter a valid positive number: ";

cin >>number;

}

return colors[number % colorsCount];

}

// The getLuckyNumber method receives an array of integers and its size

// as parameters. It prompts the user for their day of birth using the

// phrase "Enter your day of birth: ", and returns the corresponding number.

// Declare an integer variable named birthday and initialize it to 0.

// The method use the value of the birthday, modulus the size of the array, to select the

// correct index number from the array. You must ensure that the value

// entered is a positive number.

int getLuckyNumber(int numbers[], const int NUMBERS) {

int birthday = 0;

cout << "Enter your day of birth: ";

cin >> birthday;

while (birthday <= 0) {

cout << "Enter a valid positive number: ";

cin >> birthday;

}

return numbers[birthday % NUMBERS];

}

// The randomAssign method receives an array of integers and its size

// as parameters. It assigns a random value in the range of 1 to 99 to

// each element of the array. Make sure to seed the pseudo random number generator

// with the return value of the time method, casting the value as required.

// The only variable declared is the counter for the iteration control structure.

void randomAssign(int numbers[], const int NUMBERS) {

srand(static_cast(time(NULL)));

for (int i = 0; i < NUMBERS; i++) {

numbers[i] = (rand() % 99) + 1;

}

}

// The printNumbers method receives an integer array and its size as parameters and has no

// return value. It prints the contents of the array using the phrase

// "The random values stored in the array are: [first value], ..., [last value]."

// Use a for iteration control structure to print the values, making sure that a comma and

// a space are printed after all but the last values. A period and the end of line instruction

// must be printed after the last value. Make sure to add a blank line after the phrase is printed.

void printNumbers(int numbers[], const int NUMBERS) {

cout << "The random values stored in the array are: ";

for (int i = 0; i < NUMBERS; i++) {

cout << numbers[i];

if (i < NUMBERS - 1) {

cout << ", ";

}

else {

cout << ". ";

}

}

}

// The orderNumbers method receives an integer array and its size as parameters and has no

// return value. It implements a bubble sort algorithm to order the values in the array

// from highest to lowest. Declare the local integer variable temp and initialize to 0. The

// only other variables declared are the counters for the iteration control structures.

void orderNumbers(int numbers[], const int NUMBERS) {

int temp = 0;

for (int i = 0; i < NUMBERS - 1; i++)

{

for (int j = 0; j < NUMBERS - i - 1; j++)

{

if (numbers[j] < numbers[j + 1])

{

temp = numbers[j];

numbers[j] = numbers[j + 1];

numbers[j + 1] = temp;

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

What is conservative approach ?

Answered: 1 week ago

Question

What are the basic financial decisions ?

Answered: 1 week ago