473,385 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Kindly help me to find the reason:

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<iostream.h>
#include<conio.h>
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'<<x[i]<<'\t'<<y[i]<<'\t'<<r[i]<<'\n';
i++;
}while(i<5);
getch();
}

OUTPUT of the ABOVE PROGRAM goes as:

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]
I need aa helping hand from anyone who could explain how it turns up.
Please reply to me & my email ID goes as si******@yahoo.co.in
Fast reply would help me a lot.Bye.

Jun 5 '07 #1
11 1656
On Jun 5, 10:19 pm, Siddhu <siddh...@gmail.comwrote:
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<iostream.h>
#include<conio.h>
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'<<x[i]<<'\t'<<y[i]<<'\t'<<r[i]<<'\n';
i++;

}while(i<5);
getch();
}

OUTPUT of the ABOVE PROGRAM goes as:

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]
I need aa helping hand from anyone who could explain how it turns up.
Please reply to me & my email ID goes as siddh...@yahoo.co.in
Fast reply would help me a lot.Bye.
while (i<5){
r[i]=x[i]-y[i];
i++;
}

Jun 5 '07 #2
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<iostream.h>
The name of the header is <iostream>. The old form <iostream.his depreciated.
#include<conio.h>
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
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<iostream.h>
#include<conio.h>
void main()
The three lines above should be changed to

#include <iostream>
using namespace std;
#include <conio.h// platform-specific stuff, like 'clrscr'
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];
By the time the program gets to work on this statement, 'i' has
already been incremented. So, inside this 'while' loop the value
of 'i' changes from 1 to 5 (included), instead of supposedly
intended 0 to 4 (proper indexing in a C++ array of size 5).

That means that as the last iteration of this loop, you step over
the bounds of the 'r' array and assign to an element r[5] that
does NOT exist. That means the program has undefined behaviour,
and most likely it changes the value of some memory you didn't
intend to change (like y[0]).

Why do you use 'while' here instead of 'for'? Is that the
requirement of your assignment? In that case you need to think
of changing 'i' as the last statement of the loop _body_, and not
as part of the loop _condition_.
clrscr();
cout<<"\nThe contents of the array are:\n";
i=0;
do
{ cout<<'\t'<<x[i]<<'\t'<<y[i]<<'\t'<<r[i]<<'\n';
i++;
}while(i<5);
getch();
}

OUTPUT of the ABOVE PROGRAM goes as:

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]
I need aa helping hand from anyone who could explain how it turns up.
Please reply to me & my email ID goes as si******@yahoo.co.in
No, we don't reply to e-mails. Post here, read here.
Fast reply would help me a lot.Bye.
We do as we can.

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 #4
Siddhu wrote:
#include<iostream.h>
#include <iostream>

do not put .h extension on the standard library headers, it's obsolete
#include<conio.h>
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'<<x[i]<<'\t'<<y[i]<<'\t'<<r[i]<<'\n';
i++;
}while(i<5);
again, why not to use a for?
getch();
non standard, again, I think. You can use

while(std::cin.get() != '\n');
}

OUTPUT of the ABOVE PROGRAM goes as:

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]
the out-of-bound index on r in this case will modify the content of the
array y, that is contiguous in the stack. Anyway, the behaviour is
usually undefined.
I need aa helping hand from anyone who could explain how it turns up.
Please reply to me & my email ID goes as ******************
do not post your mail address in the newsgroups if you want to save you
from the spam. It's an advice ;)
Fast reply would help me a lot.Bye.
There you go!

Regards,

Zeppe
Jun 5 '07 #5
Thomas J. Gritzan wrote:
Siddhu schrieb:
>[..]
#include<iostream.h>

The name of the header is <iostream>. The old form <iostream.his
depreciated.
Interesting use of the word "depreciated", as if "the old form" used
to have some value (that the OP knows of) that it now has lost... :-)

I sincerely hope the OP won't get it confused with "deprecated" which
is not applicable to <iostream.h>...
[..]
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
On Jun 5, 4:45 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Thomas J. Gritzan wrote:
Siddhu schrieb:
[..]
#include<iostream.h>
The name of the header is <iostream>. The old form <iostream.his
depreciated.
Interesting use of the word "depreciated", as if "the old form" used
to have some value (that the OP knows of) that it now has lost... :-)
The "old form" was the de facto standard before ISO went to
town.
I sincerely hope the OP won't get it confused with "deprecated" which
is not applicable to <iostream.h>...
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
On Jun 5, 4:43 pm, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.itwrote :
I think. You can use
while(std::cin.get() != '\n');
Not a very good idea. If I terminate input using the system's
EOF character (^D under Unix, at least by default---I think it's
^Z under Windows), this goes into an endless loop.

I'm not sure why anyone would want to do input at this point
anyway.

--
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 #8
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
Victor Bazarov wrote:
Thomas J. Gritzan wrote:
>Siddhu schrieb:
>>[..]
#include<iostream.h>
The name of the header is <iostream>. The old form <iostream.his
depreciated.

Interesting use of the word "depreciated", as if "the old form" used
to have some value (that the OP knows of) that it now has lost... :-)
Oops, what a typo :-)
I sincerely hope the OP won't get it confused with "deprecated" which
is not applicable to <iostream.h>...
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 <X>
header for the <X.hheader for C++ includes, or <iostreaminstead of the
deprecated header <iostream.h>. 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
On Jun 5, 6:19 pm, Pete Becker <p...@versatilecoding.comwrote:
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.
If the toolset doesn't work, use another one that does. There
are plenty to choose from.

--
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 6 '07 #11
"James Kanze" wrote:
>On Jun 5, 6:19 pm, Pete Becker <p...@versatilecoding.comwrote:
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.
>If the toolset doesn't work, use another one that does. There
are 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © # Python © © import sys © © nn = len(sys.argv) © © if not nn==5: © print "error: %s search_text replace_text in_file out_file" % sys.argv
3
by: Josh Armstrong | last post by:
I have a dropbox (cboReasons) in a form, when the user selects a reason from the dropbox the form needs to look in another table (lvsn_DefaultSettings) and see if there is a entry for that reason...
9
by: Nak | last post by:
Hi there, I'm getting some strange warnings appearing while I'm compiling a couple of setup projects I have in a solution. I'm being informed of the following warnings, WARNING: Unable to...
1
by: francescomoi | last post by:
Hi. I'm trying to create a website where users can bet on some Sports Pools (e.g.: a football match result), but I don't find any PHP script. Do you know anyone? Thank yoy very much.
2
by: semedao | last post by:
Hi All, I developed Outlook add-in with VS 2005 , everything work fine , until the machine reboot unnormally , when I opened the solution again many projects was not there ! (I have more then...
2
by: shaveta | last post by:
The problem is like this:- A maze is a rectangular area, with m rows and n columns, with an entrance and an exit. The interior of the maze contains obstacles. The entrance is at the upper-left...
1
by: Chris | last post by:
Hi, I am working on a small which involves a touchscreen and I need to create a on-screen keyboard. I need some assistance with a few questions 1. For the the buttons, do I user their text as...
1
by: Sharad Maloo | last post by:
Hi, I am running the image file of my project, it is giving following segmentation fault: ERROR----- > Program received signal SIGSEGV, Segmentation fault. 0x004240cb in strlen () from...
9
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.