Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Integers Array manipulation examples
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { int[] arr = {9,4,1,2,7,8,6,5,0,3}; arr = sort(arr); Console.Write("The sorted array of integers: "); for (int i = 0; i < arr.Length; i++) Console.Write(arr[i] + " "); Console.WriteLine(); int sum = add(arr); Console.WriteLine("The sum of all the integers in the array: {0}", sum); Console.WriteLine(); List<int> Odds = allOdds(arr); Console.Write("The Odd numbers in the array of integers: "); foreach (var item in Odds) Console.Write(item + " "); Console.WriteLine(); List<int> Evens = allEvens(arr); Console.Write("The Even numbers in the array of integers: "); foreach (var item in Evens) Console.Write(item + " "); Console.WriteLine(); int number1 = 43; bool IsPrime = isPrime(number1); Console.Write("Is the interger {0} a Prime? : {1}", number1,IsPrime); Console.WriteLine(); int number2 = 11; int fact = factorial(number2); Console.Write("The factorial of {0} is: {1}",number2,fact); Console.WriteLine(); List<int> fibSeqence = fibonacci(number2); Console.Write("The fibonacci sequence upto number {0} is: "); foreach (var item in fibSeqence) Console.Write(item + " "); Console.WriteLine(); int[] arr1 = {10,15,17,19, 21, 23}; int[] arr2 = {6,9,11,13, 20}; List<int> mergedArray = merge(arr1,arr2); Console.Write("The merged array of two sorted arrays is : "); foreach (var item in mergedArray) Console.Write(item + " "); } public static int[] sort(int[] arr) { for (int i = 0; i < arr.Length; i++) { for (int j =0; j< arr.Length; j++) { if (arr[i] < arr[j]) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } } return arr; } public static int add(int[] arr) { int total = 0; for (int i = 0; i < arr.Length; i++) { total = total + arr[i]; } return total; } public static List<int> allOdds(int[] arr) { List<int> allOdds = new List<int>(); for (int i = 0; i < arr.Length; i++) { if (arr[i] % 2 != 0) allOdds.Add(arr[i]); } return allOdds; } public static List<int> allEvens(int[] arr) { List<int> allEvens = new List<int>(); for (int i = 0; i < arr.Length; i++) { if ((arr[i]%2) == 0) allEvens.Add(arr[i]); } return allEvens; } public static bool isPrime(int num) { bool isItPrime = true; if ((num == 0) || (num == 1)) isItPrime = false; else { for (int i = 2; i < num; i++) { if (num % i == 0) isItPrime = false; } } return isItPrime; } public static int factorial(int num) { int facto = 1; if (num == 0) facto = 0; if (num == 1) facto = 1; else facto = num * factorial(num-1); return facto; } public static List<int> fibonacci(int num) { List<int> fibb = new List<int>(); int a = 0; int b = 1; fibb.Add(a); for (int i =1; i < num; i++) { int c= a+b; a=b; b=c; fibb.Add(b); } return fibb; } public static List<int> merge(int[] arr1, int[] arr2) { List<int> finalArray = new List<int>(); for (int i =0; i < arr1.Length; i++) { for (int j = 0; j < arr2.Length; j++) { if (arr1[i] < arr2[j]) { if ((i == arr1.Length) && (j == arr2.Length)) { finalArray.Add(arr1[i]); finalArray.Add(arr2[j]); break; } else { finalArray.Add(arr1[i]); break; } } else { if (!finalArray.Contains(arr2[j])) finalArray.Add(arr2[j]); if (( j == arr2.Length-1 ) && (finalArray.Contains(arr2[j]))) finalArray.Add(arr1[i]); } } } return finalArray; } } }
run
|
edit
|
history
|
help
0
s 3
凑单
uniqueness 2.8
Homework3
Reflection 2
Action<> usage example
k
Morse code
Double Jump and Wall Jump
dynamic in C#