/* * Aresh Saharkhiz * saharkiz@gmail.com * Associate Professor / Mapua Institute of Technology / Philippines */ using System; // Introduction to Software Testing // Authors: Paul Ammann & Jeff Offutt // Chapter 1, section 1.2, page 16 internal class oddOrPos_class { public static int oddOrPos(int[] x) { // Effects: if x is null throw NullPointerException // else return the number of elements in x that // are either odd or positive (or both) int count = 0; for (int i = 0; i < x.Length; i++) { if (x[i] % 2 == 1 || x[i] > 0) { count++; } } return count; } // test: x=[-3, -2, 0, 1, 4] // Expected = 3 public static void Main(string[] argv) { // Driver method for oddOrPos // Read an array from standard input, call oddOrPos() int[] inArr = new int[argv.Length]; if (argv.Length == 0) { Console.WriteLine("Usage: java oddOrPos v1 [v2] [v3] ... "); return; } for (int i = 0; i < argv.Length; i++) { try { inArr[i] = Convert.ToInt32(argv[i]); } catch (FormatException) { Console.WriteLine("Entry must be a integer, using 1."); inArr[i] = 1; } } Console.WriteLine("Number of elements that are either odd or positive is: " + oddOrPos(inArr)); } }