首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

,数组元素读不进去 多谢

2012-08-15 
在线等,数组元素读不进去 谢谢[codeC/C++][/code] 代码是读入两组数组,实现多项式加法,把系数放一个数组,

在线等,数组元素读不进去 谢谢
[code=C/C++][/code] 代码是读入两组数组,实现多项式加法,把系数放一个数组,指数放一个数组从高到底,没有的输入零。
把对应的系数数组相加,每个多项式项数是3。所以result为系数相加的结果,把系数数组coef每个两项相加就是最有加法后的结果。 
在cout result 中是很大的数,数组coef的值没有存进去。把coef[6] expo[6]修改成全局变量也不行,输出coef[1]是零。

#include<iostream>
#define degree 6

using namespace std;

int main(){
  int i,n;
  
  int coef[6],expo[6], result[6];
  cout<<"Please enter 6 coefficients following the order of exponents for two polynomial number"<<endl;
cout<<"If a term without exponent please enter 0"<<endl;
for(n=0; n <6; n++){
cout<<"Please enter "<<n+1<<" coefficient: "; // the 1st 2nd 3rd 4th 5th 6 th coefficient;
cin >> coef[i];
  cout<<coef[i]<<endl; //[color=#FF0000][/color]正常输出
}
   
for( n=0; n <6; n++){
cout<<"Please enter "<<n+1<<" exponnet: "; // the 1st 2nd 3rd 4th 5th 6 th exponent;
cin >> expo[i];
  cout << expo[i]<<endl;[color=#FF0000][/color]//正常输出
}
   
cout<<"The result is :" <<endl;
   
for( i=0; i<6; i++){
result[i]= coef[i]+coef[i+2]; [color=#FF0000][/color]//数组6个元素中 第1个和第三个相加..
  cout<<result[i]<<"X^"<<expo[i]; [color=#FF0000][/color]// 输出是乱七八糟的长数字,单独输出 coef[1]没有值。expo[i]也没有值
}
  cout<<endl;
return 0;


}




[解决办法]
这地方开始出错啦。
for( i=0; i<6; i++){
result[i]= coef[i]+coef[i+2]; [color=#FF0000][/color]//数组6个元素中 第1个和第三个相加..
当i=5的时候,i+2是7,这已经出界了。

热点排行