arrow
arrow

PF-PUCIT(NEW-CAMPUS)

ASSIGNMENT 06:

Structure


                            
                                Structure is basically a user defined dataType.
                                Syntx;
                                struct nameOfdataType{
                                   dataType variableName1;
                                   dataType variableName1;
                                   dataType variableName1;
                                   ---------------------
                                };
                                    
                            
                        

Task 1.A phone number, such as (212) 767‐8900, can be thought of as having three parts: the area code (212), the exchange (767), and the number (8900). Write a program that uses a structure to store these three parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one, and have the user input a number for the other one. Then display both numbers. The interchange might look like this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212


                        
#include <iostream>
using namespace std;
struct phoneNumber
{
    int areaCode;
    int exchange;
    int number;
};
int main()
{
    phoneNumber phone1, phone2;

    phone1.areaCode = 212;
    phone1.exchange = 767;
    phone1.number = 8900;

    cout << "Enter your area code:" << endl;
    cin >> phone2.areaCode;
    cout << "Enter your exhahange :" << endl;
    cin >> phone2.exchange;
    cout << "Enter your number:" << endl;
    cin >> phone2.number;

    cout << "My number is (" << phone1.areaCode << ") " << phone1.exchange << "-" << phone1.number << endl;
    cout << "Your number is (" << phone2.areaCode << ") " << phone2.exchange << "-" << phone2.number << endl;

    return 0;
}
                        
                    

Task 2.A point on the two‐dimensional plane can be represented by two numbers: an x coordinate and a y coordinate. For example, (4,5) represents a point 4 units to the right of the vertical axis, and 5 units up from the horizontal axis. The sum of two points can be defined as a new point whose x coordinate is the sum of the x coordinates of the two points, and whose y coordinate is the sum of the y coordinates.
Write a program that uses a structure called point to model a point. Define three points, and have the user input values to two of them. Then set the third point equal to the sum of the other two, and display the value of the new point. Interaction with the program might look like this:
Enter coordinates for p1: 3 4
Enter coordinates for p2: 5 7
Coordinates of p1+p2 are: 8, 11


                        
#include<iostream>
using namespace std;
struct point{
    float x,y;
};
int main()
{
    point point1,point2,sum;

    cout<<"Enter the  x coordinate of point_1:"<<endl;
    cin>>point1.x;
    cout<<"Enter the  y coordinate of point_1:"<<endl;
    cin>>point1.y;
    cout<<endl;
    cout<<"Enter the  x coordinate of point_2:"<<endl;
    cin>>point2.x;
    cout<<"Enter the  y coordinate of point_2:"<<endl;
    cin>>point2.y;

    sum.x=point1.x+point2.x;
    sum.y=point1.y+point2.y;

   cout<<"The coordinates of p1+p2 are: ("<<sum.x<<" , "<<sum.y<<")"<<endl;



    return 0;
}
                        
                    

Task 3. Create a structure called employee that contains two members: an employee number (type int) and the employee’s compensation (in dollars; type float). Ask the user to fill in this data for three employees, store it in three variables of type struct employee, and then display the information for each employee.


                        
#include <iostream>
using namespace std;
struct employee
{
    int emplNumber;
    float emplCompensation;
};

int main()
{
    employee emp[3];

    for (int i = 0; i < 3; i++)
    {
        cout << "Enter the number of the empolyee " << i + 1 << " :" << endl;
        cin >> emp[i].emplNumber;
        cout << "Enter the compensation of the empolyee " << i + 1 << " in dollars:" << endl;
        cout << "$";
        cin >> emp[i].emplCompensation;
    }

    cout << "________________________________________________________________" << endl;
    cout << "The empolyee data of these three empolyees are as:" << endl;
    
    for (int i = 0; i < 3; i++)
    {
        cout << "The number of the empolyee_" << i + 1 << " : " << emp[i].emplNumber << endl;
        cout << "The compensation of the empolyee_" << i + 1 << " : $" << emp[i].emplCompensation << endl;
    }

    return 0;
}
                        
                    

