INSTITUTE OF SPACE TECHNOLOGY
ISLAMABAD
Introduction to Information Technology
Electrical Engineering Department
LAB Report 11
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";
for (int c = 0; c < n; c++)
cin >> arr1[c];//taking elements first array
cout << endl << "Enter elements of second array " << "\n";
for (int c = 0; c < n; c++)
cin >> arr2[c];//taking elements second array
cout << endl << "Sum of elements of the arrays " << "\n";
for (int c = 0; c < n; c++)
{
sum[c] = arr1[c] + arr2[c];//adding two rows and storing in third one.
cout << sum[c] << endl;
}
return 0;//program ended successfully.
}
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
Task 4: Write a program to keep track of students grades by a 3-by-4 array
studentGrades is used (table), where each Row of array represents a student
and each column represent a grade on one of four exams. An example of that
kind of table is given below for 2-by-2 array.
#include <iostream>
#include <iomanip>//library to get tabular form
using namespace std;
int main()
{
cout << "The array is:" << endl;
int st1[4] = { 77,68,86,73 };//marks of 1st student
int st2[4] = { 96,87,89,78 };//marks of 2nd student
int st3[4] = { 70,90,86,81 };//marks of 3rd student
//setw command for spacing of 20 bytes
cout << setw(20);
for (int i = 0; i < 4; i++) //using for loop
{
cout << "[" << i << "]" << setw(4);
}
cout << endl << "Student Grades[0] ";//displaying gardes of 1st student
for (int i = 0; i < 4; i++) //again using for loop
{
cout << st1[i] << setw(6);
}
cout << endl << "Student Grades[1] ";//displaying grades if 2nd student
for (int i = 0; i < 4; i++) //again using for loop
{
cout << st2[i] << setw(6);
}
cout << endl << "Student Grades[2] ";//displaying grades of 3rd student
for (int i = 0; i < 4; i++) //again using for loop
{
cout << st2[i] << setw(6);
}
cout << endl;
//for finding out minimum grade, overall
int min1 = st1[0];
for (int i = 0; i < 4; i++) //again using for loop
{
if (min1 > st1[i]) //using if conditon
min1 = st1[i];
}
int min2 = st2[0];
for (int i = 0; i < 4; i++) //using for loop
{
if (min2 > st2[i]) //again using if conditon
min2 = st2[i];
}
int min3 = st3[0];
for (int i = 0; i < 4; i++) //again using for loop
{
if (min3 > st3[i]) //again using if conditon
min3 = st3[i];
}
if (min1 < min2 && min1 < min3) //again using if conditon
cout << "Lowest Grade: " << min1 << endl;
else if (min2 < min1 && min2 < min3)
cout << "Lowest Grade: " << min2 << endl; //using else if
else
cout << "Lowest Grade: " << min3 << endl; //using else
//for finding out maximum grade, overall
int max1 = st1[0];
for (int i = 0; i < 4; i++) //again using for loop
{
if (max1 < st1[i]) //again using if conditon
max1 = st1[i];
}
int max2 = st2[0];
for (int i = 0; i < 4; i++) //again using for loop
{
if (max2 < st2[i]) //again using if conditon
max2 = st2[i];
}
int max3 = st3[0];
for (int i = 0; i < 4; i++) //again using for loop
{
if (max3 < st3[i]) //again using if conditon
max3 = st3[i];
}
//displaying highest grade
if (max1 > max2 && max1 > max3)
cout << "Highest Grade: " << max1 << endl; //again using if conditon
else if (max2 > max1 && max2 > max3)
cout << "Highest Grade: " << max2 << endl; //again using else if
else
cout << "Highest Grade: " << max3 << endl; //again using else
//for finding out average grades
float avg1, avg2, avg3, sum1 = 0, sum2 = 0, sum3 = 0;
for (int i = 0; i < 4; i++) //again using for loop
{
sum1 += st1[i];
}
avg1 = sum1 / 4;
cout << "The average grade of student [0] is " << avg1 << endl;
for (int i = 0; i < 4; i++) //again using for loop
{
sum2 += st2[i];
}
avg2 = sum2 / 4;
cout << "The average grade of student [1] is " << avg2 << endl;
for (int i = 0; i < 4; i++) //again using for loop
{
sum3 += st3[i];
}
avg3 = sum3 / 4;
cout << "The average grade of student [2] is " << avg3 << endl;
return 0; //function returned succesfully
} //end main.
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