You are on page 1of 5

INSTITUTE OF SPACE TECHNOLOGY

ISLAMABAD

Programming Language Lab


Electrical Engineering Department
Batch-19-Section-B
Lab Report #3

Submitted To:
Sir Adnan Zafar
Submitted By:
Muhammad Saad Javed
Registration No:
200401013
Task No 1: Array Sorting
The program must take input of the array from the user (you may define any length for the array) in the
main function; the entered array is then passed to an array sorting function “Array Sort ()”. You must
define the function “Array Sort ()” using bubble sorting algorithm to sort the array in ascending or
descending order (Reg no -> Even: Ascending Order & Reg no -> Odd: Descending Order). The sorted
array is then returned to the main function where it is then passed to a print function “Print Array ()”. The
printing of the sorted array should be through the “Print Array ()” function.

Pseudocode:
Step 1: Begin
Step 2: Procedure bubble sort (List array, number length of array)
Step 3: for i=1 to length of array - 1
Step 4: for j=1 to length of array – I
Step 5: if array [j] > array [ j+1] then
Step 6: temporary = array [j+1]
Step 7: array[j+1] = array [i]
Step 8: array [j] = temporary
Step 9: end if
Step 10: end of j loop
Step 11: end of i loop
Step 12: return array
Step 13: End

Code:
//M Saad Javed//
//Reg No.200401013//
#include<iostream>
using namespace std;
void swap(float* xp, float* yp)
{
float temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function for bubble sort
void bubbleSort(float arr[], float n)
{
int i, j;
for (i = 0; i < n - 1; i++)
// Last i elements are already in place
for (j = 0; j < n - i - 1; j++)
if (arr[j] < arr[j + 1]) //for descending order
swap(&arr[j], &arr[j + 1]); //swaping values
}
// Function to print an array //
void printArray(float arr[], float size)
{
int i;
for (i = 0; i < size; i++) //for printing values of array
cout << arr[i] << " "; //also printing space
cout << endl;
}
int main()
{
float arr[5]; //Declaring array of size 5
cout << "Enter GPA of five students : " << endl; //asking user for GPA
for (int i = 0; i < 5; i++) //for loop for entering 5 values
{
cin >> arr[i]; //storing values in array
}
int n = sizeof(arr) / sizeof(arr[0]); //determining size of array even though size is
given but for bubble sort function
bubbleSort(arr, n); //passing bubble sorting
cout << "Sorted array: \n"; //printing sorted array
printArray(arr, n); //passing print function
return 0;
}

Output:

Link:
https://repl.it/@SaadJaved/Task-No-1-Lab-3
Task No 2: Array Processing
Write a C++ Program that can store GPAs of ‘5’ students in an array and then ask the user to
choose from the following three (3) options. The details of the options are as follows:
Option 1: GPAs in Ascending / Descending Order
The array will be passed to a sorting function “Array Sort ()”. This function will sort the
GPAs in ascending or descending order (Reg no -> Even Ascending Order & Reg no ->
Odd Descending Order) using bubble sorting and return the sorted array to the main
function. The sorted array will be printed in the main function.
Option 2: Count the number of GPAs greater than a particular GPA
The array will be passed to a function “Count GPA ()”. This function will ask the user to
enter a GPA value and then the function will count the GPAs in the array which are
greater than the entered value and return the count value to the main function. The
count value will be printed in the main function.
Option 3: Minimum and Maximum GPAs
The array will be passed to a function “Min Max ()”. This function will find the minimum
and maximum GPA in the array and return the values to the main function. The min &
max values will be printed in the main function.

Pseudocode:
Step 1: Begin
Step 2: Using bubble sorting function to find the descending order.
Step 3: For loop starts from zero to one minus of size
Step 4: Equating and transferring value of array to find highest value
Step 5: Using count GPA to find the number of gpa greater than your gpa
Step 6: Using while loop for checking and using if statement
Step 7: Using min max function to find maximum and minimum value.
Step 8: Passing these functions in main
Step 9: Using switch statement
Step 10: In case 1 decreasing order of gpa will be output
Step 11: In case 2 number of gpa that are higher than yours will be output
Step 12: In case 3 minimum and maximum gpa values will be output
Step 13: End
Output:

Link:
https://repl.it/@SaadJaved/Task-No-2-Lab-3

You might also like