# Array's Help

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-30-2003
bmx4christ
Array's Help
Can someone please give me some guidance on this, i'm having some trouble in general about how to tackle it- this is for own personal intrest using some practice problems i found online thanks :)

<--------------------The Problem---------------------------->

PROBLEM STATEMENT
Display six randomly generated unique whole numbers between 1 and 53 inclusive. The list must be sorted and displayed in ascending order and must contain no duplicate values.

SAMPLE OUTPUT (Softcopy):
The numerals on the left of the sample output below are there for the analyst's reference only and will not appear on the screen. Underlined items indicate values entered by the user.

1
2
3
LOTTO PICKS:

3 8 12 21 22 39

SYMBOLIC CONSTANT LIST (Global):
Label Description Value Process Usage Destination
SIZE Quantity of numbers chosen 6 Loop limit ---
MAX Maximum random number value 53 Loop limit ---

LOCAL VARIABLE LIST for "main"
ARRAYS:
Label Description Source Usage Destination
PICKED MAX Integer flags indicating
which values have picked
(0 means NO, 1 means YES) Assigned Flags ---
N SIZE unique random integers
with values ranging
from 1 to MAX inclusive Random Sorted Screen

SCALARS:
Label Description Source Usage Destination
PASS Counter of array passes Assigned Loop index ---
P General purpose counter Assigned Loop index ---
T Temporary swap variable Assigned Swapping ---

ALGORITHM for "main":
This task involves four major elements:

Selecting SIZE random integers within the range of 1 to MAX
Making sure that each integer selected is unique
Sorting the list of selected numbers in ascending order
Displaying the list of selected numbers
We can use an array named N to store the Lotto numbers that we select. Array N will be declared to have six elements, identified using subscripts 0 through 5.

One method of ensuring the selection of unique numbers involves the use of a second array named PICKED. It will use MAX integer storage locations to keep track of which numbers have been selected to prevent our program from using a duplicate value. Each element of the PICKED array will hold either a 1 or a 0 to indicate whether the associated value has been used yet. For example, the first element of PICKED would have a 0 stored in it if the number 1 had been used yet, or a 0 stored there if 1 had not yet been picked. The second element of PICKED would have a 0 stored in it if the number 2 had been used yet, or a 0 stored there if 2 had not yet been picked. (And so on..)

Remember that in C the first element of PICKED would be identified as PICKED[0] and the the second element of PICKED would be identified as PICKED[1]. So we must be careful to offset our use of the subscript in the PICKED array by one. In other words, the storage location that we would use to track whether the value 8 had been selected for the Lotto would be PICKED[7], not PICKED[8].

The steps below describe how to use the arrays N and PICKED to accomplish the program objectives. The italicized text below provides detailed hints about how to accomplish the steps. Before reading the hints, see if you can figure out each step on your own. Then use the hints if you need help.

Start.
Seed the random number generator to produce different numbers for each run.
In C, the command for this action is: srand(time(NULL));
Load the array PICKED with FALSE values to assert that no values have been picked yet.
Use a loop that counts with P from 0 to one less than MAX to assign zero into the element of array PICKED having the subscript P).
Load the array N with SIZE unique random numbers.
Use a loop that counts with P from 0 to one less than SIZE to:
Fill the lotto number array with unique random numbers between 1 & MAX inclusive.
Use a (nested) trailing decision loop that performs the follow body:
Select a random number between 1 & MAX inclusive
It should repeat this body as long as the flag value in the PICKED array element at the position (determined from N[P]-1) is 1 (True).
Set the flag that notes which number was picked in the previous step.
Set the flag value in the PICKED array at the position (determined from N[P]-1) to be 1 (True).
Sort the Array of Numbers using a "Bubble Sort":
Use a loop that counts with PASS from 1 to one less than SIZE to:
Pass through the elements array N and swap any pairs that are out of order.
Use a loop that counts with P from 0 to one less than SIZE-PASS to:
Test if N's element with subscript P is greater than N's element with subscript P+1. If so, swap those two values.
Use an extra (temporary) variable to retain the element that you assign to first.
Display line 1 of the sample softcopy.
Display a blank line.
Display a line showing all values of the N array, each in a width of 5.
Use a loop that counts with P from 0 to one less than SIZE to display the element of N at subscript position P (in a width of 5).
Display a carriage return.
End.

<-------------------------End--------------------------------------->

thanks for you help guys- you're wonderful
• 11-30-2003
orbitz
This isn't homework help.
• 11-30-2003
bmx4christ
exactly did you not read what i posted at the top of the post?
• 11-30-2003
WaltP
Quote:

Originally posted by bmx4christ
exactly did you not read what i posted at the top of the post?
Did you read the forum rules -- the "sticky"s at the top?
• 11-30-2003
bmx4christ
no sir i didn't but i surely will :)
• 11-30-2003
bmx4christ
ahhh, you think i'm having you do my homework??? no no, if you would please read my post, i thought i had made it clear that i didn't want you doing it for me, but moreso pointing me in the right direction, did i not say that?
• 11-30-2003
bmx4christ
Re: Array's Help
Quote:

