Re: lottery number thing problem

From: Centurion (spam_this_at_nowhere.com)
Date: 11/12/03


Date: Thu, 13 Nov 2003 09:45:33 +1100

moi wrote:
> im getting a recurring
> error regarding the syntax of my main() function argument calling in
> lottery_numbers[] array. so any help as to whats glaringly going wrong
> here?? oh and some brief guidance on whats the easiest way to sort the
> numbers into ascending order in the array before i cout << them.

What error exactly? I'm not going to cut, paste, compile this either - an
error message would be useful (keep reading).

As for the sort, you can do a bubble sort in about 4 lines of code, or you
could use a vector and then leverage some of the STL magic for sorting etc.
Not sure if you're allowed to use the STL in your assignment tho :-/

> p.s. i am aware from some replies to earlier posts that some of the things
> like the includes' etc is a bit archaic but for the purposes of this
> assignment, i dont really need it pointing out. cheers.

<PEDANT>
So *when* do you plan on writing your code correctly? Get into good habits
and it will serve you well. :) If you're aware of the "include" errors
below, then why not just fix them?
</PEDANT>

> here's the code as it stands....

**Snipped - lets just look at "main"**

> void main(int lottery_numbers[5])

oops. try:
int main(void)

Your "main" declaration is telling the compiler to expect integers passed
from the command line - not what you are wanting. Also "void main()" is
generally accepted as B.A.D. :)

> {

initialise your array here like this:
int lottery_numbers[6];

Also, replace any "lottery_numbers[5]" with "lottery_numbers[6]". You want
SIX elements in your array, not 5. Sure the array is indexed from 0 to 5,
but you need to define your array properly otherwise your "for (int x=0; x
<= 5; x++)" will overflow the array; index 5 doesn't exist in your original
code.

Hint: I always define my arrays and loops like this:
int myArray[10]; // an array with 10 elements
for(int i=0; i<10; i++) // iterate through the array
{
    myArray[i]=i*i; // store the square of the index at the index.
    std::cout << myArray[i] << std::endl;
}

Notice "int myArray[MAX_SIZE]" and "for(...; i<MAX_SIZE;...)". Just keeps
your brain on the right track :) Notice I've used "i LESS THAN" not "<=".

> DrawNumbers(lottery_numbers[]); //'draw' the numbers into the array
> for (int i = 0; i <=5; i++) // for each of the six elements of the
> array...
> {
> cout << lottery_numbers[i] << " "; //print out the contents to screen.
> }
> getche();

insert here:
return 0; // main returns an int now, so let's give it one :)

> }

No doubt others far more skilled than I will point out my own short-comings
:P Good luck with your studies!

--James
__________________________________
A random quote of nothing:

Fatal Error: Found [MS-Windows] System -> Repartitioning Disk for Linux...
(By cbbrown@io.org, Christopher Browne)



Relevant Pages

  • A Fast sorting algorithm for almost sorted data
    ... which I am currently calling Run sort. ... entire selected run can be added to the sorted output array. ... public class RunSort implements Comparator ... public static void sort(Comparable a, int start,int end) ...
    (comp.compression)
  • Re: "Sorting" assignment
    ... If the array is already sorted, this means that you end up ... attempt to sort them. ... int quicksortPartition ... for (intIndex1 = intLeft; ...
    (comp.programming)
  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: problem with array
    ... > different sort algorythms. ... > the sort the array elements are compared and swapped. ... void selectionsort(void *base, size_t n, size_t size, ... void PrintArrays(int *a, int **pa,size_t n); ...
    (comp.lang.c)
  • Re: lottery number thing problem
    ... As for the sort, you can do a bubble sort in about 4 lines of code, or you ... Not sure if you're allowed to use the STL in your assignment tho:-/ ... int lottery_numbers; ... SIX elements in your array, ...
    (alt.comp.lang.learn.c-cpp)