Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Array indexes

Array indexes

Scheduled Pinned Locked Moved C / C++ / MFC
questionalgorithmsdata-structureshelptutorial
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Farraj

    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 ?

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #4

    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.


    1 Reply Last reply
    0
    • F Farraj

      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 ?

      F Offline
      F Offline
      Farraj
      wrote on last edited by
      #5

      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 ;)

      L 1 Reply Last reply
      0
      • F Farraj

        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 ;)

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #6

        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

        F 1 Reply Last reply
        0
        • F Farraj

          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 ?

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #7

          i believe this will do it[^]...

          image processing toolkits | batch image processing

          1 Reply Last reply
          0
          • L Luc Pattyn

            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

            F Offline
            F Offline
            Farraj
            wrote on last edited by
            #8

            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 ;)

            L 1 Reply Last reply
            0
            • F Farraj

              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 ;)

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #9

              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.


              F 1 Reply Last reply
              0
              • L Luc Pattyn

                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.


                F Offline
                F Offline
                Farraj
                wrote on last edited by
                #10

                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.

                L F 2 Replies Last reply
                0
                • F Farraj

                  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.

                  F Offline
                  F Offline
                  Farraj
                  wrote on last edited by
                  #11

                  oh im sorry i guess i forgot to compare with a[inde[]] now its working perfectly thank you a lot SIR :)

                  1 Reply Last reply
                  0
                  • F Farraj

                    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.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #12

                    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.


                    F 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      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.


                      F Offline
                      F Offline
                      Farraj
                      wrote on last edited by
                      #13

                      Yep yep yep:-) for some reason i replaces that line by mistake lol thank u a lot:) i appreciate ur help

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups