In: Computer Science
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you pass the array by reference and display its content. Call both methods from you main program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
//this is the name of my application, in your case it may
differ.
namespace ConsoleApplication2
{
class Program
{
//main method
public static void Main(string[] args)
{
//declare an integer array of size 10
int[] array = new int[10];
int i = 0;
//ask for input from user
Console.WriteLine("Enter numbers between 1-10");
//iterate
do{
//take input and convert it into integer
//check if input is valid or not
//if input is not valid then ask to enter again
//if input is valid increment the counter of array
array[i]=Convert.ToInt32(Console.ReadLine());
if (array[i] > 10 || array[i] < 1)
{
Console.WriteLine("Please enter number between 1 & 10
only");
continue;
}
else
i++;
}while(i<10);
//print elements of the array
Console.WriteLine("Here are total 10 elements\n");
//print elements by value
Console.WriteLine("Displaying elements by value");
displayByVal(array);
//print elements by reference
Console.WriteLine("\n\nDisplaying elements by reference");
displayByRef(ref array);
Console.Read();
}
//static method that print elements by value
public static void displayByVal(int [] arr)
{
for (int i = 0; i < 10; i++)
Console.Write(arr[i] + " ");
}
//static method that prints element by reference
public static void displayByRef(ref int[] arr)
{
for (int i = 0; i < 10; i++)
Console.Write(arr[i] + " ");
}
}
}
=========================================
Output