c++,just to do the flowcharts

profilemanksa
lab9.docx

Lab # 9

Q.6

Q.7

//Header section

#include <iost ream>

#include <string>

using namespace std;

//Function prototype

int binarySearch (string [ ] , int, string) ;

const int SI ZE=20 ;

int main()

{

//Create an array of names sorted in ascending order

string names [SIZE] = (

"Allision, Jeff ", "Collins, Bill", "Conroy, Pat ",

"Griffin, Jim", "Harrison, Rose", "Holland, Beth",

"Johnson, Jill ", "Kelly, Sean", "Michalski, Joe ",

"Moreno, Juan", "Moretti, bella", "Patel, Renee ",

"Rubin, Sarah", "Sanchez, Manny", "Smith, Bart ",

"Smith, Cathy", "Taylor, Tyrone ", "Whitman, Jean",

"Wolfe, Bill ", "Wu, Hong" ) ;

string searchKey; //Holds the name to search for

int results; //Holds the search results

cout<<"Enter the name you wish to search for: " ;

getline (cin, searchKey) ;

//Search for the name

results=binarySearch (names, SIZE, searchKey) ;

//If binarySearch returned -1, the ID was not found

if (results==-1)

cout<<"That name does not exist in the array. n";

else

{

//Otherwise results contains the subscripts of

//the specified name in the array

cout<<"Name "<<searchKey<<" was found in element "

<<results<<" of the arrayAn";

}

system("pause");

return 0;

}//End of main -----------------------------------------------------------------------

int binarySearch(string names[],int size,string value)

{

int first=0, //First array element

last=size-1, //Last array element

middle, //Midpoint of search

position=-1; //Position of search value

bool found=false; //Flag

while (I found && first[=last)

{

//Calculating midpoing

middle=(first+last)/2;

/*Find equalance between middle element of array and value*/

int compare=names[middle].compare(value];

//If value is found at mid

if(compare==0)

{

/*If equals change state of boolean variable to true*/

found=true;

position=middle;

}

//If value is in lower half

else if(compare>0]

last=middle-1;

//If value is in upper half

else

first=middle+1;

}//End of while

return position;//Returning found

}//End of Function

C:\Users\ada75727\Pictures\4.PNG

Q.10

//Header file section

#include ustdafx.h"

#include<iostream>

using namespace std;

//Function prototypes

void BubbleSort(int arr[],int);

void SelectionSort(int arr[],int);

//Main function

void main()

{

int array1[8]={5,1,4,6,3,8,2,7};

int array2[8]={5,1,4,6,3,8,2,7};

int i;//Loop variable

//Displaying contents of first array

cout<<"Contents of First array:"<<endl;

for(i=0; i<8;i++)

cout<<" "<<array1[i];

cout<<endl;

//Function call to sort using Bubble sort

BubbleSort(array1,8);

//Displaying contents of second array

cout<<"Contents of Second array:"<<endl;

for(i=0; i<8;i++)

cout<<" "<<array2[i];

cout<<endl;

//Function call to sort using Selectionsort

SelectionSort(array2,8);

system(upause");

}//End main

//Function definitions

void BubbleSort(int array[],int size)

{

bool swap;

int temp;

cout<<"Bubble sort"<<endl;

do { swap = false;

{

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

{

if (array[count] > array[count + 1])

{

temp = array[count];

array[count] = array[count + 1];

array[count + 1] = temp;

swap = true;

}//end if

}//End for

//Outputting elements of array after each pass

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

cout<Carray[i]<<" ";

cout<<endl;

} while (swap);

}//End Bubble sort

void SelectionSort(int array[], int size)

}

int startScan, minIndex, minValue;

cout<<"Selection Sort:"<<endl;

for (startScan = 0; startScan < (size - 1); startScan++)

{

minIndex = startScan;

minValue = array[startScan];

for(int index = startScan + 1; index < size; index++)

{

if (array[index] < minValue)

{

minValue = array[index];

minIndex = index;

}//End if

}//End for

array[minIndex] = array[startScan];

array[startScan] = minValue;

//Outputting elements of array after each pass

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

cout<Carray[i]<<" ";

cout<<endl;

} //End For

//End selection sort