I am writing a program that accepts an input file of 10 items each having revenue across 4 quarters. In this particular function I have to determine if the items profit has steadily increased, decreased or went up and down across the 4 columns, or quarters(qu) as they are referenced in my code.

I was able to cout whether the item increased or decreased across the 4 quarters but cannot seem to figure out how to record those instances and determine and display on screen if they went up, down or both. Below is my function with data file. Thank you in advance for any help you can provide.

``````void steady(string item[], double revenue[][4])
{
double max;
const int NUM_ROWS=10;
const int NUM_COLS=4;
string iteminc;
string itemdec;

cout << "\nRevenue path per item across 4 quarters: \n";
for(int i=0;i<NUM_ROWS;i++)                   /*Items*/
{
max=0;
for(int qu=0;qu<NUM_COLS;qu++)        /*Quarters*/
{
if(max<revenue[i][qu])
{
max=revenue[i][qu];
iteminc=item[i];                 /*Display Item*/
cout << iteminc << " = " << "Icreased \n";
}
if(max>revenue[i][qu])
{
max = revenue[i][qu];
itemdec=item[i];            /*Display Item*/
cout << itemdec << " = " << "Decreased \n";
}

}
}
}``````

Input Data File:

Memory 5 15 20 26
CPU 10 21 20 0
Monitor 2 17 4 42
HD 5 -8 -10 -4
Keyboards 7 10 15 -9
Mice 3 -10 3 10
Drives -1 -11 -12 -14
Software 3 5 10 8
Optical 5 7 -8 10
Printers -19 3 7 11

4
Contributors
10
Replies
15
Views
7 Years
Discussion Span
Last Post by VernonDozier
Featured Replies
• 1

I am interpreting the goal as follows. Please confirm whether my understanding is accurate. You have ten items. Each item has four data points. High numbers are "good". Low numbers are "bad". Increasing numbers are "good", decreasing numbers are "bad". So "memory" has the trend you are looking for. Things …

I would actually break this up into two functions. The function you have now would just be used to parse through the array and handle the return from the helper function. In the helper function I would do the calculations to find out if there was an increase or decrease in profits by simply subtracting the first value in the array from the last value. If the number is positive the profits increased and if its negative they decreased. To determine if it was steady or not will need a loop. Start at the front of the array and then step to the end. As you go through the array check and see if the difference between the values is always positive or negative. if the is a change then you know it is fluctuating.

In the helper function, how can I see if the difference is ALWAYS positive or negative?

I am interpreting the goal as follows. Please confirm whether my understanding is accurate.

You have ten items. Each item has four data points. High numbers are "good". Low numbers are "bad". Increasing numbers are "good", decreasing numbers are "bad".

So "memory" has the trend you are looking for. Things "improve" each quarter (i.e. numbers always get bigger).

Memory 5 15 20 26

This, on the other hand, would be "bad".

Keyboards 8 6 4 2 (made the numbers up to illustrate point)

This would be a mixed bag.

Software 3 5 10 8 (goes up, then down).

So I see three categories.

1. Always goes up
2. Always goes down
3. Neither 1 nor 2.

Is this a correct understanding?

I think I agree with Nathan Oliver. A helper function is in order here.

``````// helper function.  return 1 for increasing, 2 for decreasing, 3 for neither
int trend(int revenue[])
{
// code to determine the trend.
}``````

From the calling function, for Mice (array index 5)...

``````int theTrend = trend(revenue[5]);
if(theTrend == 1)
{
// code for increasing
}
else if(theTrend == 2)
{
// code for decreasing
}
else
{
// code for neither
}``````

>> In the helper function, how can I see if the difference is ALWAYS positive or negative?

Test for increasing. Go through the array. If the array always increases, then return 1.

Now test for decreasing. Go through the array. If the array always decreases, then return 2.

If you get this far, it's neither decreasing or increasing. Return 3.

That is exactly what needs to happen.

This is the first I am hearing of helper functions, so I will be a little slow in understanding.

As far as prototypes for the two functions, do I treat them both as regular functions:

``````void mice(string item[], double revenue[][4]);
double trend(double revenue[][4]);``````

And also while calling them in my main program, is this correct:

``````mice(prod, prices);
{
trend(prices);
}``````

As far as code for both functions I have the following:

``````double trend(double revenue[][4])  /*Helper Function*/
{
double max;
const int NUM_ROWS=10;
const int NUM_COLS=4;

for(int i=0;i<NUM_ROWS;i++)
{
max=revenue[0][0];
for(int qu=0;qu<NUM_COLS;qu++)
{
if(max<(revenue[i][qu+1]-revenue[i][qu]))               /*Determines Trend 1*/
{
max=revenue[i][qu];
return 1;
}
else if (max>(revenue[i][qu+1]-revenue[i][qu]))      /*Determines Trend 2*/
{
max=revenue[i][qu];
return 2;
}
else                                                /*Determines Trend 3*/
return 3;
}
}
}

void mice(string item[], double revenue[][4])              /*Calling Function*/
{
double theTrend = trend(revenue[][4]);     /*Compiler stops here asking for*/
if(theTrend == 1)                          /*Expected Primary Expression*/
{
cout << "Increasing /n";
}
else if(theTrend == 2)
{
cout << "Decreasing /n";
}
else
{
cout << "Up and Down /n";
}
}``````

