# C loops and array practice

 Marcus Lee · June 13, 2015 Hi all, wrote a quick lottery program to see if I learned what i been watching, before i move on to pointers... Can someone please test it and let me know if any problems in code or improvements please.. Thanks pastebin.com/b7rtQEg7

## Replies

 Dol Lod · June 14, 2015 A selection sort would be better than a bubble sort to implement to reduce the number of swaps you are doing. Instead of doing up to n^2 swaps, you could simply do N swaps. Obviously, complexity remains at n^2, but you are doing less work.Rough Pseudocode of SelectionSort:For every item in the array with the current element position being called outer,{      Create a variable called min which is set to outer     For every element inner,which is outer+1, do the following.{      do a comparison between min and inner      If the array's value at inner is less than the array's value at min,           let min equal inner.}      Swap the values at min and inner.} Marcus Lee · June 14, 2015 Thanks for reply Arjun, havent got to memory allocation and stuff yet still learning... The bubble sort type section from 66-83 does seem to sort the drawn balls in ascending order from low to high and lines 40-62 just generate a random number..Checks through the array if it already exists if it does, then generate a different one, and also check if indeed lower than value 49. then repeat for another 5 times! Wait, i see what you mean, draw not initialised before testing against preDraw, thanks for that but the drawn ball is only added to draw array AFTER its been checked, hmmmm!Thanks, i wrote the thing and i'm confusing myself now.. Dol Lod · June 14, 2015 There is an error with   int  draw[numbers];You would have to use dynamic allocation so it would be int*draw=(int*)(malloc (sizeof(int)*numbers)). Then you would have to do free(draw).Next thing is that I'm pretty sure, that sorting algorithm won't work.Another major problem is that draw's elements are uninitialized. You need to initialize them before comparing them.I'm not really sure what lines 40-62 are doing. I know they are supposed to do, but I don't really see the connection. On the other hand, that may just be me.You could test this code out on ideone.com.  That way you can know if it actually works.
• 1

## C

118,312 followers