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

PF Assignment #1

Uploaded by

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

PF Assignment #1

Uploaded by

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

|Programming Fundamentals Assignment #1|

Name: Muaz
Roll No.: 043
Class: BSCS-7A

|Submitted To: Sir Abu-Bakar|


ASSIGNMENT #1

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>

using namespace std;

int main()
{
int size;
cout << "Enter the size of array: ";
cin >> size;
int arr[size];
int sum = 0;

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


{
cout << "Enter " << i << " element: ";
cin >> arr[i];
sum += arr[i];
}

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>

using namespace std;

int main()
{
int size;
cout << "Enter the size of array: ";
cin >> size;
int arr[size];
int max = 0;

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


{
cout << "Enter " << i << " element: ";
cin >> arr[i];

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>

using namespace std;

int main()
{
int size,temp;
cout << "Enter the size of array: ";
cin >> size;
int arr[size];

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


{
cout << "Enter " << i << " element: ";
cin >> arr[i];
}

for(int l=0 ; l<size-1 ; l++)


{
for(int k = 0 ; k<size-l-1 ; k++)
if(arr[k]>arr[k+1])
{
temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
}
}

cout << "The sorted array is: ";

for (int j=0 ; j<size ; j++)


{
cout << arr[j] << " ";
}

return 0;
}
Output:

Task #4: Write a C++ program to reverse the elements of an array of characters (a string)

Code:
#include <iostream>

using namespace std;

int main()
{
char string[100];
cout << "Enter a string: ";
cin >> string;

int length = 0;

while (string[length] != '\0')


{
length++;
}

for (int i = length-1 ; i >= 0 ; i--)


{
cout << string[i];
}

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>

using namespace std;

int main()
{
int size,ind=0;

cout << "Enter the size of array: ";


cin >> size;
int arr[size];

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


{
cout << "Enter " << i << " element: ";
cin >> arr[i];
}

for(int j=0 ; j<size ; j++)


{
int flag = 0;

for(int k = 0 ; k<j ; k++)


{
if(arr[j] == arr[k])
{
flag = 1;
break;
}
}

if(flag == 0)
{
arr[ind] = arr[j];
ind++;
}
}

cout << "The array after removing duplicates is: ";

for (int l=0 ; l<ind ; l++)


{
cout << arr[l] << " ";
}

return 0;
}

Output:
Task #8: Write a C++ program to find the second largest element in an array of integers.

Code:
#include <iostream>

using namespace std;

int main()
{
int size;

cout << "Enter the size of array: ";


cin >> size;
int arr[size];

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


{
cout << "Enter " << i << " element: ";
cin >> arr[i];
}

int max = arr[0];


int smax = INT_MIN;

for(int j=0 ; j<size ; j++)


{
if(arr[j] > max)
{
smax = max;
max = arr[j];
}
else if(arr[j] > smax && arr[j] != max)
{
smax = arr[j];
}
}

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>

using namespace std;

int main()
{
int day,month,year,total;
int m[]={31,28,31,30,31,30,31,31,30,31,30,31};

cout << "Enter day: ";


cin >> day;

cout << "Enter month: ";


cin >> month;

cout << "Enter year: ";


cin >> year;

for (int i = 0 ; i < month - 1 ; i++)


{
total += m[i];
}

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>

using namespace std;

int main()
{
int roll[5];
int marks[5];

for (int i=0 ; i<5 ; i++)


{
cout << "Enter roll no. of student " << i+1 << ": ";
cin >> roll[i];

cout << "Enter marks of student " << i+1 << ": ";
cin >> marks[i];
}

int max=0;

for (int j=1 ; j<5 ; j++)


{
if(marks[j] > marks[max])
{
max = j;
}
}

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>

using namespace std;

int main()
{
int num[10];
int sq[10];
int cb[10];
int sum[10];

for(int i=0 ; i<10 ; i++)


{
num[i] = i;
sq[i] = i*i;
cb[i] = i*i*i;
sum[i] = num[i] + sq[i] + cb[i];
}

cout << "Elements in sum array are: ";

for(int j=0 ; j<10 ; j++)


{
cout << sum[j] << " ";
}

int total = 0;

for(int k=0 ; k<10 ; k++)


{
total += sum[k];
}

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>

using namespace std;

int main()
{
int numE;

cout << "Enter number of employees: ";


cin >> 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:

 3 is stored 4 times in the array.


 1 is stored 1 times in the array.

Code:
#include <iostream>

using namespace std;

int main()
{
int arr[10];
int occ[10] = {0};

cout << "Enter 10 integers: ";

for(int i=0 ; i<10 ; i++)


{
cin >> arr[i];
occ[arr[i]]++;
}
for(int j=0 ; j<10 ; j++)
{
if(occ[j]>0)
{
cout << j << " is stored " << occ[j] << " times in the array." << endl;
}
}

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>

using namespace std;

int main()
{
int marks[10];
int grade[4]={0};

cout << "Enter marks of 10 students: " << endl;

for (int i=0 ; i<10 ; i++)


{
cin >> marks[i];

if(marks[i] >= 80)


{
grade[0]++;
}
else if(marks[i] >= 60)
{
grade[1]++;
}
else if(marks[i] >= 40)
{
grade[2]++;
}
else
{
grade[3]++;
}
}

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>

using namespace std;

int main()
{
int a,b,c,d;

cout << "Enter number of customers: ";


cin >> a;

int mango[a],orange[a],banana[a],bill[a];

for(b=0 ; b<a ; b++)


{
bill[b]=0;
cout << "\nCustomer #" << b+1 << endl << "Enter number of mangoes to buy: ";
cin >> mango[b];
bill[b]+=mango[b]*20;
cout << "Enter number of oranges to buy: ";
cin >> orange[b];
bill[b]+=mango[b]*10;
cout << "Enter number of bananas to buy: ";
cin >> banana[b];
bill[b]+=mango[b]*5;
}

cout << endl << "Customer#\tMango\tOrange\tBanana\tTotal";


cout <<
"\n___________________________________________________________________\n";
for(c=0 ; c<a ; c++)
{
cout << c+1 << "\t\t" << mango[c] << "\t\t" << orange[c] << "\t\t" << banana[c]
<< "\t\t" << bill[c] << endl;
}
return 0;
}
Output:

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>

using namespace std;

int main()
{
int arr[10];
int sum = 0;
float avg;

cout << "Enter 10 numbers: " << endl;


for (int i=0 ; i<10 ; i++)
{
cin >> arr[i];
sum+=arr[i];
}

avg = sum/10.0;

cout << "The numbers greater than the average of all numbers are:\n";

for(int j=0 ; j<10 ; j++)


{
if(arr[j]>avg)
{
cout << arr[j] << endl;
}
}

return 0;
}

Output:

You might also like