From b530048fc3ddb2f8d55fbedf0ff1c08e97f93c9d Mon Sep 17 00:00:00 2001 From: Parusharma8443 Date: Thu, 24 Oct 2024 23:17:53 +0530 Subject: [PATCH 1/7] Create Hangman Game Code.cpp --- c++/Hangman Game Code.cpp | 212 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 c++/Hangman Game Code.cpp diff --git a/c++/Hangman Game Code.cpp b/c++/Hangman Game Code.cpp new file mode 100644 index 00000000..148349f6 --- /dev/null +++ b/c++/Hangman Game Code.cpp @@ -0,0 +1,212 @@ +// C++ program for the hangman game +#include +#include +#include +#include +#include +#include + +// define maxi. number of incorrect attempts +#define MAX_ATTEMPTS 6 + +using namespace std; + +// main class +class HangmanGame { +public: + // constructor to ini + HangmanGame() + { + srand(static_cast(time(nullptr))); + secretWord = getRandomWord(); + currentWord = string(secretWord.length(), '_'); + attemptsLeft = MAX_ATTEMPTS; + } + + // function to start the game. + void play() + { + cout << "Welcome to Hangman!" << endl; + cout << "Category: Fruits" << endl; + cout << "You have " << attemptsLeft + << " attempts to guess the fruit name." + << endl; + + // the main game loop which will go on till the + // attempts are left or the game is won. + while (attemptsLeft > 0) { + displayGameInfo(); + char guess; + cout << "Guess a letter: "; + cin >> guess; + + if (isalpha(guess)) { + guess = tolower(guess); + if (alreadyGuessed(guess)) { + cout << "You've already guessed that " + "letter." + << endl; + } + else { + bool correctGuess + = updateCurrentWord(guess); + // if the guess is correct, we will + // update the word and check if the word + // is completely guessed or not + if (correctGuess) { + cout << "Good guess!" << endl; + // if the word is completely + // guessed. + if (currentWord == secretWord) { + displayGameInfo(); + cout << "Congratulations! You " + "guessed the word: " + << secretWord << endl; + return; + } + } + else { + cout << "Incorrect guess." << endl; + attemptsLeft--; + drawHangman(attemptsLeft); + } + } + } + else { + cout << "Please enter a valid letter." + << endl; + } + } + + if (attemptsLeft == 0) { + displayGameInfo(); + cout << "You've run out of attempts. The word " + "was: " + << secretWord << endl; + } + } + +private: + string secretWord; + string currentWord; + int attemptsLeft; + vector guessedLetters; + + // select random word from the predefined word + string getRandomWord() + { + vector words + = { "apple", "banana", "cherry", "grape", + "kiwi" }; + int index = rand() % words.size(); + return words[index]; + } + + // checking if the word is already guessed + bool alreadyGuessed(char letter) + { + return find(guessedLetters.begin(), + guessedLetters.end(), letter) + != guessedLetters.end(); + } + + // updating the word after correct guess + bool updateCurrentWord(char letter) + { + bool correctGuess = false; + for (int i = 0; i < secretWord.length(); i++) { + if (secretWord[i] == letter) { + currentWord[i] = letter; + correctGuess = true; + } + } + guessedLetters.push_back(letter); + return correctGuess; + } + + // function to provide the info at particular point in + // the game + void displayGameInfo() + { + cout << "Word: " << currentWord << endl; + cout << "Attempts left: " << attemptsLeft << endl; + cout << "Guessed letters: "; + for (char letter : guessedLetters) { + cout << letter << " "; + } + cout << endl; + } + + // function to progressively draw the hangman + void drawHangman(int attemptsLeft) + { + // Add your hangman drawing logic here + // For simplicity, you can print a static hangman + // ASCII art Modify this function to display the + // hangman as you like + if (attemptsLeft == 5) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 4) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 3) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 2) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 1) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | /" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 0) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | / \\" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + } +}; + +// driver code +int main() +{ + + HangmanGame game; + game.play(); + + return 0; +} From 3b9c2123fc58863fedef80989f233f8fff1e295e Mon Sep 17 00:00:00 2001 From: Parusharma8443 Date: Thu, 24 Oct 2024 23:28:14 +0530 Subject: [PATCH 2/7] Create Hangman game Code --- c++/Hangman game Code | 212 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 c++/Hangman game Code diff --git a/c++/Hangman game Code b/c++/Hangman game Code new file mode 100644 index 00000000..148349f6 --- /dev/null +++ b/c++/Hangman game Code @@ -0,0 +1,212 @@ +// C++ program for the hangman game +#include +#include +#include +#include +#include +#include + +// define maxi. number of incorrect attempts +#define MAX_ATTEMPTS 6 + +using namespace std; + +// main class +class HangmanGame { +public: + // constructor to ini + HangmanGame() + { + srand(static_cast(time(nullptr))); + secretWord = getRandomWord(); + currentWord = string(secretWord.length(), '_'); + attemptsLeft = MAX_ATTEMPTS; + } + + // function to start the game. + void play() + { + cout << "Welcome to Hangman!" << endl; + cout << "Category: Fruits" << endl; + cout << "You have " << attemptsLeft + << " attempts to guess the fruit name." + << endl; + + // the main game loop which will go on till the + // attempts are left or the game is won. + while (attemptsLeft > 0) { + displayGameInfo(); + char guess; + cout << "Guess a letter: "; + cin >> guess; + + if (isalpha(guess)) { + guess = tolower(guess); + if (alreadyGuessed(guess)) { + cout << "You've already guessed that " + "letter." + << endl; + } + else { + bool correctGuess + = updateCurrentWord(guess); + // if the guess is correct, we will + // update the word and check if the word + // is completely guessed or not + if (correctGuess) { + cout << "Good guess!" << endl; + // if the word is completely + // guessed. + if (currentWord == secretWord) { + displayGameInfo(); + cout << "Congratulations! You " + "guessed the word: " + << secretWord << endl; + return; + } + } + else { + cout << "Incorrect guess." << endl; + attemptsLeft--; + drawHangman(attemptsLeft); + } + } + } + else { + cout << "Please enter a valid letter." + << endl; + } + } + + if (attemptsLeft == 0) { + displayGameInfo(); + cout << "You've run out of attempts. The word " + "was: " + << secretWord << endl; + } + } + +private: + string secretWord; + string currentWord; + int attemptsLeft; + vector guessedLetters; + + // select random word from the predefined word + string getRandomWord() + { + vector words + = { "apple", "banana", "cherry", "grape", + "kiwi" }; + int index = rand() % words.size(); + return words[index]; + } + + // checking if the word is already guessed + bool alreadyGuessed(char letter) + { + return find(guessedLetters.begin(), + guessedLetters.end(), letter) + != guessedLetters.end(); + } + + // updating the word after correct guess + bool updateCurrentWord(char letter) + { + bool correctGuess = false; + for (int i = 0; i < secretWord.length(); i++) { + if (secretWord[i] == letter) { + currentWord[i] = letter; + correctGuess = true; + } + } + guessedLetters.push_back(letter); + return correctGuess; + } + + // function to provide the info at particular point in + // the game + void displayGameInfo() + { + cout << "Word: " << currentWord << endl; + cout << "Attempts left: " << attemptsLeft << endl; + cout << "Guessed letters: "; + for (char letter : guessedLetters) { + cout << letter << " "; + } + cout << endl; + } + + // function to progressively draw the hangman + void drawHangman(int attemptsLeft) + { + // Add your hangman drawing logic here + // For simplicity, you can print a static hangman + // ASCII art Modify this function to display the + // hangman as you like + if (attemptsLeft == 5) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 4) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 3) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 2) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 1) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | /" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 0) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | / \\" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + } +}; + +// driver code +int main() +{ + + HangmanGame game; + game.play(); + + return 0; +} From d3fb5b4de6e3bc30856425161bfcf0abf44bd4de Mon Sep 17 00:00:00 2001 From: Parusharma8443 Date: Thu, 24 Oct 2024 23:37:10 +0530 Subject: [PATCH 3/7] Create login and registration system.cpp --- c++/login and registration system.cpp | 86 +++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 c++/login and registration system.cpp diff --git a/c++/login and registration system.cpp b/c++/login and registration system.cpp new file mode 100644 index 00000000..c37de927 --- /dev/null +++ b/c++/login and registration system.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +using namespace std; + + +void mainmenu(); + + +int choice; +bool cinfail; +int confirmation; +string username, password, password2; + +void writetofile(string username){ + ofstream writefile; + string file = username+".txt"; + writefile.open(file.c_str()); + writefile << password; + writefile.close(); + mainmenu(); } + +void login(){ + cout << "You are being logged in!";} + + +void registerpassword(){ + cout << "Please enter the password:" << endl; + cin >> password; + cout << "Please renter your password:" << endl; + cin >> password2; + if (password == password2){ + cin.clear(); + cin.ignore(10000,'\n'); + writetofile(username); + exit(1); + } + else;{ + cout << "Sorry invalid" << endl; + registerpassword(); + }} + + +void registerme(){ + cout << "Please enter your username: " << endl; + getline(cin, username); + cout << "\nUsername - \""<< username << "\"\nConfirm? \n\n[1] Yes\n[2] No" << endl; + cin >> confirmation; + if (confirmation == 1){ + registerpassword(); + } + + else; { + cout << "Sorry invalid input, Please try again" << endl; + cin.clear(); + cin.ignore(10000,'\n'); + registerme(); + }} + + +void exit(){ + exit(0);} + +void mainmenu(){ cout << "Hello, Would you like to log in or register\n[1] Login\n[2] Register\n[3] Exit" <> choice; do{ + cinfail = cin.fail(); + cin.clear(); + cin.ignore(10000,'\n'); + + }while(cinfail == true);{ + switch(choice){ + case 1: + login(); + break; + + case 2: + registerme(); + break; + + case 3: + exit();}}} + +main(){ +mainmenu(); +} From 5f30e78945f9c486f50558a8a08f4e9bf4e57429 Mon Sep 17 00:00:00 2001 From: Parusharma8443 Date: Thu, 24 Oct 2024 23:48:12 +0530 Subject: [PATCH 4/7] Create To Find Transpose of a Matrix --- c++/To Find Transpose of a Matrix | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 c++/To Find Transpose of a Matrix diff --git a/c++/To Find Transpose of a Matrix b/c++/To Find Transpose of a Matrix new file mode 100644 index 00000000..168b61eb --- /dev/null +++ b/c++/To Find Transpose of a Matrix @@ -0,0 +1,47 @@ +//To Find Transpose of a Matrix +#include +using namespace std; + +int main() { + int a[10][10], transpose[10][10], row, column, i, j; + + cout << "Enter rows and columns of matrix: "; + cin >> row >> column; + + cout << "\nEnter elements of matrix: " << endl; + + // Storing matrix elements + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + cout << "Enter element a" << i + 1 << j + 1 << ": "; + cin >> a[i][j]; + } + } + + // Printing the a matrix + cout << "\nEntered Matrix: " << endl; + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + cout << " " << a[i][j]; + if (j == column - 1) + cout << endl << endl; + } + } + + // Computing transpose of the matrix + for (int i = 0; i < row; ++i) + for (int j = 0; j < column; ++j) { + transpose[j][i] = a[i][j]; + } + + // Printing the transpose + cout << "\nTranspose of Matrix: " << endl; + for (int i = 0; i < column; ++i) + for (int j = 0; j < row; ++j) { + cout << " " << transpose[i][j]; + if (j == row - 1) + cout << endl << endl; + } + + return 0; +} From 9f7e05265946bda8cb726a62c217e00d77013348 Mon Sep 17 00:00:00 2001 From: Parusharma8443 Date: Thu, 24 Oct 2024 23:53:43 +0530 Subject: [PATCH 5/7] Create Add Two Distances (in inch-feet) System Using Structures.cpp --- ...(in inch-feet) System Using Structures.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 c++/Add Two Distances (in inch-feet) System Using Structures.cpp diff --git a/c++/Add Two Distances (in inch-feet) System Using Structures.cpp b/c++/Add Two Distances (in inch-feet) System Using Structures.cpp new file mode 100644 index 00000000..aa5da586 --- /dev/null +++ b/c++/Add Two Distances (in inch-feet) System Using Structures.cpp @@ -0,0 +1,38 @@ +//Add Two Distances (in inch-feet) System Using Structures + +#include +using namespace std; + +struct Distance { + int feet; + float inch; +}d1 , d2, sum; + +int main() { + cout << "Enter 1st distance," << endl; + cout << "Enter feet: "; + cin >> d1.feet; + cout << "Enter inch: "; + cin >> d1.inch; + + cout << "\nEnter information for 2nd distance" << endl; + cout << "Enter feet: "; + cin >> d2.feet; + cout << "Enter inch: "; + cin >> d2.inch; + + sum.feet = d1.feet+d2.feet; + sum.inch = d1.inch+d2.inch; + + // changing to feet if inch is greater than 12 + if(sum.inch > 12) { + // extra feet + int extra = sum.inch / 12; + + sum.feet += extra; + sum.inch -= (extra * 12); + } + + cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << " inches"; + return 0; +} From 4d7f70b8b8aa7a50ca4e8a897bfede00963a1564 Mon Sep 17 00:00:00 2001 From: Parusharma8443 Date: Thu, 24 Oct 2024 23:57:31 +0530 Subject: [PATCH 6/7] Create hangman game code.cpp --- c++/hangman game code.cpp | 212 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 c++/hangman game code.cpp diff --git a/c++/hangman game code.cpp b/c++/hangman game code.cpp new file mode 100644 index 00000000..148349f6 --- /dev/null +++ b/c++/hangman game code.cpp @@ -0,0 +1,212 @@ +// C++ program for the hangman game +#include +#include +#include +#include +#include +#include + +// define maxi. number of incorrect attempts +#define MAX_ATTEMPTS 6 + +using namespace std; + +// main class +class HangmanGame { +public: + // constructor to ini + HangmanGame() + { + srand(static_cast(time(nullptr))); + secretWord = getRandomWord(); + currentWord = string(secretWord.length(), '_'); + attemptsLeft = MAX_ATTEMPTS; + } + + // function to start the game. + void play() + { + cout << "Welcome to Hangman!" << endl; + cout << "Category: Fruits" << endl; + cout << "You have " << attemptsLeft + << " attempts to guess the fruit name." + << endl; + + // the main game loop which will go on till the + // attempts are left or the game is won. + while (attemptsLeft > 0) { + displayGameInfo(); + char guess; + cout << "Guess a letter: "; + cin >> guess; + + if (isalpha(guess)) { + guess = tolower(guess); + if (alreadyGuessed(guess)) { + cout << "You've already guessed that " + "letter." + << endl; + } + else { + bool correctGuess + = updateCurrentWord(guess); + // if the guess is correct, we will + // update the word and check if the word + // is completely guessed or not + if (correctGuess) { + cout << "Good guess!" << endl; + // if the word is completely + // guessed. + if (currentWord == secretWord) { + displayGameInfo(); + cout << "Congratulations! You " + "guessed the word: " + << secretWord << endl; + return; + } + } + else { + cout << "Incorrect guess." << endl; + attemptsLeft--; + drawHangman(attemptsLeft); + } + } + } + else { + cout << "Please enter a valid letter." + << endl; + } + } + + if (attemptsLeft == 0) { + displayGameInfo(); + cout << "You've run out of attempts. The word " + "was: " + << secretWord << endl; + } + } + +private: + string secretWord; + string currentWord; + int attemptsLeft; + vector guessedLetters; + + // select random word from the predefined word + string getRandomWord() + { + vector words + = { "apple", "banana", "cherry", "grape", + "kiwi" }; + int index = rand() % words.size(); + return words[index]; + } + + // checking if the word is already guessed + bool alreadyGuessed(char letter) + { + return find(guessedLetters.begin(), + guessedLetters.end(), letter) + != guessedLetters.end(); + } + + // updating the word after correct guess + bool updateCurrentWord(char letter) + { + bool correctGuess = false; + for (int i = 0; i < secretWord.length(); i++) { + if (secretWord[i] == letter) { + currentWord[i] = letter; + correctGuess = true; + } + } + guessedLetters.push_back(letter); + return correctGuess; + } + + // function to provide the info at particular point in + // the game + void displayGameInfo() + { + cout << "Word: " << currentWord << endl; + cout << "Attempts left: " << attemptsLeft << endl; + cout << "Guessed letters: "; + for (char letter : guessedLetters) { + cout << letter << " "; + } + cout << endl; + } + + // function to progressively draw the hangman + void drawHangman(int attemptsLeft) + { + // Add your hangman drawing logic here + // For simplicity, you can print a static hangman + // ASCII art Modify this function to display the + // hangman as you like + if (attemptsLeft == 5) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 4) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 3) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 2) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 1) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | /" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 0) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | / \\" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + } +}; + +// driver code +int main() +{ + + HangmanGame game; + game.play(); + + return 0; +} From a7f596b5c5abe5a77d1f1336f64655276a992703 Mon Sep 17 00:00:00 2001 From: Parusharma8443 Date: Thu, 24 Oct 2024 23:59:21 +0530 Subject: [PATCH 7/7] Create To Find Transpose of a Matrix.cpp --- c++/To Find Transpose of a Matrix.cpp | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 c++/To Find Transpose of a Matrix.cpp diff --git a/c++/To Find Transpose of a Matrix.cpp b/c++/To Find Transpose of a Matrix.cpp new file mode 100644 index 00000000..168b61eb --- /dev/null +++ b/c++/To Find Transpose of a Matrix.cpp @@ -0,0 +1,47 @@ +//To Find Transpose of a Matrix +#include +using namespace std; + +int main() { + int a[10][10], transpose[10][10], row, column, i, j; + + cout << "Enter rows and columns of matrix: "; + cin >> row >> column; + + cout << "\nEnter elements of matrix: " << endl; + + // Storing matrix elements + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + cout << "Enter element a" << i + 1 << j + 1 << ": "; + cin >> a[i][j]; + } + } + + // Printing the a matrix + cout << "\nEntered Matrix: " << endl; + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + cout << " " << a[i][j]; + if (j == column - 1) + cout << endl << endl; + } + } + + // Computing transpose of the matrix + for (int i = 0; i < row; ++i) + for (int j = 0; j < column; ++j) { + transpose[j][i] = a[i][j]; + } + + // Printing the transpose + cout << "\nTranspose of Matrix: " << endl; + for (int i = 0; i < column; ++i) + for (int j = 0; j < row; ++j) { + cout << " " << transpose[i][j]; + if (j == row - 1) + cout << endl << endl; + } + + return 0; +}