Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace FirstConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = 5;
- do
- {
- n = InputInt("Please enter the size of the array (unsigned integer): ");
- } while (n < 1);
- Console.WriteLine("The size of the array is {0}", n);
- int[] myArray = new int[n];
- InputArray(myArray);
- PrintOddsGreaterThan7(myArray);
- Console.WriteLine("The average is {0}.", GetAverage(myArray));
- }
- static void InputArray(int[] myArray) {
- int value = 0;
- for (int i = 0; i < myArray.Length; i++) {
- value = InputInt("Please enter element #" + i + ": ");
- myArray[i] = value;
- }
- }
- static void PrintOddsGreaterThan7(int[] myArray)
- {
- for (int i = 0; i < myArray.Length; i++) {
- if (myArray[i] % 2 != 0 && myArray[i] > 7) Console.WriteLine(myArray[i]);
- }
- }
- static double GetAverage(int[] myArray)
- {
- int sum = 0;
- for (int i = 0; i < myArray.Length; i++)
- {
- sum += myArray[i];
- }
- return sum / myArray.Length;
- }
- // This method belongs to View (Presentation Layer)
- static int InputInt(string message)
- {
- int n = 0;
- bool flag = false;
- do
- {
- Console.Write(message);
- try
- {
- n = Int32.Parse(Console.ReadLine());
- flag = false;
- }
- catch (System.FormatException e)
- {
- Console.WriteLine("Please, enter a valid integer number (e.g. 5). Exception {0}", e.Message);
- flag = true;
- }
- } while (flag);
- return n;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment