尝试让它获取输入,从 numOne 创建一个数组,数组的零点是 numTwo 并将它加 1 到数组其余位置的值,直到数组被填满,然后它应该打印数组以及数组的位置[3]和[4]的总和。
int arrayOperations(int numOne, int numTwo, int numThree,int numFour);
int main(){
int numOne;
int numTwo;
int numThree;
int numFour;
cout<<"Enter a number 3 through 9"<<endl;
cin>>numOne;
cout<<"Now a number 5 through 17"<<endl;
cin>>numTwo;
int arrayOperations(int numOne, int numTwo, int numThree,int numFour);
return 0;
}
int arrayOperations(int numOne, int numTwo, int numThree,int numFour){
int newArray[numOne];
int numSum;
newArray[0]=numTwo;
for(int i = 0; i < numOne; i++){
newArray[i]=newArray[i-1]+1;
cout<<newArray[i]<<endl;
}
numSum = newArray[3] + newArray[4];
return numSum;
}
函数调用的语法与函数声明的语法不同。此外,您应该从 1 开始 for 循环,因为 i-1 索引将给出 -1,这将返回一些垃圾值。这是正确的代码..
#include <iostream>
using namespace std;
int arrayOperations(int numOne, int numTwo, int numThree,int numFour);
int main() {
int numOne;
int numTwo;
int numThree;
int numFour;
cout<<"Enter a number 3 through 9"<<endl;
cin>>numOne;
cout<<"Now a number 5 through 17"<<endl;
cin>>numTwo;
cout<<arrayOperations(numOne,numTwo, numThree,numFour);
return 0;
}
int arrayOperations(int numOne, int numTwo, int numThree,int numFour) {
int newArray[numOne];
int numSum=0;
newArray[0]=numTwo;
for(int i = 1; i < numOne; i++) {
newArray[i]=newArray[i-1]+1;
cout<<newArray[i]<<endl;
}
numSum = newArray[3] + newArray[4];
return numSum;
}
我是一名优秀的程序员,十分优秀!