Exercise 1
Write a program for David(Cricket Coach of a local team) to accept the number of recent matches for which the scores of the batsman for these matches,and display in ascending order
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex1
{
class Program
{
static void Main(string[] args)
{
int[] a =new int[100];
Console.WriteLine("Enter the number of recent matches for which the score of batsman need to be analysed");
string s = Console.ReadLine();
int x = Int32.Parse(s);
Console.WriteLine("-------------------------");
Console.WriteLine("Enter the scores");
Console.WriteLine("-------------------------");
for(int j=0;j<x;j++)
{
string s1 = Console.ReadLine();
a[j]=Int32.Parse(s1);
}
int limit = x - 1;
for(int pass=0; pass < x -1; pass++)
{
for(int j=0;j<limit - pass;j++)
{
if(a[j] > a[j+1])
{
int k = a[j];
a[j] = a[j+1];
a[j+1]=k;
}
}
}
Console.WriteLine("----------------------------------");
Console.WriteLine("Scored scores of the batsman are: ");
for(int j =0;j<x;j++)
Console.WriteLine(a[j]+ "\n");
Console.ReadLine();
}
}
}
Exercise 2
Write a program to accept the size of one dimensional array and its values and identify the largest and smallest number in the array and based on that display the result. Help SAM.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex2
{
class Program
{
static void Main(string[] args)
{
int n;
float large, small;
int[] a = new int[50];
Console.WriteLine("Enter the size of Array: ");
string s = Console.ReadLine();
n = Int32.Parse(s);
Console.WriteLine("Enter the array elements");
for (int i = 0; i < n; i++)
{
string s1 = Console.ReadLine();
a[i] = Int32.Parse(s1);
}
Console.Write(" ");
large = a[0];
small = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] > large)
large = a[i];
else if (a[i] < small)
small = a[i];
}
Console.WriteLine("Largest element in array is " + large);
Console.WriteLine("Smallest element in array is " + small);
Console.ReadLine();
}
}
}
Exercise 3
Write a Program to enter a character array with five elements and count the number of times that charcter appears in the character array.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex3
{
class Duplicate
{
static void Main(string[] args)
{
char[] ch = new char[5] { 'd', 'c', 'a', 'e', 'a' };
int count = 0;
Console.WriteLine("Enter a character: ");
char ip = Convert.ToChar(Console.ReadLine());
for (int i = 0; i < 5; i++)
{
if (ch[i] == ip)
{
count++;
}
}
Console.WriteLine("{0} appears {1} times in the character array", ip, count);
Console.ReadLine();
}
}
}
Exercise 4
Write a program to maintain the details of the stock of a shop under the item categories.Accept item category from the user. In addition accept item name, number of units and unit price. Define all the attributes as properties. Finally, display all the details using Enumerations.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex4
{
public enum ItemCategory
{
Women, Men, Girls, Boys, Babies
}
class ShoppingItem
{
private ItemCategory Category;
private string Name;
private int Size;
private decimal UnitPrice;
public static ShoppingItem Read()
{
ShoppingItem shop = new ShoppingItem();
string cat;
Console.WriteLine("Store Items Categories");
Console.WriteLine("\t1. Women");
Console.WriteLine("\t2. Men");
Console.WriteLine("\t3 Girls");
Console.WriteLine("\t4. Boys");
Console.WriteLine("\t5. Babies");
Console.WriteLine("Enter the Category (1-5): ");
cat = Console.ReadLine();
if(cat=="1")
shop.Category = ItemCategory.Women;
else if (cat=="2")
shop.Category = ItemCategory.Men;
else if(cat=="3")
shop.Category = ItemCategory.Girls;
else if(cat=="4")
shop.Category = ItemCategory.Boys;
else if(cat=="5")
shop.Category = ItemCategory.Babies;
else
{
Console.WriteLine("Invalid item category");
System.Environment.Exit(0);
Console.ReadLine();
}
Console.Write("Item Name : ");
shop.Name = Console.ReadLine();
Console.Write("Number of Units : ");
shop.Size = Convert.ToInt32(Console.ReadLine());
Console.Write("Unit Price : ");
shop.UnitPrice = decimal.Parse(Console.ReadLine());
return shop;
}
public static void Write(ShoppingItem item)
{
Console.WriteLine();
Console.WriteLine("Category: " + item.Category);
Console.WriteLine("Description: " + item.Name);
Console.WriteLine("Number of units: " + item.Size);
Console.WriteLine("Unit Price " + item.UnitPrice);
}
public static void Main(string[] args)
{
ShoppingItem si = new ShoppingItem();
si = ShoppingItem.Read();
ShoppingItem.Write(si);
Console.ReadLine();
}
}
}
Exercise 5
Write a program to accept the number of players playing in the game. The program should ensure that the number of players entered by the user should be in the integer format.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex5
{
class Program
{
string[] names_array;
static string noofplayer;
static bool a= true;
int i =0;
public void accept(int players)
{
names_array = new string[players];
for(i=0;i<players;i++)
{
Console.WriteLine("Enter the name of the players : ");
names_array[i] = Console.ReadLine();
}
}
public void display()
{
Console.WriteLine("The names of the players: ");
for(int j=0;j < names_array.Length;j++)
{
Console.WriteLine(names_array[j]);
}
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("Please enter the number of players who want to play(between 1 - 4)");
do
{
noofplayer = Console.ReadLine();
int value;
if(int.TryParse(noofplayer,out value))
{
if(value >= 1 && value<=4)
{
p.accept(value);
p.display();
a = false;
}
else
{
Console.WriteLine("Please enter the value between 1 - 4");
a= true;
}
}
else
{
Console.WriteLine("Please enter the numeric value between 1 - 4 ");
a= true;
}
}
while(a);
}
}
}
Write a program for David(Cricket Coach of a local team) to accept the number of recent matches for which the scores of the batsman for these matches,and display in ascending order
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex1
{
class Program
{
static void Main(string[] args)
{
int[] a =new int[100];
Console.WriteLine("Enter the number of recent matches for which the score of batsman need to be analysed");
string s = Console.ReadLine();
int x = Int32.Parse(s);
Console.WriteLine("-------------------------");
Console.WriteLine("Enter the scores");
Console.WriteLine("-------------------------");
for(int j=0;j<x;j++)
{
string s1 = Console.ReadLine();
a[j]=Int32.Parse(s1);
}
int limit = x - 1;
for(int pass=0; pass < x -1; pass++)
{
for(int j=0;j<limit - pass;j++)
{
if(a[j] > a[j+1])
{
int k = a[j];
a[j] = a[j+1];
a[j+1]=k;
}
}
}
Console.WriteLine("----------------------------------");
Console.WriteLine("Scored scores of the batsman are: ");
for(int j =0;j<x;j++)
Console.WriteLine(a[j]+ "\n");
Console.ReadLine();
}
}
}
Exercise 2
Write a program to accept the size of one dimensional array and its values and identify the largest and smallest number in the array and based on that display the result. Help SAM.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex2
{
class Program
{
static void Main(string[] args)
{
int n;
float large, small;
int[] a = new int[50];
Console.WriteLine("Enter the size of Array: ");
string s = Console.ReadLine();
n = Int32.Parse(s);
Console.WriteLine("Enter the array elements");
for (int i = 0; i < n; i++)
{
string s1 = Console.ReadLine();
a[i] = Int32.Parse(s1);
}
Console.Write(" ");
large = a[0];
small = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] > large)
large = a[i];
else if (a[i] < small)
small = a[i];
}
Console.WriteLine("Largest element in array is " + large);
Console.WriteLine("Smallest element in array is " + small);
Console.ReadLine();
}
}
}
Exercise 3
Write a Program to enter a character array with five elements and count the number of times that charcter appears in the character array.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex3
{
class Duplicate
{
static void Main(string[] args)
{
char[] ch = new char[5] { 'd', 'c', 'a', 'e', 'a' };
int count = 0;
Console.WriteLine("Enter a character: ");
char ip = Convert.ToChar(Console.ReadLine());
for (int i = 0; i < 5; i++)
{
if (ch[i] == ip)
{
count++;
}
}
Console.WriteLine("{0} appears {1} times in the character array", ip, count);
Console.ReadLine();
}
}
}
Exercise 4
Write a program to maintain the details of the stock of a shop under the item categories.Accept item category from the user. In addition accept item name, number of units and unit price. Define all the attributes as properties. Finally, display all the details using Enumerations.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex4
{
public enum ItemCategory
{
Women, Men, Girls, Boys, Babies
}
class ShoppingItem
{
private ItemCategory Category;
private string Name;
private int Size;
private decimal UnitPrice;
public static ShoppingItem Read()
{
ShoppingItem shop = new ShoppingItem();
string cat;
Console.WriteLine("Store Items Categories");
Console.WriteLine("\t1. Women");
Console.WriteLine("\t2. Men");
Console.WriteLine("\t3 Girls");
Console.WriteLine("\t4. Boys");
Console.WriteLine("\t5. Babies");
Console.WriteLine("Enter the Category (1-5): ");
cat = Console.ReadLine();
if(cat=="1")
shop.Category = ItemCategory.Women;
else if (cat=="2")
shop.Category = ItemCategory.Men;
else if(cat=="3")
shop.Category = ItemCategory.Girls;
else if(cat=="4")
shop.Category = ItemCategory.Boys;
else if(cat=="5")
shop.Category = ItemCategory.Babies;
else
{
Console.WriteLine("Invalid item category");
System.Environment.Exit(0);
Console.ReadLine();
}
Console.Write("Item Name : ");
shop.Name = Console.ReadLine();
Console.Write("Number of Units : ");
shop.Size = Convert.ToInt32(Console.ReadLine());
Console.Write("Unit Price : ");
shop.UnitPrice = decimal.Parse(Console.ReadLine());
return shop;
}
public static void Write(ShoppingItem item)
{
Console.WriteLine();
Console.WriteLine("Category: " + item.Category);
Console.WriteLine("Description: " + item.Name);
Console.WriteLine("Number of units: " + item.Size);
Console.WriteLine("Unit Price " + item.UnitPrice);
}
public static void Main(string[] args)
{
ShoppingItem si = new ShoppingItem();
si = ShoppingItem.Read();
ShoppingItem.Write(si);
Console.ReadLine();
}
}
}
Exercise 5
Write a program to accept the number of players playing in the game. The program should ensure that the number of players entered by the user should be in the integer format.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch4_Ex5
{
class Program
{
string[] names_array;
static string noofplayer;
static bool a= true;
int i =0;
public void accept(int players)
{
names_array = new string[players];
for(i=0;i<players;i++)
{
Console.WriteLine("Enter the name of the players : ");
names_array[i] = Console.ReadLine();
}
}
public void display()
{
Console.WriteLine("The names of the players: ");
for(int j=0;j < names_array.Length;j++)
{
Console.WriteLine(names_array[j]);
}
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("Please enter the number of players who want to play(between 1 - 4)");
do
{
noofplayer = Console.ReadLine();
int value;
if(int.TryParse(noofplayer,out value))
{
if(value >= 1 && value<=4)
{
p.accept(value);
p.display();
a = false;
}
else
{
Console.WriteLine("Please enter the value between 1 - 4");
a= true;
}
}
else
{
Console.WriteLine("Please enter the numeric value between 1 - 4 ");
a= true;
}
}
while(a);
}
}
}





No comments:
Post a Comment