Programming with C++ - Day1 || Wong Edition || B.Tech Diaries

Lab #1 - C++ Basics (Loops, Functions, Arrays)

  1. A software company sells a package that retails for $99. Quantity discounts are given according to the following table.

    Quantity Discount
    10-19 20%
    20-49 30%
    50-99 40%
    100 or more 50%

    Write a program that asks for the number of units sold and computes the total cost of the purchase. Declare the discount as constants.Input Validation: Make sure the number of units is greater than 0.

    
    #include <iostream>
    
    int main(int argc, char const *argv[])
    {
        int qty;
        // std::cout << "Enter no of units sold: ";
        while (std::cin >> qty)
        {
            if (qty > 0)
                break;
            std::cout << "No of units should be greater than 0" << std::endl;
            // std::cout << "Enter no of units sold: ";
        }
        double dis = 0;
        if (qty < 20)
            dis = 0.2;
        else if (qty < 50)
            dis = 0.3;
        else if (qty < 100)
            dis = 0.4;
        else
            dis = 0.5;
        double price = qty * 99 * (1 - dis);
        printf("\nTotal price is: %.2lf\n", price);
        return 0;
    }
  2. Write a program that generates the day's sales report for five stores. The program asks for the sales figures as input anddisplays a bar graph comparing each store's sales.Create each bar in the bar graph by displaying a row of asterisks. Each asterisk represents $100 of sales.Here is an example of the program's output.

    
    Date: July 22, 2019
    Name of Sales Manager: Tutu Pal
    CityCentre:   **********
    Park Street:  ************
    Rajarhat:     ******************
    South City:   ********
    Dum Dum:      *******************
    The store with the top sales on July 22, 2019 is Dum Dum.
    The total sales is $xxxx.

    Use the function void printbar(string store, int sales) to generate the bar chart.

    
    #include <iostream>
    
    void printbar(std::string store, double sales, double sales_max)
    {
        std::cout << store << ": \t";
        for (int j = 0; j < int(sales * 50.0 / sales_max); j++)
        {
            std::cout << "*";
        }
        std::cout << std::endl;
    }
    
    int main(int argc, char const *argv[])
    {
        std::string date = "July 22, 2019", manager = "Tutu Pal";
        std::string store_names[5] = {"City Centre", "Park Street", "Rajarhat", "South City", "Dum Dum"};
        double store_sales[5], total_sales = 0;
        int max = 0;
    
        for (int i = 0; i < 5; i++)
        {
            // std::cout << "Enter sales for store " << i + 1 << ": ";
            std::cin >> store_sales[i];
            total_sales += store_sales[i];
            if (store_sales[max] < store_sales[i])
                max = i;
        }
        std::cout << "\nDate:\t" << date << "\nName of Sales Manager:\t" << manager << std::endl;
    
        for (int i = 0; i < 5; i++)
        {
            printbar(store_names[i], store_sales[i], store_sales[max]);
        }
    
        std::cout << "\nThe store with the top sales on " << date << " is " << store_names[max] << std::endl;
        printf("\nThe total sales is $%.2lf.\n\n", total_sales);
        return 0;
    }
  3. Write a C++ program to implement the Number Guessing Game. In this game the computer chooses a random number between 1 and 100, and the player tries to guess the number in as few attempts as possible. Each time the player enters a guess, the computer tells him whether the guess is too high, too low, or right. Once the player guesses the number, the game is over.

    
    #include <iostream>
    
    int main(int argc, char const *argv[])
    {
        std::cout << "======NUMBER GUESSING GAME=======" << std::endl;
        int guess, number = rand() % 200 + 1;
        std::cout << "Guess a number between 1 and 200" << std::endl;
        std::cin >> guess;
        while (guess != number)
        {
            if (guess > number)
            {
                std::cout << "Your guess is too high!!" << std::endl;
            }
            else
            {
                std::cout << "Your guess is too low!!" << std::endl;
            }
            std::cin >> guess;
        }
        std::cout << "You guessed the number " << number << std::endl;
        return 0;
    }
