| | |
Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 31
Reputation:
Solved Threads: 0
I am writing a program that allows the user to insert numbers and then the program sorts them into ascending order. I have this accomplished, however, I also need an array to count the number of times each number the user inserts occurs. For ex: Say the user inserts 3 6 7 6 1. I need the array to keep count of the number of times these numbers occur. I also need to display the number/numbers that occur the most. I have struggled with these issues for the past few days and am not very good with arrays. I would really appreciate any help you could offer. Thank you!
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int fill_array(int a[], int size, int& number_used); void sort(int a[], int number_used); void swap_values(int& v1, int& v2); int index_of_smallest(const int a[], int start_index, int number_used); int count = 0; int main() { cout << "This program sorts numbers from lowest to highest.\n"; int sample_array[100], number_used; fill_array(sample_array, 100, number_used); sort(sample_array, 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 << "Count will go here." << 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 sort(int a[], int number_used) { int index_of_next_smallest; for (int index = 0; index < number_used - 1; index++) { index_of_next_smallest = index_of_smallest(a, index, number_used); swap_values(a[index], a[index_of_next_smallest]); } } void swap_values(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; } int index_of_smallest(const int a[], int start_index, int number_used) { int min = a[start_index], index_of_min = start_index; for (int index = start_index + 1; index < number_used; index++) if (a[index] < min) { min = a[index]; index_of_min = index; } return index_of_min; }
for the first problem, counting the number of times each number occurs in the array, you want to creat a second array that is the same size as the first one. Initialize all cells in this new array to 0. Pass both of those arrays to your counting function. You will have two loops, one inside the other. The first loop will loop through the second array, and the second loop will loop through the first array. By comparing the value of the cell in the first array that corresponds to the cell the second array is currently on, testing each value in the first array and incrementing the value in the second array by one each time you have a match, you will end up with the second array being populated with the number of times each number occured in the first array.
This oughta get you going again...hope it helps...
C++ Syntax (Toggle Plain Text)
for(int i=0;i<asize;i++){ for(int j=0;j<asize;j++){ if(arr1[i]==arr1[j]) arr2[i]++; } } }
•
•
Join Date: Feb 2007
Posts: 31
Reputation:
Solved Threads: 0
Ok...I have tried out your suggestion and so far I have it working without any compilation errors. However, I get some pretty crazy numbers in my counter column in the results. Here is my new code:
And this is the strange result I get:
Numbers Times
1 -858993548
3 -858993460
3 -858993460
5 -858993460
6 -858993460
Any help in cleaning up my mess would be appreciated! I am also still wondering how to display the number/numbers that occur the most. Thank you!
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int fill_array(int a[], int size, int& number_used); void sort(int a[], int number_used); void swap_values(int& v1, int& v2); int index_of_smallest(const int a[], int start_index, int number_used); int 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]; fill_array(sample_array, 100, number_used); count_array(sample_array, counter, number_used); sort(sample_array, 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 sort(int a[], int number_used) { int index_of_next_smallest; for (int index = 0; index < number_used - 1; index++) { index_of_next_smallest = index_of_smallest(a, index, number_used); swap_values(a[index], a[index_of_next_smallest]); } } void swap_values(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; } int index_of_smallest(const int a[], int start_index, int number_used) { int min = a[start_index], index_of_min = start_index; for (int index = start_index + 1; index < number_used; index++) if (a[index] < min) { min = a[index]; index_of_min = index; } return index_of_min; } int 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]++; } return counter[index]; } }
And this is the strange result I get:
Numbers Times
1 -858993548
3 -858993460
3 -858993460
5 -858993460
6 -858993460
Any help in cleaning up my mess would be appreciated! I am also still wondering how to display the number/numbers that occur the most. Thank you!
•
•
Join Date: Feb 2007
Posts: 31
Reputation:
Solved Threads: 0
Ok...I initialized the array to zero like so:
But now in the result it only tells how many times the first number was displayed. The other numbers display 0 times:
Numbers Times
1 1
2 0
3 0
Why is this?
Also, I do not have anything in my code yet to display the number(s) that occur the most. I was wondering how to go about that. I am learning more with your help! Thank you!
C++ Syntax (Toggle Plain Text)
int counter[100] = {0};
But now in the result it only tells how many times the first number was displayed. The other numbers display 0 times:
Numbers Times
1 1
2 0
3 0
Why is this?
Also, I do not have anything in my code yet to display the number(s) that occur the most. I was wondering how to go about that. I am learning more with your help! Thank you!
>Why is this?
Because you misplaced your
Why are you even using a return statement at all? It's not like the last element in the array is going to be very useful without the other values...
And by the way you may want to consider using a debugger, they make errors like these really easy to spot.
Because you misplaced your
return statement. C++ Syntax (Toggle Plain Text)
int 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]++; } return counter[index]; // drop this down below the } } }
And by the way you may want to consider using a debugger, they make errors like these really easy to spot.
Last edited by John A; Mar 30th, 2007 at 11:38 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
Ok...looking through your code I don't see anything wrong with the counter function...One thing to note though, is that you don't need a return value for that counter function...arrays automatically get passed by reference...you could have just as easiler used the following function header:
and not returned a value...I just compiled a small test program on my computer and it ran the code just fine...you might try using void instead and recompiling and see how it goes...
As for the other problem (which number occurs the most...once you get your second array (the one with the count of numbers) you will have your number(s) that occur the most...You just need to figure out how to use it...
C++ Syntax (Toggle Plain Text)
void count_array(int sample_array[], int counter[], int number_used)
As for the other problem (which number occurs the most...once you get your second array (the one with the count of numbers) you will have your number(s) that occur the most...You just need to figure out how to use it...
•
•
Join Date: Feb 2007
Posts: 31
Reputation:
Solved Threads: 0
Wonderful...it now works! I will continue to work on displaying the numbers that occur the most often.
I have another question though. I have used a selection sort to sort the numbers into ascending order. Out of curiosity, how could I change it to an insertion sort, so that a number is sorted into place as soon as the user enters it? It seems it would make more sense to di it this way. Thank you!
I have another question though. I have used a selection sort to sort the numbers into ascending order. Out of curiosity, how could I change it to an insertion sort, so that a number is sorted into place as soon as the user enters it? It seems it would make more sense to di it this way. Thank you!
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int fill_array(int a[], int size, int& number_used); void sort(int a[], int number_used); void swap_values(int& v1, int& v2); int index_of_smallest(const int a[], int start_index, 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); sort(sample_array, 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 sort(int a[], int number_used) { int index_of_next_smallest; for (int index = 0; index < number_used - 1; index++) { index_of_next_smallest = index_of_smallest(a, index, number_used); swap_values(a[index], a[index_of_next_smallest]); } } void swap_values(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; } int index_of_smallest(const int a[], int start_index, int number_used) { int min = a[start_index], index_of_min = start_index; for (int index = start_index + 1; index < number_used; index++) if (a[index] < min) { min = a[index]; index_of_min = index; } return index_of_min; } 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]++; } } }
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: misplaced else and removing space characters
- Next Thread: C++ compilers
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