Task 4. Create a structure called time. Its three members, all type int, should be called hours, minutes,
and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This can be in 12:59:59 format, or each number can be entered at a separate prompt (“Enter hours:”, and so forth). The program should then store the time in a variable of type struct time, and finally print out the total number of seconds represented by this time value:
long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds


                        
#include<iostream>
using namespace std;
struct time{
  int hours;
  int minutes;
  int seconds;
};
int main()
{
    time t1;
    long totalsecs;

    cout<<"Enter the hours:"<<endl;
    cin>>t1.hours;
    cout<<"Enter the minutes:"<<endl;
    cin>>t1.minutes;
    cout<<"Enter the seconds:"<<endl;
    cin>>t1.seconds;
     
    totalsecs=(t1.hours*3600) + (t1.minutes*60) + (t1.seconds);
      
    cout<<"The total number of seconds in the time "<<t1.hours<<":"<<t1.minutes<<":"<<t1.seconds<<"  =  "<<totalsecs<<" secs"<<endl;

    return 0;
}
                        
                    

Task 5. Use the time structure from Task#4, and write a program that obtains two time values from the user in 12:59:59 format, stores them in struct time variables, converts each one to seconds (type int), adds these quantities, converts the result back to hours‐minutes‐seconds, stores the result in a time structure, and finally displays the result in 12:59:59 format.


                        
#include <iostream>
using namespace std;
struct time
{
  int hours;
  int minutes;
  int seconds;
};
int main()
{
  time t[2];
  time finalTime;
  long totalsecs = 0;
  for (int i = 0; i < 2; i++)
  {

    cout << "Enter the hours of time:" << i + 1 << endl;
    cin >> t[i].hours;
    cout << "Enter the minutes of time:" << i + 1 << endl;
    cin >> t[i].minutes;
    cout << "Enter the seconds of time:" << i + 1 << endl;
    cin >> t[i].seconds;
    cout << "_________________________________________" << endl;
    cout << endl;
    totalsecs += (t[i].hours * 3600 + t[i].minutes * 60 + t[i].seconds);
  }

  finalTime.hours = (totalsecs / 3600);
  finalTime.minutes = (totalsecs - (3600 * finalTime.hours)) / 60;
  finalTime.seconds = (totalsecs - (3600 * finalTime.hours) - (finalTime.minutes * 60));

  cout << "The total number of seconds in the  final sum of 2 times =" << finalTime.hours << ":" << finalTime.minutes << ":" << finalTime.seconds << endl;

  return 0;
}
                        
                    

Task 6.Write a program that uses a structure to store the following weather data for a particular month:
Total Rainfall
High Temperature
Low Temperature
Average Temperature
The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures. Input Validation: Only accept temperatures within the range between –100 and +140 degrees Fahrenheit.


                        
#include <iostream>
using namespace std;
struct weather
{
    float totalRain;
    float highTemp;
    float lowTemp;
    float averageTemp;
};
int main()
{
    const int size = 12;
    weather month[size];
    double average_monthRain;
    double total_rainFall;
    double total_avergeTemp;
    double average_averTemp;
    float highTemp_forYear;
    float lowTemp_forYear;
    int lowMon_num = 0;
    int highMon_num = 0;
    for (int i = 0; i < size; i++)
    {

        cout << "Enter the total rainfall of the month " << i + 1 << endl;
        cin >> month[i].totalRain;

        cout << "Enter the high temperature of the month" << i + 1 << endl;
        cin >> month[i].highTemp;
        while (month[i].highTemp > 140 || month[i].highTemp < -100)
        {
            cout << "Invalid Input!" << endl;
            cout << ": Only accept temperatures within the range between -100 and +140 degrees Fahrenheit." << endl;
            cout << "Please renter the high temperature!" << endl;
            cout << "Enter the high temperature of the month" << i + 1 << endl;
            cin >> month[i].highTemp;
        }

        cout << "Enter the low temperature of the month" << i + 1 << endl;
        cin >> month[i].lowTemp;
        while (month[i].lowTemp > 140 || month[i].lowTemp < -100)
        {
            cout << "Invalid Input!" << endl;
            cout << ": Only accept temperatures within the range between -100 and +140 degrees Fahrenheit." << endl;
            cout << "Please renter the low temperature!" << endl;
            cout << "Enter the low temperature of the month" << i + 1 << endl;
            cin >> month[i].lowTemp;
        }

        month[i].averageTemp = (month[i].highTemp + month[i].lowTemp) / 2;
    }

    total_rainFall = 0;
    total_avergeTemp = 0;
    highTemp_forYear = month[0].highTemp;
    lowTemp_forYear = month[0].lowTemp;

    for (int i = 0; i < size; i++)
    {

        total_rainFall += month[i].totalRain;
        total_avergeTemp += month[i].averageTemp;

        if (highTemp_forYear < month[i].highTemp)
        {
            highTemp_forYear = month[i].highTemp;
            highMon_num = i + 1;
        }
        if (lowTemp_forYear > month[i].lowTemp)
        {
            lowTemp_forYear = month[i].lowTemp;
            lowMon_num = i + 1;
        }
    }
    average_monthRain = total_rainFall / size;
    average_averTemp = total_avergeTemp / size;

    cout << "The average monthly rainfall = " << average_monthRain << endl;
    cout << "The total rainfall for the year = " << total_rainFall << endl;
    cout << "The highest temperature for the year " << highTemp_forYear << " in the month of " << highMon_num << " is occured." << endl;
    cout << "The lowest temperature for the year " << lowTemp_forYear << " in the month of " << lowMon_num << " is occured." << endl;
    cout << "The average of all the monthly average temperatures = " << average_averTemp << endl;

    return 0;
}
                        
                    

