473,499 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

my code has failed to print out the last number i enter in the reverse order

3 New Member
its here why isit like that
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int z[10];
  3.     int y;
  4.     void reverse_order(int x[],int y)
  5.     {
  6.     for (y = 9; y >=0; --y)
  7.       {
  8.              printf("%d \t",x[y]);
  9.         }
  10.         }
  11.  int main(void)  {
  12.  
  13.  printf("Enter any numbers of your choice \n");
  14.         for (y = 0; y <10; ++y)
  15.         {
  16. scanf("%d\t", &z[y]);
  17.  
  18.             }
  19.  printf("the reverse of the entered numbers is\n");
  20.  reverse_order(z,y);
  21.  return 0;
  22.  }
Sep 4 '07 #1
8 1630
jimwawar
15 New Member
its here why isit like that
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int z[10];
  3.     int y;
  4.     void reverse_order(int x[],int y)
  5.     {
  6.     for (y = 9; y >=0; --y)
  7.       {
  8.              printf("%d \t",x[y]);
  9.         }
  10.         }
  11.  int main(void)  {
  12.  
  13.  printf("Enter any numbers of your choice \n");
  14.         for (y = 0; y <10; ++y)
  15.         {
  16. scanf("%d\t", &z[y]);
  17.  
  18.             }
  19.  printf("the reverse of the entered numbers is\n");
  20.  reverse_order(z,y);
  21.  return 0;
  22.  }
You are using two different size loops. In you enter you are using 10 for a loop which goes 0,1,2,3,4,5,6,7,8,9 or 10 entries in your reverse loop you are 9 which goes 0,1,2,3,4,5,6,7,8 or 9 entries. Change you reverse loop to 10 and it will work as you expect.

Jim
Sep 4 '07 #2
JosAH
11,448 Recognized Expert MVP
I'd say get rid of that '\t' in the scanf format specifier.

kind regards,

Jos
Sep 4 '07 #3
Banfa
9,065 Recognized Expert Moderator Expert
You are using two different size loops. In you enter you are using 10 for a loop which goes 0,1,2,3,4,5,6,7,8,9 or 10 entries in your reverse loop you are 9 which goes 0,1,2,3,4,5,6,7,8 or 9 entries. Change you reverse loop to 10 and it will work as you expect.
You statement about the reverse for loop is not true.

it starts at 9 and iterates while y >= 0 for values 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 inside the loop.
Sep 4 '07 #4
jimwawar
15 New Member
You statement about the reverse for loop is not true.

it starts at 9 and iterates while y >= 0 for values 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 inside the loop.
You are correct, I should have said 11 for the input and 10 for the output. The result is the same, the arrays are not of equal length.

Jim
Sep 4 '07 #5
JosAH
11,448 Recognized Expert MVP
You are correct, I should have said 11 for the input and 10 for the output. The result is the same, the arrays are not of equal length.

Jim
That isn't true either; for (int y= 0; y < 10; ++y) loops ten times, not eleven times.

kind regards,

Jos
Sep 4 '07 #6
RRick
463 Recognized Expert Contributor
From the original code,
  • Why are you passing y to the subroutine, when you overwrite its value in the for loop?
  • If y is the size of the array (i.e y=10) then the - -y decrement will work
  • If y is the index to the last entry (i.e. y=9), then change - -y to y- -
Sep 4 '07 #7
Ganon11
3,652 Recognized Expert Specialist
  • If y is the size of the array (i.e y=10) then the - -y decrement will work
  • If y is the index to the last entry (i.e. y=9), then change - -y to y- -
Shouldn't both work, since the last statement in the for loop header is only executed at the end of the loop?
Sep 4 '07 #8
RRick
463 Recognized Expert Contributor
You're right. The decrement order doesn't matter. It's the middle conditional that gets checked after the intialization and each loop.

Sooo, we are back to the loops. Both loops are using a different countint base, but end up with the same number of entries.

When you remove the '\t' from the printf, does your program work correctly??
Sep 5 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6142
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get...
8
2020
by: Lucas Raab | last post by:
I am currently in the process of porting some C code into Python and am stuck. I don't claim to be the greatest C/C++ programmer; in fact, my skills at C are rudimentary at best. My question is I...
8
7542
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
26
2846
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
10
5348
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
16
2590
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project...
4
2637
by: Kosmos | last post by:
Hey guys...as a relatively new programmer, I try to give back where I can. Below is the code for a useful program I believe many of you could use...just don't use the same exact code because...well I...
3
1926
by: Hazza | last post by:
Hi, I am using PHP and mysql to create a website. I am fairly new to PHP, and thus am grateful to anyone who helps! Firstly I am running a homepage, that displays additional content if a user...
2
3846
by: jamitwidme | last post by:
Hello everyone Can someone help me fix this problem? I am using an example from Pyro(Python Remote Object) website directly. It is the last example from...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.