Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (8 votes)
See more:
A program that keeps sales data for 10 years by month. The array should have a month index of size 12. Given this data, compute by sorted order the months of the year for which sales are best.

Output Requirment:
Month 1990 1991 1992 1993 1994 1995
1 10,000 9,000 8,000 10,000 10,000 10,000
2 20,000 3,000 3,000 40,000 20,000 20,000
3 30,000 4,000 5,000 30,000 10,000 20,000
4 40,000 5,000 5,000 20,000 10,000 20,000
5 50,000 6,000 5,000 20,000 20,000 30,000
6 10,000 3,000 5,000 20,000 20,000 30,000
7 10,000 2,000 5,000 20,000 10,000 20,000
8 10,000 5,000 4,000 20,000 20,000 10,000
9 10,000 4,000 4,000 20,000 20,000 20,000
10 10,000 7,000 4,000 10,000 20,000 20,000
11 10,000 4,000 4,000 10,000 20,000 40,000
12 10,000 4,000 4,000 40,000 30,000 30,000
Total 220,000 116,000 56,000 260,000 210,000 270,000

The Best Sales is the month of DECEMBER 1995
Gross sales is P 270,000
Posted
Updated 1-Mar-11 21:13pm
v2
Comments
OriginalGriff 2-Mar-11 3:02am    
I smell homework!

Have you tried anything, or do you expect us to just give you the code?
nessa12 2-Mar-11 3:24am    
pls help me to do this project :)
Manfred Rudolf Bihy 2-Mar-11 3:03am    
Is this a case of homuworku?
thatraja 2-Mar-11 3:18am    
Please come with what you have tried. sure we will help you.
Without any effort you can't. Cheers.
nessa12 2-Mar-11 3:32am    
pls help me to do this :(

As OriginalGriff's answer is in C# and your question is tagged C++, I will try to put you on the way.

In C++ most of the necessary routines are built in the Standard C++ Library, you just have to use them.

1 Hold your data in a std::vector, for instance:
C++
#include <vector>
typedef std::vector<int> Sales_t;
typedef Sales_t::iterator Sales_it;
const int YearStart = 1990;
int sales[] =
{
    10000, 20000, 30000, 40000, 50000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, // 1990
    9000, 3000, 4000, 5000, 6000, 3000, 2000, 5000, 4000, 7000, 4000, 4000,             // 1991
    // etc ...
};
Sales_t Sales(sales, sales + sizeof(sales) / sizeof(int));

2 Now you have to convert Sales_it iterators to year and months values for instance:
C++
#include <algorithm>
int GetYear(Sales_it it)
{
    return YearStart + std::distance(Sales.begin(), it) / 12;
}
int GetMonth(Sales_it it)
{
    return std::distance(Sales.begin(), it) % 12;
}
// TODO
Sales_it Get_it(int year, int month);

3 Now use the Standard C++ Library algorithms to execute your tasks, as:
C++
#include <numeric>
#include <iostream>
int main()
{
    int totalSales = std::accumulate(Sales.begin(), Sales.end(), 0);

    Sales_it it = std::max_element(Sales.begin(), Sales.end());
    std::cout << "Maximum sale: " << *it << " Year: " << GetYear(it) << " ,Month: " << GetMonth(it) << std::endl;
}

cheers,
AR
 
Share this answer
 
We (usually) don't do the homework for you, so please, try to develop the program yourself and post here specific questions when you're stuck. And please, please, read the FAQ, before posting.
:)
 
Share this answer
 
Comments
thatraja 2-Mar-11 3:24am    
/*And please, please, read the FAQ, before posting.*/
Particularly 7th point. :)
I'm not giving you the code: your lecture notes will cover that.
But, I will give you the steps you need to go though. It may be worth implementing and checking each stage first, before moving to the next.

1) Declare your sales data: You need somewhere to store the sales information, so your code can access it. I would use a Dictionary<int, int[]> so that I could use the year as a key.
C#
Dictionary<int, int[]> salesByYear = new Dictionary<int, int[]> {
    {1990, new int[] { 10000, 20000, 30000, 40000, 50000, 10000, 10000, 10000, 10000, 10000, 10000, 10000}},
    {1991, new int[] { 9000, 3000, 4000, 5000, 6000, 3000, 2000, 5000, 4000, 7000, 4000, 4000}}
};
Gets you started!
2) Generate your totals: Again, you need to store the totals. Again, I would use a Dictionary. Fill the totals in your code - loop though each years sales, and add them together. Put the sum in a dictionary entry for that year.
3) Find the best sales year: Loop through each year in your totals, and find which is the highest.
4) You then need to sort the month numbers into order, with the sales value per month.

Good luck! If you do get stuck on some particular part, ask again, but please, do try to do it yourself!
 
Share this answer
 

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