C++ Subset array finder looping with comma at ending? - c++

Here is my code
#include "iostream"
using namespace std;
void choice();
void Start(){
system("cls");
char subset[100];
int y,x;
cout << "Enter a Set"<<endl;
int pass = 0;
while (pass < 1){
y = 0;
x = 0;
cin >> subset;
while(y < 100){
if (subset[x+1] == '}') { cout<<"Invalid Set"<<endl<<"Enter a Set"<<endl; break; }
if (subset[x] == '}'&& subset[0] == '{') { cout << "Set Accepted"<<endl; pass = 1; break;}
x = x+2;
if (y == 99) {cout <<"Invalid Set"<<endl<<"Enter a Set"<<endl; }
y++;
}
}
int nofsubset = x / 2;
int b = 1;
char arr[99];
int n = nofsubset;
while(nofsubset!= 0) {
for(int l=0; l<n; l++){
//cin >> arr[l];
arr[l] = subset[b];
b = b + 2;
}
for (int i=0; i<(1<<n); i++){
cout << "{";
for(int j=0; j<n; j++) {
if(i & (1 <<j)) {
cout << arr[j];
}
}
cout <<"}"<<endl;
}
nofsubset = 0;
}
system("pause");
choice();
}
void choice(){
system("cls");
cout <<"**************************"<<endl;
cout <<"* *"<<endl;
cout <<"* 1 - Generate Subsets *"<<endl;
cout <<"* 2 - Exit *"<<endl;
cout <<"* *"<<endl;
cout <<"**************************"<<endl;
int choice;
cin >> choice;
if(choice == 1){ Start();}
else{ return ;}
}
int main() {
choice();
}
this code would allow to find subset of set for example i entered {1,2,3} and it will generate
{}
{1}
{2}
{12}
{3}
{13}
{23}
{123}
what my professor want is to out put it on format with comma like this
{}
{1}
{2}
{1,2}
{3}
{1,3}
{2,3}
{1,2,3}
can anyone please help me edit my code i cant seem to just put "," on the loop array because itl produce {1,2,3,}
thanks in advance!

cout << "{";
bool first = true;
for(int j=0; j<n; j++) {
if(i & (1 <<j)) {
if (!first) cout << ","; // <<< try this
first = false;
cout << arr[j];
}
}
cout <<"}"<<endl;

Related

I'm trying to check my array list input if its an int or string but this seems to crash and just go into infinite loop

#include <iostream>
#include <stdio.h>
#include <ctype.h>
using namespace std;
class List{
private:
int A[10];
int i;
public:
List(){
i = 0;
}
void insert(){
int v;
for(int j = 0 ; j < 10 ; j++){
cout << "\nElement you want to insert: (" << i+1 << "). ";
cin >> v;
if(i <= 9){
A[i] = v;
i++;
}
else{
cout << "\nWrong Input" << endl;
break;
}
}
if(i > 9){
cout << "\nYour List Capacity is Full.\n" << endl;
}
}
void display(){
cout << "\n{ ";
for(int j = 0 ; j < i ; j++)
cout << A[j] << " ";
cout << "}\n" << endl;
}
void remove(){
int p;
cout << "\nElement you want to remove (0 - 9): ";
cin >> p;
if (i == 0){
cout << "\nList is empty!\n" << endl;
return;
}
if (p >= 0 && p < i){
for(int j = p ; j < i-1 ; j++)
A[j] = A[j + 1];
i--;
}
}
void size(){
cout << "\nYour list size is: " << i << endl;
}
void checkcapacity(){
int arrSize = sizeof(A)/sizeof(A[0]);
cout << "\nThe Capacity of the array is: " << arrSize << endl;
}
};
int main(){
List l;
int a;
cout << "\t\t\t\t\t\tWelcome to List Program!";
while(a != 4){
int choose;
cout << "\nThe Program have following options:\n1. Insert\n2. Display\n3. Remove\n4. Check Size\n5. Check Capacity\n6. Exit\n\nNote: Your list capacity is 10!";
cout << "\n\nChoose (1 - 5): ";
cin >> choose;
if (choose == 1){
l.insert();
}
else if (choose == 2){
l.display();
}
else if (choose == 3){
l.remove();
}
else if (choose == 4){
l.size();
}
else if (choose == 5){
l.checkcapacity();
}
else if (choose == 6){
a = 4;
}
}
cout << "Thank you for using this program!";
}
I'm using this class in my main function in which I call them but when the user inputs a char or string in the insert function it goes into infinte loop. Int i is my counter and it just contains the size of my array list im just trying to put a check of character that if user input a character is should show an error.
Here's one way to write your insert function
void insert()
{
int v;
for(int j = 0 ; j < 10 ; j++)
{
cout << "\nElement you want to insert: (" << i+1 << "). ";
if (cin >> v)
{
if (i < 10)
{
A[i] = v;
i++;
}
}
else
{
cin.clear(); // clear error
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // discard any pending input
}
}
if (i >= 10)
cout << "\nYour List Capacity is Full.\n" << endl;
}
The important part is the recovery from bad input. First cin.clear() is called to clear the stream error state, secondly cin.ignore(...) is called to discard any pending input.
More details here

How do I make this program to list out if I have entered two consecutive zeros?

#include <iostream>
using namespace std;
int main ()
{
int A[20], i, k;
cout << "Write 20 random numbers: "<<endl;
for(i=0; i<20; i++){
cout << "A[" << i << "]: ";
cin >> A[i];
}
k=0;
if (i+1 == k){
cout << "The program has two consecutive zeros";
}
else if (i+1 != k){
cout << "The program doesn't own two consecutive zeros";
}
char ch1;
cin>>ch1;
return 0;
}
This is my code but I don't know how to configure out the if to show me a message first, if there are two zeros, and second if there aren't. If there are, I need to make it so the numbers on which these zeros are shown. I'm a student please help, I really have no idea how to do it
I did it for the most part. Thank you everyone for your help! What's left now is to make it so it shows to which respective numbers the zeros are. How do I do that? I did as varleti suggested but it only shows 20 and 21
You already have a loop that you use to collect the data.
cout << "Write 20 random numbers: "<< endl;
for(i=0; i<20; i++) {
cout << "A[" << i << "]: ";
cin >> A[i];
}
You have collected the data into a 20 element array A[20].
You need to walk through the array again and test the values for 2 consecutive zeros.
two_zeros = 0;
for(i=1; i<20; i++) { // Note, starting from element [1]
if( A[i] == 0 && A[i-1] == 0 ) { // Test this and previous element for zeroness
two_zeros = 1;
}
}
if( two_zeros == 1 ) {
cout << "The program has two consecutive zeros";
} else {
cout << "The program doesn't own two consecutive zeros";
}
See this code snippet:
#include <iostream>
using namespace std;
int main ()
{
int A[20], i, k;
cout << "Write 20 random numbers: "<<endl;
for(i=0; i<20; i++){
cout << "A[" << i << "]: ";
cin >> A[i];
}
int count = 0; /* To count number of consecutive zeroes */
int flag = false; /* check wheather consecutive zeroes are found or not */
for(i=0; i<20; i++) {
if(A[i] == 0) {
count++;
if(count == 2) {
flag = true;
cout << "The program has two consecutive zeros" << endl;
break;
}
} else {
count = 0;
}
}
if(flag == false) {
cout << "The program doesn't own two consecutive zeros";
}
return 0;
}
Let me know, if having any doubt regarding anything.
You don't need any array, you only need to remember whether the last number you read was zero.
int main ()
{
bool last_zero = false;
bool two_zeros = false;
cout << "Write 20 random numbers: "<<endl;
for(int i = 0; i < 20; i++){
int x = 0;
cin >> x;
if (x == 0)
{
if (last_zero)
{
two_zeros = true;
}
last_zero = true;
}
else
{
last_zero = false;
}
}
if (two_zeros){
cout << "The program has two consecutive zeros";
}
else {
cout << "The program doesn't own two consecutive zeros";
}
}
#include <iostream>
using namespace std;
int main ()
{
int A[20], i, k=0;
cout << "Write 20 random numbers: "<<endl;
cout << "A[0]";
cin >> A[0];
for(i=1; i<20; i++)
{
cout << "A[" << i << "]: ";
cin >> A[i];
if( A[i] == 0 && A[i-1] == 0 )
{
cout << "The program has two consecutive zeros";
k=1;
break;
}
}
if(k==0)
cout << "The program hasn't two consecutive zeros";
return 0;
}
int count = 0;
for (int j = 0; j<20 - 1; j++)
{
if (arr[j] == 0 && arr[j + 1] == 0)
{
count++;
break;
}
}
if (count == 1)
{
cout << found;
}
else
cout << not found;

