Hello everyone. I have a declaration that starts off like this

inta data[100];
size_t i;

I have to write a C++ segment code that will shift data[50] and data[98] up one spot to the locations data[51] and data[99]

Then I am supposed to insert the number 42 in data[50]

Any ideas on How I can do this?

Two thoughts on this one.

A. Use memmove (which allows source/destination overlap) to relocate the values like so:

memmove(&data[51],&data[50],49*sizeof(int);
data[50]=42;

B. Use a for loop that works backwards.

for(int i=99;i>50;i--){data[i]=data[i-1];}
data[50]=42;

Frankly, the second should have been a little obvious.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.