我想要做的是将make 1函数声明为数组(我认为我得到了),另一个函数用于打印数组。代码现在完美地运行它是如何但我正在尝试在使用函数时如何执行它们。有人可以帮我吗?感谢
#include <iostream>
using namespace std;
//Function Prototypes
void ArrayInt(double&);
void printUser();
int main(){
double alpha[50];
//Call Function
ArrayInt(alpha[50]);
//Start for loop
for ( int i = 0; i < 50; i++ ){
if (i < 25){
alpha[i] = i*i;
}
else{
alpha[i] = 3 * i;
}
}
for (int i = 0; i <50; i++){
if ( (i+1) % 10 == 0 ){ //10 numbers per line
cout << endl;
}
}
system ("pause");
return 0;
}
void arrayInt(double& ){
//Declare and Initialize Array
double alpha[50];
}
void printUser(){
//Output to user
cout << alpha[i] << " ";
}
答案 0 :(得分:2)
试试这个。
#include <iostream>
using namespace std;
// Function Prototypes
void ArrayInit(double array[]); // you might want to initalize parameter array...
void PrintUser(double value); // you must call this function with parameter
/*
//Function Prototypes
void ArrayInt(double&);
void printUser();
*/
int main(){
double alpha[50];
//Call Function
// ArrayInt(alpha[50]); // this is just about alpha[50], not whole array but just double type element
ArrayInit(alpha);
/*
this can be moved to function ArrayInit()
//Start for loop
for ( int i = 0; i < 50; i++ ){
//Start if then statement
//first 25 is the square of the index variable 'i'
if (i < 25){
alpha[i] = i*i;
}
else{
alpha[i] = 3 * i;
}
} //end for loop
*/
//Start for loop
for (int i = 0; i <50; i++){
//Start if then statement
PrintUser(alpha[i]); // print user value
if ( (i+1) % 10 == 0 ){ //10 numbers per line
cout << endl;
} //end if
} //end for loop
system ("pause");
return 0;
}
// fixed ArrayInit
void ArrayInit(double alpha[]) {
//Start for loop
for ( int i = 0; i < 50; i++ ){
//Start if then statement
//first 25 is the square of the index variable 'i'
if (i < 25){
alpha[i] = i*i;
}
else{
alpha[i] = 3 * i;
}
} //end for loop
}
void PrintUser(double value){ // print parameter value
//Output to user
cout << value << " ";
}
答案 1 :(得分:1)
目标1:1声明数组的函数
目标2:打印数组的另一个功能
这个怎么样:(评论补充澄清)
#include <iostream>
using namespace std;
//Function Prototypes
void ArrayInt(double**, int);
void printUser(double*);
int main(){
double *alpha; // Defines a pointer to a double.
//Call Function
ArrayInt(&alpha, 50); // Passes a pointer to the variable 'alpha' to the function.
// OR: alpha = arrayInt2(50); works also
// Put values in our newly created array ...
for (int i = 0; i < 50; i++){
//Start if then statement
//first 25 is the square of the index variable 'i'
if (i < 25){
alpha[i] = i*i;
}
else{
alpha[i] = 3 * i;
}
} //end for loop
printUser(alpha);
system("pause");
return 0;
}
void arrayInt(double* *_alpha, int size) {
// '*_alpha' makes '_alpha' a pointer to another pointer (in this case
// a pointer to the variable 'alpha' above).
//Declare and Initialize Array
*_alpha = new double[size];
// In this case, '*_alpha' dereferences the pointer so that when we
// set a value, it will affect the variable 'alpha' we gave it.
memset(*_alpha, 0, sizeof(double)*size); // (reset all to 0)
}
double* arrayInt2(int size) {
//Declare and Initialize Array
return (double*)memset(new double[size], 0, sizeof(double)*size);
}
void printUser(double* _alpha) {
// Since we don't need to create and *return* a value back, only a
// reference to the original array is passed in.
//Output to user
//Start for loop
for (int i = 0; i <50; i++){
//Start if then statement
if ((i + 1) % 10 == 0){ //10 numbers per line
cout << _alpha[i] << " ";
} //end if
} //end for loop
// Note: [0] == *alpha - If you dereference the pointer to double
// you get the first array entry, which is the same as [0].
}