Shift elements of an array

A

axcytz

Hi all,

I have an array containing 15 elements. I want to shift the elements to the right and store the new array in another array, and keep the original array as is. Does anyone have any suggestions regarding this?

Thanks in advance
 
A

axcytz

(e-mail address removed) wrote in



Hi all,

I have an array containing 15 elements. I want to shift the elements
to the right and store the new array in another array, and keep the
original array as is. Does anyone have any suggestions regarding this?



const int N=15;

double original[N] = {/*...*/};

double result[N];



int shift=3;

std::copy(original, original+N-shift, result+shift);

std::fill(result, result+shift, 0.0);



hth

Paavo

Thanks for your help first of all. When I use this set of commands, I get the output:

0x7fff6f1e4ca0
0x7fff6f1e4ca0

What does this mean?

Thanks
 
V

Vlad from Moscow

Calls of the algorithms can be written in one line

std::copy( original, original + N - shift,
std::fill_n( result, shift, 0.0 ) );

provided that shift <= N

пÑтница, 4 октÑÐ±Ñ€Ñ 2013 г., 8:47:53 UTC+4 пользователь Paavo Helde напиÑал:
(e-mail address removed) wrote in




Hi all,

I have an array containing 15 elements. I want to shift the elements
to the right and store the new array in another array, and keep the
original array as is. Does anyone have any suggestions regarding this?



const int N=15;

double original[N] = {/*...*/};

double result[N];



int shift=3;

std::copy(original, original+N-shift, result+shift);

std::fill(result, result+shift, 0.0);



hth

Paavo
 
S

Stefan Ram

Vlad from Moscow said:
Calls of the algorithms can be written in one line
std::copy( original, original + N - shift,
std::fill_n( result, shift, 0.0 ) );

The above are /two/ lines.

This, for example, is /one/ line:

std::copy(original, original+N-shift, result+shift); std::fill(result, result+shift, 0.0);
 
V

Vlad from Moscow

You are right. More correctly it would be to say "in one statement".:)

пÑтница, 4 октÑÐ±Ñ€Ñ 2013 г., 18:35:51 UTC+4 пользователь Stefan Ram напиÑал:
 
M

Michael Groshart

Hi all,



I have an array containing 15 elements. I want to shift the elements to the right and store the new array in another array, and keep the original array as is. Does anyone have any suggestions regarding this?



Thanks in advance

There is a standard library algorithm called std::rotate_copy that does exactly what you are asking.
 
V

Vlad from Moscow

Rotating is not the same as shifting.

пÑтница, 4 октÑÐ±Ñ€Ñ 2013 г., 23:17:12 UTC+4 пользователь Michael Groshart напиÑал:
 
R

Rosario1903

Rotating is not the same as shifting.

if you have in r[15] the rotate division of n bit of a[15]
for obtain the shift division it is enought fill the range of bit
15*sizeof(*a)*CHAR_BIT..(15*sizeof(*a)*CHAR_BIT-n)
with 0 bit

if it is shift mult
and ave in r[15] rotate mult
fill the range of r[15]
0..n with 0 bit
 
R

Rosario1903

Rotating is not the same as shifting.

if you have in r[15] the rotate division of n bit of a[15]
for obtain the shift division it is enought fill the range of bit
15*sizeof(*a)*CHAR_BIT..(15*sizeof(*a)*CHAR_BIT-n)
with 0 bit

pheraps better:
fill the range of bit
(15*sizeof(*a)*CHAR_BIT-1)..(15*sizeof(*a)*CHAR_BIT-n-1)
if it is shift mult
and ave in r[15] rotate mult
fill the range of r[15]
0..n with 0 bit
 
V

Vlad from Moscow

I have understood nothing. What is the relation between the original task and your post?

воÑкреÑенье, 6 октÑÐ±Ñ€Ñ 2013 г., 14:14:40 UTC+4 пользователь Rosario1903 напиÑал:
Rotating is not the same as shifting.



