count significant digits in integer
count significant digits in integer
To provide context, I'm very new to C#, and am currently working my way through Wiley's Software Development Fundamentals. The question is the following:
You are developing a library of utility functions for your application. You need to write a method that takes an integer and counts the number of significant digits in it. You need to create a recursive program to solve the problem. How would you write such a program?.
I think I have created a solution to this question, however I'm unsure if this is correct as I don't know how to return a value stating "this is how many significant digits".
class Program { public static void Main(string[] args) { dig(55535); } public static int dig(int x) { if (x < 10) { return 1; } else { return 1 + dig(x / 10); } } }
Answer by Mark T for count significant digits in integer
You need Console.WriteLine()
in your Main()
method to print to the console.
Pass your call to dig()
as a parameter to Console.WriteLine
.
Answer by dotctor for count significant digits in integer
You should change your Main
function and use Console.WriteLine to write the result to the output window.
public static void Main(string[] args) { // storing the return value of the function in to a variable int result = dig(55535); //print the variable Console.WriteLine(result); //or call Console.WriteLine(dig(55535)); }
Answer by abelenky for count significant digits in integer
class Program { public static void Main(string[] args) { int value = 55535; Console.WriteLine("Number {0} has {1} significant digits", value, dig(value)); } public static int dig(int x) { // Exactly as you have it } }
Answer by Dour High Arch for count significant digits in integer
While answers like ?look at the return value in the debugger? or
Console.WriteLine("Number {0} has {1} significant digits", value, dig(value));
will answer the question you posed, this is not a good strategy for the future. It may be feasible to have a person inspect a single output for a single variable, but this does not scale to projects involving hundreds (or thousands) of functions with thousands (or billions) of possible return values.
Eventually you will have to implement automated tests; this is where the computer tests your output and alerts you when something goes wrong.
The topic is a large one, but you can implement a simple example like so:
public static void Main(string[] args { var tests = new [] { new []{0, 1}, new []{1, 1}, new []{10, 2}, new []{100, 3} }; foreach (var p in tests) if (dig(p[0]) != p[1]) Console.WriteLine("dig({0}) == {1}", p[0], p[1]); }
Answer by Derrick for count significant digits in integer
This does not use recursion, but is a solution to the more general case of counting significant digits in a number. This uses loose rules since it's not possible to know how many trailing zeroes after a decimal point are intended when dealing with non-strings.
public int CountSignificantDigits(decimal num) { if (num == 0) return 1; if (num < 0) num = -num; while (num < 1m) num *= 10; string numStr = num.ToString().Replace(".",""); for (int i = numStr.Length-1; i > 0; i--) { if (numStr[i] != '0') return numStr.Length - (numStr.Length - i) + 1; } return 1; }
Answer by Cobek for count significant digits in integer
As perhaps already noted, the original code does not account for the integer trailing zeros. This might be an another approach.
static void Main(string[] args) { do { Console.Write("Number = "); int number = Int32.Parse(Console.ReadLine()); int abs_number = Math.Abs(number); int numTrailingZeros = 0; bool check_ntz = true; int ndigits = GetSignificantDigits(abs_number, ref check_ntz, ref numTrailingZeros); if (numTrailingZeros == 0) Console.WriteLine("Number of signficant figures: {0}", ndigits); else Console.WriteLine("Number of signficant figures: between {0} and {1}", ndigits, ndigits + numTrailingZeros); Console.WriteLine(); Console.WriteLine("Press ESC to terminate, any other to continue."); Console.WriteLine(); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); return; } static int GetSignificantDigits(int n, ref bool check_ntz, ref int ntz) { if (n < 10) return 1; else { int new_n = (int)(n / 10); if (check_ntz) { if (n % 10 == 0) { ntz++; return GetSignificantDigits(new_n, ref check_ntz, ref ntz); } else { check_ntz = false; return 1 + GetSignificantDigits(new_n, ref check_ntz, ref ntz); } } else return 1 + GetSignificantDigits(new_n, ref check_ntz, ref ntz); } }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment