
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















0; temp^ =^ x;^ //^
temp^ gets^3 x^ =^ y;^ //^
x^ gets^4 y^ =^ temp;^ //^
y^ gets^3
1 //^ code^14 2 //^ This^ program^
sorts^ an^ array's^ values
into^ ascending^ order. 3 #include^
=^ 10;^ //^ size^ of^ array^ a 15 int^ a[^ arraySize
16 int^ hold;^ //
temporary^ location used^ to^ swap^ array elements
1718 cout^ <<^ "Data
items^ in^ original^ order\n"; 1920 //^ output^ original
array 21 for^ (^ int^ i^ =
0;^ i^ <^ arraySize;^ i++^ ) 22 cout^ <<^ setw(
4 )^ <<^ a[^ i^ ]; 23
40 cout^ <<^ "\nData
items^ in^ ascending^
order\n";
4142 //^ output^ sorted
array 43 for^ (^ int^ k^ =
0;^ k^ <^ arraySize;^ k++^ ) 44 cout^ <<^ setw(
4 )^ <<^ a[^ k^ ]; 4546 cout^ <<^ endl; 4748 return^ 0;^ //
indicates^ successful
termination
Reading Assignment: Computing Mean,Median and Mode Using Arrays^ ļ^ Mean^ ā¦^ Average (sum/number of elements)^ ļ^ Median^ ā¦^ Number in middle of sorted list^ ā¦^ 1, 2, 3, 4, 5 (3 is median)^ ā¦^ If even number of elements, take average ofmiddle two^ ļ^ Mode^ ā¦^ Number that occurs most often^ ā¦^ 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)
26 int^ frequency[
initialize^ array^ frequency
2728 //^ initialize
array^ responses 29 int^ response[
responseSize^ ]^ = 30 {^ 6,^ 7,
4041 //^ process^ responses 42 mean(^ response,
responseSize^ ); 43 median(^ response,
responseSize^ ); 444546 return^ 0;^ //
indicates^ successful
termination 4748 }^ //^ end^ main 49
50 //^ calculate^ average
of^ all^ response^ values 51 void^ mean(^ const
int^ answer[],^ int^ arraySize^ ) 52 { 53 int^ total^ =^ 0; 5455 cout^ <<^ "********\n
Mean\n********\n"; 5657 //^ total^ response
values 58 for^ (^ int^ i^ =
0;^ i^ <^ arraySize;^ i++^ ) 59 total^ +=^ answer[
i^ ]; 6061 //^ format^ and
output^ results 62 cout^ <<^ fixed
<<^ setprecision(^4
6364 cout^ <<^ "The^
mean^ is^ the^ average value^ of^ the^ data\n" 65 <<^ "items.
The^ mean^ is^ equal^ to^ the^ total^ of\n" 66 <<^ "all^
the^ data^ items^ divided
by^ the^ number\n" 67 <<^ "of^ data
items^ ("^ <<^ arraySize 68 <<^ ").^ The
mean^ value^ for\nthis
run^ is:^ " 69 <<^ total
<<^ "^ /^ "^ <<^ arraySize
70 <<^ static_cast<
double^ >(^ total^ )^ /^ arraySize 71 <<^ "\n\n"; 7273 }^ //^ end^ function
mean 74
We cast to a double to getdecimal points for theaverage (instead of aninteger).
144145 //^ function^ that
sorts^ an^ array^ with
bubble^ sort^ algorithm 146 void^ bubbleSort(
int^ a[],^ int^ size^
147 { 148 int^ hold;^ //
temporary^ location^
used^ to^ swap^ elements 149150 //^ loop^ to^ control
number^ of^ passes 151 for^ (^ int^ pass
=^ 1;^ pass^ <^ size;^ pass++^ ) 152153 //^ loop^ to
control^ number^ of^ comparisons^ per^ pass 154 for^ (^ int^
j^ =^ 0;^ j^ <^ size^ -^ 1;
j++^ ) 155156 //^ swap
elements^ if^ out^ of order 157 if^ (^ a[
j^ ]^ >^ a[^ j^ +^1 ]^ )^
158 hold
=^ a[^ j^ ]; 159 a[^ j
]^ =^ a[^ j^ +^1 ]; 160 a[^ j
+^1 ]^ =^ hold; 161162 }^ //^ end
if 163164 }^ //^ end^ function
bubbleSort 165
166 //^ output^ array^
contents^ (20^ values^
per^ row)
167 void^ printArray(
const^ int^ a[],^ int size^ ) 168 { 169 for^ (^ int^ i^ =
0;^ i^ <^ size;^ i++^ )^
170171 if^ (^ i^ %^20
==^0 )^ //^ begin^ new
line^ every^20 values 172 cout^ <<
endl; 173174 cout^ <<^ setw(
2 )^ <<^ a[^ i^ ]; 175176 }^ //^ end^ for 177178 }^ //^ end^ function
printArray
********Mode********Response^ Frequency Histogram
The^ mode^ is^ the^ most
frequent^ value. For^ this^ run^ the^ mode
is^8 which^ occurred
27 times.
Searching Arrays: Linear Search andBinary Search^ ļ^ Search array for a key value^ ļ^ Linear search^ ā¦^ Compare each element of array with keyvalue^ ļ^ Start at one end, go to other^ ā¦^ Useful for small and unsorted arrays^ ļ^ Inefficient^ ļ^ If search key not present, examines every element
1 //^ code^16 2 //^ Linear^ search
of^ an^ array. 3 #include^
const^ int^ [],^ int, int^ );^ //^ prototype 1011 int^ main() 12 { 13 const^ int^ arraySize
=^ 100;^ //^ size^ of array^ a 14 int^ a[^ arraySize
];^ //^ create
array^ a 15 int^ searchKey;
//^ value
to^ locate^ in^ a 1617 for^ (^ int^ i^ =
0;^ i^ <^ arraySize;^ i++^ )^ //^ create^ some
data
18 a[^ i^ ]^ =^2
*^ i; 1920 cout^ <<^ "Enter
integer^ search^ key:
21 cin^ >>^ searchKey; 2223 //^ attempt^ to
locate^ searchKey^ in
array^ a 24 int^ element^ =
linearSearch(^ a,^ searchKey,
arraySize^ ); 25
Takes array, search key,and array size.
26 //^ display^ results 27 if^ (^ element^
28 cout^ <<^ "Found
value^ in^ element^ " <<^ element^ <<^ endl; 29 else 30 cout^ <<^ "Value
not^ found"^ <<^ endl; 3132 return^ 0;^ //
indicates^ successful
termination 3334 }^ //^ end^ main 3536 //^ compare^ key^ to
every^ element^ of^ array
until^ location^ is 37 //^ found^ or^ until
end^ of^ array^ is^ reached;
return^ subscript^ of 38 //^ element^ if^ key
or^ -1^ if^ key^ not^ found 39 int^ linearSearch(
const^ int^ array[], int^ key,^ int^ sizeOfArray
40 { 41 for^ (^ int^ j^ =
0;^ j^ <^ sizeOfArray;
j++^ ) 4243 if^ (^ array[
j^ ]^ ==^ key^ )^ //^ if
found, 44 return^
j;^ //^ return^ location^ of^ key
4546 return^ -1;^ //
key^ not^ found 4748 }^ //^ end^ function
linearSearch