473,626 Members | 3,388 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 1633
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
6157
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 values from a html form that consists of about 10 checkbox and a textbox where user have to key in a value to perform a search. From python tutors, I learned that I have to use the following method:
8
2031
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 have the statement: "typedef unsigned long int word32" and later on: "word32 b" referencing the third bit of the integer. How do I do the same in Python?? If this is a stupid, I apologize. It's amazing what the lack of sleep does to the brain....
8
7573
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
2874
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: error: using obsolete binding at 'j'
10
5369
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 string as an input and print it in reverse order.(don't use bulit-in/library function). Sample Output Enter any string: i m Kashif
16
2612
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 where someone suggested to me I was spending too much time writing error messages, and that I was therefore missing the benefit of using a scripting language. The idea, apparently, is that the PHP interpreter writes all the error messages that are...
4
2646
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 don't work for myself if you get my drift....anyways even if this is not asked of you...it might get you on friendly terms with the lawyers in the office The code is written to extract information from a table with contract information and divide...
3
1943
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 has logged in. I set a loggedin session in the log in page, which I have tested and works fine in other scripts. I am displaying news on the homepage (which is stored in a mysql database), and this works fine when the user accesses the homepage...
2
3859
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 http://pyro.sourceforge.net/manual/8-example.htm I have two computers to run Server and Client. ############################################
0
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8641
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2628
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 we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.