Vidya
Vidya

Reputation: 223

How to get indexes alone from an array

I have the following array and index

0 1 2 3 4 5 6 7 8 <-indexes -- n values
7 7 8 8 6 9 8 7 9 <-values -- array x

I have another array with the indices

0 1 2 3 4 5 6 7 8 <-indexes -- m values
7 8 9 9 8 7 7 8 9 <-values  -- array y

what I want is to obtain all the index(in reverse order of array x):

6: 4
7: 7 1 0
8: 6 3 2
9: 8 5

And then, for each element in the y array, I want to make another array index[ ] have the values. So, index[6] = 4, index[7] = 7, 1, 0 etc.

I have done:

for (i=n-1; i>=0; i--) {
    index[y[i]] = index[y[i]]+i;
}

for (i=0; i<m; i++) {
    strings[i] = strings[i] + index[x[i]];
}

for (i=0; i<m; i++) {
    printf("%d",strings[i]);
}

The desired output is to print a sequence like 710632858563271071063285 but this is not printing the desired output.

Upvotes: 0

Views: 126

Answers (5)

cdonat
cdonat

Reputation: 2822

This should work as expected. Note, that the main loop is simpler than the output loop.

#include <stdio.h>

int idx[9][9] = {{-1,-1,-1,-1,-1,-1,-1,-1,-1},
                 {-1,-1,-1,-1,-1,-1,-1,-1,-1},
                 {-1,-1,-1,-1,-1,-1,-1,-1,-1},
                 {-1,-1,-1,-1,-1,-1,-1,-1,-1},
                 {-1,-1,-1,-1,-1,-1,-1,-1,-1},
                 {-1,-1,-1,-1,-1,-1,-1,-1,-1},
                 {-1,-1,-1,-1,-1,-1,-1,-1,-1},
                 {-1,-1,-1,-1,-1,-1,-1,-1,-1},
                 {-1,-1,-1,-1,-1,-1,-1,-1,-1}};
int x[9] = {7, 7, 8, 8, 6, 9, 8, 7, 9};

void buildIndex(int maxval, int idx_len, int x_len) {
    assert(maxval >= x_len);

    for(int i = 0; i < x_len; i++)
        for(int j = idx_len - 1; j >= 0; j--)
            if(idx[x[i]-1][j] == -1) {
                idx[x[i]-1][j] = i;
                break;
            }
}


int main() {
    buildIndex(9, 9, 9);

    /* output the idx */
    for(int k = 0; k < 9; k++)
        if(idx[k][8] != -1) { /* ignore empty idx lines */
            printf("%d: ", k+1);
            for(int l = 0; l < 9; l++)
                if(idx[k][l] != -1) /* write all valid numbers */
                    printf("%d ", idx[k][l]);
            printf("\n");
        }
}

This program outputs:

6: 4 
7: 7 1 0 
8: 6 3 2 
9: 8 5 

I am sure, you can adapt the output to your needs yourself. The "big" idea in this code is to fill the arrays in idx backwards and in the output just ignore the unset values at the beginning.

Upvotes: 0

Newaz Hossain
Newaz Hossain

Reputation: 147

try this one:

#include <stdio.h>

int main()
{
    int A[]={7,7,8,8,6,9,8,7,9};
    for(int i=0;i<=9;i++)
    {
        int flag=0;
        for(int j=8;j>=0;j--)
        {
            if(A[j]==i)
            {
                if(flag==0)
                    printf("%d:",i);
                flag=1;
                printf(" %d",j);
            }
        }
        if(flag==1)
            printf("\n");
    }
    return 0;
}

Upvotes: 0

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>
#include <string.h>

int main(void) {
    int x[] = {7, 7, 8, 8, 6, 9, 8, 7, 9};
    int n = sizeof(x)/sizeof(*x);
    int y[] = {7, 8, 9, 9, 8, 7, 7, 8, 9};
    int m = sizeof(y)/sizeof(*y);
    int index[10][10];

    memset(index, -1, sizeof(index));
    for(int i = n -1; i >= 0; --i){
        int j = 0;
        while(index[x[i]][j] >= 0)
            ++j;
        index[x[i]][j] = i;
    }
    for(int i = 0; i < m; ++i){
        for(int j = 0; j < 10 && index[y[i]][j] >= 0; ++j){
            printf("%d", index[y[i]][j]);
        }
    }
    return 0;
}

Upvotes: 2

zubergu
zubergu

Reputation: 3706

I'd say that array is not the best for this purpose as you have to iterate multiple times over it every time you want to know what numbers are at what indexes. If memory isn't an issue you will do much better with combination of data structures to efficiently perform this task.

An array is fine for matching index with value. What I suggest is to keep numbers in linked list. Every node would also keep linked list of indexes that number appears on. This way you'd do much better because of search time.

Adding value would mean that you:

1) check if there is already value at given index in an array. If there is - you remove this index from linked list of indexes for given number.

2) Then you put new value at given index and if this is first appearance of this value, else you add new index to list of indexes this value appears.

If you implement adding to the linked list of indexes in descending order to get all the numbers and indexes you just need to iterate over list of values and print list of indexes.

This might look like an overkill but I don't know a thing about the actual problem at hand. Your array might be thousands indexes long. That would set you up for handling these more efficiently.

Upvotes: 1

NeitherNor
NeitherNor

Reputation: 266

int i;
int j;
// Assuming 9 is the highest integer possible
for(j=0;j<=9;j++){
    int k=0;
    for(i=n-1;i>=0;i--){
        if(x[i]==j){
            index[j][k]=i;
            k++;
        }
    }
    // Adding a -1 to denote that no more indices were found
    index[j][k]=-1;
}

PS: Make sure that index is big enough...

Upvotes: 3

Related Questions