446,193 Members | 843 Online
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

# Kindly help me to find the reason:

 P: n/a Hai, i am student & i have struck up with the following program's output. Kindly help me to reason out why the output goes as which is given below. THE PROGRAM goes as; #include #include void main() { int i=0,x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},r[5]={0,0,0,0,0}; while(i++<5) r[i]=x[i]-y[i]; clrscr(); cout<<"\nThe contents of the array are:\n"; i=0; do { cout<<'\t'<
11 Replies

 P: n/a On Jun 5, 10:19 pm, Siddhu #include void main() { int i=0,x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},r[5]={0,0,0,0,0}; while(i++<5) r[i]=x[i]-y[i]; clrscr(); cout<<"\nThe contents of the array are:\n"; i=0; do { cout<<'\t'<

 P: n/a Siddhu schrieb: Hai, i am student & i have struck up with the following program's output. Kindly help me to reason out why the output goes as which is given below. THE PROGRAM goes as; #include The name of the header is . The old form This is not a C++ header. void main() This must be: int main() { int i=0,x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},r[5]={0,0,0,0,0}; while(i++<5) r[i]=x[i]-y[i]; The while loop body executes 5 times while i has the values 1 through 5. With r[5] you access r out of bounds. [...] The contents of the array are: 1 -1 0 2 4 -2 3 3 0 4 2 2 5 1 4 I need to know how the output for the very first line goes as 1 -1 0 though the program shows that there has been no change carried out for the array data y[0] By writing to r[5] your programm invokes undefined behaviour and in your case it chooses to manifest in this form. Just fix the bugs. It is easier to use a for loop: for (i=0; i<5; i++) { // loop body } -- Thomas http://www.netmeister.org/news/learn2quote.html Jun 5 '07 #3

 P: n/a Siddhu wrote: Hai, i am student & i have struck up with the following program's output. Kindly help me to reason out why the output goes as which is given below. THE PROGRAM goes as; #include #include void main() The three lines above should be changed to #include using namespace std; #include

 P: n/a Siddhu wrote: #include #include do not put .h extension on the standard library headers, it's obsolete #include that's non-standard void main() int main() int is the only return type allowed for main { int i=0,x[5]={1,2,3,4,5},y[5]={5,4,3,2,1},r[5]={0,0,0,0,0}; you should try to format your programs a little bit better... while(i++<5) r[i]=x[i]-y[i]; do { r[i]=x[i]-y[i]; } while(i++ < 5); or, much more readable (and less error prone for the initialization of i) for(int j = 0; j < 5; ++j) r[j]=x[j]-y[j]; clrscr(); non-standard cout<<"\nThe contents of the array are:\n"; std::cout cout doesn't use the namespaces, it's included in an obsolete version of the headers and it's definitely non-standard i=0; do { cout<<'\t'<

 P: n/a Thomas J. Gritzan wrote: Siddhu schrieb: >[..]#include The name of the header is . The old form ... [..] V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask Jun 5 '07 #6

 P: n/a On Jun 5, 4:45 pm, "Victor Bazarov" The name of the header is . The old form ... It depends on the meaning you give it. It's not deprecated by the ISO committee, but in terms of the history of C++ (which existed before 1998, when the ISO standard was published), it was standard, and it's generally not a good idea to use it in code written today. Which is pretty close to the connotation I give to "deprecated". -- James Kanze (GABI Software) email:ja*********@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 Jun 5 '07 #7

 P: n/a On Jun 5, 4:43 pm, Zeppe

 P: n/a James Kanze wrote: > I'm not sure why anyone would want to do input at this point anyway. It's for stupid IDE's in graphical environments, where command line programs run in a window that disappears as soon as the program ends. Pausing to wait for input keeps the window up so that you can see the program's output. Sigh. -- -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference." (www.petebecker.com/tr1book) Jun 5 '07 #9

 P: n/a Victor Bazarov wrote: Thomas J. Gritzan wrote: >Siddhu schrieb: >>[..]#include The name of the header is . The old form ... I just testet and with g++ -Wall I get this warning: ----------------- [...]: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the . To disable this warning use -Wno-deprecated. ----------------- So next time I call it antiqu(i)ated :-) -- Thomas http://www.netmeister.org/news/learn2quote.html Jun 5 '07 #10

 P: n/a On Jun 5, 6:19 pm, Pete Becker

 P: n/a "James Kanze" wrote: >On Jun 5, 6:19 pm, Pete Becker It's for stupid IDE's in graphical environments, where command lineprograms run in a window that disappears as soon as the program ends.Pausing to wait for input keeps the window up so that you can see theprogram's output. Sigh. >If the toolset doesn't work, use another one that does. Thereare plenty to choose from. I don't know which toolset it is that you suggest be abandoned. The OS, Windows, or the compiler, DevC bound with MingW. My experience has been that there is no perfect toolset, and if the OP abandons either of these he will flounder the rest of his life, searching for this elusive, ever more wonderful, toolset. There comes a point where you must say "This is good enough, I can live with this." PS. This advice also applies to cars and women. Jun 6 '07 #12

### This discussion thread is closed

Replies have been disabled for this discussion.