Thread: Need help...

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    6

    Need help...

    Hi everyone!

    How I told once I'am beginner in programming... I have task to make program.. To enter 10 positive numbers in an array. Find three numbers that stand near each other and that have the maximum sum.

    I understand how to make array, enter 10 number i find only one biggest number from 10.. Could anyone help to find three max number sum that stand near each other..

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Forget about code for the moment, and try to work out the logic yourself by hand, with pencil and paper if need be.

    (Note: I'm assuming that by "three numbers that stand near each other", you mean a group of three numbers in a row.)

    This might help: Go get ten coins, of assorted values, and put them in a row (randomly, in no particular order). Then find out which group of three (in a row) has the highest value when added together. This should be pretty easy for you to figure out. When you do, go through the process again, but slower this time, taking note of each little step you have to do to find the answer.

    These little steps will form the foundation of the logic you'll need to implement.

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    This is the first time in a while I've seen someone tell another person to not think about a problem programmatically but to think about it logically instead. Going straight into the code has proved painful for me on a number of occasions and when I finally get a piece of paper and think about it, I go back and just "Wow, that's so much simpler than I've been making it out to be." Kudos on that.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    Ok I tryed... Program must check each three coins (for example). 1 2 5 10 20 50 5 2 1 2. it check 1+2+5, then 2+5+10, then 5+10+20, then 10+20+50....till end. a then program make conclusion that biggest sum is 10+20+50.. right?

  5. #5
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    The largest sum of successive numbers is indeed 10+20+50 (80) Now think about how you would do that in the code, now that you know how the idea itself works.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Ale View Post
    Ok I tryed... Program must check each three coins (for example). 1 2 5 10 20 50 5 2 1 2. it check 1+2+5, then 2+5+10, then 5+10+20, then 10+20+50....till end. a then program make conclusion that biggest sum is 10+20+50.. right?
    Correct.

    Remember, a computer needs to be told each and every little step, so return to your though process as you develop code. For instance, when you do this in your head, you probably combine several steps since you intuitively understand how to do it. Sometimes it takes some extra thought to break the process down into discrete little steps.

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    I understand the idea of this task. But I don't understand how to define this size of 3 elements by which program is searching for the max sum.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There are a few ways to go about this.

    Why don't you think about it for a while and see what you come up with.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If you get stumped on solving for 3 numbers; try solving for the max number in a 10 element array.
    Maybe it will give you a hint.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    Tim,
    Seems that's searching for 1 max element is not so hard, as I use only a[i] for checking this maximum. The program is searching the max element of the 10 given elements, using "for"
    Code:
    int main()
    {
    int a[10];
    int i;
    int max;
    printf("Enter 10 numbers:");
    for (i = 0; i < 10; i++)
    {
    scanf("%d", &a[i]);
    }
    max = a[0];
    for (i = 0; i < 10; i++)
    {
    if (a[i]>max)
    {
    max = a[i];
    }
    }
    printf("The max number is %d", max);
    getch();
    }
    But how do I implement sum of 3 subsequent elements? I should add one more integer for ex. "sum", but I don't know how to "tie" this 3 subsequent elements in array.
    Thank you in advance for giving a hint for this.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest learning how to post indented code on this site.

    I would start by re-writing the code you posted to change max to either of three variables
    1. index location in the array of the current max value
    2. sum of the value(s) to be tested
    3. sum of the current max value

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed