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
|
#include <cstdlib>
#include <iostream>
#include "DynamicList.h"
#include <time.h>
using namespace std;
//creates a list of random numbers.
void createRandomList (DynamicList &aList, int size);
//sorts a list in increasing or decreasing order.
void sortList(DynamicList &aList, bool decrease);
//does the inner loop for the sortList function.
bool sort( DynamicList &aList, bool decrease);
//prints a list to the screen.
void printList (DynamicList &aList);
//counts how many times a numbers occurs in a list.
int getCount (DynamicList &aList, int element);
//creates a list that contains the number of times a number occurred for another list.
void makeCountList (DynamicList &aList, DynamicList &bList, DynamicList &cList);
//prints out two corresponding lists.
void printsTwoLists (DynamicList &aList, DynamicList &bList);
//Creates a second list from a first, which only contains every number once.
void shortenList(DynamicList &aList, DynamicList &bList);
//Sorts a list of numbers in decreasing or increasing order.
void sortList(DynamicList &aList, bool decrease)
{
int go = 0;
bool switched = true;
while (switched)
{
switched = false;
go = go + 1;
//calls on the function that does the inner loop for sorting.
switched = sort( aList, decrease);
}
}
//Does the inner loop for sorting a list of numbers in decreasing or increasing order.
bool sort( DynamicList &aList, bool decrease)
{
int go = 0;
int temp;
bool switched = false;
for (int i = 0; i < (aList.getSize()-1) - go; i++)
{
//If 1 is chosen for the Boolean value decrease, the list will order
//in decreasing order. So if a number is greater than the next, they
//will switch. If 0 is chosen the list will order in increasing
//order. So if a number is less than the next, they will switch.
if ((decrease && aList.get(i) > aList.get(i + 1)) ||
(!decrease && aList.get(i) < aList.get(i + 1)) )
{
//temp holds the value of a number on the list.
temp = aList.get(i);
//Then that value is replaced by the number of the next.
aList.set(i, aList.get(i+1));
//Then the second number is replaced by the first
//that was being kept by temp.
aList.set(i+1, temp);
switched = true;
}
}
return switched;
}
//Prints out the data of a list.
void printList (DynamicList &aList)
{
for (int i = 0; i < aList.getSize(); i++)
{
//every passage of the loop prints to the screen the
//number that's in the list at that passage.
cout << aList.get(i) << endl;
}
cout << "" << endl;
}
//Uses indexOf and removeEntry to count how many times a numbers occurs in a list.
int getCount (DynamicList &aList, int element)
{
int count = 0;
while(aList.indexOf(element) != -1)//While there is still a number in the list.
{
aList.removeEntry(element);//remove that number.
count = count + 1; //and add one to count.
}
return count;
}
//Creates a counts list, a list corresponding with another
//that contains how many times the same number occurred in it.
void makeCountList (DynamicList &aList, DynamicList &bList, DynamicList &cList)
{
for(int i = 0; i < aList.getSize(); i++)
{
//Uses the function getCount to see how
//many times a number occurred in a list,
int count = getCount(bList, aList.get(i));
//Then adds that number to a different list to keep
//track of the count of the first list.
cList.add(count);
}
}
//Prints out two corresponding lists with them staying alongside each other.
void printsTwoLists (DynamicList &aList, DynamicList &bList)
{
for (int i = 0; i < aList.getSize(); i++)
{
//Every passage of the loop prints out the number both lists contains.
cout << aList.get(i) << " appears " << bList.get(i) << " times." <<endl;
}
cout << "" << endl;
}
//Creates a second list that only contains each
//one of the numbers from the first list one time.
void shortenList(DynamicList &aList, DynamicList &bList)
{
//loops through all of the numbers in the first list.
for (int i = 0; i < (aList.getSize()); i++)
{
//If the second list does not already contain the number
//the first list has on this passage of the loop,
if(!bList.contains(aList.get(i)))
{
//the second list adds the number to its list.
bList.add(aList.get(i));
}
}
}
//Creates a list that contains random numbers.
void createRandomList (DynamicList &aList, int size)
{
srand(time(NULL));
for(int i = 0; i < size; i++)
{
//every passage of the loop adds a random number to a list.
//Until the list is the size indicated by size.
aList.add(rand());
}
}
int main()
{
DynamicList baseList, countList, newBaseList;
bool decrease;
int base;
cout << "Chose a number from 1,000 to 10,000." << endl;
cout << "> ";
cin >> base;//a number chosen by the user that determines
//how long the list of random number will be.
//creates a list called baseList that contains random numbers.
//it's size depending on what ever base is chosen to be.
createRandomList (baseList, base);
cout << "The random numbers:" << endl;
//prints the original list of random numbers.
printList (baseList);
//sorts the base list in increasing order.
sortList(baseList, 1);
cout << "The random numbers in increasing order is:" << endl;
//prints the baseList to the screan.
printList (baseList);
cout << "The lowest random number is:" << endl;
//outputs the lowest random number in the base list.
cout << baseList.get(0) << endl;
cout << endl;
//sorts the baseList in decreasing order.
sortList(baseList, 0);
cout << "The highest random number is:" << endl;
//outputs the highest random number in the base list.
cout << baseList.get(0) << endl;
cout << endl;
//checks that only one of each random number is in the list and creates
//a second list called newBaseList that contains those numbers.
shortenList(baseList, newBaseList);
//creates a count list that contains how many
//times a number in newBaseList occurred.
makeCountList (newBaseList, baseList, countList);
cout << "The count of the random number is:" << endl;
//prints out the newBaseList with the countList to
//show how many times a number did occur.
printsTwoLists (newBaseList, countList);
system("PAUSE");
return EXIT_SUCCESS;
}
|