Thread: reverse number printing

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    80

    reverse number printing

    I have written program that suppose to print reverse number for given size

    Code:
    #include<stdio.h>
    
    int reverse ( int *p, int a[5])
    {
    	int i = 5;
    	for ( i = 5; i > 0; i--)
    	{
    		p = a[i];
    		printf("%d ", *p);
    		p++;
    	}
    
    
    }
    
    
    int main ()
    {
    	int result;
    	int i = 0;
    	int a[5] = {1, 2, 3, 4, 5 };
    	
    	result = reverse (&a, a[5]);
    	
    	return 0;
    }
    Program is not working as it suppose to do.

    What's fault in program ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,343
    I suggest that the function look like this:
    Code:
    void print_reverse(const int numbers[], int size)
    Here's why:
    • print_reverse makes it clear that you're printing the numbers in reverse, not reversing them in-place.
    • The return type is void because you're not returning anything.
    • The first parameter is named numbers because that's what the array contains. I have used array notion to indicate that we expect to deal with an array, even though you must keep in mind that numbers is a pointer.
    • The type of what numbers points to is const int because you only want to print, not to modify.
    • The second parameter is named size because that's what you want to print in reverse "for given size".
    • size is an int because a size is an integer (but if you have learnt about it, it would be better to make it a size_t).

    You would then call the function like this:
    Code:
    print_reverse(a, 5);
    The idea is that a is converted to a pointer to its first element, so there's no need to take its address (and in fact that would be wrong because the types would not match). Especially if you chose to make size a size_t, you might compute the size instead of hard coding it:
    Code:
    print_reverse(a, sizeof(a) / sizeof(a[0]));
    In your original code, passing a[5] is wrong because a[5] does not exist for an array a with 5 elements, and besides you want to pass the size.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    80
    Quote Originally Posted by laserlight View Post
    • print_reverse makes it clear that you're printing the numbers in reverse, not reversing them in-place..
    I want to reverse them in place

    Imagine numbers are stored as following

    0x00 = 1
    0x01 = 2
    0x02 = 3
    0x03 = 4
    0x04 = 5

    when I reverse them in place

    0x00 = 5
    0x01 = 4
    0x02 = 3
    0x03 = 2
    0x04 = 1

    That's what I want to do

    How it can be done ? I was trying to achieve that in my code but I couldn't get result

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,343
    In that case, I suggest writing two functions. First:
    Code:
    void print(const int numbers[], int size)
    This will print the first size number of numbers. Implement and test that this works, then implement:
    Code:
    void reverse(int numbers[], int size)
    This second function will not print at all. Rather, it will only reverse in-place, and then later you call the print function to print the result.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    44
    First, write a test function:

    Code:
    bool 
    arrays_equal(int *a1, size_t len. int * a2)
    {
        if (NULL == a1 || NULL == a2) 
            return len == 0; // equal for 0 elts, otherwise not equal
    
        for (int i = 0; i < len; ++i) 
            if (a1[i] != a2[i])
                return false;
    
        return true;
    }
    Now write your main function to assert what you want to be true:

    Code:
    #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
        #error "Need C11"
    #endif
    
    #define array_length(a) (sizeof (a) / sizeof (*a))
    
    void reverse_array(int *, size_t);
    
    int 
    main(void)
    {
        reverse_array(NULL, 0);    // These had better do something!
        reverse_array(NULL, 1);    // Write a test case.
    
        int a1[] = {1, 2, 3, 4, 5};
        reverse_array(a1, array_length(a1));
        assert(arrays_equal(a1,  array_length(a1), 
                            (int[]){5, 4, 3, 2, 1}));
    
        int a2[] = {3, 7};
        reverse_array(a2, array_length(a2));
        assert(arrays_equal(a2,  array_length(a2).
                            (int[]){7, 3}));
    
        int a3[] = {5};
        reverse_array(a3, array_length(a3));
        assert(arrays_equal(a3,  array_length(a3),
                             (int[]){5}));
    
        int a4[] = {};
        reverse_array(a4, array_length(a4));
        assert(arrays_equal(a4, array_length(a4), 
                             (int[]){5}));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a string in reverse
    By Evan Burns in forum C Programming
    Replies: 4
    Last Post: 07-28-2015, 01:59 AM
  2. Printing a reverse pyramid using C
    By djadil in forum C Programming
    Replies: 9
    Last Post: 04-19-2015, 02:57 AM
  3. Reverse order printing
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 08-19-2007, 06:47 AM
  4. Printing in reverse order
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 08-17-2007, 04:30 AM
  5. Printing my name in reverse
    By Guti14 in forum C++ Programming
    Replies: 8
    Last Post: 08-11-2003, 02:15 PM

Tags for this Thread