My program does not work.

Write your question here.
So the exercise i have is to find the biggest sum of the numbers of the stairs that we can get( each stair has it's own number) until we reach the final stair. The first stair and the final stair are always number 0. The step we can make is K number.We have to chose the best number considering the steps we can make.Also we have T which is the number of test cases.
Example:
INPUT:
2 //T(Test cases)
3 // N number of stairs don't forget that there are 2 more stairs the first and the last one
1 -1 1 // numbers of stairs
2 // K
10
3 -2 -1 4 5 -2 2 -1 -4 2
2
OUTPUT:
2 // Test case 1
14 // Test case 2
But i don't understand why my program does not work it says that i have a problem with line 22.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 #include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
    int N;
    cin >> N;
    int stairs[1000];
    for ( int  i = 1; i <= N; i++)
    {
        cin >> stairs[i];
    }
    int K;
    cin >> K;
    stairs[0] = 0;
    stairs[ N + 1] = 0;
   int *p = N + 2;
   int sum = 0;
    for ( int  i = ((N+2)/2)+2;; i > 0; i --)
    {
        sum += max_element( *p - K, *p - 1);
        *p = distance(stairs,max_element( *p - K, *p - 1)) - 1;
    }
    cout << sum << endl;
    }
    return 0;
}
Last edited on
Hello NOSgraf,

You have two problems with your program.

First is line 22. You define "p" as a pointer which only holds an address and then try to assign a int to a variable that only holds an address. For what you want to do this would work better:

1
2
int* p;
*p = N + 2;


The second is the function "distance". You are using the parameters wrong and the use of the function "max_element" is not likely the best place to use here. Have a look at this for the "distance" function:
http://www.cplusplus.com/reference/iterator/distance/

When you fix these two problems it might work better, which I say because I have not fixed the code yet or tested it.

Helpful hint, a few blank lines in your code would make it easier to read.

Hope that helps,

Andy
Hello NOSgraf,

As I try to understand you problem I a having a hard time. Maybe you could post the actual program requirements that you were given because I am finding it dificult to understand what you have posted so far.

I understand the variable "T" and "N", but having trouble with the variable "K" and how to use it.

Lines 20 and 21 would not be needed if you initialize the array on line 13 like: stairs[1000]{} this sets each element to zero.

Anything you could do to help better explain what each variable is for would help.

Andy
Thanks Andy for answering and here is the exercise but i translated from google so if you don't understand something ask me.
Problem O: A ladder
On each of the N + 2 steps of the staircase an integer is written, with the number 0 on the first and last step. On the first step is a person who needs to climb to the last step. In one step he can climb any number of steps not exceeding K.

We will calculate the sum of all the numbers written on the steps to which the person has stepped. Find the greatest possible value for this sum.

Input
The first line of the standard input stream contains the number of test cases T. Each test case is a description of the ladder.

Description of the ladder consists of three lines.

The first line contains the number N, 1 ≤ N ≤ 1000.

The second line contains N integers not exceeding 1000 in modulus, separated by spaces - numbers written on the steps (with the exception of the first and last steps, on which the zeros are written).

The third line contains the maximum step size of the person K, 1 ≤ K ≤ N.

Output
For each test case, output the maximum possible sum of numbers written on the steps to which the person has stepped.
Hello NOSgraf,

Thank you. That helps. I will see what I can come up with.

Andy
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
Hello thank you Andy for the help. I will now look at the code you wrote. Personally because of my misunderstanding i modified my code a bit but im stuck right now because i don't know how to write the thing that i wrote in line 36. Here is the code by the way:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
int T;
cin >> T;

while (T--)
{
int N;
cin >> N;
int stairs[1000];

for ( int i = 1; i <= N; i++)
{
cin >> stairs[i];
}


int K;
cin >> K;

stairs[0] = 0;
stairs[ N + 1] = 0;
int a = 0;
a = N + 1;
int subst = 0;
int myarray[1000];
int myindex[1000];
int sum = 0;

for ( int i = 0; i < (N+3)/2; i++)
{
int j = 0;
while(subst < K)
{
subst++;
myarray[j] = stairs[a - subst];

if ( myarray[j] == NULL)// this is where im stuck because i don't know how to
compare something with a null number
{
break;
}

myindex[j] = a - subst;
j++;
}

subst = 0;
int MaxElem = myarray[0];

for ( int i = 1; i < K; i++)
{
if ( MaxElem < myarray[i])
{
MaxElem = myarray[i];
}
}
a = distance(myarray,find(myarray,myarray + K,MaxElem));
a = myindex[a];
sum += MaxElem;
}
cout << sum << endl;
}
return 0;
}
Last edited on
Hello NOSgraf,

In the line if ( myarray[j] == NULL) just use 0 (zero) it is much easier.

I hope you get something from what I posted earlier because I do not believe what you have last posted is working correctly. Maybe the input is, but the rest is questionable.

Andy
Hello Andy,
Thanks for the tip. By the way is the MAXSTAIRS the same like numStairs ? I'm getting a bit confused because it is in brackets . Also i saw you use constexpr int and i read that is somehow the same like const but i don't really get the difference between them. I know that const you can't change them but i don't get when someone says constant expressions.
Last edited on
Hello NOSgraf,

"MAXSTAIRS" is in capital to let you know that is defined as a constant and can not be changed. "numStair" starts with a lower case letter to let you know that it is a variable that can change value.

The point of a variable like "MAXSTIRS" is so that you can use it anywhere in the program and only need to change the value of the variable in one place. As opposed to having to hunt through the program for every place that needs a change because the size of something has changed; like an array.

This works best in one file, but can also work in a header file.

"const" is the old use. "constexpr" is the newer version. Thi may help to explain better:
https://stackoverflow.com/questions/14116003/difference-between-constexpr-and-const

As the days pass I try to learn the C++11 standards and beyond.

Hope that helps,

Andy
"MAXSTAIRS" is in capital to let you know that is defined as a constant and can not be changed. "numStair" starts with a lower case letter to let you know that it is a variable that can change value.

^^^^ FYI: This is a convention that almost all c++ programmers use, but its nothing more than good style. There is nothing in the language about case vs constant. You can have constants that are lower case (I often do, e (2.718...) for example, as many math and standard engineering symbols are lower case) and variables that are all upper case (I have never done this, no reason for it).

Unless you have a compelling reason, use the upper case = constant style. If your jargon for your job requires lower cased constants, that is an exception to be used sparingly.
Registered users can post here. Sign in or register to post.