PF Assignment #1
PF Assignment #1
Name: Muaz
Roll No.: 043
Class: BSCS-7A
Task #1: Write a C++ program that takes an array of integers as input and finds the sum of all
the elements in the array. Additionally, the program should allow the user to specify the size of
the array and input the elements dynamically.
Code:
#include <iostream>
int main()
{
int size;
cout << "Enter the size of array: ";
cin >> size;
int arr[size];
int sum = 0;
cout << "The sum of your entered elements is: " << sum;
return 0;
}
Output:
Task #2: Write a C++ program to find the maximum element in an array of integers.
Code:
#include <iostream>
int main()
{
int size;
cout << "Enter the size of array: ";
cin >> size;
int arr[size];
int max = 0;
if(max<arr[i])
{
max=arr[i];
}
}
cout << "The largest element you entered is: " << max;
return 0;
}
Output:
Task #3: Implement a C++ program to sort an array of integers in ascending order using the
bubble sort algorithm.
Code:
#include <iostream>
int main()
{
int size,temp;
cout << "Enter the size of array: ";
cin >> size;
int arr[size];
return 0;
}
Output:
Task #4: Write a C++ program to reverse the elements of an array of characters (a string)
Code:
#include <iostream>
int main()
{
char string[100];
cout << "Enter a string: ";
cin >> string;
int length = 0;
return 0;
}
Output:
Task #6: Implement a C++ program to remove duplicates from an array of integers without using
any extra space.
Code:
#include <iostream>
int main()
{
int size,ind=0;
if(flag == 0)
{
arr[ind] = arr[j];
ind++;
}
}
return 0;
}
Output:
Task #8: Write a C++ program to find the second largest element in an array of integers.
Code:
#include <iostream>
int main()
{
int size;
if(smax == INT_MIN)
{
cout << "No second largest element found!";
}
else
{
cout << "The second largest element is: " << smax;
}
return 0;
}
Output:
Task #9: Write a C++ program that takes input from the user in the form of a date (day, month,
year) and calculates the number of days from the beginning of current year to the specified date.
Note that the program does not account for leap years.
Code:
#include <iostream>
int main()
{
int day,month,year,total;
int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
total += day;
cout << "Total number of days from given date to till date are: " << total;
return 0;
}
Output:
Task #10: Write a program that uses two arrays to store the roll number and marks of student.
It inputs roll numbers and marks of five students and stores them in corresponding elements of the
arrays. (For example, if roll number 1 is stored in rno[0] then his marks must be stored in marks[0]
and so on.) The program finally displays the roll number and marks of the student with highest
marks.
Code:
#include <iostream>
int main()
{
int roll[5];
int marks[5];
cout << "Enter marks of student " << i+1 << ": ";
cin >> marks[i];
}
int max=0;
cout << "Roll no. " << roll[max] << " has the highest marks of: " << marks[max];
return 0;
}
Output:
Task #11 Write a program that four arrays numbers, squares, cubes and sums each consisting of
10 elements. The numbers array stores the values of its indexes, the squares array stores the squares
of its indexes, the cubes array stores the cubes of its indexes and sums array stores the sum of
corresponding indexes of three arrays. The program should displays the values of sums array and
the total of all values in sums array.
Code:
#include <iostream>
int main()
{
int num[10];
int sq[10];
int cb[10];
int sum[10];
int total = 0;
cout << endl << "Sum of all elements in sum array is: " << total;
return 0;
}
Output:
Task #12: Write a program that inputs the names and monthly salaries of 10 employees. The
program checks annual salary of each person. If annual salary is greater than or equal to Rs
2,50000/- then it prints name, salary and a message 'Tax to be paid' else it prints name, salary and
a message 'No tax'.
Code:
#include <iostream>
int main()
{
int numE;
string names[numE];
int salary[numE];
for(int i=0 ; i<numE ; i++)
{
cout << "Enter name of employee " << i+1 << ": ";
cin >> names[i];
cout << "Enter annual salary of employee " << i+1 << ": ";
cin >> salary[i];
}
cout << endl;
for(int j=0 ; j<numE ; j++)
{
cout << "Name: " << names[j] << endl;
cout << "Annual Salary: " << salary[j] << endl;
if(salary[j]>=250000)
{
cout << "Note: Tax has to be paid!\n\n";
}
else
{
cout << "Note : No tax has to be paid.\n\n";
}
}
return 0;
}
Output:
Task #13: Write a program that inputs ten integers in an array. It displays the number of
occurrences of each number in the array as follows:
Code:
#include <iostream>
int main()
{
int arr[10];
int occ[10] = {0};
return 0;
}
Output:
Task #14: Write a program that inputs marks of ten students. The program displays the number
of students in each grade. The criteria is as follows:
80 or above A
60 to 79 B
40 to 59 C
Below 40 F
Code:
#include <iostream>
int main()
{
int marks[10];
int grade[4]={0};
cout << "Number of students in each grade are: " << endl;
cout << "A: " << grade[0] << endl;
cout << "B: " << grade[1] << endl;
cout << "C: " << grade[2] << endl;
cout << "F: " << grade[3] << endl;
return 0;
}
Output:
Task #15: Write a program that uses three arrays Mango, Orange and Banana to store the
number of fruits purchased by customer. The program inputs the number of mangos, oranges and
bananas to be puchased by customer and stores them in corresponding arrays. The program
finally displays the total bill of each customer according to the following prices:
Rs. 20 per mango
Rs. 10 per orange
Rs. 5 per banana
Code:
#include <iostream>
int main()
{
int a,b,c,d;
int mango[a],orange[a],banana[a],bill[a];
Task #16: Write a program that inputs ten floating point numbers in an array. It displays the
values which are greater than the average value of the array.
Code:
#include <iostream>
int main()
{
int arr[10];
int sum = 0;
float avg;
avg = sum/10.0;
cout << "The numbers greater than the average of all numbers are:\n";
return 0;
}
Output: