卡在C ++函数和数组上 [英] Stuck on C++ functions and arrays

查看:55
本文介绍了卡在C ++函数和数组上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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(){
cout << "Ch 7-4 Monkey " << endl;
cout << "Created by Aaron Roberts" << endl;

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;
} 

该项目需要利用二维数组来存储每周7天中每3只猴子吃掉的食物的磅数.

This project 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.

创建一个函数来获取一周中每一天每一只猴子吃的磅.创建第二个函数来确定通过数组以计算吃掉的所有钱的总数,然后计算一天的平均消费.(你们中的一些人将其解释为所有值的总和,然后除以值的数量.其他人则将其解释为将每天的值相加并计算当天的平均值.因此,将有7行输出,而不仅仅是一行.)

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. (Some of you interpretted this as sum all values, then divide by number of values. Others interpretted this as summing the values for each day and calculating the average for that day. So, there would be 7 lines of output, not just one.)

创建第三个功能,以确定哪只猴子和哪一天吃的食物最少.还要输出猴子在那一天吃的食物量.创建第四个函数来确定哪只猴子在一天中吃的食物最多.输出猴子编号,吃的磅数和工作日.

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.

我是C ++的新手,很固执,并不真正知道如何完成此工作.感谢您的任何帮助,我真的很感激.

I'm new at c++ and stuck and don't really know how to complete this. Thanks for any help i really appreciate it.

推荐答案

您一直在做这种事情:

for (int count = 0; count < size; count++)
{
    for (int col = 0; col < count; col++)
    {
        if (array[count][weekdays] > largest)
            largest = array[count][weekdays];
    }
}

看到您正在使用工作日对数组进行索引.但是该索引无效.它可能会完成一些工作,但始终会返回下一行的第一个元素(然后在最后一行具有更确定的未定义行为).

See you are using weekdays to index your array. But that index is invalid. It might sort-of work, but will always return the first element of the next row (and then have more definite undefined behaviour on the last row).

我很确定您打算在这里使用 col 而不是工作日.

I'm pretty sure you meant to use col instead of weekdays here.

正如WhozCraig在评论中指出的那样,您可能还需要遍历整个工作日范围.这是一个经过修复的循环:

As WhozCraig pointed out in the comments, you probably need to loop over the entire weekdays range too. Here is a slightly repaired loop:

for (int count = 0; count < size; count++)
{
    for (int col = 0; col < weekdays; col++)
    {
        if (array[count][col] > largest)
            largest = array[count][col];
    }
}

类似地,您在其他任何地方都已完成此操作...

Similarly for everywhere else you've done this...

这篇关于卡在C ++函数和数组上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