if you have in r[15] the rotate division of n bit of a[15]

for obtain the shift division it is enought fill the range of bit

15*sizeof(*a)*CHAR_BIT..(15*sizeof(*a)*CHAR_BIT-n)

with 0 bit



if it is shift mult

and ave in r[15] rotate mult

fill the range of r[15]

0..n with 0 bit
 
R

Rosario1903

I have understood nothing. What is the relation between the original task and your post?

???????????, 6 ??????? 2013 ?., 14:14:40 UTC+4 ???????????? Rosario1903 ???????:
Rotating is not the same as shifting.



if you have in r[15] the rotate division of n bit of a[15]

for obtain the shift division it is enought fill the range of bit

15*sizeof(*a)*CHAR_BIT..(15*sizeof(*a)*CHAR_BIT-n)

with 0 bit



if it is shift mult

and ave in r[15] rotate mult

fill the range of r[15]

0..n with 0 bit

i read wrong it seem to me the problem was shift one array of bit...
even if i'm not a C++ expert, this is my try in shift one array of
chars:

#include <iostream>
#include <stdlib.h>

using namespace std;
#define u32 unsigned
#define u8 unsigned char

class myarray{
public:
myarray(){arr=(u8*) malloc(15);
if(arr==0) exit(-1);
s=15;
}

myarray(u32 c)
{if(c>0xFFFF) exit(-1);
arr=(u8*)malloc(c);
if(arr==0) exit(-1);
s=c;
}

myarray(char* c)
{u32 v, i;
if(c==0)
{v=1;}
else {v=strlen(c)+1;}
arr=(u8*)malloc(v);
if(arr==0) exit(-1);
if(v==1) *arr=0;
else {for(i=0; i<v ; ++i)
{arr=c;
if(c==0) break;
}
for(; i<v;++i)
arr=0;
}
s=v;
}


friend ostream& operator<<(ostream& os, myarray& ma)
{u32 i;
if(ma.s!=0)
for(i=0; i<ma.s-1; ++i)
os<<ma.arr;
return os<<ma.arr;
}

u32 shiftd(myarray& ma, u32 pos)
{u32 i, j;
if(s<ma.s)
{free(arr);
arr=(u8*)malloc(ma.s);
if(arr==0) exit(-1);
s=ma.s;
}
if(pos>=s)
{for(i=0;i<s; ++i) arr=0;}
else {/* 0 1 2 3 4 5 6 */
/* | shiftd 3*/
j=pos;
for(i=0;j<s;++j,++i)
arr=ma.arr[j];
for(;i<s;++i)
arr=0;
}
return 1;
}

u32 s;
u8 *arr;
};


int main()
{myarray v("this and that"), ma;

cout<<"at start v=["<<v <<"]\n";
ma.shiftd(v, 3);
cout<<"at end ma=["<<ma<<"]\n";

return 0;
}
 
R

Rosario1903

