473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Array Output/Bubble Sort Issue

32
Hi guys. I have a final program for school and the assignment is to show the top 5 numbers for the Cash 5 lottery over the past few days. I think I have all my loops right.I think I have the output listed correctly but it is duplicating on the output. Like the last time I ran it, it came up with the results:
12
12
12
12
20

I am starting to think the bubble sort is written incorrectly. Any help would be greatly appreciated. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. using namespace std;
  5. int x;
  6. int y;
  7. int row;
  8. int col;
  9. int days[5][5];
  10. int ranks[40][2];
  11. int number_hold;
  12. int occur_hold;
  13.  
  14. int main()
  15. {
  16. cout<<"Please enter the numbers for the Cash 5 for the past 5 days.(1-39 only)"<<'\n';
  17. cout<<"Start with the 1st number of day 1 and continue to last number of day 5."<<'\n';
  18. cout<<"Press enter after each number."<<'\n';
  19. int x = 0;
  20. for(x=0;x<40;x++)
  21. {
  22. ranks[x][0]=x;
  23. ranks[x][1]=0;
  24. }
  25. for(row=0;row<5;row++)
  26. {
  27. for(col=0;col<5;col++)
  28. {
  29. cin>>days[row][col];
  30. }
  31. }
  32.  //Bubble Sort
  33. for(row=0;row<5;row++)
  34. {
  35. for(col=0;col<5;col++)
  36. {
  37. ranks[days[row][col]][1]+=1;
  38. }
  39. }
  40.  
  41. //Occur Loop
  42. for(x=1;x<39;x++)
  43. {for(y=1;y<39;y++)
  44. {
  45. if(ranks[y][1]<ranks[y+1][1])
  46. {number_hold = ranks[y][0];
  47. occur_hold = ranks[y][1];
  48. ranks[y][0] = ranks[y+1][0];
  49. ranks[y][1] = ranks[y+1][1];
  50. ranks[y+1][1] = number_hold;
  51. ranks[y+1][1] = occur_hold;
  52. }
  53. }
  54. }
  55. cout<<"The top five numbers for the Cash 5 are:"<<'\n';
  56. cout<<ranks[1][0]<<'\n';
  57. cout<<ranks[2][0]<<'\n';
  58. cout<<ranks[3][0]<<'\n';
  59. cout<<ranks[4][0]<<'\n';
  60. cout<<ranks[5][0]<<'\n';
  61.  
  62.  
  63.  
  64.       system("PAUSE");
  65.       return 0;
  66. }
Dec 19 '07 #1
1 2105
RRick
463 Expert 256MB
A couple of things are going on. Line 50 has a bug with the second indice. Also, your sorting loop ranges are off. You want to go from 0 to the next to last index.

There is way to speed this up. Since you only need the 5 biggest values, your outer loop only needs to be done 5 times. This is because each bubble iteration moves the greatest/smaller value to the end. Currently, you move the smallest value to the bottom, but now we want the largest value at the top. Your innner loop would look something like:
Expand|Select|Wrap|Line Numbers
  1.     for(y=39;y>0;y--)
  2.     {
  3.         if(ranks[y-1][1]<ranks[y][1])
  4.  
Now you are switching the positions at y and y-1.
Dec 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: Radhika Sambamurti | last post by:
Hi, I'm a relative newbie.... I've written a program to do a bubble sort. take in numbers and sort them. My question is: at the very end of the sort() function, when I have to output the result...
6
by: Tamir Khason | last post by:
Let's say I have Point and I want to sort it circle way (e.g. 2;2.4;1,8;2,8,5;4;8,2;7,1;4 The algo for this is: if ((p.X<p.X & p.Y<Avg(p).Y & p.Y<Avg(p).Y) | (p.X>p.X & p.Y>Avg(p).Y &...
6
by: CodeRazor | last post by:
My array is int list = new int{20,99,6}; Using a for-loop, how can I order the output of this array. I don't want to use a sorted list. I know this is straightforward but can't figure it...
5
by: Irfan Mumtaz | last post by:
hi, I had posted a question in the group "how to arrange arrays in increasing order" Although i got an answer using IComparable Interface by Harfried ,which is given below. Dim InputArray()() As...
5
kuchma2000
by: kuchma2000 | last post by:
Hi. I have a C program (see code below) which I need to urgrade. All I need to do is: a. Extract the temps for the City #1 and store in a one dimensional array, display the array. b. Sort the one...
20
by: Acolyte | last post by:
Hey, I'm working on a project that involves sorting a two-dimensional array of integers using bubble sort. I've got it semi-working, my only issue is that it seems to be 'losing' values, I'm assuming...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.