I have project in banker algorithm implement in c++
But I have small mistake :') in resource request
I didn't know what the mistake. The first if statement doesn't work :(
#include<iostream>
#include<vector>
using namespace std;
int main() {
int work[4];
int allocation[5][4];
int max[5][4];
int need[5][4];
int p, pr, r, a, aval[4], req[4];
bool state[5], test;
vector < int > avl;
//----------------------------------------
test = true;
for (int i = 0; i < 4; i++)
work[i] = aval[i];
for (int i = 0; i < 5; i++)
state[i] = false;
//----------------------------enter p r---------------------------------
cout << "Enter the number of processes in the system :";
cin >> p;
cout << "\nEnter the number of recourses :";
cin >> r;
//---------------------enter alloc---
cout << "\nEnter the allocation " << endl;
if (r = 1)
{
cout << "\t A \n \t ";
}
else if (r = 2)
{
cout << "\t A B \n \t ";
}
else if (r = 3)
{
cout << " A B C\n \t ";
}
else if (r = 4)
{
cout << " A B C D\n \t ";
}
for (int i = 0; i < p; i++)
{
cout << endl << "\t" << "P" << i << ":";
for (int j = 0; j < 4; j++)
{
cin >> allocation[i][j];
cout << " ";
}
}
//-----------------------------entet max----------------
cout << "\nEnter the MAX" << endl;
if (r = 1)
cout << " A \n \t ";
else if (r = 2)
cout << " A B \n \t ";
else if (r = 3)
cout << " A B C\n \t ";
else if (r = 4)
cout << " A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "P" << i << ":";
for (int j = 0; j < 4; j++)
{
cin >> max[i][j];
need[i][j] = max[i][j] - allocation[i][j];
}
}
//-----------------enter ava--------------
cout << "\nEnter the avaliable number : " << endl;
for (int i = 0; i < 4; i++)
{
cin >> aval[i];
cout << " ";
}
//-----------------enter request--------------
cout << "\nEnter the number of process want be request : ";
cin >> pr;
cout << "\nEnter the request number : " << endl;
for (int i = 0; i < 4; i++)
{
cin >> req[i];
cout << " ";
}
//-----------------------------------COUT---------------------
cout << endl << "There are " << p << " processes in the system." << endl << endl;
cout << "There are " << r << " resource types." << endl << endl;
//----------------------------------cout allocation---------------
cout << " The allocation Matrix : " << endl << endl;
cout << "\t A B C D";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ":";
for (int j = 0; j < 4; j++)
{
cout << allocation[i][j] << " ";
}
cout << endl;
}
//----------------------------------cout max---------------
cout << endl << " The Max Matrix is... " << endl << endl;
cout << "\t A B C D";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ":";
for (int j = 0; j < 4; j++)
{
cout << max[i][j] << " ";
}
cout << endl;
}
//-------------------------cout need-------------------------------------------
cout << endl << " The Need Matrix is... " << endl << endl;
cout << "\t A B C D";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ":";
for (int j = 0; j < 4; j++)
{
cout << need[i][j] << " ";
}
cout << endl;
}
//----------------------------- cout aval ---------------------
cout << endl << "The Available Vector is..." << endl << endl;
cout << "A B C D" << endl;
for (int i = 0; i < 4; i++)
{
cout << aval[i] << " ";
}
//-----------------------------------SAFE STATE-----------------------
int k = 0;
for (k = 0; k < p; k++) {
if (state[k] == false) {
test = false;
for (int j = 0; j<r; j++) {
if (need[k][j] > work[j])
break;
if (need[k][j] <= aval[j])
test = true;
}
}
}
if (test == true) {
for (int j = 0; j < r; j++)
{
work[j] = work[j] + allocation[k][j];
}
state[k] = true;
cout << endl << endl << "THE SYSTEM IS IN A SAFESTATE!" << endl;
}
if (test == false) {
state[k] = false;
cout << endl << endl << "THE SYSTEM IS NOT IN A SAFE STATE!";
}
//-----------------------------------request------------------------
cout << "\nThe Request Vector is..." << endl;
cout << " A B C D" << endl;
cout << pr << ":";
for (int i = 0; i < 4; i++)
{
cout << req[i] << " ";
}
bool test2 = false;
for (int i = 0; i < p; i++) {
if (pr == p) {
for (int j = 0; j < 4; j++)
{
if (req[j] <= avl[j] && req[j] <= need[i][j])
{
test2 = true;
}
else
{
break;
}
}
if (test2 = true)
{
for (int n = 0; n < r; n++)
{
aval[n] = aval[n] - req[n];
allocation[i][n] = allocation[i][n] + req[n];
need[i][n] = need[i][n] - req[n];
}
cout << "THE REQUEST CAN BE GRANTED!" << endl << endl;
cout << "The Available Vector is...";
cout << "A B C D" << endl;
for (int x = 0; x < r; x++)
{
cout << aval[x] << " ";
}
}
else
{
cout << "THE REQUEST CANNOT BE GRANTED!" << endl << endl;
}
}
}
//------------------------------------------------------------------------------
system("pause");
return 0;
}
When checking if two primitive types are equal, you need to use "==" instead of "="
e.g, change your if statements from
if ( r = 1 )
to
if (r == 1)
Apart from above
work[i] = aval[i]; - aval[i] - has not been initialised
Read up on switch instead of r == 1 do this etc. ( I took into account the above statement and the chained if statements
Ditto with = true. Learn the difference between comparison and assignment
Perhaps learn how to use the debugger
You need to use cout << flush so that the output is sent.
.... I could add others - this is enough to get on with
Here is the whole working program. I made some changes in it and made the code more easy to understand.
int p, r;
bool test;
vector < int > avl;
//----------------------------enter p r---------------------------------
system("clear");
cout << "Enter the number of processes in the system: ";
cin >> p;
cout << "Enter the number of recourses: ";
cin >> r;
//test
int allocation[p][r];
int max[p][r];
int need[p][r];
int aval[r];
int state[p];
test = true;
//test
//---------------------enter alloc---
system("clear");
cout << "\nEnter the allocation " << endl;
if (r == 1)
{
cout << "\t A \n \t ";
}
else if (r == 2)
{
cout << "\t A B \n \t ";
}
else if (r == 3)
{
cout << "\t A B C \n \t ";
}
else if (r == 4)
{
cout << "\t A B C D \n \t ";
}
for (int i = 0; i < p; i++)
{
cout << endl << "\t" << "P" << i << ": ";
for (int j = 0; j < r; j++)
{
cin >> allocation[i][j];
cout << " ";
}
}
system("clear");
//-----------------------------entet max----------------
cout << "\nEnter the MAX" << endl;
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\t" << "P" << i << ": ";
for (int j = 0; j < r; j++)
{
cin >> max[i][j];
need[i][j] = max[i][j] - allocation[i][j];
}
}
system("clear");
//-----------------enter ava--------------
cout << "\nEnter the avaliable number : " << endl;
cout<<"\tAvail: ";
for (int i = 0; i < r; i++)
{
cin >> aval[i];
cout << " ";
}
//-----------------------------------COUT---------------------
system("clear");
system("clear");
cout << "There are " << p << " processes in the system." << endl;
cout << "There are " << r << " resource types." << endl << endl;
//----------------------------------cout allocation---------------
cout << " The allocation Matrix : " << endl << endl;
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ": ";
for (int j = 0; j < r; j++)
{
cout << allocation[i][j] << " ";
}
cout << endl;
}
//----------------------------------cout max---------------
cout << endl << " The Max Matrix is... " << endl << endl;
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ": ";
for (int j = 0; j < r; j++)
{
cout << max[i][j] << " ";
}
cout << endl;
}
//-------------------------cout need-------------------------------------------
cout << endl << " The Need Matrix is... " << endl << endl;
for(int i = 0; i < p; i++)
{
for(int j = 0; j < r; j++)
{
need[i][j] = max[i][j] - allocation[i][j];
}
}
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ": ";
for (int j = 0; j < r; j++)
{
cout << need[i][j] << " ";
}
cout << endl;
}
//----------------------------- cout aval ---------------------
cout << endl << "The Available Vector is..." << endl << endl;
cout<<"\tAvail: ";
for (int i = 0; i < r; i++)
{
cout << aval[i] << " ";
}
cout<<endl;
//--------SAFESTATE----------SAFESTATE----------SAFESTATE-------------------
int count = p;
cout<<endl;
cout<<"Safe sequence: ";
do{
for(int loop_var = 0; loop_var < p; loop_var++)
{
test = false;
for (int j = 0; j < r; j++) {
if(state[loop_var] == true)
{
break;
}
if(need[loop_var][j] > aval[j])
{
test = false;
state[loop_var] = false;
break;
}
else
{
test = true;
}
}
if((test))
{
count--;
state[loop_var] = true;
for(int sb = 0; sb < r; sb++)
{
aval[sb] = aval[sb] + allocation[loop_var][sb];
}
if(count == 0)
{
cout<<"P"<<loop_var<<" ";
}
else if(count > 0)
{
cout<<"P"<<loop_var<<"-->";
}
}
}
}while(count != 0);
cout<<endl;
//--------SAFESTATE----------SAFESTATE----------SAFESTATE-------------------
cout<<endl;
cout<<"The new available vector is: ";
for(int i = 0; i < r; i++)
{
cout<<aval[i]<<" ";
}
cout << endl << endl;
return 0;
Related
I just created a program in c++ about adding matrices but I just dont know how to exception handle the part where to choose a number from 1 to 10 so that the user can only choose a number from 1 to 10 and if he puts a wrong input in it shows an error message and is asked to input a number again
#include <iostream>
using namespace std;
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
void matrix(string s) {
cout << "Enter number of rows (between 1 and 10): ";
cin >> r;
cout << "Enter number of columns (between 1 and 10): ";
cin >> c;
}
void storeValues(string s) {
cout << endl << "Enter elements of " << s << " matrix:" << " " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << "Enter element " << i + 1 << j + 1 << " : ";
if (s == "1st") {
cin >> a[i][j];
}
else
cin >> b[i][j];
}
}
void addMatrices() {
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
}
void displayResult() {
cout << endl << "Sum of two matrix is: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if (j == c - 1)
cout << endl;
}
}
int main()
{
string s = "1st";
matrix(s);
storeValues(s);
s = "2nd";
storeValues(s);
addMatrices();
displayResult();
return 0;
}
You could easily make a function that handles bounded input for you, something like:
int bounded_input(int lower, int upper, std::string message) {
int r;
bool valid = false;
cout << message << " (between " << lower << " and " << upper << "): ";
do {
cin >> r; // NOTE: You will want to check for invalid input like "three" ...
if (r < lower || r > upper) {
cout << "Invalid, please enter between " << lower << " and " << upper << ":";
} else {
valid = true;
}
} while (!valid);
return r;
}
Then you can simply call this for all of your bounds:
int row_bound = bounded_input(1, 10, "Enter row bounds");
I've been trying to overcome this problem for a few hours now and I seem to have one approach to the situation. It seems that the use of selection statements worked in creating the table necessary. Although there are formatting issues.
I'd like to know if there was a way to create the same table
using only nested for-loops as mentioned by our professor.
Are the selection statements necessary or can we implement a system of nested for loops to acquire the same results?
The image below is the required table:
But the image below is what I have:
Below is my code:
for (int i = 0; i <= numChoice; ++i)
{
if (i == 0)
{
for (int k = 1; k <= numChoice; ++k)
{
cout << " " << k;
}
cout << "\n";
}
else
{
cout << i << " | ";
for (int j = 1; j <= numChoice; ++j)
{
if (j*i <= 9)
{
cout << " " << j*i << "|";
}
else if (j*i > 9 && j*i <= 100)
{
cout << " " << j*i << "|";
}
else if (j*i > 99 && j*i <= 999)
{
cout << " " << j*i << "|";
}
}
cout << "\n";
for (int k = 0; k <= numChoice; ++k)
{
if (k == 0)
{
cout << "-|";
}
else
{
cout << "----|";
}
}
cout << "\n";
}
}
The following code uses no if else constructs. The formatting can be got by using setw, used for setting the width of integers.Following code produces perfect output.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
cout<<" "<<1;//5 space chars
for(i = 2;i <= 10;++i)
cout<<" "<<i;//4 space chars
cout<<endl;
cout<<" ----|";
for(i = 2;i <= 10;++i)
cout<<"----|";
cout<<endl;
for(i = 1;i <= 10;++i)
{
cout<<setw(2)<<i<<"|";
for(j = 1;j <= 10;++j)
cout<<setw(4)<<j*i<<"|";
cout<<endl;
cout<<" -|----";
for(j = 2;j <= 9;++j)
cout<<"|----";
cout<<"|----|";
cout<<endl;
}
return 0;
}
#FranticCode. I'm also in the same class as you and was having problems with this homework assignment as well. I still don't understand it, but I figured out how to manipulate Sumeet's code to give us correct format. The ONLY thing I am having a problem with now is adding an empty space AFTER the first multiplication table and before the menu redisplay. I'll share what I have and maybe you can figure it out. Still going to ask the professor to review chapter 5 because I would like to learn it rather than just submit the homework.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "MENU" << endl
<< "a) Generate Multiplication Table" << endl
<< "q) Quit the program" << endl
<< "Please make a selection: ";
cin >> userSelection;
if (userSelection == 'a')
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
else if (userSelection != 'q')
{
cout << "Invalid Selection\n" << endl;
}
else if (userSelection == 'q')
{
cout << " You have chosen to quit the program. Thank you for using!" << endl;
}
}
while (userSelection != 'q');
//system("PAUSE");
return 0;
}
got curious to see if i could add the lines as easy as i claimed, it took a bit of fiddling, but here's the result (updated code below to also have lines).
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int counter;
int counter2;
int amount;
cout << " |-----------------------------------------------------------|" << endl; // first line of table.
for(counter=1;counter<11;counter++){ // the 2 for lines create our 2 dimensional table
for(counter2=1;counter2<11;counter2++){
cout << " | " << setw(3) << counter*counter2; // setw(3) is a function of <iomanip>,
//setting minimum width to 3 for numbers.
}
cout << " |" << endl; // this here is being added to the end of each line and starts a new line.
cout << " |-----------------------------------------------------------|" << endl; // this is being inserted between each line, and starts a new line.
}
return 0;
}
Use the following construct:
for (int i=0; i<=numChoice; i++) // display first row of numbers
cout <<"\t" << i << "\t";
cout << "\n";
for (int i=0; i <=numChoice; i++) {
cout << i << "\t";
for (int j=0; j <=numChoice; j++)
cout << i*j << "\t";
cout << "\n";
}
I'm a beginner and I have to print the letter "N" out of #'s.
So far I can only print the |\ , so I'm still missing the last 'leg'.
I don't actually know how I got so far as this.. If anyone can help me or explain!!
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i = 1; i <= 9; i++)
{
cout << "#";
for (j = 1; j <= 12; j++)
{
if (i == j)
{
cout << "#";
}
else
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
for (i = 1; i <= 9; i++) //prints one line at a time
{
cout << "#";
for (j = 1; j <= 9; j++)
{
if (i == j) cout << "#"; //Diagonal part
else cout << " ";
}
cout << "#"; // <<< You missed this
cout << endl;
}
Little more elegant (using only one for-loop):
for (i = 1; i <= 9; i++)
{
string s = "#";
s.append(i-1, ' ' );
s +='#';
s.append(9-i, ' ' );
s +='#';
cout << s << endl;
}
i would go for the "Cheeting" way printing the exact thing without mangling with loops.
cout << "## #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# ##" << endl
easy as pie.
for(int y=0; y<9;y++){
for(int i=0; i<9; i++){
if((i==8&&y==0) or(i==8&&y==8) ){std::cout<<" ";}
if(i==0 or i==8){std::cout<<"#";}else{std::cout<<" ";};
if(i>0 && i<8){if(i==y){std::cout<<"#";std::cout<<" ";}else{std::cout<<" ";};};
};std::cout<<"\n";};
How to do a function that will clean screen and return me to a main menu of program even during entering elements of matrix. And how to disable pressing other keys except arrows and Enter during navigating the menu?
This is my C++ code:
while(running)
{
gotoXY(18,1); cout << "Main Menu";
gotoXY(20,3); cout << " Enter matrix";
gotoXY(20,4); cout << " Randomize matrix";
gotoXY(20,5); cout << " Exit";
system("pause>nul"); // the >nul bit causes it the print no message
if(GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState(VK_RIGHT)) //down button pressed
{
gotoXY(18,pos); cout << " ";
pos++;
gotoXY(18,pos); cout << "->";
menu_item++;
if (pos == 6)
{
gotoXY(18,pos); cout << " ";
pos = 3;
gotoXY(18,3); cout << "->";
menu_item = 0;
}
continue;
}
if(GetAsyncKeyState(VK_UP) || GetAsyncKeyState(VK_LEFT)) //up button pressed
{
gotoXY(18,pos); cout << " ";
pos--;
gotoXY(18,pos); cout << "->";
menu_item--;
if (pos == 2)
{
gotoXY(18,pos); cout << " ";
pos = 5;
gotoXY(18,5); cout << "->";
menu_item = 2;
}
continue;
}
if(GetAsyncKeyState(VK_RETURN)){ // Enter key pressed
switch(menu_item){
case 0: {
gotoXY(20,10);
int i, j, n;
double **a, *b;
cout << "Enter NUMBER of equations: ";
cin >> n;
a = (double **)malloc(n*sizeof(double));
b = (double *)malloc(n*sizeof(double));
cout << "Enter Matrix A\n";
for(i = 0; i < n; i++)
{
a[i] = (double *)malloc(n*sizeof(double));
//Ввод a
for(j = 0; j < n; j++)
{
cout <<"a["<< i + 1 << "][" << j + 1 << "] = ";
//cin >>a[i][j];
a[i][j] = proverkafloat();
}
}
cout << "\tSee input\r\n";
cout << "Matrix A:\r\n";
for(i = 0; i < n; i++)
{
cout << "|\t";
ShowVector(n, a[i]);
};
cout << endl;
cout << "Enter Vector B\n";
for(i = 0; i < n; i++)
{
cout << "b[" << i + 1 << "] = ";
cin >> b[i]
}
cout << "\n\n";
cout << "\tSee input\r\n";
cout << "\n";
cout << "Vector B:\r\n";
cout << "|\t";
ShowVector(n, b);
system("pause");
system("CLS");
gotoXY(18,pos); cout << "->";
break;
}
case 1:
{
gotoXY(20,10);
int i, j;
double **a, *b, n;
cout << "Enter NUMBER of equations: ";
cin >> n;
srand(time(0));
a = (double **)malloc(n*sizeof(double));
b = (double *)malloc(n*sizeof(double));
//Randomizing Matrix A
for(i = 0; i < n; i++)
{
a[i] = (double *)malloc(n*sizeof(double));
//Ввод a
for(j = 0; j < n; j++)
{
a[i][j] = (double)(rand() % 100 + (-50))/((double)(rand() % 100 + (-50)));
}
}
cout << "\tSee input\r\n";
cout << "Matrix A:\r\n";
for(i = 0; i < n; i++)
{
cout << "|\t";
ShowVector(n, a[i]);
};
//Randomizing Vector B
for(i = 0; i < n; i++)
{
b[i] = (double)(rand() % 100 + (-50))/((double)(rand() % 100 + (-50)));
}
cout << "\n\n";
cout << "\tSee input\r\n";
cout << "\n";
cout << "Vector B:\r\n";
cout << "|\t";
ShowVector(n, b);
system("pause");
system("CLS");
gotoXY(18,pos); cout << "->";
break;
}
case 2:
{
gotoXY(20,10);
cout << "The program has now terminated!!";
running = false;
getch();
}
}
}
}
gotoXY(20,21);
return 0;
}
void gotoXY(int x, int y)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console,CursorPosition);
}
void ShowVector(int n, double * vec)
{
for(int i = 0; i < n; i++)
cout << fixed << setprecision(2) << vec[i] << " ";
cout << "\t";
cout << "|";
cout << "\r\n";
}
The best method to "return" from a canceled menu processing function is to return.
One issue may be stack unwinding. When the main menu processing function calls a function for menu selection process, it pushes the return address on the stack. If the selection processing function fails or needs to return quickly, it can't use a goto because there will still be that return address on the stack. This issue is more pronounced when you have nested menus.
This is similar to error handling and catching. You could try having a catch statement in your main menu function.
How to get rid of goto statement in this program and end the flip:>>>?
int main()
{
int array[10];
int sum = 0;
int desending;
int mul = 1;
float avg = 10;
int option;
do
{
flip1:
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
flip2:
if (option == 1)
{
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
}
else if (option == 2)
{
int array[10], o, index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
}
else if (option == 3)
{
system("cls");
int a[10], r, t, temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
for (t = 0; t<9; t++)
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
}
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
char q;
cin >> q;
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
} while (option != 4);
return 0;
}
You can refer the below code.
Before entering the loop.
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
do {
if (option == 1)
{
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
/* All text as same */
At the end: change
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
To.
if (q == 'e')
{
return 0;
}
system("cls");
if (q == 'm')
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
}
PS: You can also create a function that prints the menu, get input from user and return the same.
If you divide your code into chunks of cohesive functionality, you can put them into their own functions and the main function can be much simpler. Here's my recommendation:
#include <iostream>
#include <cstdlib>
using namespace std;
int getOption1()
{
int option;
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
return option;
}
char getOption2()
{
char option;
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
cin >> option;
return option;
}
void doArrayBasics()
{
int array[10];
int sum = 0;
float avg = 10;
int mul = 1;
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
}
void doArraySearching()
{
int array[10];
int o;
int index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
}
void doArraySorting()
{
system("cls");
int a[10];
int r;
int t;
int temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
for (t = 0; t<9; t++)
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
}
int main()
{
int option1;
char option2 = 'r';
do
{
if ( option2 == 'r' )
{
option1 = getOption1();
}
switch (option1)
{
case 1:
doArrayBasics();
break;
case 2:
doArraySearching();
break;
case 3:
doArraySorting();
break;
case 4:
break;
default:
cout << "Invalid option " << option1 << endl;
}
if ( option1 != 4 )
{
option2 = getOption2();
}
} while (option1 != 4 && option2 != 'e' );
return 0;
}
You can use an inner loop to remove all gotos.
Remove flip1:, change flip2: to do {. The loop should begin like that:
do
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
do
{
if (option == 1)
{
At the end of the loop, change:
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
to :
if (q == 'e')
{
return 0;
}
system("cls");
} while (q == 'r');
Breaking your code into functions will also make it more readable but is not mandatory to remove gotos.
caveat: the following code not tested
int main()
{
int array[10];
int sum = 0;
int desending;
int mul = 1;
float avg = 10;
int option;
int done = 0;
int looping = 1;
while( !done )
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
while( looping )
{
looping = 0;
switch( option )
{
case 1:
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
break;
case 2:
int array[10], o, index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
break;
case 3:
system("cls");
int a[10], r, t, temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
{
for (t = 0; t<9; t++)
{
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
}
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
break;
case 4:
done = 1;
break;
default:
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
char q;
cin >> q;
switch( q )
{
case 'm':
system("cls");
// looping already set = 0
break;
case 'r':
system("cls");
looping = 1;
break;
case 'e':
done = 1;
break;
default:
break;
} // end switch
break;
} // end switch
} // end while looping
} // end while not done
return 0;
} // end function: main