Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Homework
static int Control(int x=0)
 
     {
         string str = "(a(b(c)d)e)";
 
         for (int i = 0; i <= str.Length; i++)
         {
             if (str[i] =='(')
 
                 x++;
 
             if (str[i] == ')')
                 x--;
         }
         return x;
 
     }
 
     static void Main(string[] args)
     {
         int x = 0;
 
         bool flag = false;
         if (Control(x)==0)
 
         {
             flag = true;
         }
         Console.WriteLine(flag);
 
         Console.ReadLine();
     }
Posted 6-Jan-13 7:51am
Edited 6-Jan-13 7:54am
v2
Comments
Mika Wendelius at 6-Jan-13 13:54pm
   
What's the return value and what would you think it should be?
 
Also have you tried yet creating the recursive version?
Mika Wendelius at 6-Jan-13 14:10pm
   
Also try using the debugger to see what actually happens. You can also try a bit different string. What happens with:

...
string str = "(a(b(cde)";
...

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You were ordered to write two functions, but you have written only one, incorrectly. Also, you did not explain your question properly.
 
From the very beginning: we are not going to do your homework for your. You are given a change to solve your assignments to learn something. Use this chance well.
 
Now, in your code there I can see two bugs. First, instead of '<=' you should use '<', in the ending condition of the loop. In the last element, you will get out-of-range exception; there are no elements at the index of [str.Length] or above.
 
The second one is the function signature. You don't need x as a parameter. Declare it locally.
 
One important practical advice: never ever create single-character names of any entities. How are you going to use "Find" functionality? How can you figure out what do they mean if you read your own code later? All the names should be semantic, maybe, not too long, but self-explaining and even without use of abbreviation: instead of "i" — "index"; instead of "x" — "count", or "balance".
 
Good luck with recursion version and everything else.
 
—SA
  Permalink  
Comments
ridoy at 6-Jan-13 15:14pm
   
+5
Sergey Alexandrovich Kryukov at 6-Jan-13 19:14pm
   
Thank you,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100