C ++无法找出数组和函数 [英] C++ Can't figure out arrays and functions
问题描述
可能重复:
停留在C ++函数和数组上
这个问题:我不能让它运行,我不得不让数组函数返回到主。感谢任何帮助。
需要使用二维数组来存储一周七天中每只猴子吃3磅猴的食物。 >
创建一个函数来获取每只猴子每周的每天吃的磅数。
创建第二个函数来确定通过数组计算所有的货币的总和,然后是一天的平均吃。
创建第三个函数,以确定哪只猴子吃了最少量的食物和什么日子。还输出当天猴子吃的量。
创建第四个函数来确定哪一只猴子在一天内吃了最多的食物。输出猴子数,吃掉的磅数和工作日。
#include< iostream>
using namespace std;
const int monkeys = 3;
const int weekdays = 7;
double monkeyWeek [monkeys] [weekdays];
double maximum;
double最少;
double average;
int index;
int dayCount;
double amount;
double amountEaten(double [] [weekdays],int);
double mostEaten(double [] [weekdays],int);
double leastEaten(double [] [weekdays],int);
int main(){
double mostBananas(double [] [weekdays],int);
double leastBananas(double [] [weekdays],int);
// double bananaAverage(double [[[weekdays],int);
}
double amountEaten(double array [] [weekdays],int){
cout< 请输入每只猴子每天食用的食物量。 << endl;
double amount = array [0] [0];
for(index = 0; index< monkeys; index ++)
{
for(dayCount = 0; dayCount< weekdays; dayCount ++)
{
cout ;& endl<< 请输入猴子吃的磅数
<< (index +1)
<< endl<< for day<< (dayCount +1)< :;
cin>> monkeyWeek [monkeys] [weekdays];
if(monkeyWeek [monkeys] [weekdays]< 1)
cout< endl<<必须馈送正量< endl;
}
}
}
double mostEaten(double array [] [weekdays],int size)
{
double greater = array [0] [0];
for(int count = 0; count< size; count ++)
{
for(int col = 0; col< count; col ++)
{
if(array [count] [weekdays]> maximum)
highest = array [count] [weekdays];
}
}
return maximum;
}
double leastEaten(double array [] [weekdays],int size)
{
double最少= array [0] [0]
for(int count = 0; count {
for(int col = 0; col< size; col ++);
{
if(array [count] [weekdays]< minimum)
least = array [count] [weekdays];
}
}
return least;
}
,你还没有打电话到任何功能。使用下面的示例并相应地更改您的程序。
示例:
int main()
{
int i_array = {0,1,2,3,4,5};
function(i_array); //调用函数
printf(%d,i_array [0]); //将打印100不是0
}
void function(int [] i_array)
{
i_array [0] = 100;
}
Possible Duplicate:
Stuck on C++ functions and arrays
Stuck on this problem: I can't get it to run and i am stuck on getting the array functions to return to main. Thanks for any help.
Requires the utilization of a 2 dimensional array to store the pounds of food eaten by 3 monkeys each of the seven days of the week.
Create a function to obtain the pounds eaten for each monkey, each day of the week. Create a second function to determine pass through the array to calculate the total all of the moneys ate, and then the average eaten on one day.
Create a third function to determine which monkey ate the least amount of food and on what day. Also output the amount the monkey ate on that day. Create a fourth function to determine which monkey ate the most amount of food on a single day. Output the monkey number, the pounds eaten, and the weekday.
#include <iostream>
using namespace std;
const int monkeys = 3;
const int weekdays = 7;
double monkeyWeek[monkeys][weekdays];
double largest;
double least;
double average;
int index;
int dayCount;
double amount;
double amountEaten(double[] [weekdays], int);
double mostEaten (double[] [weekdays],int);
double leastEaten (double[][weekdays], int);
int main(){
double mostBananas (double[] [weekdays],int);
double leastBananas (double[][weekdays],int);
//double bananaAverage (double[][weekdays], int);
}
double amountEaten(double array[] [weekdays], int) {
cout << "Please enter the amount of food eaten per monkey per day." << endl;
double amount = array[0][0];
for (index = 0; index < monkeys; index++)
{
for (dayCount = 0; dayCount < weekdays; dayCount++)
{
cout << endl << "Please enter the amount of pounds eaten by monkey"
<< (index +1)
<< endl << "for day " << (dayCount +1) << ": ";
cin >> monkeyWeek[monkeys] [weekdays] ;
if (monkeyWeek[monkeys] [weekdays] < 1)
cout << endl <<"Must feed positive amount" << endl;
}
}
}
double mostEaten( double array[] [weekdays], int size)
{
double largest = array[0][0];
for (int count = 0; count < size; count++)
{
for (int col = 0; col < count; col++)
{
if (array[count][weekdays] > largest)
largest = array[count][weekdays];
}
}
return largest;
}
double leastEaten(double array[] [weekdays], int size)
{
double least = array[0][0];
for (int count = 0; count < size; count++)
{
for (int col = 0; col < size; col++);
{
if (array[count][weekdays] < least)
least = array[count][weekdays];
}
}
return least;
}
You are just declaring the functions, you haven't made a call to any functions at all. Use the sample below and change your program accordingly.
Example:
int main()
{
int i_array={0,1,2,3,4,5};
function(i_array); // Call a function
printf("%d",i_array[0]); // will print 100 not 0
}
void function(int [] i_array)
{
i_array[0]=100;
}
这篇关于C ++无法找出数组和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!