检索和显示偶数
Retrieving and displaying even number
所以我试图让我的偶数出现并告诉我用户输入的结果。不知道为什么我的函数在我的 main 中是错误的,当我试图让它在我的 even 函数中输出时不会打印。不确定为什么?也很好奇我应该锁定并使我的数组保持不变吗?它没有改变,但可能感觉我必须使其保持不变并锁定它,以防万一我必须更改某些内容以检查偶数。另外我是初学者,几乎没有学习数组。
#include <iostream>
#include <string>
using namespace std;
// putting our voids first to declare be declare and run first
void fillUp(int num[], int size)
{
//user input
cout<<"Enter "<<size<<" and then press the ENTER/RETRUN: ";
for(int i = 0; i < size; i++)
{
cin>> num[i];
}
}
//obtaining user input and totaling them up
int total(int num[], int size)
{
int total = 0;
for(int i = 0; i < size;i++)
{
total += num[i];
}
return total;
}
int EvenElements(int num[], int size, int& evenCounter)
{
for(int i = 0; i<size;i++)
{
if(num[i] % 2 == 0)
{
evenCounter++;
}
}
return(evenCounter);
}
//display the array in a row
void display(int num[], int size)
{
for(int i = 0; i<size;i++)
{
cout<<num[i]<<" ";
}
}
//main function to display the results of the user input
int main()
{
int numOne[4], numTwo[5], numThree[6], evenCounter;
fillUp(numOne, 4);
fillUp(numTwo, 5);
fillUp(numThree, 6);
cout<<"The numbers in the array are: ";
display(numOne, 4);
cout<<" and the total of these numbers is "<<total(numOne, 4)
<<endl;
cout<<"The numbers in the array are: ";
display(numTwo, 5);
cout<<" and the total of these numbers is "<<total(numTwo, 5)
<<endl;
cout<<"The numbers in the array are: ";
display(numThree, 6);
cout<<" and the total of these numbers is "<<total(numThree, 6)
<<endl;
cout<<"This is how many evens were in the array: ";
EvenElements(evenCounter);
}
您的 EvenElements()
需要三个参数,而您只传递一个参数,因此,您的代码将无法编译。尝试以下操作(阅读最后 3 行的评论):
#include <iostream>
#include <string>
using namespace std;
// putting our voids first to declare be declare and run first
void fillUp(int num[], int size)
{
//user input
cout<<"Enter "<<size<<" and then press the ENTER/RETRUN: ";
for(int i = 0; i < size; i++)
{
cin>> num[i];
}
}
//obtaining user input and totaling them up
int total(int num[], int size)
{
int total = 0;
for(int i = 0; i < size;i++)
{
total += num[i];
}
return total;
}
int EvenElements(int num[], int size, int& evenCounter)
{
for(int i = 0; i<size;i++)
{
if(num[i] % 2 == 0)
{
evenCounter++;
}
}
return(evenCounter);
}
//display the array in a row
void display(int num[], int size)
{
for(int i = 0; i<size;i++)
{
cout<<num[i]<<" ";
}
}
//main function to display the results of the user input
int main()
{
int numOne[4], numTwo[5], numThree[6], evenCounter;
fillUp(numOne, 4);
fillUp(numTwo, 5);
fillUp(numThree, 6);
cout<<"The numbers in the array are: ";
display(numOne, 4);
cout<<" and the total of these numbers is "<<total(numOne, 4)
<<endl;
cout<<"The numbers in the array are: ";
display(numTwo, 5);
cout<<" and the total of these numbers is "<<total(numTwo, 5)
<<endl;
cout<<"The numbers in the array are: ";
display(numThree, 6);
cout<<" and the total of these numbers is "<<total(numThree, 6)
<<endl;
cout<<"This is how many evens were in the array: ";
// Make sure you initialise evenCounter
evenCounter = 0;
// Call EventElement with the right parameters
EvenElements(numThree, 6, evenCounter);
// Display the result
cout<<evenCounter<<endl;
}
注意:考虑为您的尺码使用宏
#define SIZE_1 4
#define SIZE_2 5
#define SIZE_3 6
然后用适当的大小替换硬编码数字。
所以我试图让我的偶数出现并告诉我用户输入的结果。不知道为什么我的函数在我的 main 中是错误的,当我试图让它在我的 even 函数中输出时不会打印。不确定为什么?也很好奇我应该锁定并使我的数组保持不变吗?它没有改变,但可能感觉我必须使其保持不变并锁定它,以防万一我必须更改某些内容以检查偶数。另外我是初学者,几乎没有学习数组。
#include <iostream>
#include <string>
using namespace std;
// putting our voids first to declare be declare and run first
void fillUp(int num[], int size)
{
//user input
cout<<"Enter "<<size<<" and then press the ENTER/RETRUN: ";
for(int i = 0; i < size; i++)
{
cin>> num[i];
}
}
//obtaining user input and totaling them up
int total(int num[], int size)
{
int total = 0;
for(int i = 0; i < size;i++)
{
total += num[i];
}
return total;
}
int EvenElements(int num[], int size, int& evenCounter)
{
for(int i = 0; i<size;i++)
{
if(num[i] % 2 == 0)
{
evenCounter++;
}
}
return(evenCounter);
}
//display the array in a row
void display(int num[], int size)
{
for(int i = 0; i<size;i++)
{
cout<<num[i]<<" ";
}
}
//main function to display the results of the user input
int main()
{
int numOne[4], numTwo[5], numThree[6], evenCounter;
fillUp(numOne, 4);
fillUp(numTwo, 5);
fillUp(numThree, 6);
cout<<"The numbers in the array are: ";
display(numOne, 4);
cout<<" and the total of these numbers is "<<total(numOne, 4)
<<endl;
cout<<"The numbers in the array are: ";
display(numTwo, 5);
cout<<" and the total of these numbers is "<<total(numTwo, 5)
<<endl;
cout<<"The numbers in the array are: ";
display(numThree, 6);
cout<<" and the total of these numbers is "<<total(numThree, 6)
<<endl;
cout<<"This is how many evens were in the array: ";
EvenElements(evenCounter);
}
您的 EvenElements()
需要三个参数,而您只传递一个参数,因此,您的代码将无法编译。尝试以下操作(阅读最后 3 行的评论):
#include <iostream>
#include <string>
using namespace std;
// putting our voids first to declare be declare and run first
void fillUp(int num[], int size)
{
//user input
cout<<"Enter "<<size<<" and then press the ENTER/RETRUN: ";
for(int i = 0; i < size; i++)
{
cin>> num[i];
}
}
//obtaining user input and totaling them up
int total(int num[], int size)
{
int total = 0;
for(int i = 0; i < size;i++)
{
total += num[i];
}
return total;
}
int EvenElements(int num[], int size, int& evenCounter)
{
for(int i = 0; i<size;i++)
{
if(num[i] % 2 == 0)
{
evenCounter++;
}
}
return(evenCounter);
}
//display the array in a row
void display(int num[], int size)
{
for(int i = 0; i<size;i++)
{
cout<<num[i]<<" ";
}
}
//main function to display the results of the user input
int main()
{
int numOne[4], numTwo[5], numThree[6], evenCounter;
fillUp(numOne, 4);
fillUp(numTwo, 5);
fillUp(numThree, 6);
cout<<"The numbers in the array are: ";
display(numOne, 4);
cout<<" and the total of these numbers is "<<total(numOne, 4)
<<endl;
cout<<"The numbers in the array are: ";
display(numTwo, 5);
cout<<" and the total of these numbers is "<<total(numTwo, 5)
<<endl;
cout<<"The numbers in the array are: ";
display(numThree, 6);
cout<<" and the total of these numbers is "<<total(numThree, 6)
<<endl;
cout<<"This is how many evens were in the array: ";
// Make sure you initialise evenCounter
evenCounter = 0;
// Call EventElement with the right parameters
EvenElements(numThree, 6, evenCounter);
// Display the result
cout<<evenCounter<<endl;
}
注意:考虑为您的尺码使用宏
#define SIZE_1 4
#define SIZE_2 5
#define SIZE_3 6
然后用适当的大小替换硬编码数字。