Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have one array
a[80000] which consist numbers of elements I want a new array says b[80000] where the content is same as a[] but the different is in the position.

C#
for (kl=sample_length-1; kl>=9; kl--)
{
double temp = bicresample[kl];
 bicresample [kl+9]=temp;
 temp=bicresample[kl+9];
 bicresample[kl+9] =bicresample[kl];
}


This code is working for shifting down stream but i want to stream up
eg:a[1]=0;a[2]=2;a[3]=4....a[80000]
b[1]=a[9] ;
Posted
Updated 28-Jan-15 19:13pm
v2
Comments
Sergey Alexandrovich Kryukov 29-Jan-15 1:15am    
All it takes is just some thinking; which is how programming is done. What have you tried so far?

Also note that arrays are not very suitable for shifting, as you massively move data. Usually the data structure designed for such operations are used, such as stacks and queues.

—SA
[no name] 29-Jan-15 2:27am    
What is the need to shift all this data? What you are attempting can be achieved using pointers alone. What do you mean by stream down - stream up?

1 solution

Find the index in the source array based on the current index plus the shift size.

Something like this might work for you;
C
#include <stdio.h>
#include <stdlib.h>

#define DATA_SIZE 10

void fill_data(int data[], int size) {
    for (int i = 0; i < size; ++i)
        data[i] = i;
}

void print_data(int data[], int size) {
    for (int i = 0; i < size; ++i)
        printf("%d: %d\r\n", i, data[i]);
}

// This method shifts or rotates by step size
void shift_data(int source[], int target[], int size, int step, int wrap) {
    int source_index;
    for (int i = 0; i < size; ++i) {
        source_index = i + step;
        if (wrap) {
            source_index = source_index % size;
            target[i] = source[source_index];
        }
        else {
            if (source_index < 0 || source_index >= size)
                target[i] = 0; // Fill with zeros
            else
                target[i] = source[source_index];
        }
    }
}

int main() {
    int source[DATA_SIZE];
    int target[DATA_SIZE];

    // Fill with some values
    fill_data(source, DATA_SIZE);

    // Print source
    printf("Source data:\r\n");
    print_data(source, DATA_SIZE);

    // Shift data down by one
    shift_data(source, target, DATA_SIZE, 1, 0);

    // Print target
    printf("Target data, shifted down:\r\n");
    print_data(target, DATA_SIZE);

    // Shift data up by 3
    shift_data(source, target, DATA_SIZE, -3, 0);

    // Print target
    printf("Target data, shifted up by 3:\r\n");
    print_data(target, DATA_SIZE);

    // Rotate data up by 4
    shift_data(source, target, DATA_SIZE, -4, 1);

    // Print target
    printf("Target data, rotated up by 4:\r\n");
    print_data(target, DATA_SIZE);


    return 0;
}


Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900