Hello NOSgraf,
No I did not forget about you. It just took me longer to figure ut the program than I thought.
One of the first things I did was to chage some of the variable names into something that had more meaning, so the program was easier to understand. I did not see anything that requires you to use the letters "T", "N" or "K" for your variables, so changing the names to something that has more meaning works better. This is what I used along with other variables that I found that I needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int testCases{ 1 }; // originally T
int numStairs{ MAXSTAIRS }; // Should be zero. Used MAXSTAIRS for testing.
int numStepsTaken{}; // originally K.
int stairsLeft = numStairs;// { MAXSTAIRS };
int step{}; // The step you are on.
int aStepCounter{}; // Used as an inedx for "aStep".
int aStep[20]{}; // An array to hold the sums derived after entering "numStepsTaken".
// Your original.
//int stairs[1000]
// My version so I would not have to type numbers each time the program runs.
//int stairs[MAXSIZE]{ 0, 3, -2, -1, 4, 5, -2, 2, -1, -4, 2 }; // Array to hold the values of each step.
// Used for testing using constant numbers.
int stairs[MAXSIZE]{ 0, 70, 37, 99, 62, 49, 22, 64, 97, 98, 88, 59, 47, 74, 62, 3, 9, 33, 22, 27 };
int stairsCounter{};
int pos{};
int sum{};
int* p;
int lc{}; // defines a loop counter outside a for loop. Allows the vallue to be used outside a for loop.
|
These are the constants I defined above main to use in the program:
1 2 3
|
constexpr int MAXSIZE{ 1000 };
constexpr int MAXSTAIRS{ 20 };
constexpr int MAXSUMSIZE{ 20 }; // Guess for now. Could increase later.
|
Defining the constant variables this way allows you to use the anywhere in the program if needed. And has the added advantage of only needing to change sizes in one place in the program should the need arise.
The next thing I find is
cin >> T;
. When the program runs all I see is a blinking cursor with no idea of what to do. You could use something like:
1 2 3 4 5 6
|
std::cout << "\n Enter number of test cases: ";
std::cin >> testCases;
std::cout << "\n Enter number of stairs: ";
std::cin >> numStairs;
|
In the cout statements I use the "\n". On the first line the "\n" or new line gets it off the top edge of the screen and the space gets it off the left edge of the screen. At the end of the cout the ": "just gives the line a better visual appearance. The semi colon ends the line allowing the cin to follow directly to the right of the prompt and not on the next line. This may be my personal preference, but it does make it more user friendly.
I used for input for the values of each step. I think you should that it makes more sense for getting input:
1 2 3 4 5 6 7 8
|
for (lc = 1; lc < numStairs + 1; lc++)
{
std::cout << "\n Enter value for stair " << lc << ": ";
std::cin >> stairs[lc];
//stairs[lc] = rand() % 100 + 1; // <--- Used to generate random numbers if you like. Can be adjusted as you may need.
}
stairsCounter = lc; // Keeps track of how many of the elements of the array are used.
lc = 0; // Resets "lc" fo next use.
|
For testing I used this bit of code just to see what is stored in the array "stairs".
1 2 3 4 5 6 7 8 9
|
std::cout << "\n The array you are using. Delete or change this line and the for loop if not needed.\n" << std::endl;
std::cout << ' '; // <--- Leading space before the for loop starts.
for ( lc = 0; lc < MAXSTAIRS; lc++)
{
std::cout << stairs[lc];
(lc < MAXSTAIRS - 1) ? std::cout << ", " : std::cout << "\n";
}
|
After this is where the work begins. You would start the while loop based on "testCases" or "T". For now I did not use this while loop until I had the program working with just one. Easy to put the while loop in later.
Next I used this while loop:
The function is to get the "sum" of the values of the steps traveled which I stored in the array "aStep", I could have used a better name here.
After the while loop ends I printed out a couple of lines. I believe only this one is needed though.
std::cout << "\n The largest \"sum\" of steps taken is: " << *std::max_element(aStep, aStep + aStepCounter) << std::endl;
With your original use of
sum += max_element( *p - K, *p - 1);
I am thinking you misunderstand how the function is used. The function returns a pointer to the maximum value in the array. Dereferencing the return value of the function, as in the above use, will give you the value at that address. The parameters need to reference an array. I am not sure if the use of "p" will do this and dereferencing "p" will not work. This may help:
http://www.cplusplus.com/reference/algorithm/max_element/
Now that I have it working I need to work on the final output and check to make sure the requirements have been met.
Hope that helps,
Andy