Share:

OS Lab Project || Das Edition || B.Tech Diaries

Multilevel Queue Scheduling

Multilevel Queue Scheduling in Operating System

A multilevel queue scheduling is a scheduling algorithm where there are multiple queues for each type of process. The selection of level is done through priority based scheduling while each level queue scheduling are dependent entirely on the type of process. Round robin for upper layers and First come first served for lower layers. 

Source Code


/** 
 * @author  Purbayan Chowdhury
 * @brief   Multilevel Feedback Queue Scheduling Algorithm 
*/

#include <bits/stdc++.h>

class process
{
public:
    int _id, _atime, _wtime, _ttime, _btime, _priority, _percent;
    process(int id = 0, int atime = 0, int btime = 0, int priority = 0) : _id(id), _atime(atime), _btime(btime), _priority(priority), _percent(btime) {}
};

/**
 * Q3 = Batch Process having Low Priority with FCFS Algorithm 
 * Q2 = Interactive Process having Medium Priority with Priority Scheduling 
 * Q1 = System Process having High Priority with Round Robin Algorithm
*/

std::vector<process *> sortArrivalTime(std::vector<process *> p)
{
    int i, j;
    process *k;
    for (i = 0; i < p.size() - 1; i++)
    {
        for (j = p.size() - 1; j >= i + 1; j--)
        {
            if (p[j]->_atime < p[j - 1]->_atime)
            {
                k = p[j - 1];
                p[j - 1] = p[j];
                p[j] = k;
            }
        }
    }
    return p;
}

int FCFS(std::vector<process *> p, int stime, int etime)
{
    int i, c = etime - stime;
    if (c <= 0)
        return etime;
    for (i = 0; i < p.size(); i++)
    {
        if (p[i]->_percent > c)
        {
            p[i]->_percent -= c;
            return etime;
        }
        else
        {
            c = c - p[i]->_percent;
            p[i]->_ttime = stime + p[i]->_percent;
            stime += p[i]->_percent;
            p[i]->_percent = 0;
        }
    }
    return etime - c;
}

std::vector<process *> sortPriority(std::vector<process *> p)
{
    int i, j;
    process *k;
    for (i = 0; i < p.size() - 1; i++)
    {
        for (j = p.size() - 1; j >= i + 1; j--)
        {
            if (p[j]->_atime < p[j - 1]->_atime && (p[j]->_atime < p[j - 1]->_atime && p[j]->_priority < p[j - 1]->_priority))
            {
                k = p[j - 1];
                p[j - 1] = p[j];
                p[j] = k;
            }
        }
    }
    return p;
}

int PR(std::vector<process *> p, int stime, int etime)
{
    p = sortPriority(p);
    int i, c = etime - stime;
    if (c <= 0)
        return etime;
    for (i = 0; i < p.size(); i++)
    {
        if (p[i]->_percent > c)
        {
            p[i]->_percent -= c;
            return etime;
        }
        else
        {
            c = c - p[i]->_percent;
            p[i]->_ttime = stime + p[i]->_percent;
            stime += p[i]->_percent;
            p[i]->_percent = 0;
        }
    }
    return etime - c;
}

int RR(std::vector<process *> p, int stime, int etime)
{
    int i = 0, j, c = etime - stime, ct = p.size();
    int quantum = 4;
    while (ct != 0 && c != 0)
    {
        if (c < quantum)
        {
            if (p[i]->_percent > c)
            {
                p[i]->_percent -= c;
                return etime;
            }
            else
            {
                c = c - p[i]->_percent;
                p[i]->_ttime = stime + p[i]->_percent;
                stime += p[i]->_percent;
                p[i]->_percent = 0;
                ct--;
            }
            i++;
            if (i == p.size())
                break;
        }
        else
        {
            if (p[i]->_percent != 0)
            {
                if (p[i]->_percent > quantum)
                {
                    stime += quantum;
                    p[i]->_percent -= quantum;
                    c -= quantum;
                }
                else
                {
                    stime += p[i]->_percent;
                    p[i]->_ttime = stime;
                    c -= p[i]->_percent;
                    p[i]->_percent = 0;
                    ct--;
                }
            }
            i++;
            i = i % p.size();
        }
    }
    return etime - c;
}

