views:

270

answers:

6

hi im preparing for an exam and i am new to c# i am having problem with a question that i have

what i am having problem is that i need to find out how many times the number greater than or less than one appears in the array for example

if i have an array {1,1,1,2,3,-18,45,1} here numbers that are greater than or less than one appears just once

another example if i have an array {1,1,1,2,3,-18,45,1,0} here numbers that are greater than or less than one appears twice that is at the second last there is one and after that there is zero

yet another example {1,1,2,3,1,-18,26,1} here numbers that are greater than or less than one appears twice one set is {2,3} thats once and another set is {-18,26} so thats twice

so far ive been able to loop the array but im stuck .. how to do this

class Program
{
    static void Main(string[] args)
    {
        System.Console.WriteLine(isOneBalanced(new int[] { 1, 1, 1, 2, -18, 45, 1 }));
    }

    static int isOneBalanced(int[] a)
    {
        foreach(int a1 in a)
        {

        }
        return 0;
    }
}

how to do this can anybody help me

+2  A: 

Iterate through the array in a foreach loop, and test each value against 1. If it's less than 1, increment num_greater by 1, and if it's less, increment nun_less by 1.

Jonathan Sampson
A: 

You need two variables. One is a boolean, and is used to keep track of whether you are in a series of numbers which are not equal to 1 or -1. The other is an integer, and is used to keep track of the total number of series you encounter.

Alan McBee
This answer seems the most correct, but I didn't see anything about comparing with -1 in the question, so you should likely remove that.
Ryan Versaw
You're right. Brain fart on my part.
Alan McBee
A: 

You need a boolean to determine if 1 was the previous value and a integer to count the number of times you don't see a 1 when the boolean is set.

Talljoe
+2  A: 
/* Create variables to be used throughout 
   the process */
int counter = 0;
bool newgroup = true;

/* For each array entry: this loop will
   cycle through our array, accessing one
   item at a time and refer to it as 'a1' */
foreach(int a1 in a) 
{
  /* If 'a1' (our current array entry) is
     absolutely equal to 1, set the value
     of our boolean variable to true */
  if(a1 == 1)   
    newgroup = true;
  else 
  {
    /* Else (meaning, a1 isn't equal to 1), 
       if our variable 'newgroup' is true
       increment our counter variable by 1
       and set our boolean variable to false */
    if(newgroup)
    {
      counter++;    
      newgroup = false;
    }
  }
}    
return counter;
Code is not the preferred answer for homework questions.
Beska
@Beska: Since he is studying for an exam, a code answer is not necessarily a bad thing...though it's not nearly as helpful as an explanation.
Brian
Arguably true...fair enough.
Beska
(To either the poster, or someone with high enough privs to do so...this would be much more helpful with some comments...the variable names are good, but the concept may still go down a bit easier with some additional commentary about the "what you're doing")
Beska
+1  A: 

Okay...in a nutshell you want to iterate over this loop once, figure out when you're "between 1's", and increment a counter.

There's an additional trick or two, though...one "trick" is that you should pay special attention to keeping track of whether you've already incremented for a particular set or not. If your series is {1, 3, 5, 1}, you don't want to do something like:

1: don't increment 
3: increment
5: increment       // this is bad!
1: don't increment

because you'll end up with 2, instead of 1. So keep track of whether you've already incremented with a boolean...call it something like "haveIncremented", and you'll end up with a line like:

// if (haveIncremented is false)
    // increment my counter
    // set haveIncremented to true

Then, of course, you'll need to make sure that you set haveIncremented set back to false again, when you start a new set. This will happen when you read in a new "1", and are now ready to start a new "set".

I'm trying to walk a fine line between coding and explaining and helping. Hopefully this will help while not giving away the whole farm.

Good luck.

Beska
thanks Beska i got it now , thanks for explaining it to me in detail =)
jarus
A: 

This could be seen as a finite state machine problem with a sequence of inputs... keep track of your state when you iterate through, and updating your state based on the current value of the array. For instance, one of the states could be responsible for incrementing your bounded elements counter. This should be easy to figure out if you diagram it on a piece of paper... circles for states, and arrows linking states based on the type of input you see.

Good luck!

tbischel