1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>
#include "hw5_head.h"
using namespace std;
int main()
{
ofstream outFile;
const int n = 1000; //indicates array size
int list[n]; //integer array for a list
int avg,avgP,avgN,maximum;
int value, counts;
randomList(list, n);
showList(list, n);
avgs(list,n,avg,avgP,avgN);
maximum=max(list,n);
printList(outFile,list,n,avg,avgP,avgN,maximum);
cout<<"\nsearch using linear search"<<endl;
cout<<"List contains: "<<linearSearch(list,n,value)<<endl;
bubbleSort(list,n,counts);
cout<<"\nAfter bubble sort list elements are: "<<endl;
for(int i=0;i<n;i++)
cout<<list[i]<<" ";
cout<<"\n\nsearch using binary search"<<endl;
cout<<"List contains: "<<(bool)binarySearch(list,n,value)<<endl;
//closing the opened files
outFile.close();
return 0;
}
Heade File:
[code]
#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>
using namespace std;
#ifndef HW5_HEAD_H
#define HW5_HEAD_H
//generates random numbers and fills the array with those random numbers
void randomList(int list[], const int n)
{
signed seed;
cout << "Enter a random seed ";
cin>>seed;
srand(seed);
for (int i = 0; i < n; i++)
{
list[i] = rand()%1000;
}
return;
}
//finds average of positive, negative and all numbers seperately.
void avgs (const int list[ ], const int n, int &avg, int &avgP, int &avgN)
{
int posNums=0,negNums=0;
int posSum=0,negSum=0;
for(int i=0;i<n;i++)
{
if(list[i]>0)
{
posSum += list[i];
posNums++;
}
else if(list[i]<0)
{
negSum += list[i];
negNums++;
}
}
avgP = posSum/posNums;
avgN = negSum/negNums;
avg=(avgP+avgN)/2;
}
//finds the maximum value in the list
int max (const int list[], const int n)
{
int maximum =list[0];
for(int i=1;i<n;i++)
{
if(list[i] >maximum)
maximum=list[i];
}
return maximum;
}
//prints the list values,averages and maximum into the file
void printList(ofstream & outFile, const int list[], const int n, const int avg, const int avgP, const int avgN, const int max)
{
outFile<<"List elements are: "<<endl;
cout<<"List elements are: "<<endl;
for(int i=0;i<n;i++)
{
outFile<<list[i]<<" ";
cout<<list[i]<<" ";
}
outFile<<"\n\nAverage of positive numbers: "<<avgP<<endl;
cout<<"\n\nAverage of positive numbers: "<<avgP<<endl;
outFile<<"Average of negitive numbers: "<<avgN<<endl;
cout<<"Average of negitive numbers: "<<avgN<<endl;
outFile<<"Average of all numbers: "<<avg<<endl;
cout<<"Average of all numbers: "<<avg<<endl;
outFile<<"Maximum is: "<<max<<endl;
cout<<"Maximum is: "<<max<<endl<<endl;
}
//search for an element using linear search
bool linearSearch(const int list[ ], const int n, const int value)
{
for(int i=0;i<n;i++)
{
if(list[i] == value)
{
return true;
}
}
return false;
}
//sorting the list using bubble sort
void bubbleSort(int list[ ], const int n, int & counts)
{
bool swap;
int temp;
int i,j;
counts=0;
for (i = 0; i<n; i++)
{
swap = false;
for (j = 0; j < n - i - 1; j++)
{
if (list[j] > list [j+1])
{
swap = true;
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
counts++;
}
}
if(!swap)
break;
}
return;
}
//searching the array using binary serach
bool binarySearch(const int list[ ], const int n, const int value)
{
int first = 0, last = n - 1, middle, position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (list[middle] == value)
{
found = true;
position = middle;
}
else if (list[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return found;
}
void showList(const int list[], const int n)
{
cout<<"The list is ..... " <<endl;
for (int i=0; i<n; i++)
{
cout<<setw(8)<<right<<list[i];
if ((i+1)%1000==0)
cout<<endl;
}
cout<<endl;
}
#endif
| |