Write a C# program that has a method named ReadArray. In this method, ask the user to enter names of employees, the program stops taking new names when the user enters the word “quit”. All names must be stored in an array as the user enters them; name the array, empNames. In the ReadArray, call another method named ReverseArray and pass to it, the empNames. The ReverseArray method will reverse the content of empNames. In ReadArray, print the reversed array. In the main method, only call the ReadArray, without passing anything to it. You should demo the pass-by-reference in this question.
Sample run:
Please enter an employee name: Karein
Please enter an employee name: James
Please enter an employee name: Komal
Please enter an employee name: Rohit
Please enter an employee name: quit
The names are:
Rohit
Komal
James
Karen
using System;
using System.Collections.Generic;
namespace App
{
class Program
{
static void Main(string[] args)
{
ReadArray();
Console.ReadLine();
}
private static void ReadArray(){
string[] empNames = new string[100];
string name="";
int i=0;
while (name.CompareTo("quit") != 0) {
Console.Write("Please enter an employee name: ");
name = Console.ReadLine();
if (name.CompareTo("quit") != 0) {
empNames[i] = name;
i++;
}
}
ReverseArray(ref empNames);
Console.WriteLine("\nThe names are:");
for (i = 0; i < empNames.Length; i++)
{
if (empNames[i] != null)
{
Console.WriteLine(empNames[i]);
}
}
}
private static void ReverseArray(ref string[] empNames)
{
for (int i = 0; i < empNames.Length - i; i++)
{
var value = empNames[empNames.Length - i - 1];
empNames[empNames.Length - i - 1] = empNames[i];
empNames[i] = value;
}
}
}
}
Comments
Leave a comment