arrow
arrow

PF-PUCIT(NEW-CAMPUS)

ASSIGNMENT 07

Arrays


                            
                              Arrays are the data structures .
                              syntex;
                              dataType variableName [sizeOf array];
                              arrays index start from 0 ,1,2,...
                            
                        

Task 1. Write a program that lets the user enter 10 values into an array. The program should then display the largest and smallest values stored in the array


                        
#include <iostream>
using namespace std;
int main()
{
    const int size = 10;
    int arr[size] = {};
    int max;
    int min;
    cout << "The random values in the array are given as:" << endl;
    for (int i = 0; i < size; i++)
    {
        cin>>arr[i];
    }
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
    // To find the min and max;
    max = arr[0];
    min = arr[0];
    for (int i = 0; i < size; i++)
    {
        if (arr[i] > max)
        {
            max = arr[i];
        }
        if (arr[i] < min)
        {
            min = arr[i];
        }
    }
    cout << "The largest in the array is = " << max << endl;
    cout << "The minimum in the array is = " << min << endl;

    return 0;
}
                        
                    

Task 2. Write a program that lets the user enter the total rainfall for each of 12 months into an array of double s. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Input Validation: Do not accept negative numbers for monthly rainfall figures


                        
#include <iostream>
using namespace std;
int main()
{
    const int size = 4;
    double month[size];
    double totalRain = 0;
    double average_rainFall;
    double highest;
    double lowest;
    cout << "___________Average Rinfall Calculator_________" << endl;
    for (int i = 0; i < size; i++)
    {
        cout << "Enter the rainfall of the month " << i + 1 << " :" << endl;
        cin >> month[i];
        while(month[i]<0)
        {
            cout<<"invalid:Input!"<<endl;
            cout<<"Please enter the rainfall as a positive number:"<<endl;
            cin>>month[i];
        }
        totalRain = totalRain + month[i];
    }
    average_rainFall = totalRain / size;
    highest = month[0];
    lowest = month[0];
    for (int i = 0; i < size; i++)
    {
        if (month[i] > highest)
        {
            highest = month[i];
        }
        if (month[i] < lowest)
        {
            lowest = month[i];
        }
    }
    cout<<"The total rainfall of the year :"<<totalRain<<endl;
    cout<<"The average rainfall of the year "<<average_rainFall<<endl;
    cout << "The highest rainfall of the year :" << highest << endl;
    cout << "The lowest rainfall of the year :" << lowest << endl;

    return 0;
}
                        
                    

Task 3. Write a program that lets a maker of chips and salsa keep track of sales for five different types of salsa: mild, medium, sweet, hot, and zesty. The program should use two parallel 5‐element arrays: an array of strings that holds the five salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type. The salsa names should be stored using an initialization list at the time the name array is created. The program should prompt the user to enter the number of jars sold for each type. Once this sales data has been entered, the program should produce a report that displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling products. Input Validation: Do not accept negative values for number of jars sold.


                        
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    const int size = 5;
    string salsa[size] = {"Mild", "Medium", "Sweet", "Hot", "zesty"};
    int num_ofJars[size];
    int totalSlaes = 0;
    int highestSelling;
    int lowestSelling;
    cout << "___________Salsa Sales Monthly Report_________" << endl;
    for (int i = 0; i < size; i++)
    {
        cout << "Enter the number of jars sold for " << salsa[i] << " :" << endl;
        cin >> num_ofJars[i];
        while (num_ofJars[i] < 0)
        {
            cout << "Invalid input!" << endl;
            cout << "Please Enter the valid input" << endl;
            cout << "Enter the number of jars sold for " << salsa[i] << " :" << endl;
            cin >> num_ofJars[i];
        }
        totalSlaes = totalSlaes + num_ofJars[i];
    }
    highestSelling = num_ofJars[0];
    lowestSelling = num_ofJars[0];
    for (int i = 0; i < size; i++)
    {
        if (num_ofJars[i] > highestSelling)
        {
            highestSelling = num_ofJars[i];
        }
        if (num_ofJars[i] < lowestSelling)
        {
            lowestSelling = num_ofJars[i];
        }
    }
    cout << endl;
    cout << "Data of the saled products of the month:" << endl;
    for (int i = 0; i < size; i++)
    {
        cout << "The sales for the " << salsa[i] << " = " << num_ofJars[i] << endl;
    }
    cout << "The totals sales of the salsa = " << totalSlaes << endl;
    cout << "The highest seeling salsa is " << highestSelling << endl;
    cout << "The lowest seeling salsa is " << lowestSelling << endl;

    return 0;
}

                        
                    

