需要完成2D阵列程序的指导 [英] Need guidance on finishing program 2d array

查看:63
本文介绍了需要完成2D阵列程序的指导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明:
-每天告诉哪个气球(通过释放的时间识别)在当天行驶了最大距离(打印了7个值).通过名称而不是数字来标识日期.
-对于每一天,计算并打印当天释放的所有气球的实际平均距离(打印小数点后一位),这些气球实际上是回收的软管,其距离> 0)(已打印7个值).通过名称而不是数字来标识日期.
-计算并打印所有在一周内恢复的气球的平均距离(打印小数点后一位)(打印1个值).
-我正在从列出的文件中拉出我的信息,例如:
4 1200 182
4 1800 42
7 1500 284
第一列表示天
第二列表示时间
第三列表示行进距离
我对C ++非常陌生,仍然学到很多东西.谢谢您的时间

这是我的代码:您不必告诉我答案,我只需要完成程序的指导,例如我应该使用switch语句,更多void函数等.

Instructions:
--For each day, tell which balloon (identify it by time released) traveled the greatest distance on that day (7 values printed). Identify the day by name rather than by number.
--For each day, calculate and print the average distance (print one decimal position) traveled by all balloons released on that day which were actually recovered hose for which distance > 0) (7 values printed). Identify the day by name rather than by number.
--Calculate and print the average distance (print one decimal position) traveled by all the balloons which were recovered during the entire week (1 value printed).
--I am pulling my information from a file where its listed like:
4 1200 182
4 1800 42
7 1500 284
first column means day
second column means time
third column means distance traveled
I am very new to c++ and still learning a lot. Thanks for your time

Here is my code: You don''t have to tell me the answer I just need guidance on completing the program like should I use switch statements, more void function, etc.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int DAYS=7; // Global constants
const int TIME=5;
// function prototypes
void getInfile( double distance [DAYS][TIME]);
void avgDistance(double distance[][TIME]);
void dayRelease(double distance[][TIME]);
int maxDistance(double distance [][TIME], int);
void printHeading(string[]);

int main()
{
string dayNum[DAYS]= { " Sun. ", " Mon. ", " Tues. ", " Wed. ", " Thurs. ", " Fri. ", " Sat. "};
string timeRelease[TIME]= { " 600 ", " 900 ", " 1200 ", " 1500 ", " 1800 "};
double distance[DAYS][TIME]; // 2D array for storing the distance values
int day; // row subscript for distance array
int time; // column subscript for distance array
int max; // subscript for the largest distance in a row
double sum;
double totalSales;

getInfile (distance); // Input values to fill the store array.
printHeading(dayNum);
for (day = 0; day < DAYS; day++) // For each day
{
max = maxDistance (distance, day); // find the largest distance in distance array
sum = dayRelease(distance, day);
cout << setw(15) << left << timeRelease[TIME];
for (time = 0; time < TIME; time++)
cout << setw(10) << right << distance [day][time];
cout << setw(10) << sum << " " << left << dayNum[max] << endl;
}

system("pause");

推荐答案

我的建议是创建一个记录"类型来存储日期/时间/距离并创建一个数组.就速度而言,它可能不会那么高效,但是它将使您的代码更易于维护和理解.除非您迫切需要使用2D数组,否则速度至关重要.
My advice is to create a "Record" type to store the day/time/distance and create an array of that. It probably won''t be as efficient in terms of speed, but it will make your code more maintainable and easy to understand. Unless you have a pressing need to use a 2D array or speed is vital.


我的建议是可以使用map代替2D数组.这是一个更好的解决方案.

My suggestion is instead of using 2D array you can use map. Its a better solution.

typedef struct _STBALLONINFO
{
    string strDay;
    string strDistance;
    double strTime;
}STBALLONINFO , *STBALLONINFO;

typedef std::map<string> MAPBALLONINFO;
</string>



您也可以使用列表...



you can use list also ...




由于您 c ++的新手,因此没有理由在代码中使用C数组.让他们交给专家:)