friend ostream& operator<<(ostream& os, myarray& ma)
{u32 i;
if(ma.s!=0)
for(i=0; i<ma.s-1; ++i)
os<<ma.arr;
return os<<ma.arr;

^^^^^^^^^^^^^^^^^^^^^^^^^
this is one error...
it would be "return os<<ma.arr[ma.s-1]"
 
R

Rosario1903

friend ostream& operator<<(ostream& os, myarray& ma)
{u32 i;
if(ma.s!=0)
for(i=0; i<ma.s-1; ++i)
os<<ma.arr;
return os<<ma.arr;

^^^^^^^^^^^^^^^^^^^^^^^^^
this is one error...
it would be "return os<<ma.arr[ma.s-1]"


wrong too, i repost my solution
[it is possible i not had understood the problem too, excuse for the
time i make lost to you]

#include <iostream>
#include <stdlib.h>

using namespace std;
#define u32 unsigned
#define u8 unsigned char

class myarray{
public:
~myarray(){free(arr); arr=0; s=0;}
myarray(){arr=(u8*) malloc(15);
if(arr==0) exit(-1);
s=15;
}

myarray(u32 c)
{if(c>0xFFFF) exit(-1);
arr=(u8*)malloc(c);
if(arr==0) exit(-1);
s=c;
}

myarray(char* c)
{u32 v, i;
if(c==0)
{v=1;}
else {v=strlen(c)+1;}
arr=(u8*)malloc(v);
if(arr==0) exit(-1);
if(v==1) *arr=0;
else {for(i=0; i<v ; ++i)
{arr=c;
if(c==0) break;
}
for(; i<v;++i)
arr=0;
}
s=v;
}

friend ostream& operator<<(ostream& os, myarray& ma)
{u32 i;
if(ma.s!=0)
{for(i=0; i<ma.s-1; ++i)
os<<ma.arr;
return os<<ma.arr;
}
else return os;
}

u32 shiftd(myarray& ma, u32 pos)
{u32 i, j;
if(s<ma.s)
{free(arr);
arr=(u8*)malloc(ma.s);
if(arr==0) exit(-1);
s=ma.s;
}
if(pos>=s)
{for(i=0;i<s; ++i) arr=0;}
else {/* 0 1 2 3 4 5 6 */
/* | shiftd 3*/
j=pos;
for(i=0;j<s;++j,++i)
arr=ma.arr[j];
for(;i<s;++i)
arr=0;
}
return 1;
}

u32 s;
u8 *arr;
};


int main(void)
{myarray v("this and that"), ma;

cout<<"at start v=["<<v <<"]\n";
ma.shiftd(v, 3);
cout<<"at end ma=["<<ma<<"]\n";

return 0;
}
 
R

Rosario1903

u32 shiftd(myarray& ma, u32 pos)
{u32 i, j;
if(s<ma.s)
{free(arr);
arr=(u8*)malloc(ma.s);
if(arr==0) exit(-1);
s=ma.s;
}
if(pos>=s)
{for(i=0;i<s; ++i) arr=0;}
else {/* 0 1 2 3 4 5 6 */
/* | shiftd 3*/
j=pos;
for(i=0;j<s;++j,++i)

^^^^^^^^^^^^^^^

here would be "for(i=0;j<ma.s;++j,++i)"
arr=ma.arr[j];
for(;i<s;++i)
arr=0;
}
return 1;
}

u32 s;
u8 *arr;
};
 
I

Ian Collins

Paavo said:
Oh dear, in the first 9 non-empty lines of your code you managed to include
6 things which should be never used in a proper C++ program. I stopped
reading after that.

He (?) can usually be found posting such nonsense on c.l.c. I didn't
realise there had been an escape...
 
G

Geoff

Yes, I counted 9 lines and 6 issues. I did not count stdlib.h and raw
pointer, these are sometimes useful in C++ as well (though probably not
in this example).

Under what circumstances would including stdlib.h be preferable to
including cstdlib in a C++ program?
 
A

alf.p.steinbach

Under what circumstances would including stdlib.h be preferable to
including cstdlib in a C++ program?

When you're not bound by a coding guideline or work environment that forces you to do otherwise.

This was the case even with C++98 (and TC1 aka C++03), but with C++11 also the formal allows <cstdlib> & friends to pollute the global namespace.

In practical terms using the [.h] headers therefore means code that has a better chance of being portable (to other system or compiler or even just a new version of your compiler), plus it reduces needless verbosity.


Cheers & hth.,

- Alf
 
A

alf.p.steinbach

I have an array containing 15 elements. I want to shift the elements to the right and store the new array in another array, and keep the original array as is. Does anyone have any suggestions regarding this?

auto const s1 = string( "123456789ABCDEF" );
auto const s2 = '0' + s1;

Cheers & hth.,

- Alf
 
V

Vlad from Moscow

std::string is not an array. So your code have nothing common with the assignment.

понедельник, 7 октÑÐ±Ñ€Ñ 2013 г., 2:11:46 UTC+4 пользователь (e-mail address removed) напиÑал:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top