Task 4. Write a Program that take a number n input from user a. Assume that the array of size 100 contains random integers range 1 to 500. The program should display all of the numbers in the array that are greater than the number n .


                        
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    const int size = 100;
    int arr[size];
    int n;
    for (int i = 0; i < size; i++)
    {
        arr[i] = (rand() % 500) + 1;
    }
    cout << "Enter the number :" << endl;
    cin >> n;
    cout << "All of the numbers in the array that are greater than the number " << n << " are given as:" << endl;
    for (int i = 0; i < size; i++)
    {
        if (arr[i] > n)
        {
            cout << arr[i] << endl;
        }
    }

    return 0;
}
                        
                    

Task 5.Use a an array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it is not a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the "worst case" in which all 20 numbers are different. Use the smallest possible array to solve this problem.


                        
#include <iostream>
using namespace std;
int main()
{
    const int size = 10;
    int arr[size];
    int dup_count[size] = {0};
    int tally;
    cout << "______Unique Elements Detector________" << endl;
    for (int i = 0; i < size; i++)
    {
        cout << "Enter the number " << i + 1 << " :" << endl;
        cin >> arr[i];
        while (arr[i] < 10 || arr[i] > 100)
        {
            cout << "Invalid Input!" << endl;
            cout << "Enter  the number between the 10 and 100 ,inclusive." << endl;
            cout << "Enter the number " << i + 1 << " :" << endl;
            cin >> arr[i];
        }
        dup_count[i] = arr[i];
        tally++;
        int count = 0;
        for (int j = 0; j < sizeof(dup_count) / sizeof(int); j++)
        {
            if (dup_count[j] == arr[i])
            {
                count++;
            }
        }
        if (count == 2)
        {
            cout<<dup_count[i]<<" is a duplicate number!"<<endl;
            dup_count[i] = 0;

        }
        int temp;
        if (tally != 1)
        {
            if (dup_count[i - 1] == 0)
            {
                if (count == 1)
                {
                    int m = i;
                    while (dup_count[m - 1] == 0)
                    {
                        temp = dup_count[m];
                        dup_count[m] = dup_count[m - 1];
                        dup_count[m - 1] = temp;
                        m--;
                    }
                }
            }
        }
    }
    for(int i=0;dup_count[i]!=0;i++)
    {
        cout<<dup_count[i]<<" ";
    }

    return 0;
}
                        
                    

Task 6. ( Airline Reservations System)

A small airline has just purchased a computer for its new automated reservations system. You have been asked to program the new system. You are to write a program to assign seats on each flight of the airline's only plane (capacity: 10 seats).

Your program should display the following menu of alternativesPlease type 1 for "First Class" and Please type 2 for "Economy". If the person types 1, your program should assign a seat in the first class section (seats 1‐5). If the person types 2, your program should assign a seat in the economy section (seats 6‐10). Your program should print a boarding pass indicating the person's seat number and whether it is in the first class or economy section of the plane.

