Thread: Array, num...

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78

    Array, num...

    How to move an entire array for one place up, i want to free array[5], example, and to move all elements from [5] for one place so i can put some other number on its place.

    4.56

    1 2 3 4 5 6 7 8 9

    1 2 3 4 4.56 5 6 7 8 9


    to get something like this.

  2. #2
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    i managed to put it in but on wrong index.

    Code:
    for(i=n;i>0;i--)
    	{
    	
    			niz[i+1]=niz[i];
    			niz[i]=broj;
    	}
    	for(i=0;i<n;i++)
    	{
    			printf("%d", niz[i]);
    	}

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    It is quite simple, you can use advanced functions that can copy fragments of memory like memcpy or memmove reallocating the array but you can use a better indexing way to do the same job.
    Look the code and you will understand what i did.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int* MoveArray(int *ptrArray, int ptrArraySize, int ptrArrayIndex, int Value)
    {
    	if(ptrArray == NULL || ptrArrayIndex < 0 || ptrArrayIndex > ptrArraySize)
    		return NULL;
    	else
    	{
    		//Index.
    		int i;
    		//The return Array.
    		int *nArray = (int*)calloc(ptrArraySize + 1, sizeof(int));
    		if(nArray == NULL)
    			return NULL;
    		else
    		{
    			for(i = 0; i < ptrArrayIndex; i++)
    				nArray[i] = ptrArray[i];
    			if(i == ptrArrayIndex)
    				nArray[i++] = Value;
    			for(; i < ptrArraySize + 1; i++)
    				nArray[i] = ptrArray[i-1];
    			return nArray;
    		}
    	}
    }
    int main(int argc, char *argv[])
    {
    	int i;
    	int *p = NULL;
    	int ar[] = {1, 3 ,4 ,6 ,10, 9 , 11};
    	//Put element 22 in third place {0,1,2}
    	p = MoveArray(ar, 7, 2, 22);
    	printf("Init. Array: ");
    	for(i = 0; i < 7; i++)
    		printf("%d ",ar[i]);
    	printf("\n");
    	printf("Final Array: ");
    	for(i = 0; i < 8; i++)
    		printf("%d ",p[i]);
    	printf("\n");
    	free(p);
    	return 0;
    }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm still not very sure that I completely understand but, if the array is big enough, and you want to maintain sorted order, then just add the new element at the end and perform a sort. An insertion sort, maybe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You shouldn't be casting calloc(); it returns a void pointer and there's no need to do so.

    Besides, you're creating a whole new array with the new data. I think the OP wants to re-use the same array.

    Here's my stab at it:
    Code:
    void shift_array_right(int array[], size_t n) {
        size_t x;
        
        for(x = n; x > 0; x --) {
            array[x-1] = array[x];
        }
    }
    
    void insert_initial_element(int array[], size_t n, int value) {
        shift_array_right(array, n);
        array[0] = value;
    }
    
    void insert_element(int array[], size_t n, size_t pos, int value) {
        insert_initial_element(&array[pos], n - pos, value);
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    I'm still not very sure that I completely understand but, if the array is big enough, and you want to maintain sorted order, then just add the new element at the end and perform a sort. An insertion sort, maybe.
    Thats it! But when i insert the number its messed up. It replaces one number instead just adding new one. I used the same code dwk wrote. I just can't insert the bloody number properly!
    Last edited by shardin; 09-08-2007 at 01:28 AM.

  7. #7
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    My code:
    Code:
    int funkcija(int niz[], int n, int broj)
    {
    	int i;
    		
    	for(i=n;i>0;i--)
    	{
    	
    			niz[i+1]=niz[i];
    			niz[0]=broj;
    	}
    	for(i=0;i<n;i++)
    	{
    			printf("%d", niz[i]);
    	}
    }
    niz = 9
    output is: insert 1 --> 122345678,
    if i put
    Code:
    for(i=n;i>=0;i--)
    then 112345678

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well shifting is definitely the right idea. The only problem is that you still need the temporary that you have in insert sort: otherwise as you end up sliding, you never replace the other value and end up copying data like 1, 1, 1, ... or something like that. To do it right you need to iterate over the entire array anyway, so you might as well sort it.
    Code:
    #include <stdio.h>
    
    int main( void )
    {
      double niz[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      const size_t N = sizeof niz / sizeof *niz;
      size_t i, j;
    
      niz[N - 1] = 4.56;
    
      for( i = 1; i < N; i++ ) {
        double key = niz[i];
        for( j = i; j >= 1 && key < niz[j - 1]; j-- ) {
          niz[j] = niz[j - 1];
        }
        niz[j] = key;
      }
    
      for( i = 0; i < N; i++ ) {
        printf( "%.2f%s", niz[i], i + 1 == N ? "\n" : ", " );
      }
    
      return 0;
    }
    
    /**out: 1.00, 2.00, 3.00, 4.00, 4.56, 5.00, 6.00, 7.00, 8.00, 9.00 **/
    Hopefully that wasn't your homework. But step through the algorithm yourself, ans see that it works.

  9. #9
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    no, not homework,no more homework for me, i have a final exam in a few days so this is practice, I'am very bad in programing, this C - programing is my last exam before i graduate. Its not hard but it is hard for me. i hate C !

  10. #10
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Code:
    niz[N - 1] = 4.56;
    when i change it to
    Code:
    niz[N-1] = num
    , and i put num = 4.56 it works, but when i try to input num it doesnt! ??

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    How are you reading in num? You'd need to use &#37;f for scanf(), assuming that num is a float or a double.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    "%f", of course...

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by shardin View Post
    "%f", of course...
    %f in scanf is for floats...
    doubles should be read with %lf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM