Need help reversing an array c/c++

ARosch

Member
Apr 12, 2012
75
0
61
I am trying to reverse an array so that I can store it into an another array, but I am not having any luck. Here is what my code looks like, all help is appreciated. Thanks!

char array1[50];
int count1=50;
int count2=0;
for(count1=50; count1>=0;count1--){
array1[count2]=array[count1];
count2++;
}

/*
Some information about my code:
This is taking place in a function. Array only holds integer values, obviously stored as chars. I need all of the integers in array to be reversed(for example, if array held the numbers 1-10 I would want it to hold 10-1). I do not know the size of array1, but I do not expect it to ever be longer than 50. I need the reverse of array to be stored as an array.Would it work better to make count1 equal to the size of(sizeof) array1?

All suggestions welcome, thanks for looking. */
 
Last edited:

iCyborg

Golden Member
Aug 8, 2008
1,327
52
91
You don't need 2 counters since count1+count2=50 always.

It's an incomplete example, but one thing you're doing wrong is starting from 50: an array with 50 elts is indexed 0-49, max index is 49. And, yes, you'll need to know the size, and you'll need to pass it as argument most likely, sizeof(array1) will be size of the pointer to the array which is 32/64 bits.
 
Last edited:

Pia

Golden Member
Feb 28, 2008
1,563
0
0
Homework?

Like iCyborg said, the premise is messed up. To reverse an array, you must know its size first. So that information has to be available from somewhere or this can't be done.

I'm also interested in what you are supposed to do with the reversed array. For instance, returning it from a function would generally require dynamic memory allocation.
 

douglasb

Diamond Member
Apr 11, 2005
3,163
0
76
The pseudo code would be:
Code:
char array1[50];
char array2[array1.length];
for (int i=0; i < array1.length; i++) {
  array2[i] = array1[array1.length - i];
}

Sorry, been a while since I did any c/c++, so I am making the assumption that you can get the array's length like you can in Java or C#.
 

ARosch

Member
Apr 12, 2012
75
0
61
Douglas I don't think you can find length like that in c/c++.

I made an edit to my original post. I got the names array and array1 backwards...
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
Sorry, been a while since I did any c/c++, so I am making the assumption that you can get the array's length like you can in Java or C#.
You can't. There's literally no way to get an array's length given just the array. That information has to come from outside what ARosch has told us so far.
 

ARosch

Member
Apr 12, 2012
75
0
61
You don't need 2 counters since count1+count2=50 always.

It's an incomplete example, but one thing you're doing wrong is starting from 50: an array with 50 elts is indexed 0-49, max index is 49. And, yes, you'll need to know the size, and you'll need to pass it as argument most likely, sizeof(array1) will be size of the pointer to the array which is 32/64 bits.

I don't understand what you mean by only using one counter. I agree that it should be 49 not 50. So that array1[0]=array[49], how could this be done with one counter?

Homework?

Like iCyborg said, the premise is messed up. To reverse an array, you must know its size first. So that information has to be available from somewhere or this can't be done.

I'm also interested in what you are supposed to do with the reversed array. For instance, returning it from a function would generally require dynamic memory allocation.

I am converting some numbers, then I am supposed to give the answer in reverse. The array with the starting numbers is passed to the function and then I change the numbers then reverse them, save them into that array since arrays are passed by reference.
 

ARosch

Member
Apr 12, 2012
75
0
61
You can't. There's literally no way to get an array's length given just the array. That information has to come from outside what ARosch has told us so far.
I believe the sizeof(array) command works just fine.
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
I believe the sizeof(array) command works just fine.
Why do you believe that?
sizeof() gives a count of bytes a datatype takes on the current platform; sizeof(X) is only dependent on what type X is. It has nothing to do with array sizes.
 
Last edited:

douglasb

Diamond Member
Apr 11, 2005
3,163
0
76
Why do you believe that?
sizeof() gives a count of bytes a datatype takes on the current platform; sizeof(X) is only dependent on what type X is. It has nothing to do with array sizes.

Could you do sizeof(array) / sizeof(char) ? Divide the total size of the array by the size of a single char, for the # of char in the array?

Your other alternative is to have an int for your array sizes, then create your arrays to that size, set your counter to stop at that number, etc.
 

iCyborg

Golden Member
Aug 8, 2008
1,327
52
91
I don't understand what you mean by only using one counter. I agree that it should be 49 not 50. So that array1[0]=array[49], how could this be done with one counter?
If count1+count2=size at all times, then wherever you have count2, you can replace it with (size-count1). E.g., instead of array[count2] you put array[49-count1] in your example.
This is also what douglasb did in his example. Except that with native "dummy" arrays, you can't get size like that. You have to pass it from wherever you constructed the array. Or use a better container, like std::vector.
 

iCyborg

Golden Member
Aug 8, 2008
1,327
52
91
Could you do sizeof(array) / sizeof(char) ? Divide the total size of the array by the size of a single char, for the # of char in the array?
You can do that only if array was allocated on stack in the same scope. When you pass it to a function, you will receive it as a pointer. And then sizeof(array) == size of ptr which is 32/64 bits (4/8) depending on target platform, no matter what the underlying type is.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Could you do sizeof(array) / sizeof(char) ? Divide the total size of the array by the size of a single char, for the # of char in the array?

Your other alternative is to have an int for your array sizes, then create your arrays to that size, set your counter to stop at that number, etc.

As Pia and iCyborg pointed out, for sizeof() to work the array must have been allocated with a fixed size known at compile time. I'm not sure it would have to be in local scope, but the array definition (not just the declaration!) would have to be visible to the compiler at the point where it encounters sizeof(array).
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
also if this is not exercise/homework code, check out STL for common operations e.g.

Code:
#include <algorithm>

char v = {1, 2, 3};

std::reverse(v, v + sizeof(v) / sizeof(v[0]));

or

char v2[3];
std::reverse_copy(v, v + sizeof(v) / sizeof(v[0]), v2);
you should still know how to write a loop to do this yourself of course, but I'd opt for that in production code for readability, lower margin for error and potentially better optimization in some cases.
 
Last edited:

Merad

Platinum Member
May 31, 2010
2,586
19
81
Of course if this is "real" C++ code, you should really just use vectors instead of raw arrays. In which case you'd just do:

Code:
#include <vector>

using std::vector;

....

vector<char> v1;
//put something in v1

vector<char> v1Reversed(v1.rbegin(), v1.rend());
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |