locked
int array not getting correct value RRS feed

  • Question

  • this is my code it should work fine logically but somehow its not getting right value

    below is the code:-

    using System;
    using System.Linq;
    
    namespace JobTest
    {
        class Program
        {
    
            public int[] FuncArray(int[] arr,int n)
            {
                int counter=0;
                int k =arr.Length;
                for(int i=0;i<k;i++)
                {
    
                    if(arr[i]<=n)
                    {
                        counter +=  1;
                    }
                }
                int[] numArray = new int[counter];
                for (int i = 0; i < counter; i++)
                {
    
                    if (arr[i] <= n)
                    {
                        numArray[i] = numArray[i] + arr[i];
                  
                    }
                }
                return numArray;
            }
            static void Main(string[] args)
            {
                
                Program p = new Program();
                int[] evenNums= {1,5,0,1,7,4,10,3,9,16 };  // integer array
              int[]res= p.FuncArray(evenNums, 7);
                int len = res.Length;
                for(int i=0;i<len;i++)
                {
                    Console.Write(res[i]);
                }
            }
        }
    }
    
    it should print 1501743 but it prints this 1501740???
    Wednesday, July 22, 2020 1:27 PM

Answers

  • ... is it possible to achieve it without string manipulation?

    Yes.
    is this the right approach?
            public int[] FuncArray(int[] arr, int n)
            {
                int counter = 0;
                int k = arr.Length;
                for (int i = 0; i < k; i++)
                {
    
                    if (arr[i] <= n)
                    {
                        counter += 1;
                    }
                }
                int[] numArray = new int[counter];
    
                int y = 0;
                for (int i = 0; i < k;)
                {
                    if (arr[i] <= n)
                    {
    
                        numArray[y] = numArray[y] + arr[i];
                        i++;
                        y++;
                    }
                    else
                    {
                        i++;
                    }
                   
                }
                return numArray;
            }
    

    • Marked as answer by Learner177 Wednesday, July 22, 2020 4:26 PM
    Wednesday, July 22, 2020 4:19 PM

