Array indexes
-
i need help with writing a c code lets say i have an array[5] i need to make a new array that gets the indexes of the array[5] from the maximum number to the minimum one. example {2,5,1,8,4} my new array of indexes will be (counting from ZERO) {3,1,4,0,2} i though of sorting it and save the indexes in a temporary array or something but it didnt work any ideas how can i solve this ?
-
i need help with writing a c code lets say i have an array[5] i need to make a new array that gets the indexes of the array[5] from the maximum number to the minimum one. example {2,5,1,8,4} my new array of indexes will be (counting from ZERO) {3,1,4,0,2} i though of sorting it and save the indexes in a temporary array or something but it didnt work any ideas how can i solve this ?
Mmmm.... it looks like homework. What about if there are two identical numbers in different positions? Is that possible? Anyways... one possibility (hard coded) could be... you go through the array evaluating the actual number with a maximum that you update when you find that the actual number is higher than this max. If you find it, you can save the value of "i" temporary and continue evaluating. When the array is checked to the end you can know where the highest number was with your temp_i, then put a very low (or negative) number that is not going to disturb you on the next evaluation and start over again.
Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpfull answers is nice, but saying thanks can be even nicer.
-
i need help with writing a c code lets say i have an array[5] i need to make a new array that gets the indexes of the array[5] from the maximum number to the minimum one. example {2,5,1,8,4} my new array of indexes will be (counting from ZERO) {3,1,4,0,2} i though of sorting it and save the indexes in a temporary array or something but it didnt work any ideas how can i solve this ?
Here is an approach: Create another array where each element stores (v, i), where value is the value in the input array and i is its index. Then you sort this array on v. You can store v and i in a structure and then use qSort for sorting. -Saurabh
-
i need help with writing a c code lets say i have an array[5] i need to make a new array that gets the indexes of the array[5] from the maximum number to the minimum one. example {2,5,1,8,4} my new array of indexes will be (counting from ZERO) {3,1,4,0,2} i though of sorting it and save the indexes in a temporary array or something but it didnt work any ideas how can i solve this ?
Hi, the normal approach to sort an array value[] without moving its content is this: - create an index array, initialized to 0...N-1 - implement whatever sort algorithm you like on that array, except, rather than comparing index[i] and index[j], compare value[index[i]] and value[index[j]] and move the content of index[] around as a result of the sorting steps. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
i need help with writing a c code lets say i have an array[5] i need to make a new array that gets the indexes of the array[5] from the maximum number to the minimum one. example {2,5,1,8,4} my new array of indexes will be (counting from ZERO) {3,1,4,0,2} i though of sorting it and save the indexes in a temporary array or something but it didnt work any ideas how can i solve this ?
thanks for your replies guys i appreciate it ive tried this code.. it may look bad lol but i'll appreciate it a lot if someone can tell me how to correct it
int a\[\] = {2,1,5,4,7};
//assuming that the size of array is 5 -> initializing it from 0-4
int index[] = {0,1,2,3,4};
int i,j,temp;for(int i=0; i<5; i++) { for(int j=0; j<5-1; j++) { if(a\[index\[j\]\]>a\[index\[j+1\]\]) { temp = index\[a\[j+1\]\]; index\[a\[j+1\]\] = index\[a\[j\]\]; index\[a\[j\]\] = temp; } } }
thanks ;)
-
thanks for your replies guys i appreciate it ive tried this code.. it may look bad lol but i'll appreciate it a lot if someone can tell me how to correct it
int a\[\] = {2,1,5,4,7};
//assuming that the size of array is 5 -> initializing it from 0-4
int index[] = {0,1,2,3,4};
int i,j,temp;for(int i=0; i<5; i++) { for(int j=0; j<5-1; j++) { if(a\[index\[j\]\]>a\[index\[j+1\]\]) { temp = index\[a\[j+1\]\]; index\[a\[j+1\]\] = index\[a\[j\]\]; index\[a\[j\]\] = temp; } } }
thanks ;)
Farraj wrote:
temp = index[a[j+1]]; index[a[j+1]] = index[a[j]]; index[a[j]] = temp;
you should drop all the a[]'s here, you really want to swap two index entries, no more, no less. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
modified on Sunday, April 11, 2010 9:47 AM
-
i need help with writing a c code lets say i have an array[5] i need to make a new array that gets the indexes of the array[5] from the maximum number to the minimum one. example {2,5,1,8,4} my new array of indexes will be (counting from ZERO) {3,1,4,0,2} i though of sorting it and save the indexes in a temporary array or something but it didnt work any ideas how can i solve this ?
i believe this will do it[^]...
-
Farraj wrote:
temp = index[a[j+1]]; index[a[j+1]] = index[a[j]]; index[a[j]] = temp;
you should drop all the a[]'s here, you really want to swap two index entries, no more, no less. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
modified on Sunday, April 11, 2010 9:47 AM
thank you. i've got it working but the array is sorted from the minimum index to the maximum, how can i fix it the oppisite way? this is the code so far..
if(a\[j\]>a\[j+1\]) { temp = index\[j+1\]; index\[j+1\] = index\[j\]; index\[j\] = temp; }
another thing.. you've mentioned something about initializing the array from 0->N-1, ive done that while i know what size my array is. what if the size is unknown N, how can i initialize it to be from 0 to N-1? should i make another loop to initialize it or there is some other way? Thanks a lot for everyone's help ;)
-
thank you. i've got it working but the array is sorted from the minimum index to the maximum, how can i fix it the oppisite way? this is the code so far..
if(a\[j\]>a\[j+1\]) { temp = index\[j+1\]; index\[j+1\] = index\[j\]; index\[j\] = temp; }
another thing.. you've mentioned something about initializing the array from 0->N-1, ive done that while i know what size my array is. what if the size is unknown N, how can i initialize it to be from 0 to N-1? should i make another loop to initialize it or there is some other way? Thanks a lot for everyone's help ;)
Farraj wrote:
what if the size is unknown N
you create and initialize the index array after the value array is known, so you do know N.
Farraj wrote:
how can i fix it the oppisite way?
if you were to understand what you have done so far, you wouldn't be asking such question... :(
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
Farraj wrote:
what if the size is unknown N
you create and initialize the index array after the value array is known, so you do know N.
Farraj wrote:
how can i fix it the oppisite way?
if you were to understand what you have done so far, you wouldn't be asking such question... :(
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
I did understand Luc thank you for your help this is my code for now
#include <stdio.h>
#define N 5
void main()
{int a\[N\]; int index\[N\]; int i,j,temp; int count=0; for(i=0; i<N; i++) { //set the values of array a\[\] scanf("%d",&a\[i\]); //initializing the index\[\] array from 0->N-1 index\[i\]=count++; } for(i=0; i<N; i++) { for(j=0; j<N-1; j++) { if(a\[j\]>a\[j+1\]) { temp = index\[j+1\]; index\[j+1\] = index\[j\]; index\[j\] = temp; } } }
for (i = 0; i < N; ++i)
{
printf("%d ",index[i]);
}
flushall();
getchar();}
i think it should work perfectly, its just it doesnt !! though it looks exactly how it should be can u run it and tell me where is the problem please? Thanks.
-
I did understand Luc thank you for your help this is my code for now
#include <stdio.h>
#define N 5
void main()
{int a\[N\]; int index\[N\]; int i,j,temp; int count=0; for(i=0; i<N; i++) { //set the values of array a\[\] scanf("%d",&a\[i\]); //initializing the index\[\] array from 0->N-1 index\[i\]=count++; } for(i=0; i<N; i++) { for(j=0; j<N-1; j++) { if(a\[j\]>a\[j+1\]) { temp = index\[j+1\]; index\[j+1\] = index\[j\]; index\[j\] = temp; } } }
for (i = 0; i < N; ++i)
{
printf("%d ",index[i]);
}
flushall();
getchar();}
i think it should work perfectly, its just it doesnt !! though it looks exactly how it should be can u run it and tell me where is the problem please? Thanks.
-
I did understand Luc thank you for your help this is my code for now
#include <stdio.h>
#define N 5
void main()
{int a\[N\]; int index\[N\]; int i,j,temp; int count=0; for(i=0; i<N; i++) { //set the values of array a\[\] scanf("%d",&a\[i\]); //initializing the index\[\] array from 0->N-1 index\[i\]=count++; } for(i=0; i<N; i++) { for(j=0; j<N-1; j++) { if(a\[j\]>a\[j+1\]) { temp = index\[j+1\]; index\[j+1\] = index\[j\]; index\[j\] = temp; } } }
for (i = 0; i < N; ++i)
{
printf("%d ",index[i]);
}
flushall();
getchar();}
i think it should work perfectly, its just it doesnt !! though it looks exactly how it should be can u run it and tell me where is the problem please? Thanks.
Farraj wrote:
if(a[j]>a[j+1])
is obviously wrong. you need to look at the a value "through the index array". try
if(a[index[j]]>a[index[j+1]])
that is what you have to pay for the fact that you never actually move the a values around; all you do is change the index values. You have had the test correct a while ago... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
Farraj wrote:
if(a[j]>a[j+1])
is obviously wrong. you need to look at the a value "through the index array". try
if(a[index[j]]>a[index[j+1]])
that is what you have to pay for the fact that you never actually move the a values around; all you do is change the index values. You have had the test correct a while ago... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.