Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm doing some exams about web services.
I create a web service that it has to do with arrays, like finding non zero values, positive, negative, members between two given values etc.
The problem is that I don't know how to find if a given value is in range of a given array.

Here is some code I made in web service and in a costumer of the service.

Web service app.
C#
public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
       //non zero members
        public List<int> AnetaretJoZero(int [] anetaret)
        {
            var lista = new List<int>();
            for (int i = 0; i < anetaret.Length; i++)
            {
                if (anetaret[i] != 0)
                {
                    lista.Add(anetaret[i]);
                }
            }
            return lista;
           
        }

        [WebMethod]
        //negative members
        public List<int> AnetaretNegativ(int[] negativ)
        {
            var list_negativ = new List<int>();
            for (int i = 0; i < negativ.Length; i++)
            {
                if (negativ[i] < 0)
                {
                    list_negativ.Add(negativ[i]);
                }
            }
            return list_negativ;
        }
        [WebMethod]
        //positive members
        public List<int> AnetaretPozitiv(int[] pozitiv)
        {
            var list_pozitiv = new List<int>();
            for (int i = 0; i < pozitiv.Length; i++)
            {
                if (pozitiv[i] > 0)
                {
                    list_pozitiv.Add(pozitiv[i]);
                }
            }
            return list_pozitiv;
        }
        [WebMethod]
        //members between two given values
        public List<int> AnetaretMesVlerave(int[] mesvlera, int a, int b)
        {
            var list_mesvlera = new List<int>();
            for (int i = 0; i < mesvlera.Length; i++)
            {
                if (mesvlera[i]>=a & mesvlera[i]<=b)
                {
                    list_mesvlera.Add(mesvlera[i]); 
                }
            }
            return list_mesvlera;
        }
// here I'm attempting to do some code about my problem to
//find if the given value is in range of a given array

        }


The consumer code app
C#
static void Main(string[] args)
        {
           //this is a consumer array
            int[] vek = { -3, 1, 0, 2,-5, -25, 0, 5, 6, 12, 45, 0 };
            referenca.Service1 sherbimi = new referenca.Service1();
            //vek = sherbimi.Vektori(vek);
            int[] resVek = sherbimi.AnetaretJoZero(vek);
            
            //non zero values of the array
            Console.WriteLine("Anetaret jo zero të vektorit janë: ");
            Console.WriteLine();
            for (int i = 0; i < resVek.Length; i++)
            {
                Console.Write(resVek[i] + ", ");
            }
            
            //negative member of array
            int[] vekrez = sherbimi.AnetaretNegativ(vek);
            Console.WriteLine();
            Console.WriteLine("Anetaret negativ të vektorit janë: ");
            Console.WriteLine();
            for (int j = 0; j < vekrez.Length; j++)
            {
                Console.Write(vekrez[j] + ", ");
                
            }

             //positive member of array
            int[] vekpoz = sherbimi.AnetaretPozitiv(vek);
            Console.WriteLine();
            Console.WriteLine("Anetaret pozitiv jane: ");
            Console.WriteLine();
            for (int i = 0; i < vekpoz.Length; i++)
            {
                Console.Write(vekpoz[i] + ", ");
            }

          //members between two given values
            int[] vekmesvlera = sherbimi.AnetaretMesVlerave(vek, -5, 12);
            Console.WriteLine();
            Console.WriteLine("Anetaret mesvlerave jane: ");
            Console.WriteLine();
            for (int i = 0; i < vekmesvlera.Length; i++)
            {
                Console.Write(vekmesvlera[i] + ", ");
            }

            //this is what I'm trying to do to find 
            //if the value exists in the range of the array
           // this method checks if the given value exists in the array or not
            int[] ekvlera = sherbimi.EkzistonVlera(vek, -5);
            Console.WriteLine();
            Console.WriteLine("Anetari i dhënë Y: ");
            Console.WriteLine();
            if (((IList<int>)ekvlera).Contains(-5)))
            {
                Console.Write("Vlera ekziston"); 
            }
            else
            Console.WriteLine("Vlera e dhënë nuk ekziston");
                       Console.ReadLine();
        }


