9 Replies - 357 Views - Last Post: 31 December 2012 - 01:42 PM Rate Topic: -----

#1 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

struggling to debug code problem

Posted 31 December 2012 - 08:40 AM

hi all

so here is my challeneg to debug this code i will first give u what i was given then i will show what i managed to do and what error i get

public class Pal {

    public static void Main() {
        Console.WriteLine("How many chars? ");
        int size =int.Parse(Console.ReadLine());
            char [] a = new char[size];
        for (int i = 0; i < a.Length; i++) {
            Console.WriteLine(" Enter Next Character");
            
                a[i] = int.Parse(Console.ReadLine());
        }
        Console.Write(" the word is ");
        for (int i = 0; i < a.Length; i++)
            
            Console.Write(a[i]);

        for (int i = 1;  i < a.Length/2; i++)
            if (a[i] != a[a.Length-i]){
                Console.WriteLine(" is not a palidrome");
                Environment.Exit(0);
            }
        Console.WriteLine(" is a palindrome");
        Console.ReadLine();
    }
}




here is what i managed to get to

public class Pal {

    public static void Main() {
        Console.WriteLine("How many chars? ");
        int size =int.Parse(Console.ReadLine());
            char [] a = new char[size];
        for (int i = 0; i < a.Length; i++) {
            Console.WriteLine(" Enter Next Character");
            
                a[i] = char.Parse(Console.ReadLine());
        }
        Console.Write(" the word is ");
        for (int i = 0; i < a.Length; i++)
            
            Console.Write(a[i]);

        for (int i = 1;  i < a.Length; i++)
            if (a[i] != a[a.Length-i]){
                Console.WriteLine(" is not a palidrome");
                Environment.Exit(0);
            }
        Console.WriteLine(" is a palindrome");
        Console.ReadLine();
    }
}



now i am using the example with this as 4 letters o,t,t,o hoping for it to work but i am getting it display "IS not a palindrome" when it should be?

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: struggling to debug code problem

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3531
  • View blog
  • Posts: 10,935
  • Joined: 05-May 12

Re: struggling to debug code problem

Posted 31 December 2012 - 08:45 AM

Why are you starting at 1 on line 17, but started at 0 on your other loops?
Was This Post Helpful? 1
  • +
  • -

#3 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: struggling to debug code problem

Posted 31 December 2012 - 09:51 AM

ah ok i missed that but after changing that to 0, i run it with same example then it comes up with an error on line 18 - Index was outside the bounds of the array.? never had this before as i only learning this area of arrays so dont understand whats wrong with it

This post has been edited by bboyzeez: 31 December 2012 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3531
  • View blog
  • Posts: 10,935
  • Joined: 05-May 12

Re: struggling to debug code problem

Posted 31 December 2012 - 10:00 AM

If you have:
int myArray[5];



You can only access elements myArray[0] through myArray[4]. myArray.Length will be equal to 5. Notice that the maximum element you can access is myArray.Length - 1.

If all you did was change your index to to start at 0, then a.Length - 0 == a.Length, not a.Length - 1.
Was This Post Helpful? 0
  • +
  • -

#5 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: struggling to debug code problem

Posted 31 December 2012 - 10:19 AM

ok i realise that now but what im getting confused with is looking at the values as im going along line by line ....line 18 , am i right in thinking it doesnt seem to be comparing the actual chars and only looking at it as int values? should i be converting the chars into a string with arrays and then use the indexof to check letters? or is there a different way with the code only that was given in beginning?
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: struggling to debug code problem

Posted 31 December 2012 - 10:31 AM

It's comparing them as characters, don't worry about that part. The only issue in that line is the indexes and making sure you are comparing the right ones.

And there are many different ways to solve this, I'd have done it differently myself :)
Was This Post Helpful? 0
  • +
  • -

#7 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: struggling to debug code problem

Posted 31 December 2012 - 11:51 AM

ye i decided to change to a do loop and do it this way it works now so thats a plus :)/>


    public class Pal {

    public static void Main() {
        Console.WriteLine("How many chars? ");
        int size =int.Parse(Console.ReadLine());
            char [] a = new char[size];
        for (int i = 0; i < a.Length; i++) {
            Console.WriteLine(" Enter Next Character");
            
                a[i] = char.Parse(Console.ReadLine());
        }
        Console.Write(" the word is ");
        for (int i = 0; i < a.Length; i++)
            
            Console.Write(a[i]);
        int x = (a.Length - 1);
        int y = 0;
       do{
            if (a[y] == a[x] ) {
                y++;
                x--;
                
            }
           
            
            else if (a[y] != a[x])
            {

                Console.WriteLine(" is not a palindrome");
                Console.ReadLine();
                Environment.Exit(0);
            }
            
       } while  (y < x);
       Console.WriteLine("is a palidrome");
        Console.ReadLine();
    }
}


This post has been edited by bboyzeez: 31 December 2012 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4451
  • View blog
  • Posts: 7,752
  • Joined: 08-June 10

Re: struggling to debug code problem

Posted 31 December 2012 - 12:58 PM

One thing that will help you in the future is to use proper tabbing and alignment. The compiler doesn't care about your whitespace, but humans reading it (yourself included) will. I'm not saying it would have helped you solve this issue; it's more of a general advice tidbit. You'll benefit in the long run from choosing an indention style and sticking with it. In this example, your indention is all over the place. If you use VS, it'll take care of that for you. Otherwise, you should use an editor that will help you, like Notepad++ or something similar.
Was This Post Helpful? 1
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: struggling to debug code problem

Posted 31 December 2012 - 01:01 PM

public class Pal {
    public static void Main() {
        Console.Write("Enter the entire word : ");
        String input = Console.ReadLine();
        Boolean palindrome = true;

        Console.Write("The word {0}", input);

        int testLength = input.Length / 2;

        for (int i = 0; i < testLength; i++) {
            if (input[i] != input[input.Length - 1 - i]) {
                palindrome = false;
                break;
            }
        }

        Console.WriteLine(" is {0}a palindrome", palindrome ? String.Empty : "not ");
        Console.ReadLine();
    }
}


Off the top of my head, different way to do the same thing :)
Was This Post Helpful? 1
  • +
  • -

#10 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: struggling to debug code problem

Posted 31 December 2012 - 01:42 PM

yes curtis i did intend to keep to an indent style but the longer this challenge got confusing the more i just focused to get it complete i will clean up my style now thanks:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1