Taking "exit" as user input in C++

I'm currently trying to write code that takes user input as strings, and then convert them to integers if I need to. If the user decides to enter exit then the program should move on to calling a function. Here is what I have so far:
void printArray(string string_array[], int size){
for (int i = 0; i < size; i++){
cout << string_array[i];
}
}
void a_func(){
string string_array[10];
string user_input;
while (user_input != "exit"){
cout << "Please enter a number between 0 - 100: ";
cin >> user_input;
if (stoi(user_input) < 0 || stoi(user_input) > 100){
cout << "Error, please re-enter the number between 0 - 100: ";
cin >> user_input;
}
else if (user_input == "exit"){
printArray(string_array, 10);
}
int array_index = stoi(user_input) / 10;
string_array[array_index] = "*";
}
However, as I'm testing the program, the console is aborting the program if I enter exit. Is there a way for me to enter exit and then the program calls printArray?
I think this changes in your code will solve the problem. Please, try with this code:
void printArray(string string_array[], int size){
for (int i = 0; i < size; i++){
cout << string_array[i];
}
}
void a_func(){
string string_array[10];
string user_input;
while (user_input != "exit"){
cout << "Please enter a number between 0 - 100: ";
cin >> user_input;
if (user_input == "exit"){
printArray(string_array, 10);
}
else if (stoi(user_input) < 0 || stoi(user_input) > 100){
cout << "Error, please re-enter the number between 0 - 100: ";
cin >> user_input;
int array_index = stoi(user_input) / 10;
string_array[array_index] = "*";
}
}
}
Hope this helps
stoi doesn't accept non-numbers. Try this:
#include <iostream>
using namespace std;
void printArray(string string_array[], int size){
for (int i = 0; i < size; i++){
cout << string_array[i];
}
}
void a_func(){
string string_array[10];
string user_input;
while (user_input != "exit"){
cout << "Please enter a number between 0 - 100: ";
cin >> user_input;
//cout << "You have printed " << user_input << endl;
bool isNumber = true;
for(string::const_iterator k = user_input.begin(); k != user_input.end(); ++k) {
if (isdigit(*k) == false) {
isNumber = false;
break;
}
}
if (isNumber) {
int number = stoi(user_input);
if (number < 0 || number > 100){
cout << "Error, please re-enter the number between 0 - 100: ";
continue;
} else {
int array_index = stoi(user_input) / 10;
string_array[array_index] = "*";
}
} else if (user_input == "exit"){
printArray(string_array, 10);
break;
} else {
// Not a number and not "exit"; do nothing?
}
}
}
int main() {
a_func();
return 0;
}

Replay tic tac toe game

So I am trying to program a way to replay a tic tac toe game after someone wins, loses, or ties.
So basically my attempt to get replay to work, doesnt work. If player 1 won and I type 1 to replay, it would ask player 2 for their input.
Pseudocode outline:
do {
set entire 2d array to '*'
do {
player 1 input
does game tie?
does player 1 win
player 2 input
does game tie?
does player 2 win
} while no one wins
} while replay = 1
My actual code:
//tie check, replay, use pointer notation
#include <iostream>
using namespace std;
void initialize(char [][3]);
void player1(char [][3]);
void player2(char [][3]);
void display(char [][3]);
char check(char [3][3]);
int checkWin(int);
int tie(int);
int askReplay();
int main()
{
char board[3][3];
char end = '*';
int row1, column1, row2,column2;
int replay = 0;
int turns = 0;
//replay loop
do {
//set board to *
initialize(board);
display(board);
do {
//player 1 turn
player1(board);
turns++;
display(board);
//if turns = 9 then tie
replay = tie(turns);
//check if player 1 won
end = check(board);
replay = checkWin(end);
//player 2 turn
player2(board);
turns++;
display(board);
//if turns = 9 then tie
replay = tie(turns);
//check if player 2 won
end = check(board);
replay = checkWin(end);
} while (end == '*');
} while (replay == 1);
return 0;
}
void initialize(char (*array)[3])
{
for (int i = 0;i < 3;i++)
for (int j = 0;j < 3;j++)
array[i][j] = '*';
cout << "New Game\n";
}
void player1(char (*array)[3])
{
int row1, column1;
cout << "Player 1\nRow: ";
cin >> row1;
while (row1 < 0 || row1 > 2) {
cout << "Enter a number between 0 and 2 for Row:: ";
cin >> row1;
}
cout << "Column: ";
cin >> column1;
while (column1 < 0 || column1 > 2) {
cout << "Enter a number between 0 and 2 for Column: ";
cin >> column1;
}
if (array[row1][column1] == '*')
array[row1][column1] = 'X';
else {
cout << "Space Occupied\n";
player1(array);
}
}
void player2(char (*array)[3])
{
int row2,column2;
cout << "Player 2\nRow: ";
cin >> row2;
while (row2 < 0 || row2 > 2) {
cout << "Enter a number between 0 and 2 for Row: ";
cin >> row2;
}
cout << "Column: ";
cin >> column2;
while (column2 < 0 || column2 > 2) {
cout << "Enter a number between 0 and 2 for Column: ";
cin >> column2;
}
if (array[row2][column2] == '*')
array[row2][column2] = 'O';
else {
cout << "Space Occupied\n";
player2(array);
}
}
void display(char (*array)[3])
{
for (int x = 0;x < 3;x++) {
for (int y = 0;y < 3;y++)
cout << *(*(array + x) + y) << " ";
cout << endl;
}
}
char check(char (*array)[3])
{
int i;
/* check rows */
for(i = 0; i < 3; i++)
if(array[i][0] == array[i][1] && array[i][0] == array[i][2])
return array[i][0];
/* check columns */
for(i = 0; i < 3; i++)
if(array[0][i] == array[1][i] && array[0][i] == array[2][i])
return array[0][i];
/* test diagonals */
if(array[0][0] == array[1][1] && array[1][1] == array[2][2])
return array[0][0];
if(array[0][2] == array[1][1] && array[1][1] == array[2][0])
return array[0][2];
return '*';
}
int checkWin(int over)
{
if (over == '*')
return 0;
if (over == 'X')
cout << "Player 1 Won!\n";
else if (over == 'O')
cout << "Player 2 Won!\n";
//ask if they want to play again
int answer;
answer = askReplay();
switch (answer) {
case 1:
return 1;
case 2:
cout << "Thank you for playing.\n";
exit(0);
}
}
int tie(int count)
{
if (count == 9) {
int answer;
cout << "Tie game";
answer = askReplay();
switch (answer) {
case 1:
return 1;
case 2:
cout << "Thank you for playing.\n";
exit(0);
}
}
}
int askReplay()
{
int input;
do {
cout << "Play Again?\n1.Yes\n2.No\nEnter 1 or 2: ";
cin >> input;
if (input > 2 || input < 1)
cout << "Invalid Option\n";
} while(input > 2 || input < 1);
return input;
}
It sounds like you're having troubles with your main loop.
I'd suggest making a variable that controls which player is running and just toggle that.
do
{
set entire 2d array to '*'
current player = 0
do
{
(current player + 1) input
does game tie?
does (current player + 1) win
current player = (current player + 1) % 2
}while no one wins
}while replay = 1
See if that gets you further along.
You may want to look up the Memento Design Pattern.

how to process an array of even numbers from a users input and display them with spaces in C++

I need help with getting this users input of an integer and retrieving the even numbers and displaying them with spaces.I already have the input processed into an array and have it reversed (thanks to stackoverflow) now need to extract the even numbers from the array and display them.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int evenNumbers(char even[], int num[], int indexing[]);
int main()
{
char integers[5];
int numbers[5];
int even[5] = {0,2,4,6,8};
int evens;
cout << "Please enter an integer and press <ENTER>: " << endl;
for (int j = 0; j < 5; j++)
cin >> integers[j];
for (int j = 0; j < 5; j++)
{
numbers[j]= integers[j] - '0';
}
cout << endl;
for (int j = 5; j > 0; j--)
{
cout << integers[j - 1] << " ";
}
cout << endl;
//having problems finding the even numbers and displaying the even numbers
//from the users input of integers, i have only learned how to display the
//subscript by a linear search
evens = evenNumbers(integers, numbers, even);
if (evens == -1)
cout << "There are no even numbers" << endl;
else
{
cout << "The even numbers are: " << (evens + 1) << endl;
}
system("pause");
return 0;
}
int evenNumbers(char even[], int num[], int indexing[])
{
int index = 0;
int position = -1;
bool found = false;
for (int j = 0; j < 5; j++)
{
num[j]= even[j] - '0';
}
while (index < 5)
{
if (num[index] == indexing[index])
{
found = true;
position = index;
}
index++;
}
return position;
}
If you want to display the even numbers from the array integers you can use a simple for loop and if statement:
for(int i = 4; i >= 0; i--)
{
if(integers[i] % 2 == 0)
cout << integers[i] << " ";
}
Your approach is all wrong, you can't detect even numbers by searching a list, you need a mathematical test for evenness. Write a function called is_even which tests one number and returns true if it is even and false if it is not. Then you can use that function, very simply, like this
for (int j = 0; j < 5; j++)
{
if (is_even(integers[j]))
cout << integers[j] << " ";
}
cout << endl;
Now you just need to write the is_even function.
void evennumbers(int num[])
{
for(int i=0;i<5;i++)
{
if(num[i]%2==0)
cout<<num[i]<<" ";
}
}
And avoid taking input to char what if user enters a number with more than one digit
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void validNum(char valid[]);
void reverseNum(char rev[], int num2[]);
void evenNumbers(char even[], int num3[]);
void oddNumbers(char odd[], int num4[]);
int main()
{
char integer[5];
int number[5];
cout << "Your number is: ";
validNum(integer);
cout << "Your number in reverse is: ";
reverseNum(integer, number);
cout << "Even numbers: ";
evenNumbers(integer, number);
cout << endl;
cout << "Odd numbers: ";
oddNumbers(integer, number);
cout << endl;
system("pause");
return 0;
}
void validNum(char valid[])
{
char ch;
cout << "Please enter an integer and press <ENTER>: " << endl;
ch = cin.get;
while (ch < 0 || ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
{
cout << "ERROR: Please enter a positive integer and press <ENTER>: ";
for (int i = 0; i < 5; i++)
cin >> valid[i];
}
for (int j = 0; j < 5; j++)
{
cout << valid[j] - '0';
}
}
void reverseNum(char rev[], int num2[])
{
for (int j = 0; j < 5; j++)
{
num2[j]= rev[j] - '0';
}
cout << endl;
for (int j = 5; j > 0; j--)
{
cout << rev[j - 1]<< " ";
}
cout << endl;
}
void evenNumbers(char even[], int num3[])
{
for (int i = 0; i < 5; i++)
{
if (even[i] % 2 == 0)
{
cout << num3[i] << " ";
}
}
}
void oddNumbers(char odd[], int num4[])
{
for (int i = 0; i < 5; i++)
{
if (odd[i] % 2 == 1)
{
cout << num4[i] << " ";
}
}
}