Thread: can you find any bugs in my code

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    can you find any bugs in my code

    i dont feel comfortable with it, i think theres a logical bug where the variable in the if condition wont be below 100 that would mess up the code because after the if conditions both int variables should sum up to 100.

    heres my code;

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            public static string RemoveDuplicates(string input)
            {
                return new string(input.ToCharArray().Distinct().ToArray());
            }
    
            static void Main(string[] args)
            {
                string coffee = "i saw a clown";
                string ok = "at the circus with balloons";
                string a = RemoveDuplicates(coffee);
                string b = RemoveDuplicates(ok);
    
                a = a.Replace(" ", String.Empty);
                b = b.Replace(" ", String.Empty);
                double counter = 0;
                double counter_a = a.Length;
                double counter_b = b.Length;
    
                foreach(char c in a)
                {
                    char temp_1 = c;
                    foreach(char d in b)
                    {
                        char temp_2 = d;
                        if (temp_2 == ' ') continue;
                        
                        if (temp_1 == temp_2)
                        {
                            counter++;
                            break;
                        }
                    }
                }
    
                // percentage=(yourNumber/totalNumber)*100;
                double one = (counter / counter_a) * 100;
                double two = (counter / counter_b) * 100;
    
                int a_one = (int)one;
                int a_two = (int)two;
    
                if(a_one > a_two)
                {
                    a_one = 100 - a_two;
                }
                else if (a_two > a_one)
                {
                    a_two = 100 - a_one;
                }
                else if (a_two == a_one)
                {
                    a_one = 50;
                    a_two = 50;
                }
    
                Console.WriteLine(a_one);
                Console.WriteLine(a_two);
    
                Console.ReadLine();
            }
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should tell people what exactly is the code supposed to do. Without any requirements to check/test against, as long as the code compiles and runs without crashing, it works perfectly fine, but then we could replace all you did with a "hello world" program and it would still be perfectly correct even though it does something entirely different.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    what does the code do? starting at main on line 16, you have two lines of text "coffee" and "ok".
    - i first remove any duplicate characters from both strings, so each letter in the string appears only once by using removeduplicates.
    - then i get rid of the whitespaces in lines 23 and 24, then i make two variables to hold the length of the two strings.
    - then using two foreach loops, i count how many characters are in both strings.
    - then in lines 46 and 47 i count the percentage of each string that is the shared characters.
    - then in lines 49 and 50 i make the percentage into ints to get rid of the decimals.
    - then i have some if else if else conditions. if the first string has a higher percentage of shared characters, then i take the lower percentage string, and do [higher string value = (100 - the lower string value)], this is so i can give a new value to the higher value string with the intention both strings added together will sum to 100.
    - then i display the results to the screen

    - what i dont know how to check for is if (100 - the lower value) <= 100, and if not how do i fix it so it is.
    Last edited by jeremy duncan; 06-08-2017 at 05:19 PM. Reason: added parenthesis to last line for clarity, wrong less than sign

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    i found a bug, i fixed it, here's my working code if your interested, no more problems;

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string string_a = "he went rawr one more time then disappeared";
                //string ok = "so i shot him";
                string string_b = "then bear walked away after he searched for food by the fire";
    
                string_a = string_a.Replace(" ", String.Empty);
                string_b = string_b.Replace(" ", String.Empty);
                double counter = 0;
                double counter_a = string_a.Length;
                double counter_b = string_b.Length;
    
                foreach(char c in string_a)
                {
                    char temp_1 = c;
                    foreach(char d in string_b)
                    {
                        char temp_2 = d;
                        if (temp_1 == temp_2)
                        {
                            counter++;
                        }
                    }
                }
    
                // percentage=(yourNumber/totalNumber)*100;
                double make_percentage_one = (counter / counter_a) * 100;
                double make_percentage_two = (counter / counter_b) * 100;
    
                int percentage_one = (int)make_percentage_one;
                int percentage_two = (int)make_percentage_two;
    
                double one = counter_a * 100 / (counter_a + counter_b);
                double two = counter_b * 100 / (counter_a + counter_b);
    
                Console.WriteLine(one);
                Console.WriteLine(two);
    
                Console.ReadLine();
            }
        }
    }
    Last edited by jeremy duncan; 06-08-2017 at 11:41 PM. Reason: removed weird comment

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    here's my finished code, fyi;

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            public void fit_into_100(string string_a, string string_b)
            {
                string_a = string_a.Replace(" ", String.Empty);
                string_b = string_b.Replace(" ", String.Empty);
                double counter = 0;
                double counter_a = string_a.Length;
                double counter_b = string_b.Length;
    
                foreach (char c in string_a)
                {
                    char temp_1 = c;
                    foreach (char d in string_b)
                    {
                        char temp_2 = d;
                        if (temp_1 == temp_2)
                        {
                            counter++;
                        }
                    }
                }
    
                // percentage=(yourNumber/totalNumber)*100;
                double make_percentage_one = (counter / counter_a) * 100;
                double make_percentage_two = (counter / counter_b) * 100;
    
                int percentage_one = (int)make_percentage_one;
                int percentage_two = (int)make_percentage_two;
    
                // number * percentage / 100
    
                double one = counter_a * 100 / (counter_a + counter_b);
                double two = counter_b * 100 / (counter_a + counter_b);
    
                int a_one = (int)one;
                int a_two = (int)two;
    
                Console.WriteLine("\r\nstring holds what percentage of the total shared char:");
                Console.WriteLine(a_one);
                Console.WriteLine(a_two);
            }
    
            public static string RemoveDuplicates(string input)
            {
                return new string(input.ToCharArray().Distinct().ToArray());
            }
    
            public void length_of_string(string string_a, string string_b)
            {
                string a = RemoveDuplicates(string_a);
                string b = RemoveDuplicates(string_b);
    
                a = a.Replace(" ", String.Empty);
                b = b.Replace(" ", String.Empty);
                double counter = 0;
                double counter_a = a.Length;
                double counter_b = b.Length;
    
                foreach (char c in a)
                {
                    char temp_1 = c;
                    foreach (char d in b)
                    {
                        char temp_2 = d;
                        if (temp_2 == ' ') continue;
    
                        if (temp_1 == temp_2)
                        {
                            counter++;
                            break;
                        }
                    }
                }
    
                // percentage=(yourNumber/totalNumber)*100;
                double one = (counter / counter_a) * 100;
                double two = (counter / counter_b) * 100;
    
                int a_one = (int)one;
                int a_two = (int)two;
    
                if (a_one > a_two)
                {
                    a_one = 100 - a_two;
                }
                else if (a_two > a_one)
                {
                    a_two = 100 - a_one;
                }
                else if (a_two == a_one)
                {
                    a_one = 50;
                    a_two = 50;
                }
    
                Console.WriteLine("\r\nshared char is what total percentage of the string:");
                Console.WriteLine(a_one);
                Console.WriteLine(a_two);
            }
    
            static void Main(string[] args)
            {
                Program string_descriptin = new Program();
    
                string string_a = "coffee time";
                //string ok = "so i shot him";
                string string_b = "ok";
    
                Console.WriteLine("- " + string_a);
                Console.WriteLine("\r\n- " + string_b);
    
                string_descriptin.length_of_string(string_a, string_b);
    
                string_descriptin.fit_into_100(string_a, string_b);
    
                Console.ReadLine();
            }
        }
    }
    and my results;

    Code:
    - coffee time
    
    - ok
    
    shared char is what total percentage of the string:
    14
    86
    
    string holds what percentage of the total shared char:
    83
    16
    
    Press any key to continue . . .

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    this is the finished code. im posting this here because i already started and why not;

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            public void calculate(string string_a, string string_b, int num_1, int num_2, int num_3, int num_4)
            {
                int is_1_2_close = 0;
                int is_3_4_close = 0;
    
                if (num_1 > num_2)
                {
                    if ((num_1 - num_2) <= 20)
                    {
                        is_1_2_close = 0; // yes
                    }
                    else
                    {
                        is_1_2_close = 1; // no
                    }
                }
                else if (num_2 > num_1)
                {
                    if ((num_2 - num_1) <= 20)
                    {
                        is_1_2_close = 0; // yes
                    }
                    else
                    {
                        is_1_2_close = 1; // no
                    }
                }
                else
                {
                    is_1_2_close = 0; // yes
                }
    
                ///////////////
    
                if (num_3 > num_4)
                {
                    if ((num_3 - num_4) <= 20)
                    {
                        is_3_4_close = 0; // yes
                    }
                    else
                    {
                        is_3_4_close = 1; // no
                    }
                }
                else if (num_4 > num_3)
                {
                    if ((num_4 - num_3) <= 20)
                    {
                        is_3_4_close = 0; // yes
                    }
                    else
                    {
                        is_3_4_close = 1; // no
                    }
                }
                else
                {
                    is_3_4_close = 0; // yes
                }
    
                ///////////////
    
                Console.WriteLine("\r\n////////////////\r\nresults:\r\n");
    
                if ((is_1_2_close == 0) && (is_3_4_close == 1))
                {
                    Console.WriteLine("\"" + string_a + "\"");
                    Console.WriteLine("+");
                    Console.WriteLine("\"" + string_b + "\"");
                    Console.WriteLine("=");
                    Console.WriteLine("confident");
                }
                else if ((is_1_2_close == 1) && (is_3_4_close == 1) && (num_1 > num_2) && (num_4 > num_3))
                {
                    Console.WriteLine("\"" + string_a + "\"");
                    Console.WriteLine("+");
                    Console.WriteLine("\"" + string_b + "\"");
                    Console.WriteLine("=");
                    Console.WriteLine("confident");
                }
                else if ((is_1_2_close == 1) && (is_3_4_close == 1) && (num_2 > num_1) && (num_3 > num_4))
                {
                    Console.WriteLine("\"" + string_a + "\"");
                    Console.WriteLine("+");
                    Console.WriteLine("\"" + string_b + "\"");
                    Console.WriteLine("=");
                    Console.WriteLine("confident");
                }
                else
                {
                    Console.WriteLine("\"" + string_a + "\"");
                    Console.WriteLine("+");
                    Console.WriteLine("\"" + string_b + "\"");
                    Console.WriteLine("=");
                    Console.WriteLine("not confident");
                }
            }
    
            public void fit_into_100(string string_a, string string_b, ref int num_3, ref int num_4)
            {
                string_a = string_a.Replace(" ", String.Empty);
                string_b = string_b.Replace(" ", String.Empty);
                double counter = 0;
                double counter_a = string_a.Length;
                double counter_b = string_b.Length;
    
                foreach (char c in string_a)
                {
                    char temp_1 = c;
                    foreach (char d in string_b)
                    {
                        char temp_2 = d;
                        if (temp_1 == temp_2)
                        {
                            counter++;
                        }
                    }
                }
    
                // percentage=(yourNumber/totalNumber)*100;
                double make_percentage_one = (counter / counter_a) * 100;
                double make_percentage_two = (counter / counter_b) * 100;
    
                int percentage_one = (int)make_percentage_one;
                int percentage_two = (int)make_percentage_two;
    
                // number * percentage / 100
    
                double one = counter_a * 100 / (counter_a + counter_b);
                double two = counter_b * 100 / (counter_a + counter_b);
    
                int a_one = (int)one;
                int a_two = (int)two;
    
                num_3 = a_one;
                num_4 = a_two;
    
                Console.WriteLine("\r\nstring holds what percentage of the total shared char:");
                Console.WriteLine(a_one);
                Console.WriteLine(a_two);
            }
    
            public static string RemoveDuplicates(string input)
            {
                return new string(input.ToCharArray().Distinct().ToArray());
            }
    
            public void length_of_string(string string_a, string string_b, ref int num_1, ref int num_2)
            {
                string a = RemoveDuplicates(string_a);
                string b = RemoveDuplicates(string_b);
    
                a = a.Replace(" ", String.Empty);
                b = b.Replace(" ", String.Empty);
                double counter = 0;
                double counter_a = a.Length;
                double counter_b = b.Length;
    
                foreach (char c in a)
                {
                    char temp_1 = c;
                    foreach (char d in b)
                    {
                        char temp_2 = d;
                        if (temp_2 == ' ') continue;
    
                        if (temp_1 == temp_2)
                        {
                            counter++;
                            break;
                        }
                    }
                }
    
                // percentage=(yourNumber/totalNumber)*100;
                double one = (counter / counter_a) * 100;
                double two = (counter / counter_b) * 100;
    
                int a_one = (int)one;
                int a_two = (int)two;
    
                if (a_one > a_two)
                {
                    a_one = 100 - a_two;
                }
                else if (a_two > a_one)
                {
                    a_two = 100 - a_one;
                }
                else if (a_two == a_one)
                {
                    a_one = 50;
                    a_two = 50;
                }
    
                num_1 = a_one;
                num_2 = a_two;
    
                Console.WriteLine("\r\nsmaller value is: the shared char is what total percentage of the string:");
                Console.WriteLine(a_one);
                Console.WriteLine(a_two);
            }
    
            static void Main(string[] args)
            {
                int num_1 = 0;
                int num_2 = 0;
                int num_3 = 0;
                int num_4 = 0;
    
                Program string_descriptin = new Program();
    
                string string_a = "coffee time";
                //string ok = "so i shot him";
                string string_b = "ok";
    
                Console.WriteLine("- " + string_a);
                Console.WriteLine("- " + string_b);
    
                string_descriptin.length_of_string(string_a, string_b, ref num_1, ref num_2);
    
                string_descriptin.fit_into_100(string_a, string_b, ref num_3, ref num_4);
                
                string_descriptin.calculate(string_a, string_b, num_1, num_2, num_3, num_4);
    
                Console.ReadLine();
            }
        }
    }
    my results;

    Code:
    - coffee time
    - ok
    
    smaller value is: the shared char is what total percentage of the string:
    14
    86
    
    string holds what percentage of the total shared char:
    83
    16
    
    ////////////////
    results:
    
    "coffee time"
    +
    "ok"
    =
    confident
    
    Press any key to continue . . .
    
    ////////////////////////////////////
    last post in this thread unless theres a bug.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what are the bugs in this code..please help me.
    By me001 in forum C Programming
    Replies: 12
    Last Post: 09-23-2008, 10:52 AM
  2. Some bugs which i can't find
    By Lincoln_Poh in forum C++ Programming
    Replies: 7
    Last Post: 09-18-2008, 08:11 PM
  3. May anyone find some bugs from this code?
    By Mathsniper in forum C Programming
    Replies: 3
    Last Post: 12-30-2006, 12:16 PM
  4. cant find bugs
    By sworc66 in forum C Programming
    Replies: 5
    Last Post: 08-15-2003, 08:47 AM

Tags for this Thread