Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <stdio.h>
#include <stdlib.h>

void jum(int *p, int m, int n)
{
    int i;
    if(m==(n-1))
        return;
    if(n%2==0)
    {
        for(i=m;i<n/2;i++)
            (*(p+i))--;
        for(i=n/2;i<n;i++)
            (*(p+i))++;
    }
    else
    {
        for(i=m;i<n/2;i++)
            (*(p+i))--;
        for(i=(n/2)+1;i<n;i++)
            (*(p+i))++;
    }
    jum(p,0,n/2);
    jum(p,n/2,n);
    return;
}

int main()
{
    int n,i;
    printf("n = "); scanf("%d",&n);
    int v[n];
    int *p;
    p=&v[0];
    for(i=0;i<n;i++)
    {
        printf("v[%d] = ",i+1); scanf("%d",&v[i]);
    }
    jum(p,0,n);
    for(i=0;i<n;i++)
        printf("%d ",*(p+i));
    return 0;
}


There is no error/warning, but after I give my vector values, it crashes. I have a vector with n elements and then I wanted to split the vector in half, the left side will have its elements reduced by 1 and the right side will have its elements added by 1. If n is uneven, then the element in the middle will stay the same. After the vector is split in half, every half will be split again until 1 element remains.

For example:

n=5;
v={1 , 2 , 3 , 4 , 5};
Final print: 0 1 3 5 6

What I have tried:

I am not really sure what to try anymore. I kept modifying the code until I reached this stage, which, from my point of view, it should work. I tried a way simpler code with the same idea and it worked but not really sure why it crashes here. Probably because I call the function again in the function.
Posted
Updated 8-Jun-17 5:09am
v2

1 solution

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.
So stop just changing the code and seeing what happens, and look at what is happening when you run the code!

And now you enter the second stage of development (in reality it's the fourth or fifth
but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using teh debugger to find out why. Put a breakpoint on your line:
C++
p=&v[0];

and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Yes, I could probably tell you what "the problem" is - but it's not difficult to do this yourself, and you will learn something really worthwhile at the same time!
 
Share this answer
 
Comments
Member 13248267 8-Jun-17 11:20am    
jum(p,(n/2)+1,n);


The problem was the 2nd call of the function. I am a beginner and, I must say, I never used the debugger and I don't really know how it works. Though, I saw that the problem was at the call of the function inside the function. To verify my code, I usually use "//" or "/* */" and run it to see if it works. My mistake was that, inside the "jum" function, I only verified the first call, it worked and didn't verify the 2nd one. I still am not really sure what caused the code to crash. What is the difference between having (n/2)+1 and n/2 (I must say that (n/2)+1 shows the answer that I wanted)) ? And, would you mind telling me what I did wrong, as I am still learning and still couldn't find "the problem".
OriginalGriff 8-Jun-17 11:32am    
The debugger isn't complicated - or at least not at this level it isn't - all you need to do is use it to step through your code line by line to find out what is happening, looking at the variables and data as you do. This is normally very easy to do - I can't tell you what to do exactly as I have no idea what development system you are using, but if you google the name of your dev system and "debugger" you should get a lot of info: for example "Visual Studio debugger" gives you half a million hits, and the top three will tell you more than you want to know!
Give it a try: it's worth developing this skill on small programs like this instead of trying to start with a 100,000 line behemoth.

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