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: