Thread: printing backward

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    77

    printing backward

    i given a question:

    write a sample code to reverse the content to 12543

    Code:
    {
         int num[5] = {3,4,6,2,1};
         
        ADD YOUR SAMPLE CODE HERE
    
         printf("%d,%d%d%d%d", num[0],num[1],num[2],num[3],num[4]);
    return 0;
    
    }

    so i have written
    Code:
      int i, j;
     
    for(i = 0, j =4; i <5; i++, j--)
     num[j] = num[i];

    but then the output is 12621

    i cant change the printf thing nor the num[5] things.. so what should i do to print 12643

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you swap two values? Do that for half of the length. Otherwise, you're doing this:

    3 = 1
    4 = 2
    6 = 6
    2 = 2 (because you overwrote the four already)
    1 = 1 (because you overwrote the three already)


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Because,
    Code:
    num[j] = num[i];
    overwrites elements from middle to end of the array.
    You need to SWAP the values.
    Edit:too late
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by quzah View Post
    How do you swap two values? Do that for half of the length. Otherwise, you're doing this:

    3 = 1
    4 = 2
    6 = 6
    2 = 2 (because you overwrote the four already)
    1 = 1 (because you overwrote the three already)


    Quzah.
    yes i know... but i have no idea how the code already... u know how ?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by stevesmithx View Post
    Because,
    Code:
    num[j] = num[i];
    overwrites elements from middle to end of the array.
    You need to SWAP the values.
    Edit:too late
    how to i swap the value?i cant change the int num

  6. #6
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Hint 1:use a temporary variable.
    if you are not able to figure out use Hint 2.

    Hint 2:Google "swap values"
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    #include<stdio.h>
    void swap(int *i,int*j)
    {
    int t;
    t = i;
    i = j;
    j = t;
    }




    main(void)
    {
    int num[5] = {3,4,6,2,1};
    int i, j;

    for (i = 0, j = 4; i < 3, j >= 2; i++, j--){
    num[i] = num[j];
    }

    swap(&num[4], &num[0]);
    swap(&num[5], &num[1]);


    printf("%d,%d,%d,%d,%d", num[0], num[1],num[2],num[3],num[4]);
    }


    help plz what i did wrong?i still get the same thing

  8. #8
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    looks like you have taken the code somewhere from the net and used it incorrectly in your program.(probably followed the lazy "Hint 2" solution).
    My guess is that you might not have learnt functions yet?.Just try to understand what happens inside the swap() function and try to use it inside your program without copy pasting the actual solution.
    PS: Use code tags when you post code next time.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by stevesmithx View Post
    looks like you have taken the code somewhere from the net and used it incorrectly in your program.(probably followed the lazy "Hint 2" solution).
    My guess is that you might not have learnt functions yet?.Just try to understand what happens inside the swap() function and try to use it inside your program without copy pasting the actual solution.
    PS: Use code tags when you post code next time.
    yes i haven learn the swap fuction.. so do i really need to use swap?this question maybe come out in exam.. i really need to know how to do it.. pls help me

  10. #10
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    yes i haven learn the swap fuction..
    What i meant is the "Functions" concept in C.
    so do i really need to use swap?
    yes.
    ?this question maybe come out in exam.. i really need to know how to do it.. pls help me
    Read my previous post.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by stevesmithx View Post
    What i meant is the "Functions" concept in C.

    yes.

    Read my previous post.
    #
    Code:
    include<stdio.h>
    void swap(int i,int j)
    {
    	int t;    //eg.   t = number[0] = 3
    	t = i;          number[0] = number [4] = 1
    	i = j;          number[4] = t = 3 //
        j = t;
    }
    
    main(void)
    {
    	int num[5] = {3,4,6,2,1};
    		
    	swap(num[0], num[4]);
    	swap(num[1],num[3]);
    
    	
    	printf("%d,%d,%d,%d,%d", num[0], num[1],num[2],num[3],num[4]);
    }
    so what i wrong?this is what i understand

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    i and j are not changing outside of the function which they are parameters to. To change something outside of the function, you need a pointer to the object you want to modify.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    i and j are not changing outside of the function which they are parameters to. To change something outside of the function, you need a pointer to the object you want to modify.

    --
    Mats
    oh okey thx i get it already=]

  14. #14
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    If you have just to print it backward, I think a smarter way could be to read it backward and print it... you don't need to reverse it.
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Printing
    By Dual-Catfish in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-25-2002, 08:10 PM