#include <iostream> using namespace std; int fill_array(int a[], int size, int& number_used); void insertion_sort(int sample_array[], int size, int number_used); void count_array(int sample_array[], int counter[], int number_used); int count = 0; int main() { cout << "This program sorts numbers from lowest to highest.\n"; int sample_array[100], number_used; int counter[100] = {0}; fill_array(sample_array, 100, number_used); count_array(sample_array, counter, number_used); insertion_sort(sample_array, 100, number_used); cout << "\n"; cout << "\n"; cout << "Numbers\t"; cout << "Times" << endl; for (int index = 0; index < number_used; index++) { cout << sample_array << "\t"; cout << counter[index] << endl; } cout << endl; cout << "\n"; cout << "You entered " << count << " numbers." << endl; return 0; } int fill_array(int a[], int size, int& number_used) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; count++; } number_used = index; return count; } void insertion_sort(int sample_array[], int size, int number_used) { int key, i; for (int number_used=1; number_used<size; number_used++) { key=sample_array[number_used]; i=number_used-1; while(sample_array[i]>key && i>=0) { sample_array[i+1]=sample_array[i]; i--; } sample_array[i+1]=key; } } void count_array(int sample_array[], int counter[], int number_used) { for(int index=0; index<number_used; index++) { for(int index2=0; index2<number_used; index2++) { if (sample_array[index]==sample_array[index2]) counter[index]++; } } }
#include <iostream> using namespace std; int fill_array(int a[], int size, int& number_used); void insertion_sort(int sample_array[], int size, int key, int number_used); void count_array(int sample_array[], int counter[], int number_used); int count = 0; int main() { cout << "This program sorts numbers from lowest to highest.\n"; int key = 0; int sample_array[100], number_used; int counter[100] = {0}; fill_array(sample_array, 100, number_used); count_array(sample_array, counter, number_used); insertion_sort(sample_array, 100, key, number_used); cout << "\n"; cout << "\n"; cout << "Numbers\t"; cout << "Times" << endl; for (int index = 0; index < number_used; index++) { cout << sample_array[index] << "\t"; cout << counter[index] << endl; } cout << endl; cout << "\n"; cout << "You entered " << count << " numbers." << endl; return 0; } int fill_array(int a[], int size, int& number_used) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; count++; } number_used = index; return count; } void insertion_sort(int sample_array[], int size, int key, int number_used) { int i; for (int number_used=1; number_used<size; number_used++) { key=sample_array[number_used]; i=number_used-1; while(sample_array[i]>key && i>=0) { sample_array[i+1]=sample_array[i]; i--; } sample_array[i+1]=key; } } void count_array(int sample_array[], int counter[], int number_used) { for(int index=0; index<number_used; index++) { for(int index2=0; index2<number_used; index2++) { if (sample_array[index]==sample_array[index2]) counter[index]++; } }
void insertion_sort(int * sample_array, int number_used, int valueToEnter) { int i = 0; //find insertion point while(i < number_used && sample_array[i] < valueToEnter) ++i; //shift all elements from insertion point to end of array to right by 1. if(number_used > 0) { for(int w = number_used - 1; w >= i ; --w) sample_array[w + 1] = sample_array[w]; } //insert valueToEnter at index i sample_array[i] = valueToEnter; }
#include <iostream> using namespace std; int fill_array(int a[], int size, int& number_used); void insertion_sort(int * sample_array, int number_used, int valueToEnter); void count_array(int sample_array[], int counter[], int number_used); int count = 0; int main() { cout << "This program sorts numbers from lowest to highest.\n"; int valueToEnter = 0; int sample_array[100], number_used; int counter[100] = {0}; fill_array(sample_array, 100, number_used); count_array(sample_array, counter, number_used); insertion_sort(sample_array, number_used, valueToEnter); cout << "\n"; cout << "\n"; cout << "Numbers\t"; cout << "Times" << endl; for (int index = 0; index < number_used; index++) { cout << sample_array[index] << "\t"; cout << counter[index] << endl; } cout << endl; cout << "\n"; cout << "You entered " << count << " numbers." << endl; return 0; } int fill_array(int a[], int size, int& number_used) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; count++; } number_used = index; return count; } void insertion_sort(int * sample_array, int number_used, int valueToEnter) { int i =0; while(i < number_used && sample_array[i] < valueToEnter) i++; if(number_used > 0) { for(int w = number_used - 1; w >= i; --w) sample_array[w + 1] = sample_array[w]; } sample_array[i] = valueToEnter; } void count_array(int sample_array[], int counter[], int number_used) { for(int index=0; index<number_used; index++) { for(int index2=0; index2<number_used; index2++) { if (sample_array[index]==sample_array[index2]) counter[index]++; } } }
#include <iostream> using namespace std; void fill_array(int a[], int, int& ); void insertion_sort(int *, int, int); int main() { const int SIZE = 100; int sample_array[SIZE]; int number_used = 0; cout << "This program sorts numbers from lowest to highest.\n"; fill_array(sample_array, SIZE, number_used); return 0; } void fill_array(int a[], const int SIZE, int& number_used) { int next = 1; cout << "Enter up to " << SIZE << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; while(number_used < SIZE && next > 0) { cin >> next; if(next >= 0) { insertion_sort(sample_array, number_used, next); ++number_used; } } } void insertion_sort(int * sample_array, int number_used, int valueToEnter) { int i =0; while(i < number_used && sample_array[i] < valueToEnter) i++; if(number_used > 0) { for(int w = number_used - 1; w >= i; --w) sample_array[w + 1] = sample_array[w]; } sample_array[i] = valueToEnter; }
#include <iostream> using namespace std; int fill_array(int a[], int, int& ); void insertion_sort(int *, int, int); void count_array(int sample_array[], int counter[], int number_used); int count = 0; int main() { cout << "This program sorts numbers from lowest to highest.\n"; const int SIZE = 100; int sample_array[SIZE]; int number_used =0; int counter[100] = {0}; fill_array(sample_array, SIZE, number_used); count_array(sample_array, counter, number_used); cout << "\n"; cout << "\n"; cout << "Numbers\t"; cout << "Times" << endl; for (int index = 0; index < number_used; index++) { cout << sample_array[index] << "\t"; cout << counter[index] << endl; } cout << endl; cout << "\n"; cout << "You entered " << count << " numbers." << endl; return 0; } int fill_array(int sample_array[], const int SIZE, int& number_used) { int next =1; cout << "Enter up to " << SIZE << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; while (number_used < SIZE && next > 0) { cin >> next; if (next >=0) { insertion_sort(sample_array, number_used, next); ++number_used; count++; } } return count; } void insertion_sort(int * sample_array, int number_used, int valueToEnter) { int i =0; while(i < number_used && sample_array[i] < valueToEnter) i++; if(number_used > 0) { for(int w = number_used - 1; w >= i; --w) sample_array[w + 1] = sample_array[w]; } sample_array[i] = valueToEnter; } void count_array(int sample_array[], int counter[], int number_used) { for(int index=0; index<number_used; index++) { for(int index2=0; index2<number_used; index2++) { if (sample_array[index]==sample_array[index2]) counter[index]++; } } }
#include <iostream> using namespace std; int fill_array(int a[], int *, const int) void insertion_sort(int *, int*, int&, int); int main() { cout << "This program sorts numbers from lowest to highest.\n"; const int SIZE = 100; int sample_array[SIZE]; int count = 0; //to keep track of total numbers user enters int counter[SIZE] = {0}; count = fill_array(sample_array, counter, SIZE, number_used); cout << "\n"; cout << "\n"; cout << "You entered " << count << " numbers." << endl; //find max frequency value in counter here //search counter for all elements have max frequency value here //if current element of counter has max value //print out sample_array[x] and counter[x]; return 0; } int fill_array(int sample_array[], int * counter, const int SIZE) { int count = 0; int next = 1; int number_used = 0; //to keep track of unique numbers entered by user cout << "Enter up to " << SIZE << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; //user can input up to SIZE (set at 100 now) numbers while (count < SIZE && next > 0) { cin >> next; if (next >=0) { insertion_sort(sample_array, counter, number_used, next); count++; //Note: if duplicate numbers entered number_used will be less than count } } return count; } void insertion_sort(int * sample_array, int * counter, int & number_used, int valueToEnter) { int i =0; while(i < number_used && sample_array[i] < valueToEnter) i++; if(number_used == 0) //means this is the first value entered into sample_array { sample_array[0] = valueToEnter; //increment counter[0] to 1; //increment number_used by one; } else //means this is not the first value entered into sample_arrray { if //sample_array[i] is equal to valueToEnter //increment counter[i]; else { //means sample_array[i] not equal to valueToEnter //which also means valueToEnter is unique so far //shift all items at index i and beyond in sample_array and //counter to the right by one for(int w = number_used - 1; w >= i; --w) { sample_array[w + 1] = sample_array[w]; //do the same in counter here } //enter valueToEnter at sample_array[i]; sample_array[i] = valueToEnter; //set counter[i] to value of 1 since we know it is unique //increment number_used by one; } } }
| DaniWeb Message | |
| Cancel Changes | |