请帮我找到原因: [英] Kindly help me to find the reason:

查看:59
本文介绍了请帮我找到原因:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,我是学生&我已经接受了以下程序'

输出。请帮助我推断为什么输出结果如下所示:

计划如下:


#包括< 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<<" \ n数组的内容是:\ n" ;;

i = 0;

do

{cout<<''\t''<< x [i]<<''\t''<< y [i]<<'' \t''<< r [i]<<''\ n'';

i ++;

} while(i< 5) ;

getch();

}


ABOVE PROGRAM的输出如下:


数组的内容是:

1 -1 0

2 4 -2

3 3 0

4 2 2

5 1 4


我需要知道第一行的输出如何为1

-1 0

尽管该程序显示没有为
$ b执行任何更改$ b数组数据y [0]

我需要任何可以解释它如何出现的人的帮助。

请回复我&我的电子邮件ID为 si******@yahoo.co.in

快速回复会对我有很大帮助.Bye。

解决方案

6月5日晚上10点19分,Siddhu< siddh ... @ gmail.comwrote:


Hai,我是学生&我已经接受了以下程序'

输出。请帮助我推断为什么输出结果如下所示:

计划如下:


#包括< 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<<" \ n数组的内容是:\ n" ;;

i = 0;

do

{cout<<''\t''<< x [i]<<''\t''<< y [i]<<'' \t''<< r [i]<<''\ n'';

i ++;


} while( i< 5);

getch();

}


ABOVE PROGRAM的输出如下:


数组的内容是:

1 -1 0

2 4 -2

3 3 0

4 2 2

5 1 4


我需要知道第一行的输出如何为1

-1 0
虽然程序显示没有对

进行更改数组数据y [0]

我需要任何可以帮助的人解释它是如何出现的。

请回复我&我的电子邮件ID就像siddh ... @ yahoo.co.in

快速回复会帮助我很多.Bye。



while(i< 5){

r [i] = x [i] -y [i];

i ++;

}


Siddhu schrieb:


Hai,我是学生&我已经接受了以下程序'

输出。请帮助我推断为什么输出结果如下所示:

计划如下:


#包括< iostream.h>



标题的名称是< iostream>。旧表单< iostream.his折旧。


#include< conio.h>



这不是C ++标题。


void main()


这必须是:

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];



while循环体执行5次,而我的值为1到5.

使用r [5]可以访问r out of界限。


[...]


数组的内容是:

1 -1 0

2 4 -2

3 3 0

4 2 2

5 1 4


我需要知道第一行的输出如何为1

-1 0

虽然程序显示没有进行更改

数组数据y [0]



通过写入r [5]你的程序调用undefined行为和在你选择以这种形式表现的你b $ b案件中。修复错误。


使用for循环更容易:


for(i = 0; i< 5; i ++)

{

//循环体

}


-

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


Siddhu写道:


Hai,我是学生&我已经接受了以下程序'

输出。请帮助我推断为什么输出结果如下所示:

计划如下:


#包括< iostream.h>

#include< conio.h>

void main()



上面的三行应改为


#include< iostream>

使用命名空间std;

#include< conio .h //特定于平台的东西,比如''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];



当程序开始处理此语句时,''我'已经增加了

。所以,在''while''循环中,''i'的值'/ b $ b'从1改为5(包括),而不是假设

意图0到4 (适当的索引在大小为5的C ++数组中)。

这意味着作为这个循环的最后一次迭代,你跨过了

的界限''r''数组并分配给元素r [5]

不存在。这意味着该程序具有未定义的行为,

并且很可能它会改变你没有想要改变的某些内存的值(如y [0])。


为什么你在这里使用''while''而不是''for'?那是你的作业的

要求吗?在这种情况下,你需要考虑改变''我'的
作为循环_body_的最后一个语句,而不是

作为循环_条件_的一部分。 />


clrscr();

cout<<" \ n数组的内容为:\ n" ;;

i = 0;

do

{cout<<''\t''<< x [i]<<'' \t''<< y [i]<<''\t''<< r [i]<<''\ n'';

i ++;

} while(i< 5);

getch();

}


ABOVE PROGRAM的输出为:


数组的内容为:

1 -1 0

2 4 -2

3 3 0

4 2 2

5 1 4


我需要知道第一行的输出如何为1

-1 0

虽然节目笑ws表示没有进行任何更改

阵列数据y [0]

我需要任何可以解释它如何出现的人的帮助。 />
请回复我和我我的电子邮件ID为 si******@yahoo.co.in



不,我们不回复电子邮件。发布在这里,请阅读此处。


快速回复会对我有很大的帮助。



我们尽我们所能。


V

-

请在通过电子邮件回复时删除资金''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<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.

解决方案

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++;
}


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


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


这篇关于请帮我找到原因:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