Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
3.40/5 (5 votes)
See more:
Hi !
Please help me counting the number of elements between 2 elements.
For example I have a string array like this :

<br />
int[] number = new int[]{1,2,3,4,5};<br />


And I expect the code return me 3 (2,3,4 between 1 & 5)

I've fool around Google for a while but I found nothing related to this problem.
Thank you.
Posted
Updated 19-Mar-11 10:22am
v2
Comments
Toli Cuturicu 19-Mar-11 16:22pm    
> I have a string array...
That is NOT a string array!
It is clearly an int array!

You won't find a Google result for this.

This reeks of being a homework assignment, and as such, you won't get code from us written for you.

It's ridiculously easy to do. At a bare minimum, you need some kind of loop, a counter, and an if statement.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Mar-11 21:50pm    
You are right. Giving more detailed Answer simply makes no sense. So, a 5.
--SA
csdv711 19-Mar-11 2:51am    
You're right. But in this case it's not exactly like the example I've given.
I want to count the elements between the 1 elements

{1,2,4,5,1,4,5,6,1,2,3,7,8,9,1}

As you see, how to do if this array has a lot of 1 in it ?
Dave Kreskowiak 19-Mar-11 11:04am    
Actually, the answer doesn't change at all. The exact same code can be used, no matter how many of any digits are in there. Though, the description you gave in your original post can be interpreted multiple ways. Depending on the interpretation and any other information you've left out, the code would have to change to meet those new requirements.
Shahin Khorshidnia 19-Mar-11 5:24am    
Yes, of course it is simple but it's better to present a solution (code).
Dave Kreskowiak 19-Mar-11 11:02am    
No, it's not. If this is a homework assignment, you're not doing him any favors by giving him the answer. 1) He doesn't really learn anything. 2) The code he turns in will NOT look like previous code he's turned in and the prof will know that it's not his work.
Ok.
I found out a solution to this, it's simpler to handle in this case.
For someone may need it in someday.


C#
public void DoCount()
{
    string k = ",";
    // Read the file lines into a string array.
    string[] readlines = File.ReadAllLines(fname);
    // Move into List for handling easier
    List<string> read = new List<string>();
    read.AddRange(readlines);
    // Insert one semicolon into the first index
    read.Insert(0, k);
    // For counting
    int count = 0;
    // An other List to add the number of count
    List<int> result = new List<int>();
    // Loop and count
    for (int i = 0; i < read.Count; i++)
    {
        // If the element is not a semicolon, count it
        if (read[i].ToString() != k)
        {
            count++;
        }
        // If it is, get the number of count
        else
        {
            result.Add(count);
            // Reset count
            count = 0;
        }
    }
}


Best regards.
// So sorry cuz of my bad English xD
 
Share this answer
 
v2
C#
int[] items = new int[] { 1, 2, 3, 2, 7, 4, 2, 8, 9, 1, 2, 10, 11, 3, 4, 10 };
int k = 10, l = 2;
int first =k, second = l;
List<int> between = new List<int>();
for (int i = 0; i < items.Length; i++)
{
    if(items[i] == k)
    {
        first = k;
        second = l;
    }
    if (items[i] == l)
    {
        first = l;
        second = k;
    }
    if (items[i] == first)
        if (i - 1 < items.Length)
            for (int j = i + 1; j < items.Length; j++)
            {
                if (items[j] == second)
                {
                    between.Add(j - i -1);
                }
            }
}


k , l : are your 2 numbers

between : is a list that returns your values.
 
Share this answer
 
v6
Comments
csdv711 19-Mar-11 6:50am    
Thank you.
But would you please helping me again with 2 number k,l is the same value = 2 ?
Shahin Khorshidnia 19-Mar-11 8:11am    
Ok,
The code is working, even k = l = 2.
the list (between) will contain: {1,4,8,2,6,3}
csdv711 19-Mar-11 9:48am    
Thank you a lot xD
Dave Kreskowiak 19-Mar-11 11:06am    
Congratulations! You've given him the answer, albeit a convoluted one, to a homework assignment. That you for doing his work for him! Now, are you going to do his work for him when he tries to get a job as a developer??
csdv711 19-Mar-11 12:45pm    
Hi Dave,
Please take a look at my solution down here, I can be sure that I didn't use any line of Shahin's code. He's just given me a way to solve my problem, just like you.
I know you gave me the right thing, so thank you !
C#
int[] number = new int[]{1,2,3,4,5};
int searchPoint1=1;
int searchPoint2=5;
Console.WriteLine(FindCount(number,searchPoint1,swarchPoint2);


C#
int FoundCount(number,searchPoint1,searchPoint2)
{
bool point1Found=false;
bool point2Found=false;
int count=0;
for(i=0;i<number.length;i++)>
{
if(number[i]==searchPoint1 && point2Found)
{
return count
}
else if(number[i]==searchPoint2 && point1Found)
{
return count;
}
if(point1Count||point2Count)
{
count++;
}
if(number[i]==searchPoint1)
{
point1Found=true;
}
if(number[i]==searchPoint2)
{
point2Found=true;
}
}
 
Share this answer
 
Can't you just loop through the range and count? :~

Or, Is there something that I am missing and even Google could not help.
 
Share this answer
 
v2
Comments
Sandeep Mewara 8-Apr-11 12:37pm    
And why was this downvoted now? That too after some 15 days?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900