0% found this document useful (0 votes)
23 views

Task 10 Programming Fundamentals

Uploaded by

umar03029452584
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Task 10 Programming Fundamentals

Uploaded by

umar03029452584
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Task 10

Programs
1.Create a structure with name Student. It has
three variable Roll Number, Name and GPA.
Create two variables of Student Type
structure. Assign them values using
assignment operator and display them on
screen.
Answer:
#include<iostream>
#include<iomanip>
using namespace std;
struct Student
{
int rollNumber;
string name;
float GPA;
};
int main()
{
Student student1, student2;
student1.rollNumber = 101;
student1.name = "John Doe";
student1.GPA = 3.8;
student2.rollNumber = 102;
student2.name = "Jane Smith";
student2.GPA = 3.9;
cout << "Student 1 Information:" << endl;
cout << "Roll Number: " << student1.rollNumber <<
endl;
cout << "Name: " << student1.name << endl;
cout << "GPA: " << fixed << setprecision(2) <<
student1.GPA << endl;
cout << "\n------------------------\n";
cout << "Student 2 Information:" endl;
cout << "Roll Number: " << student2.rollNumber <<
endl;
cout << "Name: " << student2.name << endl;
cout << "GPA: " << fixed << setprecision(2) <<
student2.GPA << endl;
return 0;
}
2.Create a structure of name Telephone
directory. It has variables Name, Phone
Number, Email Address. Create an array of 5
variables of this structure. Input their values
and display them on screen. Implement a
search function which will take this structure
as argument asks the user to enter a name and
search this name from this array. And Display
its complete record on screen.
Answer:
#include<iostream>
#include<string>
using namespace std;
struct TelephoneDirectory
{
string name;
string phoneNumber;
string emailAddress;
};
void searchRecord(const TelephoneDirectory directory[],
int size, const string& searchName)
{
for (int i = 0; i < size; ++i)
{
if (directory[i].name == searchName)
{
cout << "\nRecord Found:" << endl;
cout << "Name: " << directory[i].name <<
endl;
cout << "Phone Number: " <<
directory[i].phoneNumber << endl;
cout << "Email Addresss: " <<
directory[i].emailAddress << endl;
return;
}
}
cout << "\nRecord not found for the given name." <<
endl;
}
int main()
{
TelephoneDirectory directory[5];
for (int i = 0; i < 5; ++i)
{
cout << "Enter Name for Entry " << i + 1 << ":
";
getline(cin, directory[i].name);
cout << "Enter Phone Number for Entry " << i +
1 << ": ";
getline(cin,directory[i].phoneNumber);
cout << "Enter Email Address for Entry " << i +
1 << ": ";
getline(cin, directory[i].emailAddress);
cout << "\n";
}
cout << "\nTelephonr Directory Entries:" << endl;
for (int i = 0; i < 5; ++i);
{
cout << "\nEnter " << i + 1 << ":" << endl;
cout << "Name: " << directory[i].name << endl;
cout << "Phone Number: " <<
directory[i].phoneNumber << endl;
cout << "Email Address: " <<
directory[i].emailAddress << endl;
}
string searchName;
cout << "\nEnter a Name to Search: ";
getline(cin, searchName);
searchRecord(directory, 5, searchName);
return 0;
}

You might also like