Machine Problem 5.5.
Write a program using two dimensional arrays that searches a number and display the number of times it occurs on the list of 12 input values.
Sample input/output dialogue:
Enter twelve numbers: 20 13 30 35 40 16 18 20 18 20
Enter a number to search: 20 Occurrence(s) : 3
using System;
namespace twodimension
{
class Program
{
public static void Main(string[] args)
{
int i,j, search, cnt;
int [ , ] array = new int[3,4];
cnt = 0;
Console.WriteLine("Enter twelve numbers:");
for (i=0; i<3; i++)
for (j=0; j<4; j++)
array[i,j] = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter a number to search: ");
search = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("Array: ");
for (i=0; i<3; i++)
{
for (j=0; j<4; j++)
{
Console.Write("{0} ", array[i,j]);
if (search == array[i,j])
cnt++;
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Occurrence(s): {0}", cnt);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Comments
Leave a comment