PF Assignment 2
PF Assignment 2
PROGRAMMING FUNDAMENTALS
ASSIGNMENT # 02
1
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
Q1. Write a C++ code to examine newly students after asking hundred of questions and answers using
maximum functions and modular techniques. Make a suitable format on console, ask ont to hundred
questions and every answer given by candidate code will aware to candidate, answer is correct
otherwise code will print correct answer, in the end code will display candidate progress.
SOURCE CODE:
#include<iostream>
#include<string>//For String
#include<iomanip>//For Setw
#include<cstring>//For Strcmp
2
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
bool askReadyForTest() {
// Display a note and get input for readiness
cout << "Note:" << endl;
cout << "Are you ready for the test (Yes/No). " << endl;
char option[4];
cin.ignore();
cin.getline(option, 4);
return (strcmp(option, "Yes") == 0);
}
3
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
"What was the Religion of the people of Gandhara Civilization?" , "What services
did Sir Syed provide to awaken the Muslims of the Sub- Continent?" , "Write points
on Begum Rana Liaqat Ali Khan's political and social services?" , "How did the
people of Khyber Pakhtoonkhwa decide for comprising in Pakistan?" , "Name the
important personalities who played vital role in the Pakistan Movement?" , "Name
the elements of a map?" , " Which are called the lines of latitude?" , " How many
lines of latitude are there?" , "How many lines of longitude are there?" ,"What is
mean by Equator?" , "What is mean by Prime Meridian?" , "Where is the mountainous
region of Pakistan located?" , "Write down the names of important desert areas of
Pakistan?", "What is called a physical region?" , "Name the important rivers
present in Balochistan Plateau?" , "What is the importance of Gawadar sea port?" ,
"What is the highest peak of Karakoram mountain?" , "What is the highest peak of
Himalayas mountain?" , "What is the highest peak of Hindu Kush mountain?" , "What
are the Important passes of Pakistan?" , "What is the largest salt mine of
Pakistan?" , "The highest peak of Koh-e-Suleman?" , "What are the Important sea-
ports of Pakistan?" , "What is the weather of Hilly areas?" ,"What is the weather
of Plain areas?" , ". Which instrument is used to measure the temperature?" ,
"What is Weather?" , "What is Climate?" , "Why there is difference of Climate in
Murree and Karachi?" , "What are the Natural Disasters?" , "Give examples of
Natural Disasters." , "What is meant by Growth Rate?" , "What is called Population
Density?" , "How can we find out Population Density?" , "How you are effected by
increasing the number of students in your class?" , "What is topographic map?" ,
"What is Road Map?" , "Why is scale used on map?" , "Name the types of scale?" ,
"What is Census?" , "What is Goods?" , "What are services?" , "What is Public
Goods?", "What is Private Goods?"};
string A[100] = {"Right to Vote, Freedom Of Speech." , "Civic duty and voting,
Jury Duty." ,"The legal right to belong to any country is called Citizenship."
,"Every human being has been gifted with some rights by the state. These right are
called human rights." , "Etiquettes are the formal manners and the rules to be a
good citizen." , "Use sidewalks when walking along the road., Listen to Parents,
Teachers and Elders carefully." , "It shapes a diversified society, It helps to
promote democracy" , "Show tolerance in daily life, Take care of others, Be honest
with all." , "Shalwar and Kameez" , "Urdu, Punjabi, Shushki, Pashto, Hindko,
Sindhi" , "Culture is the way of life language, dress, food, religious belief,
customs and traditions of the people in a society." , "Punjabi, Sindhi, Pahtoon,
Gilgit-Baltistani, Kashmiri, Balochi" , "Shushki" , "Kashmiri" , "Means of
Communication are the ways by which we transfer messages and information to
others." , "It brings information quickly, It brings people close together." ,
"Some people use internet for cybercrimes., Over use of internet is wastage of
time and money." , "Facebook, Whatsapp, Youtube, Internet" , "Ornaments of
Pakistan are very famous in the world, Sindh is famous for Sindh Cap and Ajrak." ,
"A society consisting of many different cultures is called multicultural society."
, "It can fulfill all kinds of needs of people, It increases the economy of the
country." , "It needs tolerance, It can decrease the brotherhood." , "Sindh" ,
"Languages = Sindhi and Urdu, Dress = Shalwar Kameez, Food = Biryani, Halwa Puri,
Paratha, Simple Roti, Professions = Jobs Business, Games = Hockey, Cricket,
Football" , "Pigeons, Horse Riders, Letter Writing" , "Telephone, Mobile phone,
Telivision, E-mail" , "Social media is modern mean of communication. For example
Whatsapp, Facebook, Twitter." , "It is used to broadcast information quickly, It
4
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
is easy to access and use social media." , "We listen educational and informative
programs, We listen match commentaries." , "We watch news and talks shows, We
watch matches." , "The media used to spread information and communication on a
large scale is termed as mass media." , "Newspapers and Magzines, Telvision and
Radio." , "In this system, country is ruled by kings, queens or emperors." , "In
this system, people select the government by voting." , "Opinion of the publice is
given importance. It makes policies for the betterment of general public." , "It
looks after the national economy, It runs the national administration." ,
"Protection of life and property, Provision of health and educationa Right to
vote." , "It looks after the provincial revenue, It controls the provincial
policies." , "Protection of life and property, Provision of health and educational
facilities. Right to vote, Religious independence," , "Political parties work as
per their charter, They gather the people on the basis of their charter." , "An
organized life or a society in any area is called civilization. The marks of
civilizations are trade, celebrations, architecture, life style and customs." ,
"Greek civilization, Roman civilization, Gandhara civilization" , "Alexander the
Great belonged to Greek (326 B.C)." , "Athens and Sparta" , "They fought with each
other and sometimes united to defend the Greek from the invaders." , "Olympic
games were introduced by the ancient Greek. Olympic games were started in 776 B.C
at Olympia, a city of Greek." , "People wore white togas, made from wool or
linen." , "My historical personality is Quaid-e-Azam Muhammad Ali Jinnah. He is
the founder of our beloved country, Pakistan." , "Quaid-e-Azam Muhammad Ali
Jinnah, Begum Rana Liaqat Ali Khan, Chaudary Rehmat Ali, Allama Iqbal" , "The
Hindus and the Muslims are two separate nations because both the nations differ in
their religion, history, culture and civilization. They cannot remain together" ,
"Sir Syed Ahmed Khan is known as the founder of two Nation Theory." , "Chaudary
Rehmat Ali, on 28th January, 1933" , "Sir Sahibzada Abdul Qayum established the
Islamiya College, Peshawar." , "The students of islamiya college Peshawar took
part in the Pakistan Movement and they favoured the Pakistan Resolution." , "They
were high quality engineers and architectures, They included dome-shaped and
arched structures in their buildings." , "Buddhism was the official religion of
Gandhara Civilization." , "He realized that the modern education was the key
solution for all Muslims, He warned the Muslims that the Congress was a therefore
Muslims should refrain from joining it." , "She worked with her husband, Liaqat
Ali Khan during the struggle for Pakistan, She managed the health facilities for
the refugees." , "Islamic College, Peshawar awoke the political awareness among
the Pakhtoon students and they participated in Pakistan Movement." , " Quaid-e-
Azam Muhammad Ali Jinnah, Fatima Jinnah, Sir Abdullah Haroon, Begum Abdullah
Haroon, Begum Rana Liaqat Ali Khan" , "Border, Title, Orientation Scale, Legend."
, "The imaginary lines to find the location of any area or country on globe or map
are called lines of latitude." , "There are 180 latitudes in number." ,
"Longitudes are imaginary lines drawn from North to South." , "There are 36
longitudes in number." , "The O latitude is called the Equator." , "The O
longitude is called the Prime Meridian." , "They are located in the North and
Northwest regions of Pakistan." , "Thal, Thar and Kharan" , "A physical region
have common structure of the external earth, climate, plants, civilization of the
people etc" , "Zob, Hub, Dasht, Hangol" , "It is located in Baluchistan. In
future, it will become an important economic center. It is an important part of
CPEC (China Pakistan Economic Corridor)." , "K-2" , "Nanga Purbat" , "Turch Mir" ,
5
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
"Khyber Pass, Kurram Pass, Tochi Pass, Bolan Pass" , "Khewra (District Jhelum)" ,
"Takht-e-Suleman." , "Karachi Port, Pashni Port, Bin Qasim Port, Jewani Port,
Gawadar Port" , "In hilly areas, it is very cold in winter, as it snows. In summer
the weather is moderate there." , "In plain areas the weather is moderate all the
year" , "An Thermometer." , "The atmospheric conditions at a given time and place
" , "The temporary weather conditions of a place for a short period of time." ,
"Because Murree is located at high altitude from the sea level and located in
Northern mountains. While Karachi is coastal area, so there is a difference of
climate in Murree and in Karachi." , "Natural events that cause great damage or
loss of life are called natural disasters." , "Flood, Famine Earthquake, Volcanic"
, "The annual increase in population of a country or region is called Growth
Rate." , "The number of people residing in one square kilometer of a region is
called Population Density." , "Density = Total Population / Total area of the
region" , "The teacher cannot concentrate on a large strength of the class. More
students will create a noise and disturb the class discipline." , "These maps show
important natural and cultural features such as relief, vegetation, cultivated
lands, settlements and transportation etc." , "Road maps is one of the most widely
used maps. Road maps or route maps are basically used for an navigation purposes."
, "When we draw a map of an area or a place, there is always a ratio between the
actual size on the Earth and the size on the map. In geography this ratio is
called scale." , "Linear scale, Statement Scale, Fractional Or Ratio Scale" ,
"Census is a count of the population of a country. During census, information
about people living in a country is collected." , "Goods is something that anyone
needs or wants." , "Service is an activity that someone does for us." , "The
government provides different facilities to its citizens. For example, schools,
hospitals, roads, post offices, public parks and banks etc. These are called
public goods." , "Private goods are possessed by the people. For example, House,
food, clothes, car, motor cycle etc."};
if (user_ans == A[i]) {
cout << "Correct Answer!" << endl << endl;
correct_answers++;
total_marks += 1;
cout << "Marks till now: " << total_marks << endl;
} else {
cout << "Wrong Answer!" << endl << endl;
wrong_answers++;
cout << "Do you want to see the correct answer? (Yes/No): ";
char see_answer_option[4];
cin.getline(see_answer_option, 4);
if (strcmp(see_answer_option, "Yes") == 0) {
cout << "Correct Answer: " << A[i] << endl;
}
cout << "Marks till now: " << total_marks << endl;
}
}
6
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
// Display results
cout << "\n=== RESULTS ===" << endl;
cout << "Total Correct Answers: " << correct_answers << endl;
cout << "Total Wrong Answers: " << wrong_answers << endl;
cout << "Total Marks: " << total_marks << endl;
int main() {
char name[30], father_name[30], gender[10], grade[2], std_id[10];
int age;
float percentage;
// Display header
cout << "=============== PAKISTAN STUDIES EXAM ===============" << endl <<
endl;
cout << "100 QUESTIONS OF PST (CLASS 5)" << endl;
cout << "=============== STUDENT'S BIO DATA ===============" << endl << endl;
// Check eligibility
if (checkEligibility(percentage)) {
// Ask if ready for the test
if (askReadyForTest()) {
cout << "Start the Test Please" << endl << endl << endl;
conductTest();
} else {
cout << "Invalid option. Test aborted." << endl;
}
}
return 0;
}
7
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
OUTPUT:
8
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
9
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
10
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
11
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
12
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
13
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
Q2. Briefly describe all given topics in term of bullets, write and implement code of each topic
using C++ high level language in IDE with every single line comment.
a) Function
1. Paremeter pass by value
• Makes a copy of actual parameter
• Passes copy of contents
• Original variable’s content DO NOT change
• How? Do nothing. Default
• Safer
SOURCE CODE:
#include<iostream>
using namespace std;
void swapThemByVal(int num1, int num2) {
int temp = num1;
14
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
num1 = num2;
num2 = temp;
}
int main() {
int i = 10, j = 20;
swapThemByVal(i, j);
cout<<endl<<endl<<endl;
cout << i << " " << j << endl; // displays 10 20
cout<<endl<<endl<<endl;
return 0;
}
OUTPUT:
#include<iostream>
using namespace std;
void swapThemByRef(int& num1, int& num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
int main() {
int i = 30, j = 50;
swapThemByRef(i, j);
cout<<endl<<endl<<endl;
cout << i << " " << j << endl; // displays 20 10
cout<<endl<<endl<<endl;
return 0;
}
OUTPUT:
3. Returning Value
15
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
By definition, a value-returning function returns a single value; this value is returned via the return
statement. Use void functions with reference parameters to return (modify) more than one value.
SOURCE CODE:
#include<iostream>
using namespace std;
int myFunction(int x, int y) {
return x + y;
}
int main() {
int z = myFunction(5, 3);
cout<<endl<<endl<<endl;
cout << z;
cout<<endl<<endl<<endl;
return 0;
}
OUTPUT:
4. Function Overloading
Same name of function but there is a difference in:
• No. of arguments, Types Of Arguments
SOURCE CODE:
#include <iostream>
using namespace std;
void add(int a, int b, double c)
{
cout<<"Addition: "<<a+b+c<<endl;
}
void add(char x, double y, int z)
{
cout<<"Addition: "<<x+y+z<<endl;
}
void add(double m, int n, char o)
{
cout<<"Addition: "<<m+n+o<<endl;
}
int main(){
add(3, 'X', 5.5);
add('Y', 8.5, 4);
add(6.4, 8, 'Z');
return 0;
16
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
}
OUTPUT:
b) String
1. Cin.getline.
In C++, the cin object also allows input from the user, but not multi-word or multi-line input.
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
char input[50];
cout << "Enter a line of text: ";
cin.getline(input, 50);
cout<<"You entered: "<<input<<endl;
return 0;
}
OUTPUT:
2. Cin.get.
The cin. get() in C++ is used for accessing the character array from the user that the user provides as
the input.
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
ch = cin.get();
cout << "You entered: " << ch << endl;
return 0;
}
RESULT:
3. Gets.
The gets() function reads characters from stdin and stores them in str until a newline character or end of
file is found.
SOURCE CODE:
17
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
#include <iostream>
using namespace std;
int main() {
char input1[50];
char input2[50];
cout << "Enter Number 1: ";
gets(input1);
cout << "Enter Number 2: ";
gets(input2);
int num1 = atoi(input1);
int num2 = atoi(input2);
cout<<"Addition Of "<<num1<<" and "<<num2<<" is: "<<num1+num2<<endl;
return 0;
}
OUTPUT:
4. Puts.
The puts() function in C/C++ is used to write a line or string to the output( stdout ) stream
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
char input1[50];
char input2[50];
cout << "Enter Number 1: ";
gets(input1);
cout << "Enter Number 2: ";
gets(input2);
// int num1 = atoi(input1);
// int num2 = atoi(input2);
cout<<"Number 1 you entered is"<<endl;
puts(input1);
cout<<"Number 2 you entered is"<<endl;
puts(input2);
return 0;
}
RESULT:
5. Strcat.
The strcat() function concatenates string2 to string1 and ends the resulting string with the null character.
18
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
SOURCE CODE:
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char dest[50] = "Hello, Welcome to";
char src[50] = " Dark Web";
strcat(dest, src);
cout << dest ;
return 0;
}
OUTPUT:
6. Strncmp.
strncmp() function in C++ lexicographically compares not more than count characters from the two null-
terminated strings and returns an integer based on the outcome.
SOURCE CODE:
#include <iostream>
#include <cstring>
using namespace std;
int main (){
char str1[20] = "Hello World!";
char str2[20] = "Hello";
int retval = strncmp(str1, str2, 5);
if(retval != 0)
cout<<"First 5 characters of str1 and str2 are not equal."<<endl;
else
cout<<"First 5 characters of str1 and str2 are equal"<<endl;
cout<<"Value returned by the function: "<<retval <<endl;
return 0;
}
OUTPUT:
7. Strcpy.
strcpy() is a standard library function in C++ and is used to copy one string to another.
SOURCE CODE:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char src[] = "Welcome to Sindh Madressat-ul-Islam University.";
char dest[20];
strcpy(dest,src);
19
FACULTY OF ENGINEERING SCEINCE AND TECHNOLOGY
Sindh Madressatul Islam University (SMIU) University Karachi
8. Strlen.
The strlen() function determines the length of string excluding the ending null character.
SOURCE CODE:
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char song[] = "Be dili kiya yunhi din guzar jayein ge, sirf zinda rhey ham to mar
jayein ge";
cout << strlen(song);
return 0;
}
OUTPUT:
9. Strrev.
The strrev() function is a built-in function in C and is defined in string. h header file. The strrev()
function is used to reverse the given string.
SOURCE CODE:
#include <algorithm>
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "Yasir Khanzada";
reverse(str.begin(), str.end());
cout<<"\n"<<str;
return 0;
}
OUTPUT:
20