Você está na página 1de 13

PRELAB 01

Chart explaining the implementation of the standard deviation algorithm.

STAR
T
Including header files

Function
Prototypes

Main() function
An empty <vector> with name data is declared

POST LAB 01:

Input from user i.e size of <vector>

Initiate for() loop and take input from user in each iteration

The code is for sequential


search.
a vector of 10
elements is made
and add it in
vector Firstly,
using .push_back()
function
ranging from 1 to 10 using functions. Then the user is asked to enter the
Call
function
pass data
as argument
value which is
tostandarddev()
be searched. Then
the and
function
for searching
the value the
is called in the Print
mainstandard
function deviation
which uses
a for loop
evaluated
byfor
therunning
functionall the
elements, and within that loop, there is an if statement which compares the
required value with the elements of vector, starting from zeroth position to
the position of the required value. There is also a defensive condition which
prints Value not found in case if the value entered by the user does not
exist in the vector.
Then, an algorithm find is called in the main function. This algorithm
requires a vector and the value which
is to be searched. In the code, after
END
printing the position of the first value entered by the user, the program then
asks for another value. Now it uses the find algorithm to search the value.
The iterator is previously
declared as location. The initial
pointer is
double standarddev(vector<double>
input)
subtracted from it to get the desired position.
function definition
average() function is called and vector is passed as
argument to it and store return value in variable avg . After
that for() loop is initiated from 0 to input.size() , in each
iteration the term factor is found by formula

(input [i]avg)(input [i]avg)


which is added to variable summation which is initialize to
zero in beginning. Then standard deviation is found by
taking square
root of summation/input.size()
which is return to the main() function

double average(vector<double> input)


function definition
initiate for() loop from 0 to input.size() which is the size
of vector
in each iteration of for() loop add that place element in
vector to the variable val of type double which is initialize
to 0 in beginning
return average which is val/data.size()

Prelab 03
In this code, a structure is made of the name student. The structure
contains the following information about the student:
Name of data type string
Department of data type string
Roll no. of data type int
Cgpa of data type float
Then the main function is there to input data to the structure. Firstly, the two
variables are declared of the data type student. The information of two
students is printed on the screen with the help of this structure, by using
statement out(). At last, the size of the variable, ali, is printed by usin the
statement sizeof().

POSTLAB 03
#include <iostream>

using namespace std;

struct batsman
{
int position;
string name;
string team;
float bat_avg;
int rating;
};

void input_func(batsman arr[], const int arr_size);


//prototypes
void modifying_func(batsman arr[], const int arr_size);
void display_top_10(batsman arr[]);
void display_specific(batsman arr[], const int arr_size);
void delete_func(batsman arr[], const int arr_size);

