You are on page 1of 13

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Week11AssignmentArrays
{
class Program
{
static void Main(string[] args)
{
char[] letters = "the quick brown fox jumps over the lazy
dog".ToCharArray();
int[] numbers = { 0, 2, 3, 5, 7, 1, 1, 2, 5, 6, 7, 2, 5, 2 };
string[] poem = "mary had a little lamb its fleece was white as
snow".Split();
DisplayCharArray(letters);
Console.WriteLine(); //Space Break
DisplayIntArray(numbers);
Console.WriteLine(); //Space Break
DisplayStringArray(poem);
Console.WriteLine(); //Space Break
Array.Reverse(letters); //Reverse Letters Array
foreach (char value in letters)
{
Console.Write(value);
}
Console.WriteLine(); //Space Break
Array.Sort(poem); //Sort Poem Array
foreach (string value in poem)
{
Console.Write($"{value} ");
}
Console.WriteLine(); //Space Break
int indexOne= Array.BinarySearch(numbers, 3); //Binary search for 3 in
numbers array
Console.WriteLine(indexOne);
Array.Sort(numbers); //Sort numbers Array
foreach (int value in numbers)
{
Console.Write($"{value} ");
}
Console.WriteLine(); //Space Break
int indexTwo = Array.BinarySearch(numbers, 3);
Console.WriteLine(indexTwo);
//GenerateIntArray(30);
int[] nInt = GenerateNewIntArray(10);
DisplayNewIntArray(nInt);
Console.WriteLine();//Space Break
int[] rInt = GenerateRandomintArray(30);
DisplayRandomIntArray(rInt);

}
#region Questions 4-6
static char[] DisplayCharArray(char[] letters)
{
int counter = 0;
do
{
Console.Write(letters[counter]);
counter++;

} while (counter < letters.Length);


return letters;
}
static int[] DisplayIntArray(int[] numbers)
{
int counter = 0;
do
{
Console.Write($"{numbers[counter]} ");
counter++;
} while (counter < numbers.Length);
return numbers;
}
static string[] DisplayStringArray(string[] poem)
{
int counter = 0;
do
{
Console.Write($"{poem[counter]} ");
counter++;
} while (counter < poem.Length);
return poem;
}
#endregion
#region Question 07
static int[] GenerateNewIntArray(int size)
{
int[] result = new int[size];
int i = 0;
do
{
Console.Write("Enter a number for your array: ");
int userInput = Convert.ToInt32(Console.ReadLine());
result[i] = (userInput);
i++;
} while (i < size);
return result;
}
static void DisplayNewIntArray(int[] toPrint)
{
foreach (int x in toPrint)
{
Console.Write($"{x} ");
}
}
#endregion
#region Question 08
static int[] GenerateRandomintArray(int size)
{
int[] result = new int[size];
int i = 0;
do
{
Random rand = new Random();
result[i] = rand.Next(100, 200);
i++;
} while (i < size);
return result;
}
static void DisplayRandomIntArray(int[] toPrint)
{
foreach (int x in toPrint)
{
Console.Write($"{x} ");
}
}

#endregion

}
}

