Q: Use methods to return the following question {Display Exercise Menu with method}
(1) Display array (2) Index of the first odd positive
(3) Swap second and second to last (4) Double every value in the array
(5) Total of even negative numbers (6) Add 15 to every odd negative numbers
(7) Change the value of element in array (8) Add value to an element.
*******************************************************************************
namespace ArrayWithMethod8Ex
{
class Program
{
static void Main(string[] args)
{
int[] A = { 3, -12, 6, 9, -7, -13, 19, 35, -8, -11, 15, 27, -1 };
int selection;
do
{
DisplayMenu();
Console.Write("Enter an exercise no: ");
int.TryParse(Console.ReadLine(), out selection);
switch(selection)
{
case 1:
DisplayArray(A);
break;
case 2:
int index = IndexOfFirstOddPositive(A);
Console.WriteLine("Index of First Odd Positive is: {0}", index);
break;
case 3:
Swap2N2toLast(A);
DisplayArray(A);
break;
case 4:
DoubleEveryValue(A);
DisplayArray(A);
break;
case 5:
long total = TotalOfEvenNegative(A);
Console.WriteLine("Total of even negatice is: {0}", total);
break;
case 6:
Add15toOddNegatice(A);
DisplayArray(A);
break;
case 7:
UpdateElement(A);
DisplayArray(A);
break;
case 8:
AddValue2Element(A);
DisplayArray(A);
break;
default:
break;
}
Console.ReadKey();
} while (selection != 0);
}
static void DisplayMenu()
{
Console.Clear();
Console.WriteLine("\n\n");
Console.WriteLine("\t===========================================");
Console.WriteLine("\tGiven Array: 3, -12, 6, 9, -7, -13, 19, 35, -8, -11, 15, 27, -1 ");
Console.WriteLine("\t===========================================");
Console.WriteLine("\t1. Display array");
Console.WriteLine("\t2. Index of the first odd positive");
Console.WriteLine("\t3. Swap second and second to last");
Console.WriteLine("\t4. Double every value in the array");
Console.WriteLine("\t5. Total of even negative numbers");
Console.WriteLine("\t6. Add 15 to every odd negative numbers");
Console.WriteLine("\t7. Change the value of element in array");
Console.WriteLine("\t8. Add value to an element.");
Console.WriteLine("\t9. Exit Application");
Console.WriteLine("\t===========================================\n");
}
//1. Display the array in single line.============================================
static void DisplayArray(int[] array)
{
Console.WriteLine("\n========================================");
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0}, ", array[i]);
}
Console.WriteLine("\n========================================");
}
//2. Display the index of first odd positive value it finds.=============================
static int IndexOfFirstOddPositive(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] % 2 != 0 && array[i] > 0)
return i;
}
return -1;
}
//3. Swap second value and second to last value.===================================
static void Swap2N2toLast(int[]array)
{
int temp = array[1];
array[1] = array[array.Length - 2];
array[array.Length - 2] = temp;
}
//4. Double every value in array.===============================================
static void DoubleEveryValue(int[]array)
{
for (int i = 0; i < array.Length; i++)
{
array[i] *= 2;
}
}
//5. Add all the even negatice numbers==========================================
static long TotalOfEvenNegative(int[]array)
{
long sum = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] % 2 == 0 && array[i] < 0)
sum += array[i];
}
return sum;
}
//6. Add 15 to every odd negatice value==========================================
static void Add15toOddNegatice(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] % 2 != 0 && array[i] < 0)
array[i] += 15;
}
}
//7. Provide a way for user to change the value of any element in array.================
static void UpdateElement (int[] array)
{
int index;
do
{
Console.Write("Enter index between 0 and {0}: ", array.Length - 1);
index = int.Parse(Console.ReadLine());
} while (index < 0 || index >= array.Length);
Console.Write("Enter new integer value: ");
int newValue = int.Parse(Console.ReadLine());
array[index] = newValue;
}
//8. Request a value from user then add to elements in array.============================
static void AddValue2Element(int[] array)
{
int x;
Console.Write("Enter new value to all in array: ");
x = int.Parse(Console.ReadLine());
for (int i = 0; i < array.Length; i++)
{
array[i] += x;
}
}
}
}
No comments:
Post a Comment