All Codes Page 1 | C++

 

 Even Numbers in the array:

    // Printing even numbers of array..
    cout << endl;
    cout << "       Even numbers in the array are: " << endl;
    cout << "       ";
    int evenNumberCheck = 0;
    for (int i = 0i < sizeOfArrayi++)
    {
        if (arr[i]%2 == 0)
        {
            cout << arr[i<< " ";
            evenNumberCheck = 1;
        }
    }
    if (evenNumberCheck == 0)
    {
        cout << "There are no even numbers in the Array." << endl;
    }
    cout << endl;

Prime Numbers in the array:


// printing prime numbers
    cout << endl;
    cout << "       Prime numbers in the array are: " << endl;
    cout << "       ";
    int primeNumberCheck = 0;
    int primeNumber;
    for (int i = 0i < sizeOfArrayi++)
    {
        primeNumber = 1;
        for (int j = 2j <= (arr[i]/2); j++)
        {
            if ((arr[i] % j) == 0)
            {
                primeNumber = 0;
                break;
            }
        }
        if (primeNumber == 1)
        {
            cout << arr[i<< " ";
            primeNumberCheck = 1;
        }
    }
    if (primeNumberCheck == 0)
    {
        cout << "There are no prime numbers in the Array.";
    }
    cout << endl;


Perfect Squares in the array:


// printing complete squares.
    cout << endl;
    cout << "       Complete square numbers in the array are: " << endl;
    cout << "       ";
    int completeSquareCheck = 0;
    for (int i = 0i < sizeOfArrayi++)
    {
        for (int j = 2j <= (arr[i]/3)+1j++)
        {
            if ((j*j) == arr[i])
            {
                cout << arr[i<< " ";
                completeSquareCheck = 1;
            }
        }
    }
    if (completeSquareCheck == 0)
    {
        cout << "There are no complete square numbers in the Array.";
    }
    cout << endl;

Duplicate numbers in the array:

int totalDuplicateElementsPossible = sizeOfArray/2;
    int duplicateElements[totalDuplicateElementsPossible];
    int duplicateArrayIndex = 0;
    int isDuplicateAlready = 0;
 
    // Printing duplicate numbers of array.
    cout << endl;
    cout << "       Duplicate numbers in the array are: " << endl;
    cout << "       ";
    int duplicateNumberCheck = 0;
    for (int i = 0i < (sizeOfArray-1); i++)
    {
        for (int j = i+1j < sizeOfArrayj++)
        {
            if (arr[i] == arr[j])
            {
                if (duplicateNumberCheck == 0)
                {
                    cout << arr[i<< " ";
                    duplicateNumberCheck = 1;
                    duplicateElements[duplicateArrayIndex] = arr[i];
                    duplicateArrayIndex++;
                    break;
                }
                else
                {
                    isDuplicateAlready = 0;
                    for (int k = 0k < duplicateArrayIndexk++)
                    {
                        if (arr[i] == duplicateElements[k])
                        {
                            isDuplicateAlready = 1;
                            break;
                        }
                    }
                    if (isDuplicateAlready == 0)
                    {
                        cout << arr[i<< " ";
                        duplicateElements[duplicateArrayIndex] = arr[i];
                        duplicateArrayIndex++;
                        break;
                    }
                }
            }
        }
    }
    if (duplicateNumberCheck == 0)
    {
        cout << "There are no duplicate numbers in the array.";
    }
    cout << endl;

Printing Array:


// traversing/printing array 1.
    cout << endl;
    cout << "       Array 1: " << endl;
    for (int i = 0i < sizeOfArray1i++)
    {
        if (i == 0) {
            cout << "       ";
        }
        cout << arr1[i<< " ";
    }
    cout << endl;

Common Elements in 2 arrays:


// Printing common elements:
    cout << endl;
    cout << "       Common elements in both arrays are: " << endl;
    cout << "       ";
 
    int commonElementCheck = 0;   // check if there are any common elements in the array or not.
    int smallerArraysize;   // variable for creating another array of common elements
    if (sizeOfArray1 > sizeOfArray2)
    {
        smallerArraysize = sizeOfArray2;
    }
    else
    {
        smallerArraysize = sizeOfArray1;
    }
    int commonElements[smallerArraysize];  
    int commonElementsMover = 0;    // this is the index for moving through common elements array.
    int isCommonAlready = 0;
 
    for (int i = 0i < sizeOfArray1i++)
    {
        for (int j = 0j < sizeOfArray2j++)
        {
            if (arr1[i] == arr2[j])
            {
                if (commonElementCheck == 0)
                {
                    cout << arr1[i<< " ";
                    commonElements[commonElementsMover] = arr1[i];
                    commonElementsMover++;
                    commonElementCheck = 1;
                    break;
                }
                else
                {
                    isCommonAlready = 0;
                    for (int k = 0k < commonElementsMoverk++)
                    {
                        if (arr1[i] == commonElements[k])
                        {
                            isCommonAlready = 1;
                            break;
                        }
                    }
                    if (isCommonAlready == 0)
                    {
                        cout << arr1[i<< " ";
                        commonElements[commonElementsMover] = arr1[i];
                        commonElementsMover++;
                        break;
                    }
                }
            }
        }
    }
    if (commonElementCheck == 0)
    {
        cout << "There are no common elements in both arrays.";
    }
    cout << endl;

Sorting an Array:

for (int i = 0i < sizeOfArrayi++)
    {
        for (int j = i+1j < sizeOfArrayj++)
        {
            if (arr[i]>arr[j])
            {
                double temp;
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }


Palindrome Checker:


// checking that the sequence is palindromic or not
    int inputLenght = input.length();
    int match = 0;    // a check switch. 0 means same 1 means not same.
    for (int i = 0j = (inputLenght - 1); i < inputLenghti++, j--)
    {
        if (input[i] == input[j])
        {
            continue;
        }
        else
        {
            match = 1; // changing check switch to 1 which means not same.
            break;
        }
    }
 
    // printing the results based on check switch.
    cout << endl;
    if (match == 0)
    {
        cout << "          You entered a Palindrome..!" << endl;
    }
    else
    {
        cout << "          You entered a normal word !!!" << endl;
    }
    cout << endl << endl
;


Insert, delete, Print an Array:


#include <iostream>
#include <limits>
#include <conio.h>
 
using namespace std;
 
void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "       Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
 
main ()
{
    cout << endl;
    cout << "       The pre-defined array has space of maximum of 7 elements." << endl;
    cout << endl;
    cout << "       The orginal array has following values: " << endl;
    int arr[7];
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 3;
 
    int totalElementsInArray = 3;
 
    for (int i = 0i < totalElementsInArrayi++)
    {
        cout << "       arr[" << i << "] = " << arr[i<< endl
    }
    cout << endl;
 
    menuAgain:
    cout << endl;
    cout << "       What do you want to do?" << endl;
    cout << "       1) Print Array." << endl;
    cout << "       2) Insert Element." << endl;
    cout << "       3) Delete Element." << endl;
    cout << "       4) Exit." << endl;
 
    cout << endl;
    cout << "       Select an option: ";
    int ans;
    int deleteLocation;
    int elementInsert;
    int insertLocation;
    ansAgain:
    numberInput(ans);
    switch (ans)
    {
    case 1:
        if (totalElementsInArray == 0)
        {
            cout << endl;
            cout << "       The array is empty. Insert some elements first.." << endl;
        }
        else
        {
            cout << endl;
            cout << "       Printing Array..." << endl;
            for (int i = 0i < totalElementsInArrayi++)
            {
                cout << "       arr[" << i << "] = " << arr[i<< endl
            }
        }
        goto menuAgain;
 
    case 2:
        if (totalElementsInArray < 7)
        {
            cout << endl;
            cout << "       Which element do you want to insert: ";   
            numberInput(elementInsert);
            if (totalElementsInArray == 0)
            {
                insertLocation = 0;
                goto insert1stElement;
            }
            else
            {
                cout << endl;
                cout << "       At which location do you want to enter the element (0-" << totalElementsInArray << "): ";
                insertLocationAgain:
                numberInput(insertLocation);
                if (insertLocation < 0 || insertLocation > totalElementsInArray)
                {
                    cout << "       Invalid location. Location can be from 0 to " << totalElementsInArray << ". Insert again: ";
                    goto insertLocationAgain;
                }
                for (int i = (totalElementsInArray+1), j = totalElementsInArrayj >= (insertLocation); i--, j--)
                {
                    arr[i] = arr[j];
                }
            }
            insert1stElement:
            arr[insertLocation] = elementInsert;
            totalElementsInArray += 1;
            cout << endl;
            cout << "       The new array becomes:" << endl;
            for (int i = 0i < totalElementsInArrayi++)
            {
                cout << "       arr[" << i << "] = " << arr[i<< endl
            }
            cout << endl;
            goto menuAgain;
        }
        else
        {
            cout << endl;
            cout << "       The array size is Full. Delete some elements first." << endl;
            cout << endl;
            for (int i = 0i < totalElementsInArrayi++)
            {
                cout << "       arr[" << i << "] = " << arr[i<< endl
            }
            goto menuAgain;
        }
 
    case 3:
        if (totalElementsInArray > 0)
        {
            if (totalElementsInArray == 1)
            {
                cout << endl;
                cout << "       Do you want to delete the element at index 0? (y/n)" << endl;
                cout << "       ";
                char deleteAns;
                cin >> deleteAns;
                if (deleteAns == 'y' || deleteAns == 'Y')
                {
                    deleteLocation = 0;
                    goto delete1stElement;
                }
                else
                {
                    cout << "       Okay.." << endl;
                    cout << endl;
                    for (int i = 0i < totalElementsInArrayi++)
                    {
                        cout << "       arr[" << i << "] = " << arr[i<< endl
                    }
                    goto menuAgain;
                }
            }
            else
            {
                cout << endl;
                cout << "       At which location do you want to delete the element (0-" << (totalElementsInArray-1<< "): ";
                deleteLocationAgain:
                numberInput(deleteLocation);
                if (deleteLocation < 0 || deleteLocation > (totalElementsInArray-1))
                {
                    cout << "       Invalid location. Location can be from 0 to " << (totalElementsInArray-1<< ". Insert again: ";
                    goto deleteLocationAgain;
                }
                else
                {
                    delete1stElement:
                    for (int i = deleteLocationj = (deleteLocation+1); j < totalElementsInArrayi++, j++)
                    {
                        arr[i] = arr[j];
                    }
                    totalElementsInArray -= 1;
                }
 
                if (totalElementsInArray == 0)
                {
                    cout << endl;
                    cout << "       Array is empty now.." << endl;
                }
                else
                {
                    cout << endl;
                    cout << "       The new array becomes:" << endl;
                    for (int i = 0i < totalElementsInArrayi++)
                    {
                        cout << "       arr[" << i << "] = " << arr[i<< endl
                    }
                    cout << endl;
                }
            }
            goto menuAgain;
        }
        else
        {
            cout << endl;
            cout << "       Array is empty.. Insert some elements first."  << endl;
            goto menuAgain;
        }
        
    case 4:
        cout << endl;
        cout << "       Thanks for using the program..!" << endl;
        system("pause");
        break;
        
        
    
    default:
        cout << endl;
        cout << "       Wrong option select. Select again: ";
        goto ansAgain;
    }
 
    return 0;
}


In-Place merge two Sorted arrays:

#include <iostream>
#include <limits>
 
using namespace std;
 
void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "       Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
 
main()
{
    // Input of Array 1.
    cout << endl;
    cout << "       Enter size of Array 1: ";
    int sizeOfArray1;
    sizeOfArray1Again:
    numberInput(sizeOfArray1);
    // Checking if the user has determined the size less than 1.
    if (sizeOfArray1 < 0) {
        cout << "       Size of array can be atleast 0. Enter Again: ";
        goto sizeOfArray1Again;
    }
    int arr1[sizeOfArray1];
    for (int i = 0i < sizeOfArray1i++)
    {
        cout << "       Enter element number " << i+1 << " of array 1: ";
        numberInput(arr1[i]);
    }
    
    // Input of Array 2
    cout << endl;
    cout << "       Enter size of Array 2: ";
    int sizeOfArray2;
    sizeOfArray2Again:
    numberInput(sizeOfArray2);
    if (sizeOfArray2 < 0) {
        cout << "       Size of array can be atleast 0. Enter Again: ";
        goto sizeOfArray2Again;
    }
    int arr2[sizeOfArray2];
    for (int i = 0i < sizeOfArray2i++)
    {
        cout << "       Enter element number " << i+1 << " of array 2: ";
        numberInput(arr2[i]);
    }
 
    // traversing/printing array 1.
    cout << endl;
    cout << "       Original Arrays: " << endl;
    cout << endl;
    cout << "       A[] = {";
    for (int i = 0i < sizeOfArray1i++)
    {
        cout << arr1[i];
        if (i < sizeOfArray1-1)
        {
            cout << ", ";
        }
    }
    cout << "}" << endl;
 
    // traversing/printing array 2.
    cout << "       B[] = {";
    for (int i = 0i < sizeOfArray2i++)
    {
        cout << arr2[i];
        if (i < sizeOfArray2-1)
        {
            cout << ", ";
        }
    }
    cout << "}" << endl;
 
    // making another array and copying values and sorting and then again copying from sorted to individual array.
    int sizeOfTotalArray = sizeOfArray1 + sizeOfArray2;
    int arr3[sizeOfTotalArray];
 
    int elementsInTotalArray = 0;
 
    for (int i = 0i < sizeOfArray1i++)
    {
        arr3[i] = arr1[i];
        ++elementsInTotalArray;
    }
    for (int i = 0i < sizeOfArray2i++)
    {
        arr3[elementsInTotalArray] = arr2[i];
        ++elementsInTotalArray;
    }
    
    // sorting.
    for (int i = 0i < sizeOfTotalArrayi++)
    {
        for (int j = i+1j < sizeOfTotalArrayj++)
        {
            if (arr3[i]>arr3[j])
            {
                double temp;
                temp = arr3[i];
                arr3[i] = arr3[j];
                arr3[j] = temp;
            }
        }
    }
    
    elementsInTotalArray = 0;
    for (int i = 0i < sizeOfArray1i++)
    {
        arr1[i] = arr3[i];
        elementsInTotalArray += 1;
    }
 
    for (int i = 0i < sizeOfArray2i++)
    {
        arr2[i] = arr3[elementsInTotalArray];
        elementsInTotalArray += 1;
    }
    
    // traversing/printing array 1.
    cout << endl;
    cout << endl;
    cout << "       Output:" << endl;
    cout << endl;
    cout << "       A[] = {";
    for (int i = 0i < sizeOfArray1i++)
    {
        cout << arr1[i];
        if (i < sizeOfArray1-1)
        {
            cout << ", ";
        }
    }
    cout << "}" << endl;
 
    // traversing/printing array 2.
    cout << "       B[] = {";
    for (int i = 0i < sizeOfArray2i++)
    {
        cout << arr2[i];
        if (i < sizeOfArray2-1)
        {
            cout << ", ";
        }
    }
    cout << "}" << endl;
 
    // end.
    cout << endl;
    system("pause");
    system("cls");
    return 0;
}

Rotating NxN matrix by 90 degree 

#include <iostream>
#include <limits>
#include <iomanip>
 
using namespace std;
 
void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
 
void swap (int &aint &b)
{
    int temp = a;
    a = b;
    b = temp;
}
 
main ()
{
    // Setting some variables including dimension of matrix etc.
    int dimension = 3;
    int last = dimension -1;
    int level = 0;
    int totalNumberOfLevel = dimension / 2;
 
    // making matrix and initializing all the memebers of matrix.
    int matrix[dimension][dimension];
    int count = 1;
    for (int i = 0i < dimensioni++)
    {
        for (int j = 0j < dimensionj++)
        {
            matrix[i][j] = count;
            count++;
        }
        
    }
 
    // printing original matrix.
    cout << endl;
    cout << "========== Original Matrix ==========" << endl;
    cout << endl;
    cout << "       ";
    for (int i = 0i < dimensioni++)
    {
        cout << "|";
        for (int j = 0j < dimensionj++)
        {
            cout << setw(3<< matrix[i][j];
            if (j < dimension-1)
            {
                cout << " ";
            }
        }
        cout << "|" << endl;
        cout << "       ";
    }
    
    // rotating matrix.
    while (level < totalNumberOfLevel)
    {
        for (int i = leveli < lasti++)
        {
            swap(matrix[level][i], matrix[i][last]);
            swap(matrix[level][i], matrix[last][last-i+level]);
            swap(matrix[level][i], matrix[last-i+level][level]);
        }
        ++level;
        --last;
    }
    
    // printing rotated matrix.
    cout << endl;
    cout << "========== Rotated Matrix ==========" << endl;
    cout << endl;
    cout << "       ";
    for (int i = 0i < dimensioni++)
    {
        cout << "|";
        for (int j = 0j < dimensionj++)
        {
            cout << setw(3<< matrix[i][j];
            if (j < dimension-1)
            {
                cout << " ";
            }
        }
        cout << "|" << endl;
        cout << "       ";
    }
 
    // The end.
    cout << endl << endl;
    system("pause");
    system("cls");
    return 0;
}


Left Diagonal sum/Right Diagonal sum.

#include <iostream>
#include <limits>
#include <iomanip>
#include <cstdlib>
 
using namespace std;
 
void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
 
void swap (int &aint &b)
{
    int temp = a;
    a = b;
    b = temp;
}
 
main ()
{
    // Setting some variables including dimension of matrix etc.
    cout << endl;
    cout << "          Enter dimension of NxN matrix: ";
    int dimension;
    numberInput(dimension);
    cout << endl;
 
    // making matrix and initializing all the memebers of matrix.
    srand((unsignedtime(0));
    int matrix[dimension][dimension];
    for (int i = 0i < dimensioni++)
    {
        for (int j = 0j < dimensionj++)
        {
            matrix[i][j] = rand() % 100 +1;
        }
        
    }
 
    // printing original matrix.
    cout << endl;
    cout << "========== Original Matrix ==========" << endl;
    cout << endl;
    cout << "          ";
    for (int i = 0i < dimensioni++)
    {
        cout << "|";
        for (int j = 0j < dimensionj++)
        {
            cout << setw(3<< matrix[i][j];
            if (j < dimension-1)
            {
                cout << " ";
            }
        }
        cout << "|" << endl;
        cout << "          ";
    }
    
    // Calculating left diagonal sum
    int leftDiagonal = 0;
    for (int i = 0i < dimensioni++)
    {
        leftDiagonal += matrix[i][i];
    }
 
    // Calculating right diagonal Sum.
    int rightDiagonal = 0;
    for (int i = 0j = dimension-1i < dimensioni++, j--)
    {
        rightDiagonal += matrix[i][j];
    }
    
    // printing rotated matrix.
    cout << endl;
    cout << "       The sum of left diagonal: " << leftDiagonal << endl;
    cout << "       The sum of right diagonal: " << rightDiagonal << endl;
 
    // The end.
    cout << endl << endl;
    system("pause");
    system("cls");
    return 0;
}

Add, Subtract, Multiply Matrices:

#include <iostream>
#include <limits>
#include <iomanip>
 
using namespace std;
 
void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}

main()
{
    // Taking input of matrix 1.
    cout << endl;
    cout << "       Enter rows and colums of 1st matrix (e.g. 3 3): " << endl;
    cout << "       ";
    int m1rowsm1columns;
    m1DimensionAgain:
    numberInput(m1rows);
    numberInput(m1columns);
    if (m1rows < 1 || m1columns < 1)
    {
        cout << "       Rows and Columns can be atleast 1. Enter again: ";
        goto m1DimensionAgain;
    }
 
    int matrix1[m1rows][m1columns];
    cout << endl;
    for (int i = 0i < m1rowsi++)
    {
        for (int j = 0j < m1columnsj++)
        {
            cout << "       Enter " << i+1 << "x" << j+1 << " element: ";
            numberInput(matrix1[i][j]);
        }
    }
    // Displaying matrix 1.
    cout << endl;
    for (int i = 0i < m1rowsi++)
    {
        cout << "|";
        for (int j = 0j < m1columnsj++)
        {
            cout << setw(3<< matrix1[i][j];
            if (j < m1columns-1)
            {
                cout << " ";
            }
        }
        cout << "|" << endl;
    }
 
    // taking input of matrix 2
    cout << endl;
    cout << endl;
    cout << "       Enter rows and colums of 2nd matrix (e.g. 3 3): " << endl;
    cout << "       ";
    int m2rowsm2columns;
    m2DimensionAgain:
    numberInput(m2rows);
    numberInput(m2columns);
    if (m2rows < 1 || m2columns < 1)
    {
        cout << "       Rows and Columns can be atleast 1. Enter again: ";
        goto m2DimensionAgain;
    }
 
    int matrix2[m2rows][m2columns];
    cout << endl;
    for (int i = 0i < m2rowsi++)
    {
        for (int j = 0j < m2columnsj++)
        {
            cout << "       Enter " << i+1 << "x" << j+1 << " element: ";
            numberInput(matrix2[i][j]);
        }
    }
    // Displaying matrix 2
    cout << endl;
    for (int i = 0i < m2rowsi++)
    {
        cout << "|";
        for (int j = 0j < m2columnsj++)
        {
            cout << setw(3<< matrix2[i][j];
            if (j < m2columns-1)
            {
                cout << " ";
            }
        }
        cout << "|" << endl;
    }
 
    // Menu
    menuAgain:
    cout << endl;
    cout << endl;
    cout << "       1) Add" << endl;
    cout << "       2) Substract" << endl;
    cout << "       3) Multiply" << endl;
    cout << "       4) Exit" << endl;
    cout << endl;
 
    cout << "       Choose an operation: ";
    int operationSelect;
    operationSelectAgain:
    numberInput(operationSelect);
 
    switch (operationSelect)
    {
    case 1:
        if (m1rows == m2rows && m1columns == m2columns)
        {
            int resultantMatrix[m1rows][m1columns];
            for (int i = 0i < m2rowsi++)
            {
                for (int j = 0j < m2columnsj++)
                {
                    resultantMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
                }
            }
            
            cout << endl;
            cout << "       Adding matrices.." << endl;
            cout << endl;
            for (int i = 0i < m1rowsi++)
            {
                cout << "|";
                for (int j = 0j < m1columnsj++)
                {
                    cout << setw(3<< resultantMatrix[i][j];
                    if (j < m1columns-1)
                    {
                        cout << " ";
                    }
                }
                cout << "|" << endl;
            }
            goto menuAgain;
        }
        else
        {
            cout << endl;
            cout << "       Addition cannot be performed on these matrices as they don't have identical dimension." << endl;
            goto menuAgain;
        }
 
    case 2:
        if (m1rows == m2rows && m1columns == m2columns)
        {
            int resultantMatrix[m1rows][m1columns];
            for (int i = 0i < m2rowsi++)
            {
                for (int j = 0j < m2columnsj++)
                {
                    resultantMatrix[i][j] = matrix1[i][j] - matrix2[i][j];
                }
            }
            
            cout << endl;
            cout << "       Substracting matrices.." << endl;
            cout << endl;
            for (int i = 0i < m1rowsi++)
            {
                cout << "|";
                for (int j = 0j < m1columnsj++)
                {
                    cout << setw(3<< resultantMatrix[i][j];
                    if (j < m1columns-1)
                    {
                        cout << " ";
                    }
                }
                cout << "|" << endl;
            }
            goto menuAgain;
        }
        else
        {
            cout << endl;
            cout << "       Addition cannot be performed on these matrices as they don't have identical dimension." << endl;
            goto menuAgain;
        }
        

    case 3:
        if (m1columns == m2rows)
        {
            int resultantMatrix[m1rows][m2columns];
            // initializing matrix values with 0.
            for (int i = 0i < m1rowsi++)
            {
                for (int j = 0j < m2columnsj++)
                {
                    resultantMatrix[i][j] = 0;
                }
            }
            // calculating values.
            for (int rr = 0rr < m1rowsrr++)
            {
                for (int rc = 0rc < m2columnsrc++)
                {
                    for (int m1c = 0m2r = 0m1c < m1columnsm1c++, m2r++)
                    {
                        resultantMatrix[rr][rc] += matrix1[rr][m1c]*matrix2[m2r][rc];
                    }
                }
            }
            // printing resultant matrix.
            cout << endl;
            cout << "Multiplying matrices.." << endl;
            cout << endl;
            for (int i = 0i < m1rowsi++)
            {
                cout << "|";
                for (int j = 0j < m2columnsj++)
                {
                    cout << setw(3<< resultantMatrix[i][j];
                    if (j < m2columns-1)
                    {
                        cout << " ";
                    }
                }
                cout << "|" << endl;
            }
            goto menuAgain;
        }
        else
        {
            cout << endl;
            cout << "       Multiplication cannot be performed on these matrices (columns is not equal to rows)." << endl;
            goto menuAgain;
        }
        
 
    case 4:
        cout << endl;
        cout << "       Thanks for using the program.." << endl << endl;
        system("pause");
        system("cls");
        return 0;
 
    
    default:
        cout << "       Invalid option. Select again: ";
        goto operationSelectAgain;
    }
}

Frequency Sorting in Linked List:

#include <iostream>
#include <limits>
 
using namespace std;
 
void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "       Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
 
struct node
{
    int data;
    node *next;
};
 
main()
{
    // asking size of array creating array of that size.
    int sizeOfArray;
    cout << endl;
    cout << "       Enter size of array: ";
    sizeOfArrayAgain:
    numberInput(sizeOfArray);
    if (sizeOfArray < 1)
    {
        cout << "       Size of array cannot be less than 1. Enter again: ";
        goto sizeOfArrayAgain;
    }
    int arr1[sizeOfArray];
 
    // Taking input of array values.
    cout << endl;
    for (int i = 0i < sizeOfArrayi++)
    {
        cout << "       Enter element " << i+1 << ": ";
        numberInput(arr1[i]);
    }
    
    // making other arrays and alloting values in them.
    int arr2[sizeOfArray];
    int repeatency[sizeOfArray];
    int numberOfElementsInOtherArray = 0;
    int check = 0;
    for (int i = 0i < sizeOfArrayi++)
    {
        if (i != 0)
        {
            check = 0;
            for (int j = 0j < numberOfElementsInOtherArrayj++)
            {
                if (arr1[i] == arr2[j])
                {
                    check = 1;
                    break;
                }
            }
            if (check == 1)
            {
                continue;
            }
        }
        
        int count = 1;
        for (int j = i+1j < sizeOfArrayj++)
        {
            if (arr1[i] == arr1[j])
            {
                count += 1;
            }
        }
        arr2[numberOfElementsInOtherArray] = arr1[i];
        repeatency[numberOfElementsInOtherArray] = count;
        numberOfElementsInOtherArray += 1;
    }
 
    // sorting other array;
    for (int i = 0i < numberOfElementsInOtherArrayi++)
    {
        for (int j = i+1j < numberOfElementsInOtherArrayj++)
        {
            if (repeatency[i]>repeatency[j])
            {
                double temp;
                temp = repeatency[i];
                repeatency[i] = repeatency[j];
                repeatency[j] = temp;
 
                temp = arr2[i];
                arr2[i] = arr2[j];
                arr2[j] = temp;
            }
        }
    }
    
 
    // creating list and alloting values.
    node *head, *newNode, *temp;
    head = NULL;
    for (int i = 0i < numberOfElementsInOtherArrayi++)
    {
        newNode = new node;
        newNode -> data = arr2[i];
        newNode-> next = NULL;
        if (head == NULL)
        {
            head = temp = newNode;
        }
        else
        {
            temp -> next = newNode;
            temp = newNode;
        }
    }
 
    // Printing newly created singly linked list.
    cout << endl;
    cout << "       New Singly Linked list:   ";
    temp = head;
    while (temp != NULL)
    {
        cout << temp -> data << " ";
        temp = temp -> next;
    }
    cout << endl;
    
    // The end.
    cout << endl;
    system("pause");
    system("cls");
    return 0;
}


Delete Duplicates from Linked List:

#include <iostream>
#include <limits>
#include <windows.h>
 
using namespace std;
 
void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
 
struct node
{
    int data;
    node *next;
};
 
class LinkList
{
 public:
    node *head = NULL;
    node *temp = NULL;
    node *newNode;
    int totalElementsInList = 0;
    LinkList()
    {
        head = NULL;
        temp = NULL;
    }
    void createNode()
    {
        temp = head;
        if(temp != NULL)
        {
            while (temp->next != NULL)
            {
                temp = temp->next;
            }
        }
        newNode = new node;
        totalElementsInList++;
        cout << "       Enter Element " << totalElementsInList << " of the list: ";
        numberInput(newNode->data);
        newNode->next = NULL;
        if (head == NULL)
        {
            head = temp = newNode;
        }
        else
        {
            temp-> next = newNode;
            temp = newNode;
        }
    }
    void displayList()
    {
        temp = head;
        while (temp != NULL)
        {
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }
    void DeleteDuplicates()
    {
        node *temp2;
        node *temp2previous;
        node *tempprevious;
 
        temp = head;
        temp2 = temp->next;
        temp2previous = temp;
        tempprevious = NULL;
 
        while (temp != NULL)
        {
            while (temp2 != NULL)
            {
                if (temp->data == temp2->data)
                {
                    temp2previous->next = temp2->next;
                    if (temp != head)
                    {
                        tempprevious->next = temp->next;
                    }
                    else
                    {
                        head = temp->next;
                    }
                    temp2 = temp2->next;
                    temp = temp->next;
                    totalElementsInList -= 2;
                    if (temp == NULL && temp2 == NULL)
                    {
                        break;
                    }
                    if (temp == temp2)
                    {
                        temp2previous = temp2;
                        temp2 = temp2->next;
                    }
                    if (temp2 != temp->next)
                    {
                        temp2 = temp->next;
                        temp2previous = temp;
                    }
                    continue;
                }
                temp2previous = temp2;
                temp2 = temp2->next;
            }
            if (temp == NULL && temp2 == NULL)
            {
                break;
            }
            tempprevious = temp;
            temp = temp->next;
            if (temp != NULL)
            {
                temp2previous = temp;
                temp2 = temp->next;
            }
        }
    }
};
 
main()
{
    LinkList l1;
    cout << endl;
    cout << "       How much elements do you want to enter in list: ";
    int totalelements;
    numberInput(totalelements);
    int i = 0;
    cout << endl;
    while (i < totalelements)
    {
        l1.createNode();
        i++;
    }
    cout << endl;
    cout << "       Original List:   ";
    l1.displayList();
    cout << endl;
    cout << "       Deleting Duplicates (if there are any)";
    for (int i = 0i < 3i++)
    {
        Beep(0,500);
        cout << ". ";
    }
    cout << endl;
    l1.DeleteDuplicates();
    cout << endl;
    cout << "            New List:   ";
    l1.displayList();
 
    cout << endl;
    system("pause");
    system("cls");
    return 0;
}

Update Every Element by adding previous and next element in Array:

#include <iostream>
#include <limits>
 
using namespace std;
 
void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "       Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
 
main()
{
    // asking size of array creating array of that size.
    int sizeOfArray;
    cout << endl;
    cout << "       Enter size of array: ";
    sizeOfArrayAgain:
    numberInput(sizeOfArray);
    if (sizeOfArray < 1)
    {
        cout << "       Size of array cannot be less than 1. Enter again: ";
        goto sizeOfArrayAgain;
    }
    int arr1[sizeOfArray];
 
    // Taking input of array values.
    cout << endl;
    for (int i = 0; i < sizeOfArray; i++)
    {
        cout << "       Enter element " << i+1 << ": ";
        numberInput(arr1[i]);
    }
 
    // creating another array of the same size and inserting values according to the question demand.
    int arr2[sizeOfArray];
    for (int i = 0; i < sizeOfArray; i++)
    {
        if (i == 0)
        {
            if (i+1 < sizeOfArray)
            {
                arr2[i] = arr1[i+1];
            }
            else
            {
                arr2[i] = 0;
            }
        }
        else if (i == (sizeOfArray-1))
        {
            arr2[i] = arr1[i-1];
        }
        else
        {
            arr2[i] = arr1[i-1] + arr1[i+1];
        }
    }
    
    // printing original and new arrays.
    cout << endl;
    cout << "       Original Array:    ";
    for (int i = 0; i < sizeOfArray; i++)
    {
        cout << arr1[i] << " ";
    }
    cout << endl;
 
    cout << endl;
    cout << "       New Array:         ";
    for (int i = 0; i < sizeOfArray; i++)
    {
        cout << arr2[i] << " ";
    }
    cout << endl;
    
    // the end.
    cout << endl;
    system("pause");
    system("cls");
    return 0;
}

Insert, Delete, and Show a singly linked list.

#include <iostream> #include <limits>
 
using namespace std;
 
void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "       Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}
 
struct node
{
    int data;
    node *next;
};
 
class LinkedList
{
public:
    node *newNode;
    node *temp;
    node *temp2;
    node *head;
    int totalElementsInList = 0;
    LinkedList()
    {
        head = NULL;
    }
 
    void showList()
    {
        temp = head;
        if (temp == NULL)
        {
            cout << endl;
            cout << "       List is empty. Insert Element first." << endl;
        }
        else
        {
            cout << endl;
            cout << "       List is shown below:" << endl;
            cout << "     -------------------------" << endl;
            while (temp != NULL)
            {
                cout << "         " << temp->data << endl;
                temp = temp->next;
            }
            cout << "     -------------------------" << endl;
        }
    }
 
    void createNode()
    {
        if (totalElementsInList >= 1)
        {
            cout << endl;
            cout << "          1) Insert an Element at the start of the list." << endl;
            cout << "          2) Insert at the end of the list." << endl;
            cout << "          3) Insert at a specific location." << endl;
            cout << "          4) Return to main Menu." << endl;
            cout << endl;
            cout << "       Choose: ";
            int choice3;
        choice3Again:
            numberInput(choice3);
            if (choice3 == 1)
            {
                newNode = new node;
                cout << "       Enter element number " << totalElementsInList + 1 << " of the list: ";
                numberInput(newNode->data);
                temp = head;
                head = newNode;
                newNode->next = temp;
                totalElementsInList++;
 
                system("cls");
                cout << endl;
                cout << "       Element is added." << endl;
            }
            else if (choice3 == 2)
            {
                newNode = new node;
                cout << "       Enter element number " << totalElementsInList + 1 << " of the list: ";
                numberInput(newNode->data);
                newNode->next = NULL;
                temp = head;
                while (temp->next != NULL)
                {
                    temp = temp->next;
                }
 
                temp->next = newNode;
                totalElementsInList++;
 
                system("cls");
                cout << endl;
                cout << "       Element is added." << endl;
            }
            else if (choice3 == 3)
            {
                cout << endl;
                cout << "       Enter position where you want to enter the number (1-" << totalElementsInList + 1 << "): ";
                int pos;
            posAgain:
                numberInput(pos);
                if (pos < 1)
                {
                    cout << "       Position can be from 1 to " << totalElementsInList + 1 << ". Enter again: ";
                    goto posAgain;
                }
                if (pos > totalElementsInList + 1)
                {
                    cout << "       Position can be from 1 to " << totalElementsInList + 1 << ". Enter again: ";
                    goto posAgain;
                }
 
                newNode = new node;
                cout << "       Enter element number " << totalElementsInList + 1 << " of the list: ";
                numberInput(newNode->data);
 
                temp = NULL;
                temp2 = head;
 
                for (int i = 1i < posi++)
                {
                    temp = temp2;
                    temp2 = temp2->next;
                }
                if (temp == NULL)
                {
                    head = newNode;
                    newNode->next = temp2;
                }
                else
                {
                    temp->next = newNode;
                    newNode->next = temp2;
                }
 
                totalElementsInList++;
 
                system("cls");
                cout << endl;
                cout << "       Element is added." << endl;
            }
            else if (choice3 == 4)
            {
                // no action;
            }
            else
            {
                cout << "       Wrong choice. Choose again: ";
                goto choice3Again;
            }
        }
        else
        {
            newNode = new node;
            cout << endl;
            cout << "       Enter element number 1 of the list: ";
            numberInput(newNode->data);
            newNode->next = NULL;
            head = newNode;
            totalElementsInList++;
 
            system("cls");
            cout << endl;
            cout << "       Element is added." << endl;
        }
    }
 
    void deleteNode()
    {
        if (totalElementsInList > 0)
        {
            cout << endl;
            cout << "          1) Delete a specific number (at first occurance)." << endl;
            cout << "          2) Delete an element at a specific location." << endl;
            cout << "          3) Return to main Menu." << endl;
            cout << endl;
            cout << "       Choose: ";
            int choice2;
        choice2Again:
            numberInput(choice2);
            if (choice2 == 1)
            {
                cout << endl;
                cout << "       Enter number which you want to delete: ";
                int item;
                numberInput(item);
                bool inList = false;
                temp = head;
                temp2 = NULL;
                while (temp != NULL)
                {
                    if (temp->data == item)
                    {
                        if (temp == head)
                        {
                            head = temp->next;
                            free(temp);
                            totalElementsInList--;
                            inList = true;
                            break;
                        }
                        else
                        {
                            temp2->next = temp->next;
                            free(temp);
                            totalElementsInList--;
                            inList = true;
                            break;
                        }
                    }
                    temp2 = temp;
                    temp = temp->next;
                }
                if (inList == false)
                {
                    cout << endl;
                    cout << "       Element is not present in the list." << endl;
                }
                else
                {
                    cout << endl;
                    cout << "       Element is deleted." << endl;
                }
            }
            else if (choice2 == 2)
            {
                cout << endl;
                cout << "       Enter position which you want to delete (1-" << totalElementsInList << "): ";
                int location;
            locationAgain:
                numberInput(location);
                if (location < 1)
                {
                    cout << "       Position can be from 1 to " << totalElementsInList << ". Enter again: ";
                    goto locationAgain;
                }
                if (location > totalElementsInList)
                {
                    cout << "       Position can be from 1 to " << totalElementsInList << ". Enter again: ";
                    goto locationAgain;
                }
                temp = head;
                temp2 = NULL;
                for (int i = 1i < locationi++)
                {
                    temp2 = temp;
                    temp = temp->next;
                }
                
                if (temp == head)
                {
                    head = temp->next;
                }
                else
                {
                    temp2->next = temp->next;
                }
                free(temp);
                totalElementsInList--;
                cout << endl;
                cout << "       Element at position " << location << " is deleted." << endl;
            }
            else if (choice2 == 3)
            {
                // no action.
            }
            else
            {
                cout << "Invalid choice. Choose again: ";
                goto choice2Again;
            }
        }
        else
        {
            cout << endl;
            cout << "       List is empty. Insert Element first." << endl;
        }
    }
};
 
main()
{
    LinkedList l1;
    cout << endl;
    cout << "       ---------- Linked List actions of insertion and deletion ----------" << endl;
menuAgain:
    cout << endl;
    cout << ">>>>>>>>>>>>>> Main Menu <<<<<<<<<<<<<<" << endl;
    cout << endl;
    cout << "       Choose an action :" << endl;
    cout << "          1) Insert Element" << endl;
    cout << "          2) Delete Element" << endl;
    cout << "          3) Show List" << endl;
    cout << "          4) Exit" << endl;
    cout << endl;
    cout << "       Choose: ";
    int choice;
choiceAgain:
    numberInput(choice);
    switch (choice)
    {
    case 1:
        system("cls");
        l1.createNode();
        goto menuAgain;
 
    case 2:
        system("cls");
        l1.deleteNode();
        goto menuAgain;
 
    case 3:
        system("cls");
        l1.showList();
        goto menuAgain;
 
    case 4:
        system("cls");
        break;
 
    default:
        cout << "       Invalid Choice. Select again: ";
        goto choiceAgain;
    }
    cout << endl;
    cout << "       ------- Thanks for using the program -------" << endl;
    cout << endl;
    system("pause");
    return 0;
}


Binary Search Array:

#include <iostream>
#include <limits>

using namespace std;

void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}

int main()
{
    int arr[102];
    for (int i = 0, number = 100; i < 102; i++, number++)
    {
        arr[i] = number;
    }

    for (int i = 0; i < 102; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
    
    
    int found = false;
    int lb = 0;
    int ub = 101;
    int middle = 0;
    
    cout << endl;
    cout << "Enter the number: ";
    int item;
    numberInput(item);
    
    while (lb <= ub)
    {
        middle = (lb+ub)/2;
        if (arr[middle] == item)
        {
            cout << "Element is present in the array at index " << middle << "." << endl; 
            found = true;
            break;
        }
        else if (item < arr[middle])
        {
            ub = middle-1;
        }
        else if (item > arr[middle])
        {
            lb = middle+1;
        }
    }
    if (found == false)
    {
        cout << "element is not found in the array" << endl;
    }
    
    cout << endl;
    system("pause");
    system("cls");
    return 0;
}

Infix expression Check:

#include <iostream>
#include <limits>
#include <string>

using namespace std;

void numberInput(int &a)
{
    while (!(cin >> a))
    {
        cout << "Invalid Input. Enter number only: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}

int main()
{
    string infixnotation;
    cout << "Enter infix notation:" << endl;
infixnotationAgain:
    cin >> infixnotation;

    int operand1 = 0, op = 0, bracket = 0;

    int i = 0;
    while (i < infixnotation.length())
    {
        if (infixnotation[i] >= 'A' && infixnotation[i] <= 'Z' || infixnotation[i] >= 'a' && infixnotation[i] <= 'z')
        {
            if (operand1 == 0)
            {
                operand1 = 1;
            }
            else if (operand1 == 1 && op == 1)
            {
                operand1 = 1;
                op = 0;
            }
            else
            {
                cout << "Expression is not correct. Please enter again:" << endl;
                goto infixnotationAgain;
            }
        }
        else if (infixnotation[i] == '(' || infixnotation[i] == '{' || infixnotation[i] == '[')
        {
            if (operand1 == 1 && op == 1)
            {
                bracket++;
                operand1 = 0;
                op = 0;
            }
            else
            {
                cout << "Expression is not correct. Please enter again:" << endl;
                goto infixnotationAgain;
            }
        }
        else if (infixnotation[i] == ')' || infixnotation[i] == '}' || infixnotation[i] == ']')
        {
            if (operand1 == 1 && op == 0 && bracket > 0)
            {
                bracket--;
                operand1 = 1;
                op = 0;
            }
            else
            {
                cout << "Expression is not correct. Please enter again:" << endl;
                goto infixnotationAgain;
            }
        }
        else if (infixnotation[i] == '+' || infixnotation[i] == '-' || infixnotation[i] == '/' || infixnotation[i] == '*' || infixnotation[i] == '^')
        {
            if (operand1 == 1 && op == 0)
            {
                op = 1;
            }
            else
            {
                cout << "Expression is not correct. Please enter again:" << endl;
                goto infixnotationAgain;
            }
        }
        else
        {
            cout << "Expression is not correct. Please enter again:" << endl;
            goto infixnotationAgain;
        }
        i++;
    }
    if (operand1 == 1 && op == 0)
    {
        cout << "Entered statement is correct which is shown below:" << endl;
        cout << infixnotation;
    }
    else
    {
        cout << "Expression is not correct. Please enter again:" << endl;
        goto infixnotationAgain;
    }

    cout << endl;
    system("pause");
    system("cls");
    return 0;
}

Comments