All replies

  • Hello,

    Set breakpoints and then step through your code and examine values to find the problem. Others may provide a solution which you don't learn from when it's simply given to you, learn to use the debugger.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Naomi N Wednesday, July 22, 2020 1:44 PM
    Wednesday, July 22, 2020 1:43 PM

  •             int[] numArray = new int[counter];
                for (int i = 0; i < counter; i++)
                {
    
                    if (arr[i] <= n)
                    {
                        numArray[i] = numArray[i] + arr[i];
                  
                    }
                }

    Hint -- Each member of numArray is initialized to 0 when the array is created.  So why do you think that the array contains one of these initial values after the for statement that sets the values has completed?

    This is the kind of thing where you could easily check your logic with a piece of paper and a pencil.

    • Edited by RLWA32 Wednesday, July 22, 2020 1:55 PM
    Wednesday, July 22, 2020 1:54 PM
  • Hello,

    Set breakpoints and then step through your code and examine values to find the problem. Others may provide a solution which you don't learn from when it's simply given to you, learn to use the debugger.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    i did use debugger at the iteration where it gets value 10 it doesn't go further then that and comes out of the loop and pass 0 as the last value of the array which is of size 7.

    it should not be coming out of the loop when it reaches 10 value in evenNums but it is coming out of for loop.

    i can't figure out how and why?

    EDIT:- i think i got the issue...its not iterating above 6 because thats the condition set on the loop...but now i cannot think of a logic how to make a new array and store only numbers less then and equal to 7

    the logic i have used above is not correct!

     
    • Edited by Learner177 Wednesday, July 22, 2020 2:10 PM
    Wednesday, July 22, 2020 1:56 PM
  • As the others wrote, arrays of int are initialized with 0.

    The first loop counts how many numbers are <= n, 7 in the given case.

    The second loop checks the first 7, the number from above, again. When it's lesser or equal 7 then the number is put to the output.

    In your data the 7th number is greater than 7, thus the initialized 7th item does not get an value added. So it sticks to zero.

    In short, your result is expected and correct.. just what you're trying to do is a mystery as you haven't given use more context.


    Wednesday, July 22, 2020 2:13 PM
  • The way you originally wrote you would need to iterate using original array length again in the second loop until some other counter variable reaches your counter variable and then you can break out.

    Or if you're allowed to use LINQ you can do one liner using toArray() method and not do any loops at all.

     

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Wednesday, July 22, 2020 2:22 PM
  • The way you originally wrote you would need to iterate using original array length again in the second loop until some other counter variable reaches your counter variable and then you can break out.

    Or if you're allowed to use LINQ you can do one liner using toArray() method and not do any loops at all.

     

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    thanks i got it that my logic is wrong in the question asked...but now the issue is that as you said i need to iterate over all the numbers in the first array but how do i dynamically set the size of the second array?

    issue is that if stored in 2nd array it will just put 0's in place of numbers greater then 7.

    Wednesday, July 22, 2020 2:34 PM
  • So I dd a bit of googling, found https://docs.microsoft.com/en-us/dotnet/api/system.array.resize?view=netcore-3.1

    In other words, if you want to sort of re-using your original logic of looping and not using LINQ, then just allocate a new array with the same size as your original array, do a single loop populating new array while iterating your passed array and then reduce your array to the needed size.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Wednesday, July 22, 2020 2:43 PM
  • i have this problem and i can't make logic to change array size at run time after every iteration i need to increase the size of the 2nd array 

    this is the thing i want to do i have an array 1{1,5,0,1,7,4,10,3,9,16}now i want to remove all the values greater then 7 and store only numbers less then equal to 7 in a new array.i can seem to figure it.

    i think only possible solution is to convert it to string first?

    Wednesday, July 22, 2020 3:00 PM
  • First the code iterates over the input array to determine how many items should be in the array returned by FuncArray.  So far so good.

    But the code that stores values into the result array increments the index whether or not the corresponding element in the input array should be used.  Fix the indexing.

    Wednesday, July 22, 2020 3:02 PM
  • Wednesday, July 22, 2020 3:07 PM
  • Is your question in the correct forum? Your other was in C#. Completely different language.

    In .NET (C#): You would use List<Int> instead of your array.

    Wednesday, July 22, 2020 3:18 PM
  • First the code iterates over the input array to determine how many items should be in the array returned by FuncArray.  So far so good.

    But the code that stores values into the result array increments the index whether or not the corresponding element in the input array should be used.  Fix the indexing.

     public void FuncArray(int[] arr,int n)
            {
                
                int k =arr.Length;
                int[] numArray = new int[k];
                string stre=null;
                for (int i=0;i<k;i++)
                {
    
                    if (arr[i] <= n)
                    {
    
                         stre +=Convert.ToString(arr[i])+"," ;
    
                    }
                }
    
                int poi = stre.LastIndexOf(",");
                stre = stre.Remove(poi);
              //  string cht = stre.Remove(',');
    
                Console.Write(stre);
               // return numArray;
            }

    this did the work but is it possible to achieve it without string manipulation?

    Wednesday, July 22, 2020 3:31 PM
  • ... is it possible to achieve it without string manipulation?

    Yes.
    Wednesday, July 22, 2020 3:41 PM
  • ... is it possible to achieve it without string manipulation?

    Yes.
    is this the right approach?
            public int[] FuncArray(int[] arr, int n)
            {
                int counter = 0;
                int k = arr.Length;
                for (int i = 0; i < k; i++)
                {
    
                    if (arr[i] <= n)
                    {
                        counter += 1;
                    }
                }
                int[] numArray = new int[counter];
    
                int y = 0;
                for (int i = 0; i < k;)
                {
                    if (arr[i] <= n)
                    {
    
                        numArray[y] = numArray[y] + arr[i];
                        i++;
                        y++;
                    }
                    else
                    {
                        i++;
                    }
                   
                }
                return numArray;
            }
    

    • Marked as answer by Learner177 Wednesday, July 22, 2020 4:26 PM
    Wednesday, July 22, 2020 4:19 PM
  • Is your question in the correct forum? Your other was in C#. Completely different language.

    In .NET (C#): You would use List<Int> instead of your array.

    we can use int[] in c# too
    Wednesday, July 22, 2020 4:26 PM
  • It's one way to do it.

    Another way would be to iterate over the arr array using foreach.

    Wednesday, July 22, 2020 4:35 PM
  • I'd say, if you want to find the longest executing algorithm, then this should work :)

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Wednesday, July 22, 2020 4:41 PM
  • I'd say, if you want to find the longest executing algorithm, then this should work :)

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    and what would be the shortest one?
    Wednesday, July 22, 2020 5:13 PM
  • var newArray = originalArray.Where(x=>x<=i).toArray(); // Not tested

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    • Edited by Naomi N Wednesday, July 22, 2020 5:20 PM
    Wednesday, July 22, 2020 5:19 PM