Click here to Skip to main content
15,846,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose We have 5 number 2277,4004,11,23,583835,1010 and we need to create password from these.
Conditions are:

password = Sum of all stable numbers - Sum of all Unstable numbers

where stable numbers are the numbers those who have same frequency/occurrence of every digit in the number.
And Unstable numbers are those who have different frequency/occurrence of digits in number.

the method should look like

public int findpassword(int input1,int input2,int input3,int input4,int input5)
{
}

What I have tried:

I can check for a single number whether it is stable or not buthow could I check for all the numbers passed through a function argument whether they are sable or not?
Posted
Updated 19-Jan-18 5:41am

Change method to
public int findpassword(params int[] input)
{
   if(input == null)
       throw new ArgumentNullException(nameof(input));

   for(int i=0; i < input.Length; i++)
   {
   }
}

maybe?
 
Share this answer
 
Comments
Member 10585568 19-Jan-18 10:05am    
I am supposed to do with that function only that's where the main challenge.
Alex Schunk 19-Jan-18 11:50am    
public int findpassword(int input1,int input2,int input3,int input4,int input5)
{
   int findpassword(params int[] input)
   {
      if(input == null)
          throw new ArgumentNullException(nameof(input));

      for(int i=0; i < input.Length; i++)
      {
         // Do Stuff
      }
   }

   var result = findpassword(new int[] { input1, input2, input3, input4, input5 });
}
Member 10585568 20-Jan-18 0:18am    
Thank you for the help.I am working with C# 5.0 where nested functions are not allowed as I saw in some articles.Anyways thank you sir.
Alex Schunk 20-Jan-18 3:59am    
Nested functions are only a compiler trick... The compiler will create a second private method out of the nested one... A thing which you can also do.
Quote:
I am supposed to do with that function only that's where the main challenge.

It's the same thing, only with params you don't have to name each argument individually, and you can pass as many or as few arguments as you need.

But ... if your homework requires you to use that exact method signature, then start by writing a method which takes an integer, and returns a bool:
C#
private static bool IsStable(int value)
   {
   ...
   }
Then call that five times in your code:
C#
int stableCnt = 0;
int unstableCnt = 0;
if (IsStable(input1)) stableCnt++;
else unstableCnt++;
if (IsStable(input2)) stableCnt++;
else unstableCnt++;
...
 
Share this answer
 
Comments
Member 10585568 20-Jan-18 14:16pm    
Thank you sir for the solution.successfully completed the whole work.Thanks a lot sir.
OriginalGriff 20-Jan-18 14:59pm    
You're welcome

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

  Print Answers RSS


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