### help please -Choose limit of an array

I'd like the user to be able to input a range for the array, I've made it able to be set to a high, but I don't know how to set the low
i.e:
right now you can set 20 and it'll print 1-20
but I want it to print (for example) 7-20, or 13-20, etc etc
 ``123456789101112131415161718192021222324252627282930`` ``````//Brandon Thomas # include using namespace std; int main( ) { srand(time(0)); int counter = 0; int a; cout << "Please enter length of array (zero through x)" << endl; cin >> a; a++; system("CLS"); int y[a]; for(int loop = 0; loop <= a; loop++){ y[counter] = counter; counter++; } int z = sizeof(y)/sizeof(y[0]); random_shuffle(y, y+z); for(int x = 0; x < z; x++){ cout << y[x] << endl; } system("PAUSE"); } ``````
Last edited on
Try using the modulo operator. This gives the remainder, after being divided by it. For example:

20%10 means that if you divide 20 by 10, you have a remainder of 0, so it evaluates to 0.

7%5 means that if you divide 7 by 5, you have a remainder of 2, so it evaluates to 2.

I'll let you figure out how that is implemented. Good luck!

i really don't understand how that is implemented, this is for my programming class in HS and i'm *very* new to dealing with arrays
If I test this using for example: a low of 10 and a high of 20, it will print 10-20, but for numbers like 1-9 it prints garbage out of memory.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````//Brandon Thomas # include using namespace std; int main() { srand(time(0)); //H/L Variables int a, b; //Instructions cout << "Please enter high and low of array" << endl; //Sets Length of the Array (ie. A = 20, 0-20 is generated) cin >> a; //Sets lowest number to appear on screen cin >> b; //Sets the rising counter to the lowest number int counter = b; system("CLS"); //Sets the Array to A (Length) int y[a]; //Loads Numbers into Array for(int loop = 0; loop <= a-b; loop++) { //Loop is position in Array, so starts at 0 //Counter is equivalent to the Low, so I'll use 4 as example y[loop] = counter; counter++; } //Shuffles numbers within the Array int z = sizeof(y)/sizeof(y[0]); random_shuffle(y, y+z); //Prints shuffled numbers for(int x = 0; x < z; x++) { cout << y[x] << endl; } system("PAUSE"); } ``````
Sorry, I'll explain a bit more.

If you have, say, N%10 where n is any number, it means that you can have the values 0 through 9. That is because you would have 0 as a remainder, after you divide n by 10, or you can have up to 9 as a remainder. For example, 13%10 gives you 3, 19%10 returns 9, 20%10 returns 0, etc.

In general, if you have the expression: a+N%b, where a, b are constants and N is a variable integer, then you have a minimum value of (b-a) and a maximum value of (a+b)-1.

example: 6+N%12
a = 6, b = 12
min: 6
max: 17

Double check this- its late where I live and im quite tired

Edit: I think i may have misunderstood what you're trying to do. Can you explain exactly what it is youre doing?
Last edited on

-Generate Numbers based off user input
-Have them outputted in a random order
-User Input selects range of the numbers to be output: (Low and High)
-No 2 numbers can be repeated

Try running the program I have right above your most recent post, first try the Input
20 (enter) 1

it prints the numbers 1-20 in a random order without any repeats

then try the input
20 (enter) 10

It's doesnt print 1-9 (as it's told) but instead of printing 1,2,3,4,5,6,7,8 and 9, it prints garbage in place of those integers
Topic archived. No new replies allowed.