Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.

C++
#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");
Posted
Updated 7-May-11 11:39am
v2
Comments
Ed Nutting 7-May-11 17:40pm    
Please use pre (code) blocks when pasting and remember to set their 'lang' attribute, in this case C++.
klangston92 7-May-11 17:50pm    
ok thank you I didn't know how to do that, thanks
Keith Barrow 9-May-11 17:22pm    
This would have been better on the forums as the question is open ended.

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.
 
Share this answer
 
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 ...
 
Share this answer
 
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:
C++
// 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;
}


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
 
Share this answer
 
Comments
Niklas L 10-May-11 14:45pm    
Ah! So you also had that assignment! :p
Alain Rist 10-May-11 15:13pm    
Some pupils of mine might have it one day :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900