Lab TASK 11
Lab TASK 11
ISLAMABAD
Submitted To:
Haroon Ibrahim
Submitted By:
Noor ul Hassan
Registration No.:
21040178
Task 1: Write a program to find the maximum value in the array.
#include <iostream>
using namespace std;
int main()
{
int j, num = 6; //initiating variables
float array[6]{}; //initiating array on float
for (j = 0; j < 6; j++) // using for loop
{
cout << " Enter Element " << j + 1 << " "; // displat enter element
cin >> array[j]; //user input
}
for (j = 1; j < num; ++j) // again for loop
{
if (array[0] < array[j]) //using if condition
array[0] = array[j]; //for second line also
}
cout << endl << "Largest number is = " << array[0] << endl; //display largest number
return 0;
}
Algorithm:
Step 1: Start
Step 2: Declare variables
Step 3: Use if conditions and for loops
Step 4: Display answer
Task 2: Write a program to input data into two different arrays and then to
add the two arrays and store the result in a third array.
#include <iostream>
using namespace std;
int main()
{
int n;//assigning int type variable for no. of elements in array
cout << "Enter the number of elements in the array " << endl;//prompt for input
cin >> n;//input number of elements
int arr1[6], arr2[6], sum[6];//assigning three arrays for 'n' no. of elements.
cout << endl << "Enter elements of first array " << "\n";
Algorithm:
Step 1: Start
Step 2: Declare variables
Step 3: Use if conditions and for loops
Step 4: display answer
Task 3: Write a program to sort the mentioned below example by using bubble
sorting method
4 19 1 13
#include<iostream>
using namespace std;
int main()
{
int temporarynum; // declaring temporary number
int array[4] = { 4,19,1,13 }; // declaring array of 4 elements
for (int i = 0; i < 4; i++) // using for loop
{
for (int j = i + 1; j < 4; j++) // again using for loop
{
if (array[i] > array[j]) // using if statement
{
temporarynum = array[i]; // temporary number equals array i
array[i] = array[j]; // array i equals array j
array[j] = temporarynum; // array equals temporary number
}
}
}
cout << "Sorted array is " << " "; // display sorted array
for (int i = 0; i < 4; i++) // again using for loop
{
cout << array[i] << " "; // display array i
}
cout << endl; //endline
return 0;
}
Algorithm:
Step 1: Start
Step 2: Declare variables
Step 3: Use if conditions and for loops
Step 4: display answers
if (min1 < min2 && min1 < min3) //again using if conditon
cout << "Lowest Grade: " << min1 << endl;
else
cout << "Lowest Grade: " << min3 << endl; //using else
else
cout << "Highest Grade: " << max3 << endl; //again using else
Algorithm:
Step 1: Start
Step 2: Declare variables
Step 3: Use if, else if and else conditions and for loops
Step 4: display answers
Task 5: Write a program that uses array response initialized with 99 responses
to a survey. 99 responses are represented by constant variable responseSize.
Each of the responses is a number from 1 to 9. Program computes: The mean
median and mode of the 99 value.
#include<iostream>
#include<iomanip>
using namespace std;
const int responseSize = 99; // declaring cnstant variable
void mean(int response[]) // void main
{
double sum = 0; // declaring double variable
for (int i = 0; i < responseSize; i++) // using for loop
{
sum += response[i]; // sum increment equals response
}
cout << "********" << endl << " Mean " << endl << "********" << endl; // display mean heading
cout << endl << "The mean is the average value of the data items. The mean is equal to the total of all
the data items divided by the" << endl << "number of data items(" << responseSize << ").The mean value
for this run is : " << sum << " / " << 99 << " = " << sum / 99 << endl << endl; // display mean
}
void sorted(int response[]) // declaring a function
{
bool reverse; // using bool variable
int temvar; // intitiating temporary variable
do // using do while loop
{
reverse = false; //reverse equals false
for (int i = 0; i < responseSize - 1; i++) // using for loop
{
if (response[i] > response[i + 1]) // using if condition
{
temvar = response[i]; //temporary variable equals response
response[i] = response[i + 1]; // response euqlas response increment
response[i + 1] = temvar; // response increment equals temporary variable
reverse = true; //reverse equals true
}
}
} while (reverse); //using while loop
cout << endl << "The sorted array is" << endl; // display sorted array
for (int i = 0; i < responseSize; i++) //using for loop
{
cout << response[i] << "\t"; // display repsonse
}
cout << endl << "The median is the middle number in a sorted, ascending or descending, list of numbers
and can be more" << endl << "descriptive of that data set than the average which is 49 " << endl; //
display median
}
void mode(int response[]) // again delcaring another function
{
int repeatednum[10] = { 0 }; //initializing repeated number
int index; // delcaring index
for (int i = 0; i < responseSize; i++) //using for loop
{
switch (response[i]) // using switch case
{
case (0): repeatednum[0]++; // again using switch case
break; // using break
case (1): repeatednum[1]++; // again using switch case
break; // using break
case (2): repeatednum[2]++; // again using switch case
break; // using break
case (3): repeatednum[3]++; // again using switch case
break; // using break
case (4): repeatednum[4]++; // again using switch case
break; // using break
case (5): repeatednum[5]++; // again using switch case
break; // using break
case (6): repeatednum[6]++; // again using switch case
break; // using break
case (7): repeatednum[7]++; // again using switch case
break; // using break
case (8): repeatednum[8]++; // again using switch case
break; // using break
case (9): repeatednum[9]++; // again using switch case
break; // using break
}
}
int maximum = repeatednum[0]; // initialzing maximum
for (int i = 0; i < 10; i++) // again using for loop
{
if (repeatednum[i] > maximum) // again using if condition
{
maximum = repeatednum[i]; // maximum equals repeated number
index = i; // index equals i
}
}
cout << endl << "Response" << setw(20) << "Frequency" << setw(20) << "Histogram" << endl; //
display response frequency and histogram
for (int i = 0; i < 10; i++) // again using for loop
{
int counter = 0; // declaring counter
cout << i << setw(22) << repeatednum[i] << setw(16); // display repeated num
while (counter < repeatednum[i])
{
cout << "*"; // display star for counter
counter++; // counter increment
}
cout << endl; // endline
}
cout << endl << "Mode is the most repeating value" << endl << "For these responses mode is " <<
index << " which occured " << repeatednum[index] << " times." << endl; // display mode
}
int main() // initializing main
{
int response[responseSize]; // declaring for loop
for (int i = 0; i < responseSize; i++) // again using for loop
{
response[i] = rand() % 10; // respone equals random number
}
mean(response); // mean function for response
cout << "********* " << endl << " Median " << endl << "********* " << endl; // display median heading
cout << endl << "The unsorted array is " << endl; // display unsorted array
for (int i = 0; i < responseSize; i++) // again using for loop
{
cout << response[i] << "\t"; // display response
}
sorted(response); // sorted function for response
cout << endl << "******" << endl << " Mode" << endl << "******" << endl; // display mode heading
mode(response); // mode function for response
return(0);
}
Algorithm:
Step 1: Start
Step 2: Declare variables
Step 3: Declare functions
Step 4: Use if conditions and for loops
Step 5: Use switch and break cases
Step 6: Display answers