Last active
August 29, 2015 14:20
-
-
Save nstanevski/3aa7c183f87bfb1cc3c6 to your computer and use it in GitHub Desktop.
Homework 02 - Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* | |
* Write a method GetMax() with two parameters that returns the larger of two integers. | |
* Write a program that reads 2 integers from the console and prints the largest of them | |
* using the method GetMax(). | |
*/ | |
class BiggerNumber | |
{ | |
private static int GetMax(int first, int second) | |
{ | |
return first > second ? first : second; | |
} | |
static void Main() | |
{ | |
int firstNumber = int.Parse(Console.ReadLine()); | |
int secondNumber = int.Parse(Console.ReadLine()); | |
int max = GetMax(firstNumber, secondNumber); | |
Console.WriteLine(max); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* | |
* Write a method that returns the last digit of a given integer as an English word. | |
* Test the method with different input values. Ensure you name the method properly. | |
*/ | |
class LastDigitOfNumber | |
{ | |
private static string GetLastDigitAsWord(int num) | |
{ | |
string[] digitWords = { "zero", "one", "two", "three", "four", | |
"five","six","seven","eight","nine"}; | |
int remainder = num % 10; | |
return digitWords[remainder]; | |
} | |
static void Main() | |
{ | |
int number = int.Parse(Console.ReadLine()); | |
Console.WriteLine(GetLastDigitAsWord(number)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
/* | |
* Write a method that checks if the element at given position | |
* in a given array of integers is larger than its two neighbours | |
* (when such exist). | |
*/ | |
class LargerThanNeighbours | |
{ | |
private static bool IsLargerThanNeighbours(int[] numbers, int i) | |
{ | |
//check first element: | |
if(i==0){ | |
if(numbers[0]>numbers[1]) | |
return true; | |
else | |
return false; | |
} | |
//check last element | |
if (i == numbers.Length-1) | |
{ | |
if (numbers[numbers.Length - 1] > numbers[numbers.Length - 2]) | |
return true; | |
else | |
return false; | |
} | |
//check other elements | |
if (numbers[i] > numbers[i - 1] && numbers[i] > numbers[i + 1]) | |
return true; | |
else | |
return false; | |
} | |
static void Main() | |
{ | |
Console.Write("Please, enter a sequence of space-separated integers: "); | |
int[] numbers = Console.ReadLine().Trim().Split().Select(p => int.Parse(p)).ToArray(); | |
for (int i = 0; i < numbers.Length; i++) | |
{ | |
Console.WriteLine(IsLargerThanNeighbours(numbers, i)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
/* | |
* Write a method that returns the index of the first element in array that is larger | |
* than its neighbours, or -1 if there's no such element. Use the method | |
* from the previous exercise | |
*/ | |
class FirstLargerThanNeighbours | |
{ | |
private static bool IsLargerThanNeighbours(int[] numbers, int i) | |
{ | |
//check first element: | |
if (i == 0) | |
{ | |
if (numbers[0] > numbers[1]) | |
return true; | |
else | |
return false; | |
} | |
//check last element | |
if (i == numbers.Length - 1) | |
{ | |
if (numbers[numbers.Length - 1] > numbers[numbers.Length - 2]) | |
return true; | |
else | |
return false; | |
} | |
//check other elements | |
if (numbers[i] > numbers[i - 1] && numbers[i] > numbers[i + 1]) | |
return true; | |
else | |
return false; | |
} | |
private static int GetFirstElementLargerThanNeighbours(int[] array) | |
{ | |
for (int i = 0; i < array.Length; i++) | |
{ | |
if (IsLargerThanNeighbours(array, i)) | |
{ | |
return i; | |
} | |
} | |
return -1; | |
} | |
static void Main() | |
{ | |
Console.Write("Please, enter a sequence of space-separated integers: "); | |
int[] numbers = Console.ReadLine().Trim().Split().Select(p => int.Parse(p)).ToArray(); | |
int indexFirstLarger = GetFirstElementLargerThanNeighbours(numbers); | |
Console.WriteLine("Index of first larger element: " + indexFirstLarger); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* | |
* Write a method that reverses the digits of a given floating-point number. | |
*/ | |
class ReverseNumber | |
{ | |
private static string ReverseString(string str) | |
{ | |
if (str == null) | |
return ""; | |
char[] charArr = str.ToCharArray(); | |
Array.Reverse(charArr); | |
return new string(charArr); | |
} | |
private static double GetReversedNumber(double num) | |
{ | |
bool isNegative = num < 0; | |
num = Math.Abs(num); | |
string numStr = num.ToString(); | |
numStr = ReverseString(numStr); | |
double result = double.Parse(numStr); | |
return isNegative ? -result : result; | |
} | |
static void Main() | |
{ | |
double num = double.Parse(Console.ReadLine()); | |
double revNum = GetReversedNumber(num); | |
Console.WriteLine(revNum); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
class NumberCalculations | |
{ | |
private static int minimum(int[] array) | |
{ | |
int min = int.MaxValue; | |
for (int i = 0; i < array.Length; i++) | |
if (array[i] < min) | |
min = array[i]; | |
return min; | |
} | |
private static int maximum(int[] array) | |
{ | |
int max = int.MinValue; | |
for (int i = 0; i < array.Length; i++) | |
if (array[i] > max) | |
max = array[i]; | |
return max; | |
} | |
private static double average(int[] array) | |
{ | |
if (array.Length == 0) return 0.0; | |
return ((double)sum(array)) / array.Length; | |
} | |
private static int sum(int[] array) | |
{ | |
int result = 0; | |
for (int i = 0; i < array.Length; i++) | |
{ | |
result += array[i]; | |
} | |
return result; | |
} | |
private static int product(int[] array) | |
{ | |
int result = 1; | |
for (int i = 0; i < array.Length; i++) | |
{ | |
result *= array[i]; | |
} | |
return result; | |
} | |
//double | |
private static double minimum(double[] array) | |
{ | |
double min = double.MaxValue; | |
for (int i = 0; i < array.Length; i++) | |
if (array[i] < min) | |
min = array[i]; | |
return min; | |
} | |
private static double maximum(double[] array) | |
{ | |
double max = double.MinValue; | |
for (int i = 0; i < array.Length; i++) | |
if (array[i] > max) | |
max = array[i]; | |
return max; | |
} | |
private static double average(double[] array) | |
{ | |
if (array.Length == 0) return 0.0; | |
return ((double)sum(array)) / array.Length; | |
} | |
private static double sum(double[] array) | |
{ | |
double result = 0; | |
for (int i = 0; i < array.Length; i++) | |
{ | |
result += array[i]; | |
} | |
return result; | |
} | |
private static double product(double[] array) | |
{ | |
double result = 1; | |
for (int i = 0; i < array.Length; i++) | |
{ | |
result *= array[i]; | |
} | |
return result; | |
} | |
static void Main() | |
{ | |
Console.Write("Please, enter a sequence of space-separated integers: "); | |
int[] intNumbers = Console.ReadLine().Trim().Split().Select(p => int.Parse(p)).ToArray(); | |
Console.WriteLine("Integer array minimum: {0}", minimum(intNumbers)); | |
Console.WriteLine("Integer array maximum: {0}", maximum(intNumbers)); | |
Console.WriteLine("Integer array average: {0}", average(intNumbers)); | |
Console.WriteLine("Integer array sum: {0}", sum(intNumbers)); | |
Console.WriteLine("Integer array product: {0}", product(intNumbers)); | |
Console.Write("Please, enter a sequence of space-separated doubles: "); | |
double[] doubleNumbers = Console.ReadLine().Trim().Split().Select(p => double.Parse(p)).ToArray(); | |
Console.WriteLine("Double array minimum: {0}", minimum(doubleNumbers)); | |
Console.WriteLine("Double array maximum: {0}", maximum(doubleNumbers)); | |
Console.WriteLine("Double array average: {0}", average(doubleNumbers)); | |
Console.WriteLine("Double array sum: {0}", sum(doubleNumbers)); | |
Console.WriteLine("Double array product: {0}", product(doubleNumbers)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* | |
* Write a method which takes an array of any type and sorts it. | |
* Use bubble sort or selection sort (your own implementation). | |
* You may re-use your code from a previous homework and modify it. | |
* Use a generic method (read in Internet about generic methods in C#). | |
* Make sure that what you're trying to sort can be sorted – | |
* your method should work with numbers, strings, dates, etc., | |
* but not necessarily with custom classes like Student. | |
*/ | |
class GenericArraySort | |
{ | |
private static void GenericBubbleSort<T>(T[] genericArr) where T : IComparable | |
{ | |
int len = genericArr.Length; | |
for (int i = 0; i < len; i++) | |
{ | |
for (int j = 0; j < len-1; j++) | |
{ | |
if (genericArr[j].CompareTo(genericArr[j+1]) > 0) | |
{ | |
T temp = genericArr[j]; | |
genericArr[j] = genericArr[j+1]; | |
genericArr[j+1] = temp; | |
} | |
} | |
} | |
} | |
static void Main() | |
{ | |
String line=""; | |
DateTime[] dates = | |
{ | |
new DateTime(1969,7,8), new DateTime(1970,12,1), new DateTime(2005,11,22) | |
}; | |
GenericBubbleSort(dates); | |
line = string.Join(", ", dates); | |
Console.WriteLine(line); | |
int[] intArr = new int[] { -5, 8, 2, -1, 5, 0, 7}; | |
GenericBubbleSort(intArr); | |
line = string.Join(", ", intArr); | |
Console.WriteLine(line); | |
string[] names = new string[] {"John", "Peter", "Alex", "Иван", "Петър", "Алекс" }; | |
GenericBubbleSort(names); | |
line = string.Join(", ", names); | |
Console.WriteLine(line); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment