### Working with arrays

Hello everyone, I am having trouble with this assignment for school.

I am able to come up with the random numbers in that range and find the average, but that is where I get stuck. I have done a lot of research and still cant figure it out. Here is the code I have so far. Any help would be greatly appreciated.

 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include #include using namespace std; void main() { float sum=0; float avg=0; const int size = 200; int array[size]; srand((unsigned)time(0)); for(int a=0; a
Last edited on
A function like this, called with `sort_increasing(array);` will sort the array in increasing order and a small modification will allow for a `void sort_decreasing(int arr[])` function

 ``12345678910111213141516`` ``````void sort_increasing(int arr[]) { int temp; for(int x = 0; x < 200; x++) { for(int y = 0; y < 199; y++) { if(arr[y] > arr[y+1]) { temp = arr[y+1]; arr[y+1] = arr[y]; arr[y] = temp; } } } }``````
Thanks for replying. I appreciate it.
I still don't understand how this is done. Also does my array have a name? Im a complete beginner and really have little idea what im doing. when I enter your code into mine it gives an error that arr is undefined. thank you in advanced for any further help.
The sort increasing() function needs to be declared (prototyped) before main(), and the actual function comes after main(). sort increasing() is called from within main() with array is passed as a parameter. Here is some code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include #include #include using namespace std; // prototype for the sort_increasing function void sort_increasing(int arr[]); int main() { float sum=0; float avg=0; const int size = 200; int array[size]; srand((unsigned)time(0)); for(int a=0; a arr[y+1]) { temp = arr[y+1]; arr[y+1] = arr[y]; arr[y] = temp; } } } }``````

Another function could sort the array so the numbers are descending. And functions can be written to find the mode and median, have go and post the code and I'll help if I can. Good luck
Last edited on
Thank for the help. I was able to get the median but still cant get the mode. Also to sort the array in decreasing order do I just do the opposite of the sort increasing function. here is what I have so far, didn't change much just added the part to find the median

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include #include #include using namespace std; // prototype for the sort_increasing function void sort_increasing(int arr[]); int main() { float sum=0; float avg=0; const int size = 200; int array[size]; srand((unsigned)time(0)); for(int a=0; a arr[y+1]) { temp = arr[y+1]; arr[y+1] = arr[y]; arr[y] = temp; } } } }``````
Last edited on
The sort_decreasing() function os easy, it's the same as the sort_increasing() with one modification: `if(arr[y] > arr[y+1])` --> `if(arr[y] < arr[y+1])`

The mode function is more tricky, most of the time your array will be bimodal and there may be many modes, so I'm not sure if you are supposed to give them all as your answer in this problem. Try the get_mode function I wrote and see that most of the time there are several modes...

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123`` ``````#include #include #include using namespace std; // prototype for the sort_increasing function void sort_increasing(int arr[]); void sort_decreasing(int arr[]); int get_mode(int arr[]); int main() { float sum=0; float avg=0; const int size = 200; int array[size]; srand((unsigned)time(0)); for(int a=0; a arr[y+1]) { temp = arr[y+1]; arr[y+1] = arr[y]; arr[y] = temp; } } } } // sort_decreasing function void sort_decreasing(int arr[]) { int temp; for(int x = 0; x < 200; x++) { for(int y = 0; y < 199; y++) { if(arr[y] < arr[y+1]) { temp = arr[y+1]; arr[y+1] = arr[y]; arr[y] = temp; } } } } int get_mode(int arr[]) { int highest_counter = 0; int mode; for(int x = 0; x < 200; x++) { int counter = 0; for(int y = 0; y < 200; y++) { if(arr[x] == arr[y]) counter++; } if(counter > highest_counter) { highest_counter = counter; mode = arr[x]; cout << arr[x] << "(" << counter << ") "; } else if(counter == highest_counter) { cout << arr[x] << "(" << counter << ") "; } } return mode; }``````
Thank you for all your help. I see what you mean about the modes. it looks correct to me. its giving me a mode and I guess that's all I can really ask for. Thanks again for helping me.
Topic archived. No new replies allowed.