Thank you in advance for your reply.
Posted
Updated 26-Feb-15 3:41am
v3
Comments
[no name] 26-Feb-15 11:02am    
What exactly do you mean by "if a given value is in range of a given array" ?

1 solution

One author of one book (sorry, I cannot give an exact quite and reference at this time), reasonably noted: "'range' is a bad term!". The set of (allowed values) is called "domain". There are two domains related to the arrays: one is the domain of its indices, most usually called 'range', and the domain of values of the elements.

If I saw only the title, I would have decided that you are talking about indices. Then you could have said some integer index value is in domain or not, this is simply index >= 0 && index <= vec.length - 1. This is almost always so, but not always. You could create not a zero-based array using the class System.Array, the you would need to do a bit more complicated check:
C#
bool isInRange = index >= ver.GetLowerBound(0) && index <= vec.GetUpperBound(0);

Please see:
https://msdn.microsoft.com/en-us/library/system.array%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.array.getlowerbound%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.array.getupperbound%28v=vs.110%29.aspx[^].

To check is some value is an element of the set of array values, you can write a function traversing all array element with foreach loop and compare the value for equivalence using '==' operator. Don't forget to return immediately if the matching element is found. This is too trivial to explain. But you don't have to do it, because you can use this method: https://msdn.microsoft.com/en-us/library/eha9t187%28v=vs.110%29.aspx[^].

You can use it with zero-based arrays anyway (which is the case with your arrays), but, generally, not with others, because "not found" is indicated by −1.

—SA
 
Share this answer
 
Comments
dr_iton 26-Feb-15 12:14pm    
What I'm pretending to do is, if I give an integer value, I want to see in the array if that value is inside of arrays domain. for example:
if I'm trying to see if number 4 is inside arrays domain
Array has the following members int[] array = {-3,-1, 1, 5, 6}
so mathematically the domain for this array is (-3,6)
that means the 4 should be inside the domain.
Hopefully you got my point.
Cheers
Sergey Alexandrovich Kryukov 26-Feb-15 12:25pm    
I just explained that there are two different domains related to a single array. You could read about it and start with indicated that what domain you are talking with. You are talking about domain of element values.

Now, it's a misconception that "domain for this array is {-3, 6}". The domain is exactly {-3,-1, 1, 5, 6}.
But if the array was [-3,-1, 1, 5, 6, 1, 1, 1, 1], that domain still would be {-3,-1, 1, 5, 6}. Please try to get it.

You did not define what set do you want to find, but it could be the set
x: x >= min(domain) && x <= max(domain)
You did not formulate, only gave an example, but no example is a definition.
This would not be the "range" or "domain" (of anything); it would be "envelope" or "convex hull" (in the trivial case of being "convex", in case of just integer sets) of the domain.

If this is what you want, your "problem" is reduced to finding min and max (do I even have to define minimum and maximum, which, due to the fact you deal with finite set of integers, always exist?). Now, do I need to help you to find min and max? Just the hint: to find maximum, start with int.MinValue and traverse the array elements, to find minimum, start with int.MaxValue.

If this is what you need, did you get how to calculate it? If you did, will you accept my answer formally?

—SA
dr_iton 26-Feb-15 17:33pm    
My mother tongue is not English, that is the reason that you misunderstand me, or better said, there was a lack of English words from my side.
Definitely I've got the point now.
Once again thank you.
Sergey Alexandrovich Kryukov 26-Feb-15 17:54pm    
English itself is not a problem, but understanding can be. This is not my native language either.
You are very welcome.
Good luck, call again.
—SA
Santosh K. Tripathi 26-Feb-15 23:48pm    
5+ from me. :)

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