# printing backward

This is a discussion on printing backward within the C Programming forums, part of the General Programming Boards category; i given a question: write a sample code to reverse the content to 12543 Code: { int num[5] = {3,4,6,2,1}; ...

1. ## 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};

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. 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.

3. Because,
Code:
num[j] = num[i];
overwrites elements from middle to end of the array.
You need to SWAP the values.
Edit:too late

4. Originally Posted by quzah
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. Originally Posted by stevesmithx
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. Hint 1:use a temporary variable.
if you are not able to figure out use Hint 2.

7. #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. 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.

9. Originally Posted by stevesmithx
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. 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

11. Originally Posted by stevesmithx
What i meant is the "Functions" concept in C.

yes.

#
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. 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

13. Originally Posted by matsp
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. 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.