Posts

Area of Polygon using C++ Inheritance

/* This is a menu-based program where the user will choose an option in the menu to find the area Base Class: Polygon Sub Base Class: Triangle, Qudrangle, Pentagon Sub Sub Base Class (Qudrangle): Rectangle, Square, Parallelogram */  #include <iostream> using namespace std; class polygon{ protected:     int length, height;     float area; public:     void input(){     cout << "Enter the length and height of polygon: ";     cin >> length >> height;     }     void display(){     cout << "Length: " << length << " units" << endl;     cout << "Height: " << height << " units" << endl;     cout << "Area: " << area << " units square" << endl;     } }; class triangle : public polygon{ public:     void area_of_polygon(){     input();     area = 0.5*length*height;     cout << endl;     cout << "Formula used: 0.5*base*

Guess The Number C code

#include <stdio.h> #include <stdlib.h> #include <time.h>  // for random number generator seed int main() {     int randomNumber = 0;     int guess = 0;     int numberOfGuesses;     // Initialization of random number generator     srand(time(0));     // get the random number     randomNumber = rand() % 21;     printf("\nThis is a guessing game.");     printf("\nI have chosen a number between 0 and 20, which you must guess. \n");     for(numberOfGuesses = 5; numberOfGuesses > 0; --numberOfGuesses)     {         printf("\nYou have %d tr%s left.", numberOfGuesses, numberOfGuesses == 1 ? "y" : "ies");         printf("\nEnter a guess: ");         scanf("%d", &guess);         if(guess == randomNumber)         {             printf("\nCongratulations.  You guessed it!\n");             return;         }         else if(guess < 0 || guess > 20)  // checking for an invalid guess          

Menu Based Program using C++

 /*         This challenge is about using a collection (list) of integers and allowing the user     to select options from a menu to perform operations on the list.          Your program should display a menu options to the user as follows:          P - Print numbers     A - Add a number     M - Display mean of the numbers     S - Display the smallest number     L - Display the largest number     Q - Quit     Enter your choice: */    #include <iostream>     #include <vector>     using namespace std;     void print(vector<int> &numbers);     void add(vector<int> &numbers);     void display_mean(vector<int> &numbers);     void smallest_value(vector<int> &numbers);     void largest_value(vector<int> &numbers);     int main() {         char selection {};          vector<int> numbers {};          int num_to_add;         do{             // Display menu             cout << "\nP - Print numbers" << endl;

Tic Tac Toe C++ code

#include <stdio.h> #include <stdlib.h> #include <conio.h> char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; int choice, player; int checkForWin(); void displayBoard(); void markBoard(char mark); int main() {     int gameStatus;     char mark;     player = 1;     do     {       displayBoard();       // change turns       player = (player % 2) ? 1 : 2;       // get input       printf("Player %d, enter a number: ", player);       scanf("%d", &choice);       // set the correct character based on player turn       mark = (player == 1) ? 'X' : 'O';       // set board based on user choice or invalid choice       markBoard(mark);       gameStatus = checkForWin();       player++;     }while (gameStatus == -1);     if (gameStatus == 1)         printf("==>\aPlayer %d win ", --player);     else         printf("==>\aGame