int main()
{
batsman top[30];
top[0] = {1 ,"A.B.de Villiers","South Africa",54.21,900};

top[1] ={2,"V.Kohli","India",51.82,804};
top[2] = {3,"H.M. AMla","South Africa",52.70,776};

top[3]={4,"K.S. Williamson","Newzeland",47.86,770};

top[4] = {5,"T.M. Dilshan","Srilanka",39.44,760};

top[5]= {6,"M.S. Dhoni","India",51.35,741};

top[6] = {7,"S Dhawan","India",43.47,730};

top[7] = {8,"M.J. Guptill","Newzeland",42.71,722};

top[8]= {9,"L.R.P.L. Taylor","Newzeland",43.90,720};

top[9] = {10,"G.J. Maxwell","Australia",36.25,717};

top[10] ={ 11,"F. du Plessis","South Africa",39.65,715};

top[11]= {12,"Q. de Kock","South Africa",39.86,715};

top[12]= {13,"R.G. Sharma","India",41.60,702};

top[13]= {14,"A.J. Finch","Australia",39.09,696};

top[14] = {15,"G.J. Bailey","Australia",43.87,693};

int password, check2, check3, check4;


char check;

cout << "In order to display top 10 batsmen press a" <<
endl << endl;

cout << "In order to search a particular batsman press b"


<< endl << endl;
cout << "In order to enter in admin mode press c" << endl
<< endl;
cout << "Wrong input at any instant will end the program."
<< endl << endl;

cin >> check;


if (check == 'a')
{
display_top_10(top);
}

else if (check == 'b')


{
cout << endl;
display_specific(top, 30);
}

else if (check == 'c')


{
cout << "\n\n********* Welcome to Admin mode
********" << endl << endl;
cout << "Please enter password: ";
cin >> password;
if (password == 264)
{

cout << endl << endl;


cout << "In order to input element press 3"
<< endl << endl;
cout << "In order to modify an array element
press 4" << endl << endl;
cout << "In order to delete an array element
press 5" << endl << endl;

cin >> check2;


cout << endl << endl;
if (check2 == 3)
{

cout << endl;


input_func(top, 30);
cout << "To display edited
list press 9" << endl << endl;

cin >> check4;


if (check4 == 9)
{
cout << endl;
display_top_10(top);
}
else
{

cout << "Invalid input"


<< endl << endl;
return 6;
}
}

else if (check2 == 4)
{
cout << endl;
modifying_func(top, 30);
cout << "To display edited list press
9" << endl << endl;

cin >> check3;


if (check3 == 9)
{
cout << endl;
display_top_10(top);
}
else
{
cout << "Invalid input"
<< endl << endl;
return 6;
}
}
else if (check2 == 5)
{

cout << endl;


delete_func(top, 30);
cout << "To display edited list press
9" << endl << endl;

cin >> check3;


if (check3 == 9)
{
cout << endl;

display_top_10(top);
}

else
{
cout << "Invalid input"
<< endl << endl;
return 6;
}
}

}
else
{
cout << "\nIncorrect password" << endl;
return 0;

}
}
}

void modifying_func(batsman arr[], const int arr_size)


{
int search, i;
cout << "Enter the position you want to modify: ";
cin >> search;
for (i = 0; i < arr_size; ++i)
{
if (i == search)
{
cout << "\nEnter the batsman position: ";
cin >> arr[i-1].position;
cout << "Enter the batsman name: ";
cin >> arr[i-1].name;
cout << "Enter the batsman team: ";
cin >> arr[i-1].team;
cout << "Enter the batsman batting average: ";
cin >> arr[i-1].bat_avg;
cout << "Enter the batsman rating: ";
cin >> arr[i-1].rating;
}

void display_top_10(batsman arr[])


{
cout << "********** Displaying the top 10 batsmen
**********" << endl << endl;
int i;
for (i = 0; i < 10; ++i)
{
cout << "Position: " << arr[i].position << endl;
cout << "Batsman name: " << arr[i].name << endl;
cout << "Batsman team: " << arr[i].team << endl;
cout << "Batsman batting average: " <<
arr[i].bat_avg << endl;
cout << "Batsman rating: " << arr[i].rating << endl
<< endl;
}
}

void display_specific(batsman arr[], const int arr_size)


{
int i, search;
cout << "Enter the position you want to search: ";
cin >> search;
for (i = 0; i < arr_size; ++i)
{
if (i == search)
{

cout << "\nPosition: " << arr[i-1].position <<


endl;
cout << "Batsman name: " << arr[i-1].name << endl;
cout << "Batsman team: " << arr[i-1].team << endl;
cout << "Batsman batting average:" << arr[i1].bat_avg << endl;
cout << "Batsman rating: " << arr[i-1].rating <<
endl << endl;
}
}
}

void delete_func(batsman arr[], const int arr_size)


{
int del, i, maxi;
maxi = arr_size - 1;
cout << "Enter the position number you want to delete: ";
cin >> del;
for (i = (del-1); i < maxi; ++i)
{
arr[i] = arr[i + 1];
}

void input_func(batsman arr[], const int arr_size)


{
int arr_position;
cout << "Enter the number you want to input information
to: ";

cin >> arr_position;


cout << "\nEnter the batsman position: ";
cin >> arr[arr_position-1].position;
cout << "Enter the batsman name: ";
cin

>> arr[arr_position-1].name;

cout << "Enter the batsman team: ";


cin >> arr[arr_position-1].team;
cout << "Enter the batsman batting average: ";
cin >> arr[arr_position-1].bat_avg;
cout << "Enter the batsman rating: ";
cin >> arr[arr_position-1].rating;
cout << endl << endl;
}

Você também pode gostar