LP Assignments 5 Sol
LP Assignments 5 Sol
Assignment 5
1. Write a program that reads an integer values from the console. If the value is even, divide it
by 2, otherwise multiply it by 3 and add 1. Repeat this operation until you get the value 1. You will
always get the value 1, sooner or later. Print each of the values until you get 1.
Example:
5 * 3 + 1 = 16
16 / 2 = 8
8/2=4
4/2=2
2/2=1
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.Write("Starting value (0 to exit program): ");
string sv = Console.ReadLine();
int v = 0;
bool res = int.TryParse(sv, out v);
if (v == 0)
{
Environment.Exit(0);
}
while (v != 1)
{
if (v % 2 == 0)
v = v / 2;
else
v = 3 * v + 1;
Console.Write("{0} ", v);
}
Console.WriteLine();
}
}
Page 1 / 7
Programming Laboratories
Assignment 5
2. You are given a list of int with the ages of students in a course. Write a program that:
2.4. Prints all the ages int the list, except the first and the last.
2.5. Prints the highest and the lowest values of the ages in the list.
class Program
{
static void Main(string[] args)
{
var ages = new List<int>() { 18, 20, 21, 22, 19, 20, 22, 18, 19, 21, 23, 22, 21,
20 };
Console.WriteLine("----------------------------");
Console.WriteLine("Number of ages: {0}", ages.Count);
Console.WriteLine("All the ages: ");
foreach (int a in ages)
Console.Write("{0} ", a);
Console.WriteLine();
Console.WriteLine("All the ages in reverse: ");
for (int i = ages.Count - 1; i >= 0; i--)
Console.Write("{0} ", ages[i]);
Console.WriteLine();
Console.WriteLine("All the ages except first and last: ");
for (int i = 1; i < ages.Count - 1; i++)
Console.Write("{0} ", ages[i]);
Console.WriteLine();
Console.WriteLine("Highest and lowest: ");
int high = 0;
int low = 999;
foreach (int a in ages)
{
if (high < a)
high = a;
if (low > a)
low = a;
}
Console.WriteLine("lowest: {0} highest: {1}", low, high);
Page 2 / 7
Programming Laboratories
Assignment 5
double sum = 0;
foreach (int a in ages)
sum += a;
Console.WriteLine("average: {0:#0.##}", ages.Count > 0 ? sum / ages.Count : 0);
Console.WriteLine();
Console.ReadKey();
}
}
3. Write a program that, given a string, builds a dictionary with the positions where the
characters appear in the string.
Example:
L: [0]
a: [1, 5]
b: [2]
o: [3, 7]
r: [4, 8]
… etc.
class Program
{
static void Main(string[] args)
{
// key: char
// value: list of positions (int) of char in the string
var d = new Dictionary<char, List<int>>();
string s = "Let's play with dictionaries";
for (int i=0; i< s.Length; i++)
{
char ch = s[i]; // char to process
List<int> pos;
bool exists = d.TryGetValue(s[i], out pos); // ch already in dictionary?
if (! exists) // create entry for char ch
{
d.Add(ch, new List<int>());
}
d[s[i]].Add(i); // adds position of ch to list of positions
}
Page 3 / 7
Programming Laboratories
Assignment 5
4. You want to manage your fruit store. For each type of fruit, you shall keep information on the
quantity you purchased, the purchase price per kilo, the selling price per kilo, and the quantity in
stock.
Two solutions, one with dictionaries and the other with lists:
class Fruit
{
internal string Name { get; set; }
internal double PPrice { get; set; }
internal double SPrice { get; set; }
internal double PQuant { get; set; }
internal double QStock { get; set; }
public Fruit(string n, double pp, double sp, double pq, double qs)
{
Name = n;
PPrice = pp;
SPrice = sp;
PQuant = pq;
QStock = qs;
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
Page 4 / 7
Programming Laboratories
Assignment 5
if (!(obj is Fruit))
return false;
Fruit o = (Fruit)obj;
return Name == o.Name;
}
public override string ToString()
{
return string.Format("{0} pp: {1:###,##0.0} sp: {2:###,##0.0} pq: {3:###,##0.0} qs:
{4:###,##0.0}",
Name, PPrice, SPrice, PQuant, QStock);
}
}
class StoreWithDict
{
Dictionary<string, Fruit> store;
public StoreWithDict()
{
store = new Dictionary<string, Fruit>();
}
internal void Add(Fruit f)
{
if (!store.ContainsKey(f.Name))
store.Add(f.Name, f);
}
internal double GetProfit()
{
double profit = 0;
foreach (Fruit f in store.Values)
{
profit += (f.PQuant - f.QStock) * (f.SPrice - f.PPrice);
}
return profit;
}
internal Fruit GetMostExpensive()
{
Fruit mostExp = null;
foreach (Fruit f in store.Values)
{
if (mostExp == null || f.SPrice > mostExp.SPrice)
{
mostExp = f;
}
}
return mostExp;
}
internal void PrintStore()
{
Page 5 / 7
Programming Laboratories
Assignment 5
Page 6 / 7
Programming Laboratories
Assignment 5
{
static void Main(string[] args)
{
// var s = new StoreWithDict();
var s = new StoreWithLists();
s.Add(new Fruit("Apple", 1.1, 1.6, 100, 20));
s.Add(new Fruit("Pear", 1.2, 1.8, 200, 90));
s.Add(new Fruit("Orange", 1.5, 2.1, 300, 190));
s.Add(new Fruit("Pear", 1.5, 2.2, 120, 30)); // repeated ==> ignored
s.Add(new Fruit("Grapes", 1.3, 1.9, 30, 10));
Console.WriteLine("----------------------------");
s.PrintStore();
Console.WriteLine("----------------------------");
Console.WriteLine("Profit: {0}", s.GetProfit());
Console.WriteLine("Most expensive: {0}", s.GetMostExpensive());
Console.ReadKey();
}
}
Page 7 / 7