Use a one‐dimensional array to represent the seating chart of the plane. Initialize all the elements of the array to 0 to indicate that all seats are empty. As each seat is assigned, set the corresponding elements of the array to 1 to indicate that the seat is no longer available. Your program should, of course, never assign a seat that has already been assigned. When the first class section is full, your program should ask the person if it is acceptable to be placed in the economy section (and vice versa). If yes, then make the appropriate seat assignment. If no, then print the message "Next flight leaves in 3 hours".


                        
#include <iostream>
using namespace std;
int main()
{
    const int seats = 5;
    bool flag = true;
    int n;
    int arr_first[seats] = {0};
    int arr_economy[seats] = {0};
    int first = 1;
    int economy = 6;
    char ch;
    do
    {
        cout << " _______________________________________________________" << endl;
        cout << "|________________Airline Reservations System____________|" << endl;
        cout << "|                       Welcome Sir!                    |" << endl;
        cout << "| Press 1 to book seat in bussiness Section             |" << endl;
        cout << "| Press 2 to book seat in economySection                |" << endl;
        cout << " _______________________________________________________" << endl;
        cin >> n;
        while (n != 1 && n != 2)
        {
            cout << "Invalid Input" << endl;
            cout << "Please enter the valid input as 1 or 2" << endl;
            cin >> n;
        }
        switch (n)
        {
        case 1:
        {
            if (first <= 5)
            {
                arr_first[first - 1] = first;
                cout << "There is available seat in this section!" << endl;
                cout << " ________________________________________________" << endl;
                cout << "|****************|_1_|-BOARDING PASS*************|" << endl;
                cout << "| YOUR SECTION : | 1                             |" << endl;
                cout << "| YOUR SEAT #  : | " << arr_first[first - 1] << "                             |" << endl;
                cout << "| SEATS AVAILABLE IN FIRST CLASS SECTION:        |" << endl;
                cout << "| Seat # 1:      |" << arr_first[0] << "                              |" << endl;
                cout << "| Seat # 2:      |" << arr_first[1] << "                              |" << endl;
                cout << "| Seat # 3:      |" << arr_first[2] << "                              |" << endl;
                cout << "| Seat # 4:      |" << arr_first[3] << "                              |" << endl;
                cout << "| Seat # 5:      |" << arr_first[4] << "                              |" << endl;
                cout << " ________________________________________________" << endl;
                first++;
            }
            else if (first > 5)
            {
                cout << "There is \"NO\" seat available in FIRST CLASS SECTION." << endl;
                if (economy <= 10)
                {
                    cout << "There is available seat in the ECONOMOY SECTION:" << endl;
                    cout << "Are you want to buy it press \"Y\" FOR YES and \"N\" FOR NO!" << endl;
                    cin >> ch;
                    ch = tolower(ch);
                    while (ch != 'y' && ch != 'n')
                    {
                        cout << "Invalid Input" << endl;
                        cout << "Please enter the valid input as y or n" << endl;
                        cin >> ch;
                    }
                    ch = tolower(ch);
                    switch (ch)
                    {
                    case 'n':
                    {
                        cout << "Next flight leaves in 3 hours:" << endl;
                        break;
                    }
                    case 'y':
                    {
                        if (economy <= 10)
                        {
                            arr_economy[economy - 6] = economy;
                            cout << "There is available seat in this section!" << endl;
                            cout << " ________________________________________________" << endl;
                            cout << "|****************|_2_|-BOARDING PASS*************|" << endl;
                            cout << "| YOUR SECTION : | 2                             |" << endl;
                            cout << "| YOUR SEAT #  : | " << arr_economy[economy - 6] << "                             |" << endl;
                            cout << "| SEATS AVAILABLE IN ECONOMY CLASS SECTION:      |" << endl;
                            cout << "| Seat # 1:      |" << arr_economy[0] << "                              |" << endl;
                            cout << "| Seat # 2:      |" << arr_economy[1] << "                              |" << endl;
                            cout << "| Seat # 3:      |" << arr_economy[2] << "                              |" << endl;
                            cout << "| Seat # 4:      |" << arr_economy[3] << "                              |" << endl;
                            cout << "| Seat # 5:      |" << arr_economy[4] << "                              |" << endl;
                            cout << " ________________________________________________" << endl;
                            economy++;
                        }
                        break;
                    }
                    }
                }
                if (economy > 10)
                {
                    cout << "You have booked last seat,There is also \"NO\" seats  available now in FIRST CLASS SECTION." << endl;
                    cout << "You have booked last seat,There is also \"NO\" seats  available now in ECONOMY CLASS SECTION." << endl;
                    cout << "Next flight leaves in 3 hours:" << endl;
                    flag = false;
                }
            }
            break;
        }
        case 2:
        {
            if (economy <= 10)
            {
                arr_economy[economy - 6] = economy;
                cout << "There is available seat in this section!" << endl;
                cout << " ________________________________________________" << endl;
                cout << "|****************|_2_|-BOARDING PASS*************|" << endl;
                cout << "| YOUR SECTION : | 2                             |" << endl;
                cout << "| YOUR SEAT #  : | " << arr_economy[economy - 6] << "                             |" << endl;
                cout << "| SEATS AVAILABLE IN ECONOMY CLASS SECTION:       |" << endl;
                cout << "| Seat # 1:      |" << arr_economy[0] << "                              |" << endl;
                cout << "| Seat # 2:      |" << arr_economy[1] << "                              |" << endl;
                cout << "| Seat # 3:      |" << arr_economy[2] << "                              |" << endl;
                cout << "| Seat # 4:      |" << arr_economy[3] << "                              |" << endl;
                cout << "| Seat # 5:      |" << arr_economy[4] << "                              |" << endl;
                cout << " ________________________________________________" << endl;
                economy++;
            }
            else if (economy > 10)
            {
                cout << "There is \"NO\" seat available in ECONOMY CLASS SECTION." << endl;
                if (first <= 5)
                {
                    cout << "There is available seat in the FIRST SECTION:" << endl;
                    cout << "Are you want to buy it press \"Y\" FOR YES and \"N\" FOR NO!" << endl;
                    cin >> ch;
                    ch = tolower(ch);
                    while (ch != 'y' && ch != 'n')
                    {
                        cout << "Invalid Input" << endl;
                        cout << "Please enter the valid input as y or n" << endl;
                        cin >> ch;
                    }
                    ch = tolower(ch);
                    switch (ch)
                    {
                    case 'n':
                    {
                        cout << "Next flight leaves in 3 hours:" << endl;
                        break;
                    }
                    case 'y':
                    {
                        if (first <= 5)
                        {
                            arr_first[first - 1] = first;
                            cout << "There is available seat in this section!" << endl;
                            cout << " ________________________________________________" << endl;
                            cout << "|****************|_1_|-BOARDING PASS*************|" << endl;
                            cout << "| YOUR SECTION : | 1                             |" << endl;
                            cout << "| YOUR SEAT #  : | " << arr_first[first - 1] << "                             |" << endl;
                            cout << "| SEATS AVAILABLE IN FIRST CLASS SECTION:        |" << endl;
                            cout << "| Seat # 1:      |" << arr_first[0] << "                              |" << endl;
                            cout << "| Seat # 2:      |" << arr_first[1] << "                              |" << endl;
                            cout << "| Seat # 3:      |" << arr_first[2] << "                              |" << endl;
                            cout << "| Seat # 4:      |" << arr_first[3] << "                              |" << endl;
                            cout << "| Seat # 5:      |" << arr_first[4] << "                              |" << endl;
                            cout << " ________________________________________________" << endl;
                            first++;
                        }
                        break;
                    }
                    }
                }
                if (first > 5)
                {
                    cout << "You have booked last seat,There is also \"NO\" seats  available now in FIRST CLASS SECTION." << endl;
                    cout << "You have booked last seat,There is also \"NO\" seats  available now in ECONOMY CLASS SECTION." << endl;
                    cout << "Next flight leaves in 3 hours:" << endl;
                    flag = false;
                }
            }
            break;
        }
        }

    } while (flag == true);

    return 0;
}
                        
                    

Whats make this website differents from others?

Amazing Feature That Increase your Productivity

If you don't find any problem,in this website and if you have a very difficult assignment question that is not present on internet ,then you dont bother yourself,we are here to solve every problem of your life you just click on the Problem asking button and write your problem we slove the code of your problem and sent you on your email addres that you put in the sin up form.