Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,157 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,127 people online right now. Registration is fast and FREE... Join Now!




Array sort question

 
Reply to this topicStart new topic

Array sort question, Array sort

longboard100
7 Dec, 2007 - 04:42 AM
Post #1

New D.I.C Head
*

Joined: 12 Nov, 2007
Posts: 7


My Contributions
I am trying to do a lottery with 6 numbers I have everything working but I wnat to sort the numbers like a standard lotto ticket. Where would the sort function go? Check my code and any help is greatly appreciated.
code:
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#define SIZE 6

void printArray(int a[], int s);
void fillArray(int a[], int s);

int main()
{
srand(time(NULL));

int array[] = {0,0,0,0,0,0};

fillArray(array, SIZE);
printArray(array, SIZE);

printf("\n\nEND PROGRAM\n");
system("PAUSE");
return 0;
}

void fillArray(int a[], int s)
{
for(int x=0; x<s; x++)
{
a[x] = (int) (rand()%53+1);

for(int y=0; y<x; y++)
{
if(a[y] == a[x])
{
a[x] = (int) (rand()%53+1);
x=-1;
}
}
}
}

void printArray(int a[], int s)
{

printf("\nPrinting the array\n");
for(int x=0; x<s; x++)
{

printf("%d ", a[x]);

}
printf("\n");
}
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Array Sort Question
7 Dec, 2007 - 05:01 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 523



Thanked: 3 times
Dream Kudos: 125
My Contributions
A: what is it doing now, and what would you expect/want it to do.

B: put your code inside code tags to keep it readable.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Array Sort Question
7 Dec, 2007 - 05:04 AM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,443



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE

Where would the sort function go?

What would the sort function do?

I mean, obviously it's going to "sort" something, but what will it start with & what will the end result be?

This post has been edited by no2pencil: 7 Dec, 2007 - 05:04 AM
User is online!Profile CardPM
+Quote Post

longboard100
RE: Array Sort Question
7 Dec, 2007 - 05:05 AM
Post #4

New D.I.C Head
*

Joined: 12 Nov, 2007
Posts: 7


My Contributions
It generates 6 random numbers between 1 and 53. But they are not in order. That is what I am trying to accomplish.
[#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#define SIZE 6

void printArray(int a[], int s);
void fillArray(int a[], int s);

int main()
{
srand(time(NULL));

int array[] = {0,0,0,0,0,0};

fillArray(array, SIZE);
printArray(array, SIZE);

printf("\n\nEND PROGRAM\n");
system("PAUSE");
return 0;
}

void fillArray(int a[], int s)
{
for(int x=0; x<s; x++)
{
a[x] = (int) (rand()%53+1);

for(int y=0; y<x; y++)
{
if(a[y] == a[x])
{
a[x] = (int) (rand()%53+1);
x=-1;
}
}
}
}

void printArray(int a[], int s)
{

printf("\nPrinting the array\n");
for(int x=0; x<s; x++)
{

printf("%d ", a[x]);

}
printf("\n");
}]
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Array Sort Question
7 Dec, 2007 - 05:09 AM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,443



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(longboard100 @ 7 Dec, 2007 - 06:05 AM) *

It generates 6 random numbers between 1 and 53. But they are not in order. That is what I am trying to accomplish.


Generating is not the same as sorting... I'm still a bit confused at what you want to sort.

btw code.gif

This post has been edited by no2pencil: 7 Dec, 2007 - 05:09 AM
User is online!Profile CardPM
+Quote Post

longboard100
RE: Array Sort Question
7 Dec, 2007 - 05:12 AM
Post #6

New D.I.C Head
*

Joined: 12 Nov, 2007
Posts: 7


My Contributions
The output is 6 random numbers. These numbers are from 1-53. I want them to be in ascending order i.e. 1,2,3,4,5,6 not 6,4,3,1,2,5.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Array Sort Question
7 Dec, 2007 - 05:19 AM
Post #7

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,443



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
I would put the sort function in your printArray function, or apply it to the array just before calling printArray.
User is online!Profile CardPM
+Quote Post

longboard100
RE: Array Sort Question
7 Dec, 2007 - 05:32 AM
Post #8

New D.I.C Head
*

Joined: 12 Nov, 2007
Posts: 7


My Contributions
What would the code look like to do this? Thank you for your help.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Array Sort Question
7 Dec, 2007 - 05:49 AM
Post #9

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,443



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
I posted a link to : http://en.wikipedia.org/wiki/Sort_(C++) on the previous post, just in case you might ask =-P
User is online!Profile CardPM
+Quote Post

longboard100
RE: Array Sort Question
7 Dec, 2007 - 06:14 AM
Post #10

New D.I.C Head
*

Joined: 12 Nov, 2007
Posts: 7


My Contributions
Culd someone please just give me the code and were to put it. I have been trying to get this to work for almost a week and I want to be done with it. Please do not use cout or cin i do not know what they do. Thanks.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:23PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month