Originally posted by bmx4christ
Can someone please give me some guidance on this, i'm having some trouble in general about how to tackle it
Now up there did it say, hello everyone, can someone please do this work for me? I'm a lazy bum who doesn't like to do anything and i hate trying to learn.

No, it didn't- i merely asked for someone to point me in the direction i need to be looking to tackle this- it is confusing me without a flowchart and it's either the migraine i have or the killing pain in my neck- or i just don't have the mental capacity to think this up, i have sort of a coder's block and i need someone to show me what i'm overlooking- NOT to do my work for me.
• 11-30-2003
WaltP
Quote:

Originally posted by bmx4christ
ahhh, you think i'm having you do my homework??? no no, if you would please read my post, i thought i had made it clear that i didn't want you doing it for me, but moreso pointing me in the right direction, did i not say that?
No, I trust you when you said this isn't homework. What you didn't do is post code you've tried and what happende when it didn't work.

In your post starting at Start you have what you need to get started. Program that and when you get stuck, we're here to help.
• 11-30-2003
bmx4christ
Ahhh i should of said, help me look where to start then, because the algorithm written like that, to me, looks vague enough to make my migrain 10 fold the pain it is now.
• 11-30-2003
Thantos
Here is a hint. Break the problem up into the smallist parts possible. The work on one little part completely ignoring the others. Once you solve each part then worry about how to get them all to fit together.
• 11-30-2003
bmx4christ
ahhh thanks- i should of thought of that- i feel like my brain is running slow for some reason... hmm... i should get it checked out
• 12-01-2003
bmx4christ
Okay this may seem like a stupid question but what does it mean by load the arroay? did i do that? i'm getting kinda confused w/ this- this is what i have so far i think i'm doing it all wrong

Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 6 #define MAX 53 void main (void) {         srand(time(NULL));         int PICKED[0][1][7];                 P=0;         while (P= MAX - 1)                 {                         PICKED= PICKED[P];                         P= P+1;                 }```
am i doing this right? or wrong? what do they mean by loading an array? plz help- i'm a newb
• 12-01-2003
bmx4christ
actually i think i realized something a little different, this is it coded on the algorithm C. section, can anyone tell me if i did it right? i hate it being worded out.

Code:

```{         srand(time(NULL));         int PICKED[0][1][7];         int N[6]                P=0;         while (P= MAX - 1)                 {                         P= P+1;                         PICKED[P]=0;                                         }```
• 12-01-2003
bmx4christ
This is it finished, but there are some bugs in here somewhere can someone help me find them?
Code:

``` #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 6 #define MAX 53 int PICKED[MAX]; int N[SIZE]; int PASS; int P; int T; void main (void) {  /*    C        */         srand(time(NULL));         for(P=0; P<MAX-1; P+1)         PICKED[P]=0;         /*    D      */         P=0;         while (P < SIZE - 1)                 {                         do                         {                                 PICKED[N[P]-1] = rand() % MAX + 1;                         }                         while(N[P]-1 == 1);             PICKED[N[P]-1] = 1;             P = P + 1;                 }         /*    E      */         PASS=1;         while(PASS<SIZE)                 {                         P=0;                         while(P<SIZE-PASS)                                 {                                         if (N[P]>N[P+1])                                                 {                                                         T= N[P];                                                         N[P]=N[P] + 1;                                                         N[P + 1]= T;                                                 }                                                 P= P+1;                                 }                 }                 /*      The Rest of the Algorithm  */                 printf("LOTTO PICKS:\n\n");                 while (P = 0 && P < SIZE-1)         printf("%5d", N[P]);                 P= P+1;             printf("\n"); }```
• 12-01-2003
WaltP
Quote:

Originally posted by bmx4christ
This is it finished, but there are some bugs in here somewhere can someone help me find them?
OK, first off, use spaces, not tabs. Your indentation is rather extreme
Code:

``` #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 6 #define MAX 53 int PICKED[MAX]; int N[SIZE]; int PASS; int P; int T; void main (void) {  /*    C        */     srand(time(NULL));     for(P=0; P<MAX-1; P+1)             PICKED[P]=0;     /*    D      */     P=0;     while (P < SIZE - 1)     {         do              {  // the array N is uninitialize, probably all 0's             // therefore N[P]-1] is -1,             // therefore PICKED[-1] is an out of bounds error             PICKED[N[P]-1] = rand() % MAX + 1;         } while(N[P]-1 == 1);         PICKED[N[P]-1] = 1;         P = P + 1;// This can be simplified to P++;     } // At this point, print out the important values to // see if the data is good.  Then continue with the rest of the // program once the first segment is correct     /*    E      */     PASS=1;     while(PASS<SIZE)     {         P=0;         while(P<SIZE-PASS)         {             if (N[P]>N[P+1])             {                 T= N[P];                 N[P]=N[P] + 1;                 N[P + 1]= T;             }             P= P+1;         }     }     /*      The Rest of the Algorithm  */     printf("LOTTO PICKS:\n\n");     while (P = 0 && P < SIZE-1)     printf("%5d", N[P]);     P= P+1;     printf("\n"); }```

Also, a for() statement would probably work better than the first while().
Code:

`for (P=0; P < SIZE; P++)`
```P=0; while (P < SIZE - 1) {     ...     P = P + 1; }```