这是您解决问题的起点:
Hi,

As you are very new to c++ there is no reason why you should use C arrays in your code. Leave them to the experts :)

Here is a starting point for your problem:
// Balloons.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>

enum e_Day {Sun = 1, Mon, Tue, Wed, Thu, Fri, Sat};

struct Balloon
{
    int day;
    int time;
    int distance;
    // default ctor used by vector<Balloon>::push_back
    Balloon() : day(0), time(0), distance(0)
    {}
    // Stream ctor used by ReadBalloons()
    Balloon(std::istream& in)
    {
        in >> day >> time >> distance;
    }
    // Param functor for DayBalloon
    struct IsDayBalloon
    {
        e_Day day;
        IsDayBalloon(e_Day d) : day(d)
        {}
        bool operator() (const Balloon& balloon)
        {
            return balloon.day == day;
        }
    };
    // Param function for MaxDayDistance
    static bool CompareDistance(const Balloon& b1, const Balloon& b2)
    {
        return b1.distance < b2.distance;
    }
    // Param function for AverageDistance
    static double AddDistance(double distance, const Balloon& balloon)
    {
        return distance + balloon.distance;
    }
};

typedef std::vector<Balloon> Balloons;

// Day Balloons
Balloons DayBalloons(const Balloons& balloons, e_Day day)
{
    Balloons day_balloons;
    std::copy_if(balloons.begin(), balloons.end(), std::back_inserter(day_balloons), Balloon::IsDayBalloon(day));
    return day_balloons;
}

// return balloon which traveled the greatest distance
Balloon MaxDistance(const Balloons& balloons)
{
    return balloons.size() ?
        *std::max_element(balloons.begin(), balloons.end(), Balloon::CompareDistance) :
        Balloon();
}
// return balloon which traveled the greatest distance on a day
Balloon MaxDayDistance(const Balloons& balloons, e_Day day)
{
    return MaxDistance(DayBalloons(balloons, day));
}
// return each day MaxDistance balloon
Balloons MaxDistances(const Balloons& balloons)
{
    Balloons max_dist_balloons;
    for (int day = Sun; day <= Sat; ++day)
        max_dist_balloons.push_back(MaxDayDistance(balloons, (e_Day)day));
    return max_dist_balloons;
}

// return the average distance for all balloons
double AverageDistance(const Balloons& balloons)
{
    return balloons.size() ?
        std::accumulate(balloons.begin(), balloons.end(),  double(), Balloon::AddDistance) / balloons.size() :
        0;
}
// return the average distance balloons released on one day
double AverageDayDistance(const Balloons& balloons, e_Day day)
{
    return AverageDistance(DayBalloons(balloons, day));
}
// return each day AverageDistance value
std::vector<double> AverageDayDistances(const Balloons& balloons)
{
    std::vector<double> average_distances;
    for (int day = Sun; day <= Sat; ++day)
        average_distances.push_back(AverageDayDistance(balloons, (e_Day)day));
    return average_distances;
}

// Read Balloons data
size_t ReadBalloons(std::istream& in, Balloons& balloons)
{
    balloons.clear();
    while (in)
    {
        balloons.push_back(Balloon(in));
    }
    return balloons.size();
}


int main()
{
    Balloons balloons;
    std::ifstream in("test.txt");
    ReadBalloons(in, balloons);
    Balloons max_dist_balloons = MaxDistances(balloons);
    // You have to output Name_of_Day balloon.time ballon.distance from max_dist_balloons
    std::vector<double> average_distances = AverageDayDistances(balloons);
    // You have to output Name_of_Day distance from average_distances
    std::cout << "Average distance: " << AverageDistance(balloons) << std::endl;

    return 0;
}



当您了解并完成了它之后,您仍然对c ++还是很陌生,仍然学到了很多,但是在一个更好的学习道路上.

欢呼声,
AR



When you have understood and completed it you will still be very new to c++ and still learning a lot but on a better learning track.

cheers,
AR


这篇关于需要完成2D阵列程序的指导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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