At this moment it stops here:

``double theTrend = trend(revenue[][4]);``

"Expected Primary Expression"

I hope I'm not too far off track with my code..

You appear to have two arrays, one with the name of the product/item and the other with quarterly prices. The row index of the prices is the same as the index for the product name. In mice() you want to evaluate the data for each product. In order to do that you will call a helper function called trend() to look for a pricing trend. If that's correct then:

Remember that if() will only evaluate a single event whereas loops can evaluate a series of events using the same criteria. So in mice you should use a loop to evaluate each produt, calling trend() on each product and doing something with the return value of trend().

To send the array to trend you only need to put the name of the array in the () of the function call, not the [][]s

Edited by Lerner: n/a

For the helper function, I think it's better to go with my design.

``````// helper function.  return 1 for increasing, 2 for decreasing, 3 for neither
int trend(int revenue[])
{
// code to determine the trend.
}``````

Note that revenue[] is a SINGLE dimensional array, not a two dimensional array. You are passing it a an array from your 2-D revenue[][4] array.

Hence, my previous example call.

``int theTrend = trend(revenue[5]);``

This passes {3, -10, 3, 10} to the trend function. Note that these are the "Mice" numbers and there are 4 of them. Note that "Mice" is the 5th item (indexes always start at 0, not 1) on your list.

By my definition of the trend function, {3, -10, 3, 10} neither increases nor decreases, so it should return 3.

A helper function is just another function. It is declared, written, and behaves no differently than any other C++ function. "Helper" is not a technical C++ term. Rather it's just a descriptive term. A helper function's sole purpose is to help another function do its job.

The "max" part of everything seems to have gotten lost somewhere. I think some things got combined. How about we design TWO helper functions, max and trend. I'll let you write them, but I'll provide the spec and the calls from the other function to give you a skeleton. To make the helper functions more generic, let's pass them the size this time.

``````// helper.  returns the maximum of the four numbers that are passed to it
double maximum(double array[], const int array_size)
{
// you write the code.
}

// helper function.  return 1 for increasing, 2 for decreasing, 3 for neither
int trend(double array[], const int array_size)
{
// code to determine the trend.
}

{
double max;
int theTrend;
const int NUM_ROWS=10;
const int NUM_COLS=4;

cout << "\nRevenue path per item across 4 quarters: \n";
for(int i=0;i<NUM_ROWS;i++)                   /*Items*/
{
cout << "Item name = " << item[i];
max = maximum(revenue[i], NUM_COLS);
cout << "  Maximum = " << max;
theTrend = trend(revenue[i], NUM_COLS);
if(theTrend == 1)
{
cout << "  Revenue increases across quarters.\n";
}
else if(theTrend == 2)
{
cout << "  Revenue decreases across quarters.\n";
}
else
{
cout << "  Revenue neither increases nor decreases across quarters.\n";
}
}
}``````

Thank you so much, that worked like a charm, for the most part. The max function works perfectly. Good idea separating that function. I'm just having a little trouble with the trend function. I was able to determine an "Increase" and "Decrease" across quarters, but not "Neither increase nor decrease".

``````// helper function. return 1 for increasing, 2 for decreasing, 3 for neither
int trend(double array[], const int array_size)
{

double theTrend = array[0];
for(int i=0;i<array_size;i++)
{

if (array[i+1]>theTrend)
{
theTrend=array[i+1];
return 1;
}
else if (array[i+1]<theTrend)
{
theTrend=array[i+1];
return 2;
}
else
{
return 3;
}

}

}``````

You're welcome. This might be a case where it's best to give you the code and have you study it rather than point out the problems with your approach. This is ONE way (i.e. not the ONLY way) of doing it. The approach I'll use is as follows. I'll set up two boolean variables, increasing and decreasing, and set them initially to true. Then I will iterate through the array. If I find a place where it is not decreasing, I'll set that variable false. Ditto increasing.

``````// helper function. return 1 for increasing, 2 for decreasing, 3 for neither
int trend(double array[], const int array_size)
{
bool increasing = true;
bool decreasing = true;

for(int i=1;i<array_size;i++)
{
if (array[i] >= array[i-1])
{
decreasing = false;
}
if (array[i] <= array[i-1])
{
increasing = false;
}
}

if(increasing)
{
return 1;
}
else if(decreasing)
{
return 2;
}
else
{
return 3;
}
}``````

Wow I would have never thought to use Boolean var. Works perfectly.

What I also started trying was using the counter in the loop to determine the trend. For example run the first if statement for positive trend. If the i var reaches 5 then that particular if statement completed its run, whether positive or negative. Set the trend to 1 or 2. If it never reaches 5 then its up and down.

I tried a few examples of this and couldn't get it to work. Does it sound like a good approach to get the results?

Thank you as always for your help.

Lots of ways to do it. Not sure any of them are any easier to write and understand than the one I posted. The last one you wrote has the flaw that it will never go through one full iteration of the loop. Hence it's not a loop.