Kindly help me to find the reason:

S

Siddhu

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=x-y;
clrscr();
cout<<"\nThe contents of the array are:\n";
i=0;
do
{ cout<<'\t'<<x<<'\t'<<y<<'\t'<<r<<'\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 (e-mail address removed)
Fast reply would help me a lot.Bye.
 
Y

YiPingQi

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=x-y;
clrscr();
cout<<"\nThe contents of the array are:\n";
i=0;
do
{ cout<<'\t'<<x<<'\t'<<y<<'\t'<<r<<'\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 (e-mail address removed)
Fast reply would help me a lot.Bye.


while (i<5){
r=x-y;
i++;
}
 
T

Thomas J. Gritzan

Siddhu said:
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 said:
#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=x-y;


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
}
 
V

Victor Bazarov

Siddhu said:
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;
{ 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=x-y;


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<<'\t'<<y<<'\t'<<r<<'\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 (e-mail address removed)


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
 
Z

Zeppe

Siddhu said:
#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=x-y;


do {
r=x-y;
} 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<<'\t'<<y<<'\t'<<r<<'\n';
i++;
}while(i<5);


again, why not to use a for?

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
 
V

Victor Bazarov

Thomas said:
Siddhu said:
[..]
#include<iostream.h>

The name of the header is <iostream>. The old form <iostream.h> is
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 said:

V
 
J

James Kanze

Thomas said:
Siddhu said:
[..]
#include<iostream.h>
The name of the header is <iostream>. The old form <iostream.h> is
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".
 
J

James Kanze

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

Pete Becker

James said:
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)
 
T

Thomas J. Gritzan

Victor said:
Thomas said:
Siddhu said:
[..]
#include<iostream.h>
The name of the header is <iostream>. The old form <iostream.h> is
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.h> header for C++ includes, or <iostream> instead of the
deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
 
J

James Kanze

James Kanze wrote:
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.
 
O

osmium

James Kanze said:
James Kanze wrote:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top