Print reversed array made of random numbers

Hi, I need to print first whole array normally, after that I need print it reversed. But because I get numbers from srand, I cant make 2 seperate loops for because I would get different numbers.



#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(time(0));
int n, x, y, j, v, w;
j=0;
cout << "Enter, how many numbers you want to generate" << endl;
cin >> n;
cout << "Numbers will be generated from <x;y> (enter x)" << endl;
cin >> x;
cout << "Numbers will be generated from <" << x << ";y> (enter y)" << endl;
cin >> y;
cout << "Numbers will be generated in interval <" << x << ";" << y << ">:" <<endl;
int p[n];
int r[n];
for (int i=0;i<n;i++)
{
v=rand()%(y+1-x)+x;
w=v;
p[i]=v;
cout << p[i] << " ";
j++;
if (j%10==0)
{
cout << endl;
}
}
for (int k=n;k>0;k--)
{
r[k]=w;
cout << r[k] << " ";
}

return 0;
}
But because I get numbers from srand, I cant make 2 seperate loops for because I would get different numbers.


That makes no sense at all.


Here are some things wrong with this code:

int n, x, y, j, v, w;
You are not being charged by the letter. Do not create such badly named variables. I should be able to tell what information a variable represents from the name.

1
2
int p[n];
int r[n];

This is just plain illegal. It's not C++. C++ doesn't allow variable sized arrays like this.

1
2
3
v=rand()%(y+1-x)+x;
w=v;
p[i]=v;

Why does v exist? It doesn't do anything of use. Why does w exist? It doesn't do anything of use.

1
2
3
4
5
for (int k=n;k>0;k--)
{
r[k]=w;
cout << r[k] << " ";
}

Why are you outputting some array named r? That's not where the numbers you generated are. You put them in the array named p, so what is this array named r for? What is w doing here? This makes no sense at all.
Last edited on
Let's say you have an array with 3 elements in it, {5, 2, 4}.

How would you "manually" reverse these elements?
Something like this:
1
2
swap(arr[0], arr[2]);
swap(arr[1], arr[1]); // does nothing, but important to see a pattern 


Now let's make it bigger, with an even number of elements: {5, 2, 4, 3}; (size = 4)
1
2
swap(arr[0], arr[3]);
swap(arr[1], arr[2]);


Okay, more: {5, 2, 4, 3, 9, 8, 7}; (size = 7)
1
2
3
4
swap(arr[0], arr[6]);
swap(arr[1], arr[5]);
swap(arr[2], arr[4]);
swap(arr[3], arr[3]);


One more: {5, 2, 4, 3, 9, 8, 7, 12}; (size = 8)
1
2
3
4
swap(arr[0], arr[7]);
swap(arr[1], arr[6]);
swap(arr[2], arr[5]);
swap(arr[3], arr[4]);


See a pattern?
You should write a for loop that swaps elements r[i] with r[n-1-i].
where n is the size of the array, that loops while i < n/2 (integer division, rounded down).
for (int i = 0; i < n/2; i++) { ... }
Try to program it.

For swap, #include <algorithm>


PLEASE: Use code format tags when posting code. Edit your post and add [code][/code] around your code.
Last edited on
Okay, I renamed my variables, and deleted unuseful variables and other stuff, but I still cant figure out how can I print out normal array, and get these same numbers and put them in another array and reverse it (I swear Im figuring this out for over 2 hours)

int numbers, low, high, arr, jump;
jump=0;
cout << "Enter, how many numbers you want to generate" << endl;
cin >> numbers;
cout << "Numbers will be generated from <x;y> (enter x)" << endl;
cin >> low;
cout << "Numbers will be generated from <" << low << ";y> (enter y)" << endl;
cin >> high;
cout << "Numbers will be generated in interval <" << low << ";" << high << ">:" <<endl;
int arr[n];
for (int i=0;i<n;i++)
{
arr[i]=rand()%(y+1-x)+x;
cout << arr[i] << " ";
jump++;
if (jump%10==0)
{
cout << endl;
}
Last edited on
Refresh to see my post as well. Write a for loop and try to follow the pattern.
try to use code tags (<>) in the format to the side when posting.

to reverse-copy an array, rolling your own, brute force style looks like this
1
2
3
4
5
int a[100];
int b[100];
int i, j;
for(i = 0, j = 99; i < 100; )
   b[j--] = a[i++];  //b[99] = a[0], then b[98] = a[1], etc... 


1
2
3
4
5
a better design, since you know you need it, would be this
for (int i=0;i<n;i++)
{
arr[i]=rand()%(y+1-x)+x;
arr2[max-i] = arr[i]; //generate the reversed array at the same time as the original, avoiding a second loop over the data 


Last edited on
Ganado thank you very much!!

I'll try to program my and let you guys know how did.

And I'm sorry for not using format tags, I'm pretty new here, but I'll make sure doing so next time posting!
Algright, still can't figure it out :/ ....

And I still dont get it how is it possible to print 2 different arrays ONE AFTER ANOTHER with numbers from srand reversed
jonnin basically already gave you the answer, you just need to integrate into your code.

Okay, let's start here first:
1
2
3
4
5
6
for (int i=0;i<n;i++)
{
    arr[i] = rand()%(y+1-x)+x;
    cout << arr[i] << " ";
    // ...
}

You just assigned n numbers to your array, from indices [0] to [n-1], right? This much you get?

Just like the for loop above, now you just need to make another for loop, but this time you only need i to go from 0 to n/2 instead of n.
At each iteration in the for loop, do swap(arr[i], arr[n-1-i]);

If you still can't figure it out, post what you tried.
Last edited on
Is there any other method without using swap ?
> But because I get numbers from srand, I cant make 2 seperate loops for
> because I would get different numbers.
man wrote:
The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.
(missing the point)


> but I still cant figure out how can I print out normal array, and get these
> same numbers and put them in another array and reverse it
let's simplify the task
copy the elements of the array `a' to the array `b'


> how is it possible to print 2 different arrays ONE AFTER ANOTHER
don't yell.
let's say you have a function print_array(int array[], int size) which will print the array pass it as a parameter
then you may do
1
2
print_array(some_array, n);
print_array(a_different_array, n);
how is it possible to print 2 different arrays ONE AFTER ANOTHER


1
2
int first_array[10];
int second_array[10];

That's two different arrays.

1
2
3
4
5
6
7
8
9
for (int i = 0 ; i < 10; ++i)
{
  cout << first_array[i] << ' ';
}

for (int i = 0 ; i < 10; ++i)
{
  cout << second_array[i] << ' ';
}

And that's them printed out ONE AFTER ANOTHER
let's simplify the task
copy the elements of the array `a' to the array `b'


1
2
3
4
5
6
7
for (int i=0;i<numbers;i++)
  {
   arr[i]=rand()%(high+1-low)+low;;
   cout << arr[i] << " ";
   arr2[i]= arr[i];
   cout << arr2[i] << " ";
  }


And I get different numbers when outputting arr2 with elements from arr
let's say you have a function print_array(int array[], int size) which will print the array pass it as a parameter
then you may do
1
2
print_array(some_array, n);
print_array(a_different_array, n);


thats deffinitely true
> And I get different numbers when outputting arr2 with elements from arr
post the (compilable) code and its output

also, I simply asked you to copy the array
I said nothing about random numbers or cout.
Last edited on
And I get different numbers when outputting arr2 with elements from arr


Not with that code you wrote there, you don't.

https://ideone.com/LVSwP5

Output is clearly the same value in arr1 and arr2.

Topic archived. No new replies allowed.