========================================PART
THREE======================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FinalPartThree
{
class Program
{
static void Main(string[] args)
{
//Question 3
CalculateTuitionFee(4, 449.99);
CalculateTuitionFee(5, 449.99);
CalculateTuitionFee(6, 449.99);
//Question 4
CalculateCommission(900);
CalculateCommission(1000);
CalculateCommission(2000);
//Question 5
MoneyRequired();
MoneyRequired();
//Question 6
CalculateGravitationalAttraction(5.972 * 10e24, 7.348 * 10e22,
384400000 );
//Question 7
CalculateAreaOfTriangle(5.83, 4.24, 8.00);
//Question 8
ConvertCelsiusToFahrenheit(0);
ConvertCelsiusToFahrenheit(50);
ConvertCelsiusToFahrenheit(100);
//Question 9
ConvertFahrenheitToCelsius(0);
ConvertFahrenheitToCelsius(32);
ConvertFahrenheitToCelsius(212);
//Question 10
ConvertCelsiusToKelvin(-196);
ConvertCelsiusToKelvin(0);
ConvertCelsiusToKelvin(100);
//Question 11
ConvertFahrenheitToKelvin(32);
ConvertFahrenheitToKelvin(70);
ConvertFahrenheitToKelvin(212);

}
static double CalculateTuitionFee(int numberOfCourses,double costPerCourse)
//QUESTION 3
{
double tuitionFee = numberOfCourses * costPerCourse;
Console.WriteLine($"{numberOfCourses} will cost a total of
{tuitionFee:C}");
return tuitionFee;
}
static double CalculateCommission(double saleAmount) //QUESTION 4
{
double commission = saleAmount;
if(saleAmount > 1000)
{
commission = saleAmount * .01;
}
else
{
commission = 0;
}
Console.WriteLine($"A sale amount of {saleAmount:C} will yield a
commission of {commission:C}");
return commission;
}
static double MoneyRequired() // QUESTION 5
{
Console.Write("How many burgers would you like to buy?: ");
int userInput = Convert.ToInt32(Console.ReadLine());
double cost = userInput * 1.39;
Console.WriteLine($"Your order will be {cost:C}");
return cost;
}
static double CalculateGravitationalAttraction(double massOne, double
massTwo, double distance) //QUESTION 6
{
double g = 6.673e-11;
double force = g * ((massOne * massTwo) / (distance * distance));
Console.WriteLine($"Force: {force}");
return force;
}
static double CalculateAreaOfTriangle(double a, double b, double c) //
Question 7
{
double s = (a + b + c) / 2;
double area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
Console.WriteLine($"Area: {area}");
return area;
}
static double ConvertCelsiusToFahrenheit(double celsius) //QUESTION 8
{
double fahrenheit = celsius * 9 / 5 + 32;
Console.WriteLine($"Celsius: {celsius} Fahrenheit: {fahrenheit}");
return fahrenheit;
}
static double ConvertFahrenheitToCelsius(double fahrenheit) // Question 9
{
double celsius = (fahrenheit - 32) * 5 / 9;
Console.WriteLine($"Fahrenheit: {fahrenheit} Celsius: {celsius}");
return celsius;
}
static double ConvertCelsiusToKelvin(double celsius)
{
double kelvin = celsius + 273.15;
Console.WriteLine($"Celsius: {celsius} Kelvin: {kelvin}");
return kelvin;
}
static double ConvertFahrenheitToKelvin(double fahrenheit)
{
double c = ConvertFahrenheitToCelsius(fahrenheit);
double kelvin = ConvertCelsiusToKelvin(c);
Console.WriteLine($"Fahrenheit: {fahrenheit} Kelvin: {kelvin}");
return kelvin;
}
}
}

===========================================FINAL PART FIVE


PROGRESS====================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FinalPartFive
{
class Program
{
static void Main(string[] args)
{
//Question 01
int[] array = { 1, 2, 3, 4, 5 };
DisplayIntArray(array);
Console.WriteLine(); //SPACE BREAK
//Question 02
int[] a = GenerateRandomIntArray(15, 10);
DisplayIntArray(a);
int[] b = GenerateRandomIntArray(15, 10);
DisplayIntArray(b);
int[] c = GenerateRandomIntArray(15, 10);
DisplayIntArray(c);
Console.WriteLine(); //SPACE BREAK
//Question 03
}
static void DisplayIntArray(int[] numbers)
{
foreach (int x in numbers)
{
Console.Write($"{x} ");
}
}
static int[] GenerateRandomIntArray(int numberOfItems, int largestValue)
{
Random generator = new Random();
int[] result = new int[numberOfItems];
int i = 0;
do
{
result[i] = generator.Next(largestValue);
i++;
} while (i < numberOfItems);
return result;
}
}
}

=============================================PART 5================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FinalPartFive
{
class Program
{
static void Main(string[] args)
{
//Question 01
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
DisplayIntArray(array);
Console.WriteLine(); //SPACE BREAK
//Question 02
int[] a = GenerateRandomIntArray(15, 10);
DisplayIntArray(a);
Console.WriteLine(); //SPACE BREAK
int[] b = GenerateRandomIntArray(25, 10);
DisplayIntArray(b);
Console.WriteLine(); //SPACE BREAK
int[] c = GenerateRandomIntArray(30, 100);
DisplayIntArray(c);
Console.WriteLine(); //SPACE BREAK
//Question 03
int[] randomArray = GenerateRandomIntArray(10, 10);
DisplayIntArray(randomArray);
Console.WriteLine();
int [] d = CountEvenOdd(randomArray);
DisplayIntArray(d);
Console.WriteLine(); //SPACE BREAK
//QUESTION 04
int[] e = CalculateDigitFrequencies(randomArray);
DisplayIntArray(e);
Console.WriteLine(); //SPACE BREAK
//Question 05
int[] f = CalculateLastDigitFrequencies(randomArray);
DisplayIntArray(f);
//Question 06
int[] largerArray = GenerateRandomIntArray(10, 99);
DisplayIntArray(largerArray);
int[] g = CalculateNumberFrequencies(largerArray);
DisplayIntArray(g);

}
#region Method 01
static void DisplayIntArray(int[] numbers)
{
foreach (int x in numbers)
{
Console.Write($"{x} ");
}
}
#endregion
#region Method 02
static int[] GenerateRandomIntArray(int numberOfItems, int largestValue)
{
Random generator = new Random();
int[] result = new int[numberOfItems];
int i = 0;
do
{
result[i] = generator.Next(largestValue);
i++;
} while (i < numberOfItems);
return result;
}
#endregion
#region method 03
static int[] CountEvenOdd(int[] array)
{
int even = 0;
int odd = 0;
foreach (int x in array)
{
if (x % 2 == 0)
{
even++;
}
else
{
odd++;
}
}
int[] result = { even, odd };
return result;
}
#endregion
#region Method 04
static int[] CalculateDigitFrequencies(int[] array)
{
int[] result = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
foreach (int x in array)
{
result[x]++;
}
return result;
}
#endregion
#region Method 05
static int[] CalculateLastDigitFrequencies(int[] array)
{
int[] result = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
foreach (int x in array)
{
if ( x != 0)
{
result[x] = x % 10 / x;
}
}
return result;
}
#endregion
#region Method 06
static int[] CalculateNumberFrequencies(int[] array)
{
int[] result = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
foreach (int x in array)
{
while (x >= 10)
{
result[x] = (x - (x % 10)) / 10;
}
}
return result;
}
#endregion

========================================FIVE=====================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FinalPartFive
{
class Program
{
static void Main(string[] args)
{
// Random Arrays used in Q3-6

//Question 01
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
DisplayIntArray(array);
Console.WriteLine(); //SPACE BREAK
//Question 02
int[] a = GenerateRandomIntArray(15, 10);
DisplayIntArray(a);
Console.WriteLine(); //SPACE BREAK
int[] b = GenerateRandomIntArray(25, 10);
DisplayIntArray(b);
Console.WriteLine(); //SPACE BREAK
int[] c = GenerateRandomIntArray(30, 100);
DisplayIntArray(c);
Console.WriteLine(); //SPACE BREAK
//Question 03
Console.WriteLine("Random Array");
int[] randomArray = GenerateRandomIntArray(10, 10);
DisplayIntArray(randomArray); //Display Random Array used in
Q3/Q4
Console.WriteLine(); //Space Break
int[] d = CountEvenOdd(randomArray);
DisplayIntArray(d); //Display Count
Console.WriteLine(); //SPACE BREAK
//QUESTION 04
int[] e = CalculateDigitFrequencies(randomArray);
DisplayIntArray(e); //Display Count
Console.WriteLine(); //SPACE BREAK
//Question 05
int[] largerRandomArray = GenerateRandomIntArray(10, 40);
Console.WriteLine("Larger Array");
DisplayIntArray(largerRandomArray); //Display random array used
in Q5/Q6
int[] f = CalculateLastDigitFrequencies(largerRandomArray);
DisplayIntArray(f);
Console.WriteLine(); //Space Break
//Question 06
int[] g = CalculateNumberFrequencies(largerRandomArray);
DisplayIntArray(g);

}
#region Method 01
static void DisplayIntArray(int[] numbers)
{
foreach (int x in numbers)
{
Console.Write($"{x} ");
}
}
#endregion
#region Method 02
static int[] GenerateRandomIntArray(int numberOfItems, int
largestValue)
{
Random generator = new Random();
int[] result = new int[numberOfItems];
int i = 0;
do
{
result[i] = generator.Next(largestValue);
i++;
} while (i < numberOfItems);
return result;
}
#endregion
#region method 03
static int[] CountEvenOdd(int[] array)
{
int even = 0;
int odd = 0;
foreach (int x in array)
{
if (x % 2 == 0)
{
even++;
}
else
{
odd++;
}
}
int[] result = { even, odd };
return result;
}
#endregion
#region Method 04
static int[] CalculateDigitFrequencies(int[] array)
{
int[] result = new int[10];
foreach (int x in array)
{
result[x]++;
}
return result;
}
#endregion
#region Method 05
static int[] CalculateLastDigitFrequencies(int[] array)
{
int[] result = new int[10];
foreach (int x in array)
{
if (x != 0)
{
if (x % 10 == x)
{
int n = x % 10;
}
}
else if(x == 0)
{
result[x]++;
}
}
return result;
}
#endregion
#region Method 06
static int[] CalculateNumberFrequencies(int[] array)
{
==
int[] result = new int[10]
for (int i = 0; i < array.Length; i++)
{
result[array[i] % 10]++
}
==
int[] result = new int[80];
foreach (int x in array)
{
int n = x / 10;
if (x / 10 == n)
{
result[x] = result[x] + n;
}
/*
while (x >= 10)
{
result[x] = x / 10;
}
while(x < 10)
{
result[x]++;
}
*/
}
return result;
}
#endregion
#region Question 07
static int[] CalculateLetterFrequency(string input)
{
int counter = 0;
int[] result = new int[26]; // create frequency array

// counts frequency
while (counter < input.Length)
{
char c = input[counter];
if (c >= 'A' && c <= 'Z')
{
++result[c - 65];
}
++counter;
}

return result;
}
#endregion
}
}

}
}

============Extra code=================================
for (int i = 0; i < input.Length; i++)
{
if (input[i] == 'a' || input[i] == 'e' || input[i] == 'i' || input[i] ==
'o' || input[i] == 'u')
{
count++;
}
}
=================
int tot_mins = 28983;
int days = tot_mins / 1440;
int hours = (tot_mins % 1440)/60;
int mins = tot_mins % 60;

==================================================
// Use TimeSpan structure
int min = 28983;
TimeSpan elapsedTime = new TimeSpan( 0, min, 0 );

int day = elapsedTime.Days;


int hour = elapsedTime.Hours;
Console.WriteLine(string.Format("{0} days {1} hours", day, hour));

// or just plain mathematics

double days = min/60/24;


double hours = (min - days*24*60)/60;

Console.WriteLine(string.Format("{0} days {1:0} hours", days, hours));


===================================================

using System;

class Program
{
static void Main()
{
//
// Convert miles to kilometers.
//
double miles1 = 200;
double kilometers1 = ConvertDistance.ConvertMilesToKilometers(200);
Console.WriteLine("{0} = {1}", miles1, kilometers1);

//
// Convert kilometers to miles.
//
double kilometers2 = 321.9;
double miles2 = ConvertDistance.ConvertKilometersToMiles(321.9);
Console.WriteLine("{0} = {1}", kilometers2, miles2);

//
// Convert kilometers to miles (again).
//
double kilometers3 = 500;
double miles3 = ConvertDistance.ConvertKilometersToMiles(500);
Console.WriteLine("{0} = {1}", kilometers3, miles3);

//
// Convert miles to kilometers (again).
//
double miles4 = 310.7;
double kilometers4 = ConvertDistance.ConvertMilesToKilometers(310.7);
Console.WriteLine("{0} = {1}", miles4, kilometers4);
}
}

public static class ConvertDistance


{
public static double ConvertMilesToKilometers(double miles)
{
//
// Multiply by this constant and return the result.
//
return miles * 1.609344;
}

public static double ConvertKilometersToMiles(double kilometers)


{
//
// Multiply by this constant.
//
return kilometers * 0.621371192;
}
}
====================
foreach(char ch in sentence.ToLower())
if("aeiou".Contains(ch))
total++;

public static HashSet<char> SVowels = new HashSet<char>{'a', 'e', 'i', 'o', 'u'};
public static int VowelsFor(string s) {
int total = 0;
foreach(char c in s)
if(SVowels.Contains(c))
total++;
return total;
}
=======

var sentence = "Hello my good friend";


var sb = sentence.ToLower().ToCharArray();
var count = 0;
foreach (var character in sb)
{
if (character.Equals('a') || character.Equals('e') ||
character.Equals('i') || character.Equals('o') ||
character.Equals('u'))
{
count++;
}
}

You might also like