Average
#include <iostream>
#include <cstdlib>
using namespace std;
bool check(double element[], int count);
int main()
Average
#include <iostream>
#include <cstdlib>
using namespace std;
bool check(double element[], int count);
int main()
{
char redo;
do
{
system ("cls");
double elements = 0, i = 0, j = 0, average_elements = 0;
int k[1];
bool correct;
cout<< "This program will show the average element of a sequence.\n\n";
cout<< "Enter the number of elements: ";
cin>>elements;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(elements < 1 || elements - int(elements) != 0)
cout << "Input must be a positive whole number.";
else
{
double element[int(elements)];
for(int count = 0; count < elements; count++)
{
cout<< "Enter the sequence "<<count + 1<< ": ";
correct = check(element, count);
if(correct == false)
break;
}
if(correct == true)
{
bool first_layer = false, second_layer = false;
int k_count = 0;
for(int count = 0; count < elements - 1; count++)
{
for(int count2 = count + 1; count2 < elements; count2++)
{
average_elements = (element[count] + element[count2]) / 2;
for(int count3 = 0; count3 < elements; count3++)
{
if(average_elements == element[count3])
{
i = count + 1;
j = count2 + 1;
k[k_count++] = count3 + 1;
first_layer = true;
second_layer = true;
}
}
if(second_layer == true)
break;
}
if(first_layer == true)
break;
}
cout<< "\nThe number of Average Elements is: "<<k_count<<endl<<endl;
if(k_count > 0)
{
for(int count = 0; count < k_count; count++)
{
cout<< "i = "<<i<< ", "<< "j = "<<j<< ", "<< "k = "<< k[count]<< endl;
cout<< "Ak = ("<<element[int(i - 1)]<< " + "<<element[int(j- 1)]<< ") / 2 = "<<average_elements<<endl<<endl;
}
}
}
}
}
cout<< "\n\nWant to try again?[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
return 0;
}
bool check(double element[], int count)
{
cin>>element[count];
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(element[count] < 1 || element[count] - int(element[count]) != 0)
{
cout<< "Input must be a positive whole number";
return false;
}
else
return true;
}
}
BCCS Election
#include <iostream>
#include <cstdlib>
using namespace std;
int large_func(double candidate[], double candidates);
bool check(double voter[], int count);
int main()
{
char redo;
do
{
system ("cls");
double candidates, voters;
cout<< "This program will show who will be in the third place.\n\n";
cout<< "Enter the number of candidates: ";
cin>>candidates;
if(!cin)
{
cout << "Invalid input.";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout << "Invalid input.";
else if(candidates < 1 || candidates - int(candidates) != 0)
cout<< "Input must be positive whole number";
else
{
cout<< "Enter the number of voters: ";
cin>>voters;
if(!cin)
{
cout << "Invalid input.";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout << "Invalid input.";
else if(voters < 1 || voters - int(voters) != 0)
cout<< "Input must be positive whole number";
else
{
double voter[int(voters)], candidate[int(candidates)];
bool correct;
for(int count = 0; count < voters; count++)
{
cout<< "Enter the vote of voter "<<count + 1<<": ";
correct = check(voter, count);
if(correct == false)
break;
}
if(correct == true)
{
for(int count = 1; count <= candidates; count++)
{
int total_vote = 0;
for(int count2 = 0; count2 < voters; count2++)
{
if(count == voter[count2])
total_vote++;
}
candidate[count - 1] = total_vote;
}
int third_candidate = 0;
for(int count = 0; count < 3; count++)
third_candidate = large_func(candidate, candidates);
cout<< "\nCandidate "<<third_candidate + 1<< " is third.";
}
}
}
}
}
cout<< "\n\nWant to try again/[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
}
int large_func(double candidate[], double candidates)
{
int largest = candidate[0], set_target = 0;
for(int count = 1; count < candidates; count++)
{
if(largest < candidate[count])
{
largest = candidate[count];
set_target = count;
}
}
candidate[set_target] = 0;
return set_target;
}
bool check(double voter[], int count)
{
cin>>voter[count];
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(voter[count] < 1 || voter[count] - int(voter[count]) != 0)
{
cout<<"Input must be positive whole number";
return false;
}
else
return true;
}
}
Cellular Jail
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char redo;
do
{
int empty_jail, dimensions;
cout<< "This program will show the longest distance of a given jails.\n\n";
cout<< "Enter the number of empty jail: ";
cin>>empty_jail;
cout<< "Enter the number of dimensions: ";
cin>>dimensions;
int array[empty_jail * dimensions];
int size = 0;
for(int count = 0; count < empty_jail; count++)
{
cout<< "Enter the coordinate of jail "<<count + 1<< ": ";
for(int count2 = 0; count2 < dimensions; count2++)
cin>>array[size++];
}
int longest_length = 0;
int start[dimensions], ending[dimensions];
for(int count = 0; count < size - dimensions; count += dimensions)
{
for(int count2 = count + dimensions; count2 < size; count2 += dimensions)
{
int length = 0;
for(int count3 = count, count4 = count2; count3 < dimensions; count3++, count4++)
{
if(array[count3] > array[count4])
length = array[count3] - array[count4] + length;
else
length = array[count4] - array[count3] + length;
}
if(longest_length < length)
{
longest_length = length;
for(int count5 = count, count6 = count2, count7 = 0; count7 < dimensions; count5++, count6++, count7++)
{
start[count7] = array[count5];
ending[count7] = array[count6];
}
}
}
}
cout<< "Starts in "<<endl;
for(int count = 0; count < dimensions; count++)
cout<<start[count]<<" ";
cout<< "\nand Ends in "<<endl;
for(int count = 0; count < dimensions; count++)
cout<<ending[count]<<" ";
cout<< "With distance of "<<longest_length;
cout<< " \n\nWant to try again/[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
return 0;
}
List of Books
#include <iostream>
#include <cstdlib>
using namespace std;
int book_process(int book[], int book_ith[], int count, int books);
int main()
{
char redo;
do
{
system ("cls");
int num_book_borrowed, books;
cout<< "This program will show the book borrowed.\n\n";
cout<< "Enter the number of books: ";
cin>>books;
int book[books];
for(int count = 0; count < books; count++)
{
cout<< "Enter the book "<<count + 1<< ": ";
cin>>book[count];
}
cout<< "Enter the number of borrowed books: ";
cin>> num_book_borrowed;
int book_ith[num_book_borrowed];
for(int count = 0; count < num_book_borrowed; count++)
{
cout<< "Enter the position of borrowed book "<<count + 1<< ": ";
cin>> book_ith[count];
}
double book_borrowed[num_book_borrowed];
cout<< "The books borrowed are: ";
for(int count = 0; count < num_book_borrowed; count++)
{
book_borrowed[count] = book_process(book, book_ith, count, books);
cout<< book_borrowed[count]<< " ";
}
cout<< "\n\nWant to try again/[y/n]: ";
cin>>redo;
}while(redo == 'Y' || redo == 'y');
return 0;
}
int book_process(int book[], int book_ith[], int count, int books)
{
int count3 = 0;
for(int count2 = 0; count2 < books; count2++) // 4
{
if(book[count2] != (NULL))
count3++;
if(count3 == book_ith[count]) // 4
{
int holder = book[count2];
book[count2] = (NULL);
return holder;
}
}
}
Multiple Caterpillar
#include <iostream>
#include <cstdlib>
using namespace std;
bool check(double caterpillar_length[], int count, int input_leaves);
int main()
{
char redo;
do
{
system ("cls");
double caterpillar, input_leaves, damaged_leaves = 0, undamaged_leaves = 0;
bool eaten_leaf = true, correct;
cout<<"Enter the number of leaves: ";
cin>>input_leaves;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(input_leaves - int(input_leaves) != 0)
cout<< "Input must be whole number";
else if(input_leaves < 1)
cout<< "Input must be positive";
else
{
cout<<"Enter the number of caterpillar: ";
cin>>caterpillar;
cout<<endl;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(caterpillar - int(caterpillar) != 0)
cout<< "Input must be whole number";
else if(caterpillar < 1)
cout<< "Input must be positive";
else
{
double caterpillar_length[int(caterpillar)];
for(int count = 0; count < caterpillar; count++)
{
cout<<"Enter the length of caterpillar "<<count + 1<<": ";
correct = check(caterpillar_length, count, input_leaves);
if(correct == false)
break;
}
if(correct == true)
{
double leaves[int(input_leaves)], bitten[int(caterpillar)][int(input_leaves)];
for(int count = 0; input_leaves > count; count++)
leaves[count] = count + 1;
cout<<endl;
bool stop = false;
for(int count = 0; caterpillar > count; count++)
{
stop = false;
cout<<"The Damaged leaves done by caterpillar "<<count + 1<<" are: ";
for(int count2 = 0; input_leaves > count2; count2 += caterpillar_length[count])
{
cout<<leaves[count2];
bitten[count][count2] = count2 + 1;
if(input_leaves > 2 && count2 + caterpillar_length[count] * 2 < input_leaves)
cout<< ", ";
if(caterpillar_length[count] < input_leaves && count2 + caterpillar_length[count] * 2 >= input_leaves && stop == false)
{
cout<< " & ";
stop = true;
}
}
cout<<endl;
}
for(int count = 0; caterpillar > count; count++)
{
for(int count2 = 0; input_leaves > count2; count2++)
{
if(leaves[count2] == bitten[count][count2])
leaves[count2] = eaten_leaf;
}
}
cout<<endl;
stop = false;
cout<<"The Undamaged leaves at positions ";
for(int count = 0; input_leaves > count; count++)
{
if(leaves[count] != eaten_leaf)
undamaged_leaves++;
else
damaged_leaves++;
}
for(int count = 0; input_leaves > count; count++)
{
if(leaves[count] != eaten_leaf)
{
cout<<leaves[count];
if(undamaged_leaves > 2 && count + 3 < input_leaves)
cout<< ", ";
if(undamaged_leaves > 1 && count + 4 > input_leaves && stop == false)
{
stop = true;
cout<< " & ";
}
}
}
if(undamaged_leaves == 0)
cout<<"0";
cout<<"\n\nThe total number of undamaged leaves is "<<undamaged_leaves;
}
}
}
}
}
cout<<"\n\nWant to try again?[y/n]: ";
cin>>redo;
}
while(redo == 'y' || redo == 'Y');
}
bool check(double caterpillar_length[], int count, int input_leaves)
{
cin>>caterpillar_length[count];
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(caterpillar_length[count] - int(caterpillar_length[count]) != 0 || caterpillar_length[count] < 1)
{
cout<< "Input must be a positive whole number";
return false;
}
else if(caterpillar_length[count] > input_leaves)
{
cout<< "Input must be less than the number of leaves,\nelse the only eaten leaf is 1.";
return false;
}
else
return true;
}
}
Pascal's Triangle
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char redo;
do
{
system ("cls");
double rows, coef = 1;
cout<<"Enter number of rows: ";
cin>>rows;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(rows < 1 || rows - int(rows) != 0)
cout<< "Input must be a positive whole number";
else
{
cout<<endl<<endl;
for(int row = 0; row < rows; row++)
{
for(int space = 0; space < rows - row; space++)
cout<<" ";
for(int column = 0; column <= row; column ++)
{
if (column == 0 || row == 0)
coef = 1;
else
coef=coef * (row - column + 1) / column;
cout<<" "<<coef;
}
cout<<endl;
}
}
}
cout<< "\n\nWant to try again/[y/n: ";
cin>> redo;
}while(redo == 'y' || redo == 'Y');
}
Railway Catering Contracts
#include <iostream>
#include <cstdlib>
using namespace std;
bool check(double station[], int count);
int main()
{
char redo;
do
{
system ("cls");
double stations, bid;
cout<< "This program will show the maximum profit the contractor gets and the stations \nhe bid with its contiguous sequence.\n\n";
cout<< "Enter the number of stations: ";
cin>>stations;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(stations < 1 || stations - int(stations) != 0)
cout<< "Input must be positive whole number";
else
{
cout<< "Enter the minimum number to bid: ";
cin>>bid;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(bid < 1 || bid - int(bid) != 0)
cout<< "Input must be positive whole number";
else
{
double station[int(stations)], max_profit = 0, start, ending;
bool correct;
for(int count = 0; count < stations; count++)
{
cout<< "Enter the profit of station "<<count + 1<< ": ";
correct = check(station, count);
if(correct == false)
break;
}
if(correct == true)
{
for(int count = 0; count < stations - bid + 1; count++)
{
for(int count2 = 0; count2 <= stations - bid - count; count2++)
{
int profit = station[count];
for(int count3 = count + 1; count3 < stations - count2; count3++)
profit = profit + station[count3];
if(max_profit < profit)
{
max_profit = profit;
start = count + 1;
ending = stations - count2;
}
}
}
cout<<endl<<endl;
cout<<max_profit<< " max profit"<<endl<<endl;
cout<< "station: "<<endl;
for(int count = start; count <= ending; count++)
cout<< " "<<count;
cout<<endl;
cout<< "\nContiguous sequence: "<<endl;
for(int count = start - 1; count < ending; count++)
cout<< " "<<station[count];
}
}
}
}
}
cout<< "\n\nWant to try again/[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
return 0;
}
bool check(double station[], int count)
{
cin>>station[count];
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(station[count] - int(station[count]) != 0)
{
cout<< "Input must be positive whole number";
return false;
}
else
return true;
}
}
Sinuseri Sports Stadium
#include <iostream>
#include <cstdlib>
using namespace std;
bool check(double starting_date[], double& length, int count);
int main()
{
char redo;
do
{
system ("cls");
double events, length;
cout<<"This program will show will use the Stadium.\n\n";
cout<<"Enter the number of events: ";
cin>>events;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(events < 1 || events - int(events) != 0)
cout<< "Input must be a positive whole number";
else
{
double starting_date[int(events)], ending_date[int(events)];
bool correct;
for(int count = 0; count < events; count++)
{
cout<<"Enter the starting date and length of event "<<count + 1<<": ";
correct = check(starting_date, length, count);
if(correct == false)
break;
ending_date[count] = starting_date[count] + length - 1;
}
if(correct == true)
{
for(int count = 0; count < events - 1; count++)
{
for(int count2 = events - 1; count2 > count; count2--)
{
if(starting_date[count2] < starting_date[count2 - 1])
{
int holder = starting_date[count2 - 1];
starting_date[count2 - 1] = starting_date[count2];
starting_date[count2] = holder;
int holder2 = ending_date[count2 - 1];
ending_date[count2 - 1] = ending_date[count2];
ending_date[count2] = holder2;
}
if(starting_date[count2] == starting_date[count2 - 1] && ending_date[count2] > ending_date[count2 - 1])
{
int holder3 = ending_date[count2 - 1];
ending_date[count2 - 1] = ending_date[count2];
ending_date[count2] = holder3;
}
}
}
for(int x= 0; x < events; x++)
cout<<starting_date[x]<<"\t"<< ending_date[x]<<endl;
int count_events = events;
bool close[int(events)], first_layer = false;
for(int count = 0; count < events - 1; count++)
{
for(int count2 = count + 1; count2 < events; count2++)
{
if(starting_date[count] != starting_date[count2])
{
if(ending_date[count] < starting_date[count2] || starting_date[count] > ending_date[count2])
close[count] = false;
else
{
close[count] = true;
count_events--;
break;
}
}
if(starting_date[count] == starting_date[count2] && count2 == events - 1)
{
close[count] = true;
first_layer = true;
count_events--;
break;
}
}
}
cout << "\nThe possible maximum event is "<<count_events<<endl<<endl;
for(int count = 0; count < events; count++)
{
if(close[count] == false)
{
cout << "The staring date "<<starting_date[count]<< " with the length "
<<ending_date[count] - starting_date[count] + 1 << endl;
}
}
}
}
}
cout<<"\n\nWant to try again/[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
return 0;
}
bool check(double starting_date[], double& length, int count)
{
cin>>starting_date[count]>>length;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(starting_date[count] < 1 || length < 1 ||
starting_date[count] - int(starting_date[count]) != 0 || length - int(length) != 0)
{
cout<< "Input must be a positive whole number";
return false;
}
else
return true;
}
}
The End of Corruption
#include <iostream>
#include <cstdlib>
using namespace std;
int large_func(double citizen_wealth[], int count);
bool check(double citizen_wealth[], int& count_to_proceed, int count, double citizens);
int main()
{
char redo;
do
{
system ("cls");
double citizens, king_visit, dead_count = 0;
cout<<"This program will display the wealth of headless citizens.\n\n";
cout<<"Enter the number of citizens: ";
cin>>citizens;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100 , '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(citizens < 1 || citizens - int(citizens) != 0)
cout<< "Input must be a positive whole number";
else
{
cout<<"Enter the number of King's visit: ";
cin>>king_visit;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(king_visit < 1 || king_visit - int(king_visit) != 0)
cout<< "Input must be a positive whole number";
else
{
double citizen_wealth[int(citizens) + int(king_visit)];
double headless_wealth[int(king_visit)];
bool correct;
int count_to_proceed = 0;
for(int count = 0; count < (citizens + king_visit); count++)
{
cout<<"Enter the citizen's wealth or King's visit: ";
correct = check(citizen_wealth, count_to_proceed, count, citizens);
if(correct == false)
break;
}
bool layer;
for(int count = 0; count < king_visit + citizens - 1; count++)
{
for(int count2 = count + 1; count2 < king_visit + citizens; count2++)
{
if(citizen_wealth[count] == citizen_wealth[count2] && citizen_wealth[count] != -1)
{
cout<< "Citizen's wealth must not be the same with the others";
correct = false;
layer = false;
break;
}
}
if(layer == false)
break;
}
if(correct == true && count_to_proceed == king_visit)
{
for(int count = 0; count < (citizens + king_visit); count++)
{
if(citizen_wealth[count] == -1)
headless_wealth[int(dead_count++)] = large_func(citizen_wealth, count);
}
for(int print = 0; print < king_visit; print++)
cout<<endl<<"The wealth of headless citizen on King's visit "<<print + 1<<": "<<headless_wealth[print];
}
else if(count_to_proceed != king_visit && correct != false)
cout<< "\nInput is not equal to king's visit";
}
}
}
}
cout<<"\n\nWant to try again?[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
return 0;
}
int large_func(double citizen_wealth[], int count)
{
int richest = citizen_wealth[0], holder;
for(int count2 = 1; count2 < count; count2++)
{
if(richest < citizen_wealth[count2])
{
richest = citizen_wealth[count2];
holder = count2;
}
}
citizen_wealth[holder] = NULL;
return richest;
}
bool check(double citizen_wealth[], int& count_to_proceed, int count, double citizens)
{
cin>>citizen_wealth[count];
if(citizen_wealth[count] == -1)
count_to_proceed++;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(citizen_wealth[count] < -1 || citizen_wealth[count] - int(citizen_wealth[count]) != 0)
{
cout<< "Input must be a positive whole number";
return false;
}
else if(count == 0 && citizen_wealth[count] == -1)
{
cout<< "First input must not be the king's Visit.";
return false;
}
else if(citizens < 2)
{
cout<<endl<<"The wealth of headless citizen on King's visit 1: "<< citizen_wealth[count];
return false;
}
else
return true;
}
}
The Sculptor
#include <iostream>
#include <cstdlib>
using namespace std;
bool check(double rod[], int count);
int main()
{
char redo;
do
{
system ("cls");
double rods;
cout<< "This program will show the usable steel rods he needs for his new sculptor.\n\n";
cout<< "Enter the number of steel rods: ";
cin>>rods;
if(!cin)
{
cout< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(rods < 1 || rods - int(rods) != 0)
cout<< "Input must be positive whole number";
else
{
double rod[int(rods)];
bool correct;
for(int count = 0; count < rods; count++)
{
cout<< "Enter the length of steel rods "<<count + 1<< ": ";
correct = check(rod, count);
if(correct == false)
break;
}
if(correct == true)
{
for(int count = 0; count < rods - 1; count++)
{
for(int count2 = rods - 1; count2 < count; count2++)
{
if(rod[count2] < rod[count2 - 1])
{
int holder = rod[count2 - 1];
rod[count2 - 1] = rod[count2];
rod[count2] = holder;
}
}
}
int max_length = 0, starting, ending, its_length;
for(int count = 0; count < rods - 2; count++)
{
for(int count2 = count + 1; count2 < rods - 1; count2++)
{
int length = rod[count2] - rod[count], count_exist = 2;
for(int count3 = count2 + 1; count3 < rods; count3++)
{
if(rod[count2] + length == rod[count3])
count_exist++;
if(max_length < count_exist)
{
its_length = length;
max_length = count_exist;
starting = rod[count];
ending = rod[count3];
}
}
}
}
cout<< "\nThe maximum number of rods form this collection is: "<<max_length;
cout<< "\n\nThe largest set of rods that can be used in his sculptor are: ";
while(starting <= ending)
{
cout<<starting<< " ";
starting = starting + its_length;
}
}
}
}
cout<< "\n\nWant to try again/[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
return 0;
}
bool check(double rod[], int count)
{
cin>>rod[count];
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(rod[count] < 1 || rod[count] - int(rod[count]) != 0)
{
cout<< "Input must be positive whole number";
return false;
}
else
return true;
}
}
What is the Rank
#include <iostream>
#include <cstdlib>
using namespace std;
int sort_rank(double merchant_wealth[], int count);
bool check(double merchant_wealth[], int count);
int main()
{
char redo;
do
{
system ("cls");
double merchants_num, merchant_rank;
cout<<"This program tells the rank of merchant as he enter the hall.\n\n";
cout<<"Enter the number of merchants: ";
cin>>merchants_num;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(merchants_num < 1 || merchants_num - int(merchants_num) != 0)
cout<< "Input must be a positive whole number";
else
{
double merchant_wealth[int(merchants_num)];
bool correct;
cout<<endl;
for(int count = 0; count < merchants_num; count++)
{
cout<<"Enter the wealth of merchant "<<count + 1<<": ";
correct = check(merchant_wealth, count);
if(correct == false)
break;
}
if(correct == true)
{
for(int count = 0; count < merchants_num;)
{
if(count == 0)
merchant_rank = 1;
else
merchant_rank = sort_rank(merchant_wealth, count);
cout<<endl<<"The rank of merchant "<<++count<<" as he enter the hall is: "<<merchant_rank;
}
}
}
}
cout<<"\n\nWant to try again[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
}
int sort_rank(double merchant_wealth[], int count)
{
int hold_to_find = merchant_wealth[count];
for(int count2 = count; count2 > 0; count2--)
{
if(merchant_wealth[count2] > merchant_wealth[count2 - 1])
{
int holder = merchant_wealth[count2 - 1];
merchant_wealth[count2 - 1] = merchant_wealth[count2];
merchant_wealth[count2] = holder;
}
}
for(int count3 = 0; count3 <= count; count3++)
{
if(hold_to_find == merchant_wealth[count3])
{
return (count3 + 1);
break;
}
}
}
bool check(double merchant_wealth[], int count)
{
cin>>merchant_wealth[count];
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(merchant_wealth[count] < 1 || merchant_wealth[count] - int(merchant_wealth[count]) != 0)
{
cout<< "Input must be a positive whole number";
return false;
}
else
return true;
}
}
Winning Strategy
#include <iostream>
#include <cstdlib>
#include <climits>
using namespace std;
int small_func(double ELO_Rating[], int size);
int large_func(double ELO_Rating[], int size);
int player_func(int smallest_rating, double ELO_rating[], int size);
bool check(double ELO_Rating[], int x);
int main()
{
char redo;
do
{
system ("cls");
double size;
cout<<"This program will find a way to win the overall game for Sinuseri Team.\n\n";
cout<<"Enter the number of players: ";
cin>>size;
if(!cin)
{
cout<<"Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<<"Invalid input";
else if(size <= 0)
cout<<"Input must be a positive";
else if(size - int(size) != 0)
cout<<"Input must be a whole number";
else
{
int Sinusari[int(size)], Navalur[int(size)], Sinusari_rating, Navalur_rating,
Sinusari_win = 0, Navalur_win = 0, elim_Navalur;
double Sinusari_ELO_Rating[int(size)], Navalur_ELO_Rating[int(size)];
if(int(size) % 2 != 0)
elim_Navalur = size / 2;
else
elim_Navalur = size / 2 - 1;
bool correct, correct2;
cout<<endl;
for(int x = 0; x < size; x++)
{
cout<<"Enter the Sinuseri ELO Rating of Player "<<x + 1<<": ";
correct = check(Sinusari_ELO_Rating, x);
if(correct == false)
break;
}
if(correct == true)
{
cout<<endl;
for(int x = 0; x < size; x++)
{
cout<<"Enter the Navalur ELO Rating of Player "<<x + 1<<": ";
correct2 = check(Navalur_ELO_Rating, x);
if(correct2 == false)
break;
}
if(correct2 == true)
{
if(size == 2)
{
Navalur_rating = large_func(Navalur_ELO_Rating, size);
Sinusari_rating = large_func(Sinusari_ELO_Rating, size);
if(Sinusari_rating < Navalur_rating)
elim_Navalur++;
}
for(int x = 0; x < size; x++)
{
if(x < elim_Navalur)
{
Sinusari_rating = small_func(Sinusari_ELO_Rating, size);
Sinusari[x] = player_func(Sinusari_rating, Sinusari_ELO_Rating, size);
}
else
{
Sinusari_rating = large_func(Sinusari_ELO_Rating, size);
Sinusari[x] = player_func(Sinusari_rating, Sinusari_ELO_Rating, size);
}
Navalur_rating = large_func(Navalur_ELO_Rating, size);
Navalur[x] = player_func(Navalur_rating, Navalur_ELO_Rating, size);
if(Sinusari_rating > Navalur_rating)
Sinusari_win++;
else if(Sinusari_rating < Navalur_rating)
Navalur_win++;
else
{
Sinusari_win++;
Navalur_win++;
}
}
cout<<endl;
if(Sinusari_win > Navalur_win)
cout<<"The Sinuseri Team win: "<<Sinusari_win<<"/"<<size;
else if(Sinusari_win == Navalur_win)
cout<<"The overall Game is a tie: "<<Sinusari_win<<"/"<<size;
else
cout<<"The Sinuseri Team lose: "<<Sinusari_win<<"/"<<size;
cout<<endl<<endl;
bool stop = false;
for(int x = 0; x < size; x++)
{
cout<<Sinusari[x]<<" v/s "<<Navalur[x]<<endl;
if(x + 2 >= size && stop == false)
{
cout<<endl<< " and "<<endl<<endl;
stop = true;
}
}
}
}
}
}
cout<<"\n\nWant to try again?[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
}
int small_func(double ELO_Rating[], int size)
{
int small_rating = large_func(ELO_Rating, size);
for(int x = 0; x < size; x++)
{
if(small_rating > ELO_Rating[x] && ELO_Rating[x] != NULL)
small_rating = ELO_Rating[x];
}
return small_rating;
}
int large_func(double ELO_Rating[], int size)
{
int large_rating = ELO_Rating[0];
for(int x = 1; x < size; x++)
{
if(large_rating < ELO_Rating[x])
large_rating = ELO_Rating[x];
}
return large_rating;
}
int player_func(int find_rating, double ELO_rating[], int size)
{
for(int x = 0; x < size; x++)
{
if(find_rating == ELO_rating[x])
{
ELO_rating[x] = NULL;
return (x + 1);
}
}
}
bool check(double ELO_Rating[], int x)
{
cin>>ELO_Rating[x];
if(!cin)
{
cout<< "Invalid input";
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(ELO_Rating[x] < 1)
{
cout<< "Input must be positive";
return false;
}
else if(ELO_Rating[x] - int(ELO_Rating[x]) != 0)
{
cout<< "Input must be a whole number";
return false;
}
else
return true;
}
}
Book Sorting
#include <iostream>
using namespace std;
int main()
{
int numberOfbooks;
cout<<"\n\t\tBook Sorting\n"<<endl;
cout<<" Enter number of books in Indraneel's library: ";
cin>>numberOfbooks;
int arrays_permutation[numberOfbooks];
cout<<endl;
cout<<" Enter the initial state of Indraneel's book shelf: ";
for(int index = 0; index < numberOfbooks; index++)
{
cin>>arrays_permutation[index];
}
int moves = 0;
for(int index1 = 0; index1 < numberOfbooks; index1++)
{
for(int index1_inner1 = index1+1; index1_inner1 < numberOfbooks; index1_inner1++)
{
if(arrays_permutation[index1_inner1] == index1+1){
for(int index1_inner2 = index1_inner1; index1_inner2 > index1; index1_inner2--)
{
arrays_permutation[index1_inner2] = arrays_permutation[index1_inner2-1];
}
//arrays_permutation[index1] = index1+1;
moves++;
/*for(int i = 0; i < numberOfbooks; i++)
{
cout<<arrays_permutation[i]<<" ";
}
cout<<endl;*/
}
}
}
cout<<"\n Minimum number of moves to sort Indraneel's book shelf: "<<moves;
cin.get();
return 0;
}
char redo;
do
{
system ("cls");
double elements = 0, i = 0, j = 0, average_elements = 0;
int k[1];
bool correct;
cout<< "This program will show the average element of a sequence.\n\n";
cout<< "Enter the number of elements: ";
cin>>elements;
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout<< "Invalid input";
else if(elements < 1 || elements - int(elements) != 0)
cout << "Input must be a positive whole number.";
else
{
double element[int(elements)];
for(int count = 0; count < elements; count++)
{
cout<< "Enter the sequence "<<count + 1<< ": ";
correct = check(element, count);
if(correct == false)
break;
}
if(correct == true)
{
bool first_layer = false, second_layer = false;
int k_count = 0;
for(int count = 0; count < elements - 1; count++)
{
for(int count2 = count + 1; count2 < elements; count2++)
{
average_elements = (element[count] + element[count2]) / 2;
for(int count3 = 0; count3 < elements; count3++)
{
if(average_elements == element[count3])
{
i = count + 1;
j = count2 + 1;
k[k_count++] = count3 + 1;
first_layer = true;
second_layer = true;
}
}
if(second_layer == true)
break;
}
if(first_layer == true)
break;
}
cout<< "\nThe number of Average Elements is: "<<k_count<<endl<<endl;
if(k_count > 0)
{
for(int count = 0; count < k_count; count++)
{
cout<< "i = "<<i<< ", "<< "j = "<<j<< ", "<< "k = "<< k[count]<< endl;
cout<< "Ak = ("<<element[int(i - 1)]<< " + "<<element[int(j- 1)]<< ") / 2 = "<<average_elements<<endl<<endl;
}
}
}
}
}
cout<< "\n\nWant to try again?[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
return 0;
}
bool check(double element[], int count)
{
cin>>element[count];
if(!cin)
{
cout<< "Invalid input";
cin.clear();
cin.ignore(100, '\n');
return false;
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
{
cout<< "Invalid input";
return false;
}
else if(element[count] < 1 || element[count] - int(element[count]) != 0)
{
cout<< "Input must be a positive whole number";
return false;
}
else
return true;
}
}
BCCS Election
#include <iostream>
#include <cstdlib>
using namespace std;
int large_func(double candidate[], double candidates);
bool check(double voter[], int count);
int main()
{
char redo;
do
{
system ("cls");
double candidates, voters;
cout<< "This program will show who will be in the third place.\n\n";
cout<< "Enter the number of candidates: ";
cin>>candidates;
if(!cin)
{
cout << "Invalid input.";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout << "Invalid input.";
else if(candidates < 1 || candidates - int(candidates) != 0)
cout<< "Input must be positive whole number";
else
{
cout<< "Enter the number of voters: ";
cin>>voters;
if(!cin)
{
cout << "Invalid input.";
cin.clear();
cin.ignore(100, '\n');
}
else
{
cin.ignore(100, '\n');
if(cin.gcount() > 1)
cout << "Invalid input.";
else if(voters < 1 || voters - int(voters) != 0)
cout<< "Input must be positive whole number";
else
{
double voter[int(voters)], candidate[int(candidates)];
bool correct;
for(int count = 0; count < voters; count++)
{
cout<< "Enter the vote of voter "<<count + 1<<": ";
correct = check(voter, count);
if(correct == false)
break;
}
if(correct == true)
{
for(int count = 1; count <= candidates; count++)
{
int total_vote = 0;
for(int count2 = 0; count2 < voters; count2++)
{
if(count == voter[count2])
total_vote++;
}
candidate[count - 1] = total_vote;
}
int third_candidate = 0;
for(int count = 0; count < 3; count++)
third_candidate = large_func(candidate, candidates);
cout<< "\nCandidate "<<third_candidate + 1<< " is third.";
}
}
}
}
}
cout<< "\n\nWant to try again/[y/n]: ";
cin>>redo;
}while(redo == 'y' || redo == 'Y');
}
int large_func(double candidate[], double candidates)
{
int largest = candidate[0], set_target = 0;
for(int count = 1; count < candidates; count++)
{
if(largest < candidate[count])
{
largest = candidate[count];
set_target = count;
}
}
candidate[set_target] = 0;
return set_target;
}
bool check(double voter[], int count)
{
cin>>voter[cou
obinaysamanoden 0 Newbie Poster
David W 131 Practically a Posting Shark
Hey ... best to submit one problem at a time.
We are not machines.
I would suggest you think a lot more about, perhaps, your most pressing problem, and ask for help with that code, first.
Then explain what it is you want that code to do.
With just a quick scan of the muddled code conglomerate you just threw this way, it looks like you could use a much more modular design and thus break up big jobs into individual parts, each with its own well named function.
Also, if you wish to learn how to write portable code, then there are problems with your code in that regard.
ravenous 266 Posting Pro in Training
Even broken up into individual little programs, I don't think these will even compile. Probably best to get them compiling first and go from there. Efficiency is not your biggest issue right now.
If you don't know how to do that, then ask questions about the errors that occur when you try it.
David W 131 Practically a Posting Shark
This example of taking in valid numeric input,
in an loop,
into a STL vector container,
and then finding sum, average, min, max,
using C++ library functions ...
this is a fairly common C++ beginning student type problem.
It may give you some ideas about how to get started to recode,
especially your first problem above ...
and how to use a modular design, (how to functions).
// takeInPositiveFindAverage.cpp //
#include <iostream>
#include <iomanip> // re. setprecision, fixed
#include <vector>
#include <numeric> // re. accumulate( begin, end, startVal )
#include <algorithm> // re. min_element, max_element
using namespace std;
// used here to take in valid positive integers
template < typename T >
T takeIn( const char* msg, const char* errmsg )
{
T val;
while( true )
{
cout << msg << flush;
if( cin >> val && cin.get() == '\n' )
break;
else
{
cout << errmsg;
cin.clear(); // clear error flasgs
cin.sync(); // 'flush' cin stream ...
}
}
return val;
}
int takeInChr( const char* msg )
{
cout << msg << flush;
int c = cin.get();
cin.sync(); // 'flush' cin stream
return c;
}
bool more()
{
int c = takeInChr( "More (y/n) ? " );
if( c == 'n' || c == 'N' ) return false;
// else ...
return true;
}
// used here to facilitate printing out the vector info ... //
ostream& operator << ( ostream& os, const vector< unsigned >& v )
{
os << v.size() << " number(s):\n";
double sum = accumulate( v.begin(), v.end(), 0 );
unsigned max = *max_element( v.begin(), v.end() );
unsigned min = *min_element( v.begin(), v.end() );
for( size_t i = 0; i < v.size(); ++ i )
{
os << v[i] << ' ';
if( (i+1) % 10 == 0 && (i+1) != v.size() )
os << '\n';
}
os << setprecision(2) << fixed // show 2 decimal places //
<< "\nThe average number entered was: "<< sum/v.size()
<< "\nThe largest number entered was: " << max
<< "\nThe smallest ..... entered was: " << min;
return os;
}
int main()
{
cout << "This program prompts the user to enter a list of numbers,"
<< "\nIt prompts for each next number on a new line.\n\n";
vector< unsigned > v; // get an empty vector to hold positive integers
do
{
for( ; ; ) // loop forever ... until break is reached
{
int value = takeIn< int >( "Enter next number: ",
"Integers only please ...\n\n" );
if( value < 0 )
{
cout << "Positive integers only please ... \n\n";
continue; // jump now to top of forever loop
}
v.push_back( value );
if( !more() ) break;
}
cout << "You entered " << v << endl;
v.clear(); // empty vector to get READY for next loop
cout << "\n";
}
while( more() );
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
cin.sync(); // 'flush' cin stream ...
Sadly, it's not that easy. istream::sync
is in no way guaranteed to read and discard remaining characters in the stream.
The best approach, in my opinion is either 1) don't depend on the stream being 'clean' in the first place or 2) ensure that it's clean by reading full lines and parsing them in memory where you have more control.
David W 131 Practically a Posting Shark
@deceptikon, thank you for keeping fresh this C++ (and C) problematic issue.
It is good for students to know the content of the above link.
However, students may also appreciate knowing that the author of C++,
Bjarne Stroustrup himself,
recommended cin.sync() to flush the cin stream,
as used in the above code.
And I have never had a problem on a modern compiler ...
when used for keyboard input, as in the example above.
(But yes ... it never worked on really old compilers like Turbo C++)
I have had the rare occasion where it did not work as expected.
These were always to do with file reads.
And there, when reading files, just using ...
ignore some_big_num_like_max_int_size
did the trick.
But yes, students be aware of this!!!
And as well stated above:
The best approach, in my opinion is either 1) don't depend on the stream being 'clean' in the first place or 2) ensure that it's clean by reading full lines and parsing them in memory where you have more control.
On production code, reading the whole line, where ever possible, and then parsing that line, and validating the parts ... is highly recommended.
But for quick and easy ...
beginning student-type numeric input type coding problems ...
cin was made for you ... as long as you understand how it works.
Edited by David W
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
However, students may also appreciate knowing that the author of C++, Bjarne Stroustrup himself, recommended cin.sync() to flush the cin stream, as used in the above code.
Yes, I'm aware. However, Bjarne is not infallible, and this is one of those cases where he's wrong. The C++ standard categorically contradicts his recommendation, and when there's a contradiction of advice, the standard rules.
And I have never had a problem on a modern compiler ...
'It works on my compiler' is very rarely a defensible position.
But for quick and easy ...
beginning student-type numeric input type coding problems ...
I agree. That's why I didn't burn you more harshly. ;) There's a certain iterative process in learning, where the strict quality of code improves bit by bit as edge cases like this are learned. So I'm tolerant of poorer examples even though I still prefer to mention that they're poor and point to supplementary resources for improvement if the student is so inclined.
David W 131 Practically a Posting Shark
But for quick and easy ... beginning student-type numeric input type coding problems ...
I agree. That's why I didn't burn you more harshly. ;) There's a certain iterative process in learning, where the strict quality of code improves bit by bit as edge cases like this are learned. So I'm tolerant of poorer examples even though I still prefer to mention that they're poor and point to supplementary resources for improvement if the student is so inclined.
Yeh! I could not have said it better. Amen!
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.