std::vector<int> calculateTime(std::vector<process *> p)
{
    std::vector<int> t(2);
    t[0] = t[1] = 0;
    int i;
    for (i = 0; i < p.size(); i++)
    {
        p[i]->_ttime -= p[i]->_atime;
        t[0] += p[i]->_ttime;
        p[i]->_wtime = p[i]->_ttime - p[i]->_btime;
        t[1] += p[i]->_wtime;
    }
    return t;
}

void displayTime(std::vector<process *> p)
{
    std::cout << std::setfill(' ') << std::setw(10) << "Id"
              << std::setfill(' ') << std::setw(10) << "Priority"
              << std::setfill(' ') << std::setw(10) << "A_Time"
              << std::setfill(' ') << std::setw(10) << "B_Time"
              << std::setfill(' ') << std::setw(10) << "T_Time"
              << std::setfill(' ') << std::setw(10) << "W_Time"
              << std::endl;
    for (int i = 0; i < p.size(); i++)
        std::cout << std::setfill(' ') << std::setw(10) << p[i]->_id
                  << std::setfill(' ') << std::setw(10) << p[i]->_priority
                  << std::setfill(' ') << std::setw(10) << p[i]->_atime
                  << std::setfill(' ') << std::setw(10) << p[i]->_btime
                  << std::setfill(' ') << std::setw(10) << p[i]->_ttime
                  << std::setfill(' ') << std::setw(10) << p[i]->_wtime
                  << std::endl;
}

void displayPercent(std::vector<process *> p)
{
    int i;
    for (i = 0; i < p.size(); i++)
        printf("%d\t", p[i]->_percent);
    printf("\n");
}

int main(int argc, char const **argv)
{
    std::vector<process *> q[3];
    int n = 10, i, j, c = 10;
    std::vector<process *> p(n);
    std::vector<process> cp(n); // 10 Processes
    cp[0] = process(1, 7, 5, 1);
    cp[1] = process(2, 4, 2, 0);
    cp[2] = process(3, 5, 7, 1);
    cp[3] = process(4, 0, 6, 0);
    cp[4] = process(5, 3, 24, 0);
    cp[5] = process(6, 7, 4, 2);
    cp[6] = process(7, 0, 7, 1);
    cp[7] = process(8, 3, 12, 2);
    cp[8] = process(9, 2, 7, 1);
    cp[9] = process(10, 4, 8, 2);
    for (i = 0; i < n; i++)
        p[i] = &cp[i];
    p = sortArrivalTime(p);
    int pburst = 0, pend = 0;
    int co = 0;
    // displayPercent(p);
    while (c != 0)
    {
        c = 10;
        for (i = 0; i < n; i++)
        {
            if (p[i]->_percent != 0)
            {
                if (p[i]->_atime <= pburst)
                {
                    q[p[i]->_priority].push_back(p[i]);
                }
                else
                {
                    pend = p[i]->_atime;
                    break;
                }
            }
        }
        if (pend == pburst)
            pend = 999;
        for (j = 0; j < 3; j++)
        {
            switch (j)
            {
            case 0:
                if (!(q[j].empty()))
                    pburst = RR(q[j], pburst, pend);
                break;
            case 1:
                if (!(q[j].empty()))
                    pburst = PR(q[j], pburst, pend);
                break;
            case 2:
                if (!(q[j].empty()))
                    pburst = FCFS(q[j], pburst, pend);
                break;
            }
            if (pburst == pend)
                break;
        }
        for (i = 0; i < n; i++)
            if (p[i]->_percent == 0)
                c--;
        q[0].clear();
        q[1].clear();
        q[2].clear();
    }
    std::vector<int> t = calculateTime(p);
    displayTime(p);
    printf("Average Turnaround Time: %d\nAverage Waiting Time: %d\n", t[0], t[1]);
    return 0;
}


Share: