Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

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 siddh@yahoo.co.in
Fast reply would help me a lot.Bye.

On Jun 5, 10:19 pm, Siddhu <siddh@gmail.com> wrote:

while (i<5){
    r[i]=x[i]-y[i];
    i++;

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.h> is 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

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

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

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

Thomas J. Gritzan wrote:
> Siddhu schrieb:
>> [..]
>> #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 <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
On Jun 5, 4:45 pm, "Victor Bazarov" <v.Abaza@comAcast.net> wrote:

> Thomas J. Gritzan wrote:
> > Siddhu schrieb:
> >> [..]
> >> #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".

--
James Kanze (GABI Software)             email:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

On Jun 5, 4:43 pm, Zeppe

<zep_p@.remove.all.this.long.comment.yahoo.it> wrote:
> 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:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

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)

Victor Bazarov wrote:
> Thomas J. Gritzan wrote:
>> Siddhu schrieb:
>>> [..]
>>> #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.
-----------------

So next time I call it antiqu(i)ated :-)

--
Thomas
http://www.netmeister.org/news/learn2quote.html

On Jun 5, 6:19 pm, Pete Becker <p@versatilecoding.com> wrote:

> 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:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

"James Kanze" wrote:
>On Jun 5, 6:19 pm, Pete Becker <p@versatilecoding.com> wrote:
> 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.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc