reverse order - arrays

This is a discussion on reverse order - arrays within the C Programming forums, part of the General Programming Boards category; I'm now learning arrays and I'm stuck trying to print an array in reverse order. Here's the working part of ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    reverse order - arrays

    I'm now learning arrays and I'm stuck trying to print an array in reverse order. Here's the working part of my code. I've deleted the lead up (printf and scanf statements).

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    
    int divide (int, int);		/*function prototype*/
    
    int remainder (int, int);  /*function prototype*/
    
    int reverse (int []);  /*function prototype*/
    
    int main ()
    {
        int value, base, list[20], count = 0;
    
       printf("\n** This program facilitates the conversion of a               decimal   number");
       printf("\n   to bases 2 through 9 **\n");
    	
    
     while(count<20 && value!=0)
    
       {
    
          list[count] = remainder (value, base);
    
         value = divide (value,base);
    
         printf("%d",list[count]);
    
         ++count;
    
       }
    
        printf(" to base %d is the result.\n",base);
    
        getch();
    
        return 0;
    
    }
    
    
    /*This function returns the whole number*/
    
    int divide (int a, int b )
    {
    int result;
    
    result = a/b;
    
    return result;
    }
    
    /*This function returns the remainder*/
    
    int remainder (int x, int y)
    {
    int remain;
    
    remain = x%y;
    
    return remain;
    }
    
    
    /* This function reverses the order of the remainders in the array */
    
    
    int reverse (int temp[])
    {
    
    
    
    
    return 0;
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    void rev_array ( int *a, int n )
    {
      int i, j;
    
      for ( i = 0, j = n - 1; i < j; i++, j-- ) {
        int hold = a[i];
        a[i] = a[j];
        a[j] = hold;
      }
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    how do i eliminate the leading zeros?

    Thanks for your last response. My new question is in the subject.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Count the number of leading zeros and reduce the array to the number of ints that need reversing. Modifying Preludes code -

    Code:
    #include <stdio.h>
    
    void rev_array ( int *a, int n )
    {
      int i, j, k;
    
      k=0;
      /*count number of leading zeros*/
      for(i=0;i<n;i++)
      {
    	  if(a[i]==0)
    		  k++;
    	  else
    		  break;
      }
    
      /*limit array accordingling*/
      a+=k;
      n-=k;
    
      for ( i = 0, j = n - 1; i < j; i++, j-- )
      { 
        int hold = a[i];
        a[i] = a[j];
        a[j] = hold;
      }
    }
    
    
    void print_array(int* array, int N)
    {
    	int i=0;
    	while(array[i]==0)
    		i++;
    
    	for(;i<N;i++)
    	{
    		printf("%d",array[i]);
    	}
    }
    
    
    int main()
    {
    	int array[10]={0,0,0,5,0,0,0,3,2,1};
    	rev_array(array,10);
    	print_array(array,10);
    	return 0;
    }
    Joe

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I dont know where you have the leading zeros, but before you print, you can check the value, and if it is a zero, dont print it.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    Could you simplify it?

    some of the concepts in Prelude's reply we haven't dealt with yet in class, so I don't quite grab them. (thanks anyway)

    However, the simple version from Salem, that works for me! could you incorporate your response into that code instead?

    The array is for 20 elements. I have an answer of only three elements, so I have 17 zeros and then the 3 digits.

    Thanks.

  7. #7
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    When I run the code, I get 123005. What leading zeros are you referring to?

    Code:
    void print_array(int* array, int N)
    {
    int i=0;
    
    while(array[i]==0)
    i++;
    
    for(;i<N;i++)
    {
    if(array[i] != 0)  // To stop the printing of 0s
    printf("%d",array[i]);
    }
    }
    This took out the zeros when I ran the code you provided.
    Last edited by MethodMan; 11-03-2002 at 05:34 PM.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    those zeros

    I'm testing with 45 converting to base 6. Remainders are 311.

    Reverse order is 113, but I'm getting 00000000000000000113. These are the zeros I'm trying to eliminate.

    Please note that I am running my original code with the inclusion of Salem's reverse order code.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: those zeros

    Where do you initialise variables value and base before using them? I can't see that happening.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    complete code

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    
    int divide (int, int);		/*function prototype*/
    
    int remainder (int, int);  /*function prototype*/
    
    int reverse (int []);      /*function prototype*/
    
    int main ()
    {
    	int value, base,i, list[20], count = 0;
    
       printf("\n** This program facilitates the conversion of a decimal number");
       printf("\n   to bases 2 through 9 **\n");
    	printf("\nEnter a decimal value to be converted\t:");
    	scanf("%d",&value);
    	printf("\nEnter the (base) to be converted to :\t");
    	scanf("%d",&base);
       printf("\n");
    
    
       for (i=0;i<20;i++)  /*initializing the array */
       		list[i] =0;
    
    
    	while(count<20 && value!=0)
    
       {
    
    	  list[count] = remainder (value, base);
    
         value = divide (value,base);
    
         printf("%d",list[count]);
    
         ++count;
    
       }
    
    
        printf(" is the original order of the remainders.\n\n");
    
    
        reverse(list);
        printf(" to base %d is the result.\n",base);
        /* I can't get rid of these leading zeros. */
    
        getch();
    
    	 return 0;
    }
    
    
    /*This function returns the whole number*/
    
    		int divide (int a, int b )
    		{
    		int result;
    
    		result = a/b;
    
    		return result;
    		}
    
    /*This function returns the remainder*/
    
    		int remainder (int x, int y)
    		{
    		int remain;
    
    		remain = x%y;
    
    		return remain;
    		}
    
    
    /* This function reverses the order of the remainders in the array */
    
          int reverse (int temp[])
          {
           int i;
    
           for ( i = 19 ; i >= 0 ; i-- )
    
    
           printf( "%d", temp[i] );
    
          return 0;
    
          }

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    If you just need to print the array backwards rather than actually reversing it (as per Salems code) then -

    Code:
    int main()
    {
    	int i,j;
    	int array[10]={0,0,0,5,0,0,0,3,1,1};
    	for ( i = 9 ; i >= 0 ; i-- ) 
    	{
    		
    		if(array[i]==0)
    		{
    			j=i;
    			while(j>=0)
    			{
    				if(array[j]!=0)
    					break;
    				else
    					j--;
    			}
    			if(j<0)
    				continue;
    					
    		}
    			
    		printf( "%d ", array[i] );
    	}
    
    	return 0;
    }
    Joe

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    
    void PrintRev(int arr[], int len)
    {
        int *s = arr;
        int *e = arr + len - 1;
    
        while (*e == 0 && e >= s) e--;    
        
        while (e >= s) 
            printf("%d ", *e--);
    }
    
    int main()
    {
        int arr1[10]={1,2,3,4,0,0,0,0,0,0};
        
        PrintRev(arr1, sizeof arr1 / sizeof arr1[0]);   
        putchar('\n');
        return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    Smile thanks guys

    everyone's been great!!

    Have a great week.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print Array in reverse order
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 12:41 PM
  2. Divide and Conquer: array in the reverse order
    By Steamer in forum C Programming
    Replies: 11
    Last Post: 03-08-2004, 06:31 PM
  3. Array's Help
    By bmx4christ in forum C Programming
    Replies: 15
    Last Post: 12-08-2003, 11:40 AM
  4. Printing name in reverse order and in caps
    By Guti14 in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2003, 07:02 AM
  5. read into array and spit out in reverse order
    By steven in forum C Programming
    Replies: 4
    Last Post: 09-07-2001, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21