Task 7. Write a program that uses a structure to store the following data about a customer account:
Name
Address
City, State, and ZIP
Telephone Number
Account Balance
Date of Last Payment
The program should use an array of at least 10 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu‐driven user interface.
Also add the code that allows the user to search the structure array for a particular customer’s account. It should take input a part of the customer’s name as an input and then search for an account with a name that matches it. All accounts that match should be displayed. If no account matches, a message saying so should be displayed.
Input Validation: When the data for a new account is entered, be sure the user enters data for all the fields. No negative account balances should be entered


                        
#include <iostream>
#include <string>
using namespace std;
struct date
{
    int year;
    int month;
    int day;
};
struct city
{
    string city;
    string state;
    string zip;
};
struct customer
{
    string name;
    string address;
    city ct;
    long long int number;
    float balance;
    date date;
};
int main()
{
    customer cust[10];
    char ch;
    char ele;
    int i = 0; // index
    int j;
    bool flag = true;
    bool element = true;
    string nameSearch;
    do
    {

        cout << " -----------------------------------------------------" << endl;
        cout << "| Enter \"I\" to Insert Customer:                       |" << endl;
        cout << "| Enter \"E\" to Edit the existing Customer:            |" << endl;
        cout << "| Enter \"A\" to Print all the Customers:               |" << endl;
        cout << "| Enter \"S\" to Search the particular Customer:        |" << endl;
        cout << "| Enter \"Q\" to Exit the program!:                     |" << endl;
        cout << " -----------------------------------------------------" << endl;
        cin >> ch;
        ch = tolower(ch);
        switch (ch)
        {
        case 'i':
        {
            cout << "Adding customer:" << i + 1 << endl;
            cin.ignore();
            cout << "Enter the name of the customer " << i + 1 << " :" << endl;
            getline(cin, cust[i].name);
            while (cust[i].name == "")
            {
                cout << "Please fill the input!" << endl;
                cout << "Enter the name of the customer " << i + 1 << " :" << endl;
                getline(cin, cust[i].name);
            }
            cout << "Enter the address of the customer " << i + 1 << " :" << endl;
            getline(cin, cust[i].address);
            while (cust[i].address == "")
            {
                cout << "Please fill the input!" << endl;
                cout << "Enter the address of the customer " << i + 1 << " :" << endl;
                getline(cin, cust[i].address);
            }
            cout << "Enter the City of the customer " << i + 1 << " :" << endl;
            getline(cin, cust[i].ct.city);
            while (cust[i].ct.city == "")
            {
                cout << "Please fill the output!" << endl;
                cout << "Enter the City of the customer " << i + 1 << " :" << endl;
                getline(cin, cust[i].ct.city);
            }
            cout << "Enter the State of the customer " << i + 1 << " :" << endl;
            getline(cin, cust[i].ct.state);
            while (cust[i].ct.state == "")
            {
                cout << "Please fill the output!" << endl;
                cout << "Enter the State of the customer " << i + 1 << " :" << endl;
                getline(cin, cust[i].ct.state);
            }
            cout << "Enter the ZIP of the customer " << i + 1 << " :" << endl;
            getline(cin, cust[i].ct.zip);
            while (cust[i].ct.zip == "")
            {
                cout << "Please fill the output!" << endl;
                cout << "Enter the ZIP of the customer " << i + 1 << " :" << endl;
                getline(cin, cust[i].ct.zip);
            }
            cout << "Enter the TELEPNOE # of the customer " << i + 1 << " :" << endl;
            cin >> cust[i].number;
            while (cust[i].number < 0)
            {
                cout << "Please fill the positive number output!" << endl;
                cout << "Enter the Telephone of the customer " << i + 1 << " :" << endl;
                cin >> cust[i].number;
            }
            cout << "Enter the Account Balance of the customer " << i + 1 << " :" << endl;
            cout << "$";
            cin >> cust[i].balance;
            while (cust[i].balance < 0)
            {
                cout << "Please fill the positive account balance output!" << endl;
                cout << "Enter the  account balance of the customer " << i + 1 << " :" << endl;
                cout << "$";
                cin >> cust[i].balance;
            }
            cout << "Enter the date for the customer " << i + 1 << " :" << endl;
            cout << "Enter year:";
            cin >> cust[i].date.year;
            cout << "Enter month:";
            cin >> cust[i].date.month;
            cout << "Enter the day:";
            cin >> cust[i].date.day;

            i++;
            break;
        }
        case 'e':
        {
            if (i == 0)
            {
                cout << "Please first add the minimum 1 customer data:" << endl;
            }
            else
            {
                cout << "Enter the customer index to change [1 to " << i << "]:" << endl;
                cin >> j;
                j--;
                do
                {
                    cout << cust[j].name << endl;
                    cout << cust[j].address << endl;
                    cout << cust[j].number << endl;
                    cout << cust[j].balance << endl;
                    cout << cust[j].ct.city << endl;
                    cout << cust[j].ct.state << endl;
                    cout << cust[j].ct.zip << endl;
                    cout << cust[j].date.year << endl;
                    cout << cust[j].date.month << endl;
                    cout << cust[j].date.day << endl;
                    cout << " -------------------------------------------" << endl;
                    cout << "| To Edit the elements:                     |" << endl;
                    cout << "| N = Name              |   A= Adrdress     |" << endl;
                    cout << "| T = Telephone         |   B= Balance      |" << endl;
                    cout << "| C = City              |   S= State        |" << endl;
                    cout << "| Z = ZIP               |   Y= year         |" << endl;
                    cout << "| M = Month             |   D= Day          |" << endl;
                    cout << "| E = Done editing this customer            |" << endl;
                    cout << " -------------------------------------------" << endl;
                    cin >> ele;
                    ele = tolower(ele);
                    cin.ignore();
                    switch (ele)
                    {
                    case 'n':
                    {
                        cout << "Enter the edited name:" << endl;
                        getline(cin, cust[j].name);
                        break;
                    }
                    case 'a':
                    {
                        cout << "Enter the edited address:" << endl;
                        getline(cin, cust[j].address);
                        break;
                    }
                    case 't':
                    {
                        cout << "Enter the edited Telephone number:" << endl;
                        cin >> cust[j].number;
                        break;
                    }
                    case 'b':
                    {
                        cout << "Enter the edited balance:" << endl;
                        cin >> cust[j].balance;
                        break;
                    }
                    case 'c':
                    {
                        cin.ignore();
                        cout << "Enter the edited city:" << endl;
                        getline(cin, cust[j].ct.city);
                        break;
                    }
                    case 's':
                    {
                        cout << "Enter the edited State:" << endl;
                        getline(cin, cust[j].ct.state);
                        break;
                    }
                    case 'z':
                    {
                        cout << "Enter the edited ZIP:" << endl;
                        getline(cin, cust[j].ct.zip);
                        break;
                    }
                    case 'y':
                    {
                        cout << "Enter the edited year:" << endl;
                        cin >> cust[j].date.year;
                        break;
                    }
                    case 'm':
                    {
                        cout << "Enter the edited month:" << endl;
                        cin >> cust[j].date.month;
                        break;
                    }
                    case 'd':
                    {
                        cout << "Enter the edited day:" << endl;
                        cin >> cust[j].date.day;
                        break;
                    }
                    case 'e':
                    {
                        element = false;
                        break;
                    }
                    }
                } while (element == true);
            }
            break;
        }
        case 'a':
        {
            if (i == 0)
            {
                cout << "Please first add the minimum 1 customer data:" << endl;
            }
            else
            {
                cout << "The data of all the customers is:" << endl;
                for (int k = 0; k < i; k++)
                {
                    cout << "DATA OF CUSTOMER " << k + 1 << " :" << endl;
                    cout << " __________________________________________________________________" << endl;
                    cout << "| Name:              |           " << cust[k].name << "                 " << endl;
                    cout << "| Address:           |           " << cust[k].address << "              " << endl;
                    cout << "| Telephone #:       |           " << cust[k].number << "               " << endl;
                    cout << "| Account Balance:   |           " << cust[k].balance << "              " << endl;
                    cout << "| City:              |           " << cust[k].ct.city << "              " << endl;
                    cout << "| State:             |          " << cust[k].ct.state << "              " << endl;
                    cout << "| ZIP:               |          " << cust[k].ct.zip << "                " << endl;
                    cout << "| Year:              |          " << cust[k].date.year << "             " << endl;
                    cout << "| Month:             |          " << cust[k].date.month << "            " << endl;
                    cout << "| Day:               |          " << cust[k].date.day << "              " << endl;
                    cout << " ___________________________________________________________________" << endl;
                    cout << endl;
                }
            }
            break;
        }
        case 's':
        {
            if (i == 0)
            {
                cout << "Please first add the minimum 1 customer data:" << endl;
            }
            else
            {
                cout << "Search  the particular customer by its name :" << endl;
                cout << "Enter the name of the customer:" << endl;
                cin.ignore();
                getline(cin, nameSearch);
                for (int s = 0; s < i; s++)
                {
                    if (cust[s].name == nameSearch)
                    {
                        cout << "DATA OF CUSTOMER " << nameSearch << " :" << endl;
                        cout << " __________________________________________________________________" << endl;
                        cout << "| Name:              |           " << cust[s].name << "                 " << endl;
                        cout << "| Address:           |           " << cust[s].address << "              " << endl;
                        cout << "| Telephone #:       |           " << cust[s].number << "               " << endl;
                        cout << "| Account Balance:   |           " << cust[s].balance << "              " << endl;
                        cout << "| City:              |           " << cust[s].ct.city << "              " << endl;
                        cout << "| State:             |          " << cust[s].ct.state << "              " << endl;
                        cout << "| ZIP:               |          " << cust[s].ct.zip << "                " << endl;
                        cout << "| Year:              |          " << cust[s].date.year << "             " << endl;
                        cout << "| Month:             |          " << cust[s].date.month << "            " << endl;
                        cout << "| Day:               |          " << cust[s].date.day << "              " << endl;
                        cout << " ___________________________________________________________________" << endl;
                        cout << endl;
                    }
                }
            }
            break;
        }
        case 'q':
        {
            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.