need help with building a function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: need help with building a function

Hybrid View

  1. #1
    Join Date
    May 2011
    Posts
    7

    Question need help with building a function

    Hello folks,
    I have a task that I cannot solve, maybe you guys can help me out .
    To explain it I'll start with an example.
    1234=46
    2222=8
    These equations are false, but if you add plusses between the digits you can make them be true, like this:
    12+34=46
    2+2+2+2=8
    You need to build a function that gets 2 numbers (the left side and the right side of the equation), puts the plusses in right places between the digits of the left number, and then returns the equation in it's true form.
    It is important to notice that there can be a random number of plusses.
    That's it, have any idea?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,217

    Re: need help with building a function

    Generally, for homework questions, it's always a good idea to post the code that you have (even if it doesn't yet work right).

  3. #3
    Join Date
    May 2011
    Posts
    7

    Re: need help with building a function

    I tried to write it, but the code was too long and complicated. Not to mention it didn't work. So I thought that there is an easier way for sure, maybe a trick that I don't know. Basicaly, I tried to get all the possible positions of plusses between the digits, then check if the sum matches the right number. The problem is that I have no idea how to get the collection of all of these positions.

  4. #4
    Join Date
    May 2011
    Location
    Nazareth
    Posts
    5

    Re: need help with building a function

    public int result (string x)
    {
    //string x = "1234";
    string num1, num2;
    int n1, n2, res;

    num1=(x.Remove(2,2));
    num2=(x.Remove(0, 2));
    n1=int.Parse(num1);
    n2=int.Parse(num2);
    res = n1 + n2;
    return res;
    }

  5. #5
    Join Date
    May 2011
    Posts
    7

    Re: need help with building a function

    I write on c#.
    Remove is an interesting function, but it doesn't solve the task.

  6. #6
    Join Date
    May 2011
    Location
    Nazareth
    Posts
    5

    Re: need help with building a function

    c or c#

  7. #7
    Join Date
    May 2011
    Location
    Nazareth
    Posts
    5

    Re: need help with building a function

    what is the task exactly > my function work to me very well .!!!??!!?

  8. #8
    Join Date
    May 2011
    Posts
    7

    Re: need help with building a function

    Read the first post.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,217

    Re: need help with building a function

    Quote Originally Posted by anter View Post
    Read the first post.
    Read my first reply.

    If you want help with homework, you have to post the code you have written.

  10. #10
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: need help with building a function

    Here is a little console program that I think will do what you are wanting... like the others, I believe you should actually write some attempts, or at least show what you attempted, as then we can feel better about helping you out... however, I also believe you can study the code below and still learn from it, and perhaps improve on it as you gather some understanding.

    Good luck!

    Code:
    class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine(Test("12345", 15));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                
                WaitForUser();
            }
    
            static string Test(string source, int answer)
            {
                // add placeholder for every possible + position
                for (int i = 1; i < source.Length; i += 2)
                    source = source.Insert(i, " ");
    
                for (int i = 1; i < source.Length; i += 2)
                {
                    int j = i;
                    while (j < source.Length)
                    {
                        source = source.Remove(j, 1);
                        source = source.Insert(j, "+");
    
                        string formula = source.Replace(" ", "");
                        if (answer == GetAnswerForForumla(formula))
                            return formula;
    
                        j += 2;
                    }
    
                    source = source.Replace("+", " ");
                }
    
                return "unresolved";
            }
    
            static int GetAnswerForForumla(string formula)
            {
                int answer = 0;
                string[] parts = formula.Split('+');
    
                foreach (string part in parts)
                {
                    int partValue = 0;
                    if (Int32.TryParse(part, out partValue))
                        answer += partValue;
                }
    
                return answer;
            }
    
            static void WaitForUser()
            {
                Console.WriteLine("");
                Console.WriteLine("Press any key to close application...");
                while (!Console.KeyAvailable) ;
            }
    
        }
    Last edited by fcronin; May 19th, 2011 at 03:30 PM.

  11. #11
    Join Date
    May 2011
    Posts
    7

    Re: need help with building a function

    Sorry I wasn't around for so long, but I still don't have a full solution to this exercise.
    fcronin, thank you, your post helped me very much. But your solution doesn't check all the options, for example if the left number is 12345 and the right one is 240 (1+234+5) your solution won't find the answer, although it exists.
    I will try to fix it and if I will, I'll post it here.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center