Download Computer programing lab 12 and more Exercises Advanced Computer Programming in PDF only on Docsity!
BAHRIA UNIVERSITY LAHORE CAMPUS
LAB JOURNAL 1 2
MUHAMMAD HAMID AZEEM
BSIT 1A
SUBMITTED TO (SIR ZIA UR REHMAN)
COMPUTER PROGRAMING LAB
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) 76 7 - 8900 Your number is (415) 555- 1212.
CODE: #include #include using namespace std; struct phone { string area; string exchange; string number; }; int main() { cout << "\n\t\t\t\t##########################################################"; cout << "\n\t\t\t\t\t Programme Usning Structure For Phone Number: " << endl; cout << "\t\t\t\t##########################################################"<< endl; phone p1 = { "212","767","8900" }; phone p2; cout << "\n\t\t\t\tEnter your area code, exchange, and number: "; cin >> p2.area >> p2.exchange >> p2.number; cout << "\n\t\t\t\t========================================================="; cout << "\n\t\t\t\tMy number is(" << p1.area << ") "<< p1.exchange<<"-" <<p1.number<<endl; cout << "\n\t\t\t\tYour number is(" << p2.area << ") "<<p2.exchange<<"- "<<p2.number<<endl; return 0; }
TASK 2
A record contains name of cricketer, his age, number of test matches that he has
played and the average runs that he has scored in each test match. Create an array
of structure to hold records of 20 such cricketer and then write a program to read
these records
CODE: #include #include using namespace std; typedef int (compfn)(const void, const void); int comp(struct cricketer, struct cricketer); struct cricketer { char name[10]; int age, tottest, avgrun; }player[20]; int main() { cout << "\n\t\t#################################"; cout << "\n\t\t Record of Cricketers" << endl; cout << "\t\t#################################" << endl; int i, n; cout << "\t\tEnter numbers of Players : "; cin >> n; for (i = 0; i < n; ++i) { cout << "\t\tName : "; cin >> player[i].name; cout << "\t\tAge : "; cin >> player[i].age; cout << "\t\tTotal Test Matches : "; cin >> player[i].tottest; cout << "\t\tAvg Run : "; cin >> player[i].avgrun; } qsort((void)&player, n, sizeof(struct cricketer), (compfn)comp); // Printing cout << "\t\t" << "Name" << "\t" << "Age" << "\t" << "Tests" << "\t" << "Avg"; for (i = 0; i < n; ++i) { cout << endl << "\t\t" << player[i].name << "\t" << player[i].age << "\t" << player[i].tottest << setw(8) << player[i].avgrun; } return 0; } int comp(struct cricketer* a1, struct cricketer* a2) { if (a1->avgrun < a2->avgrun) return - 1; if (a1->avgrun > a2->avgrun)
return 1; else return 0; } OUTPUT:
cout << endl; } } cout << endl; }
OUTPUT:
THANK YOU ☺