LP Assignments 3 Sol
LP Assignments 3 Sol
Assignments 3
Write a method that takes as parameters an unsorted array of double and a double value. It shall
search for the value in the array, returning its position, or -1 if it doesn’t find the value.
Example:
double[] v = new double[] { 31, 23, 44, 12, 17, 53, 1, 35, 4, 50, 52, 3}
int p = SearchUnsorted( v, 12)
// p = 3
class Program
{
Page 1 / 9
Programming Laboratories
Assignments 3
Write a method that takes as parameters a sorted array of double and a double value. It shall
search for the value in the array, returning its position, or -1 if it doesn’t find the value.
Example:
double v[] = new double[] { 1, 3, 4, 12, 17, 23, 31, 35, 44, 50, 52, 53}
int p = SearchSorted( v, 12)
// p = 3
class Program
{
static int SearchSorted(double[] arr, double elem)
{
if (arr.Length == 0)
{
return -1;
}
int l = 0;
int u = arr.Length - 1;
if (arr[0] == elem)
{
return 0;
}
if (arr[u] == elem)
{
return u;
}
while (u > l)
{
int m = (l + u) / 2;
if (arr[m] == elem)
{
return m;
}
if (arr[m] > elem)
{
u = m - 1;
}
else
{
l = m + 1;
}
}
return -1;
}
static void Main(string[] args)
{
double[] arr = new double[] { 2.5, 5.4, 6, 7.2, 7.4, 8.9, 9.8, 11.3, 12.4 };
Page 2 / 9
Programming Laboratories
Assignments 3
Write a method that takes as parameters an unsorted array of double and a double value. It shall
return the number of occurrences of the value in the array.
Example:
double[] v = new double[] { 31, 23, 4, 1, 17, 53, 1, 35, 4, 1, 52, 3}
int c = Occurrences( v, 1)
// c = 3
class Program
{
static int Occurrences(double[] arr, double elem)
{
int occ = 0;
foreach (double e in arr)
{
if (e == elem)
occ++;
}
return occ;
}
static void Main(string[] args)
{
double[] arr = new double[] { 2, 5.4, 6, 7.2, 1, 2, 7.4, 2, 8.9, 9.8, 11.3, 12.4 };
//double[] arr = new double[] { };
Console.WriteLine("{0} {1}", 2, Occurrences(arr, 2));
Console.WriteLine("{0} {1}", 2.5, Occurrences(arr, 2.5));
Console.WriteLine("{0} {1}", 12.4, Occurrences(arr, 12.4));
Console.WriteLine("{0} {1}", 5, Occurrences(arr, 5));
Console.WriteLine("{0} {1}", -5, Occurrences(arr, -5));
Console.WriteLine("{0} {1}", 55, Occurrences(arr, 55));
Console.ReadKey();
}
}
Page 3 / 9
Programming Laboratories
Assignments 3
Write a method that takes as parameters a sorted array of double and a double value. It shall
insert the double value into the array, keeping the array sorted. Do not use the Sort method of the
Array class or other similar sorting function from the libraries.
Example:
double[] v = new double[] {2, 4, 6, 6, 7, 7, 7, 9, 10, 11, 11, 15, 20}
double[] vnew = Insert(v, 6)
// vnew = {2, 4, 6, 6, 6, 7, 7, 7, 9, 10, 11, 11, 15, 20}
class Program
{
static double[] Insert(double[] arr, double elem)
{
double[] res = new double[arr.Length + 1]; // result array
bool inserted = false; // elem not yet inserted
// pos1: position of next element in source array
// pos2: next free position in destination array
for (int pos1 = 0, pos2 = 0; pos1 < arr.Length;)
{
// found an element of the array greater than the element to insert (elem)
// elem should be copied to destination before this element (only once)
if (!inserted && arr[pos1] > elem)
{
res[pos2] = elem; // copy element to destination array
pos2++;
inserted = true; // element already inserted, don’t insert again
}
// copy element from source to destination
res[pos2] = arr[pos1];
pos2++;
pos1++;
}
// reached the end of source array without inserting elem?
// element to insert (elem) if greater than any element of the array
if (!inserted)
{
res[res.Length-1] = elem; // copy element to destination array
}
return res;
}
static void PrintArray( double[] arr)
{
foreach (double e in arr)
{
Page 4 / 9
Programming Laboratories
Assignments 3
Write a method that takes as parameters two sorted arrays do double, and returns a sorted array
that contains the elements of both given arrays.
Example:
double[] v = new double[] { 2, 4, 6, 6, 7, 12}
double[] w = new double[] { 2, 3, 5, 6, 9, 9, 10, 14}
double[] novo = Merge(v, w)
' novo tem o valor {2, 2, 3, 4, 5, 6, 6, 6, 7, 9, 9, 10, 12, 14}
class Program
{
static double[] Merge(double[] arr1, double[] arr2)
{
double[] res = new double[arr1.Length + arr2.Length];
int len1 = arr1.Length;
int len2 = arr2.Length;
int pos1 = 0; // position of next element of arr1
int pos2 = 0; // position of next element of arr1
int posres = 0; // position of next element of destination array
bool end = false;
if (len1 == 0) // arr1 is an empty array?
{
return arr2;
}
if (len2 == 0) // arr2 is an empty array?
Page 5 / 9
Programming Laboratories
Assignments 3
{
return arr1;
}
while (!end)
{
if (arr1[pos1] < arr2[pos2]) // next element is from arr1
{
res[posres] = arr1[pos1];
pos1++;
posres++;
if (pos1 >= len1) // end of array arr1?
end = true;
}
else
{
res[posres] = arr2[pos2];
pos2++;
posres++;
if (pos2 >= len2) // end of array arr2?
end = true;
}
}
while (pos1 < len1) // any elements left on arr1?
{
res[posres] = arr1[pos1];
pos1++;
posres++;
}
while (pos2 < len2) // any elements left on arr2?
{
res[posres] = arr2[pos2];
pos2++;
posres++;
}
return res;
}
static void PrintArray(double[] arr)
{
foreach (double e in arr)
{
Console.Write("{0} ", e);
}
Console.WriteLine();
}
static void Main(string[] args)
{
double[] v = new double[] { 2, 4, 6, 6, 7, 12 };
double[] w = new double[] { 2, 3, 5, 6, 9, 9, 10, 14 };
Page 6 / 9
Programming Laboratories
Assignments 3
PrintArray(Merge(v, w));
// special case: all elements of arr2 greater than elements of arr1
v = new double[] { 2, 4, 6, 6, 7, 12 };
w = new double[] { 22, 23, 25, 26, 29, 29, 30, 40 };
PrintArray(Merge(v, w));
// special case: all elements of arr1 greater than elements of arr2
v = new double[] { 22, 24, 26, 26, 27, 212 };
w = new double[] { 2, 3, 5, 6, 9, 9, 10, 14 };
PrintArray(Merge(v, w));
Console.ReadKey();
}
}
6) Sequence of Fibonacci
Then:
f(0) = 0
f(1) = 1
f(2) = f(1) + f(0) = 1 + 0 = 1
f(3) = f(2) + f(1) = 1 + 1 = 2
f(4) = f(3) + f(2) = 2 + 1 = 3
f(5) = f(4) + f(3) = 3 + 2 = 5
etc.
Page 7 / 9
Programming Laboratories
Assignments 3
Console.ReadKey();
}
}
}
b) Write a program that, given an integer n, prints the n th term of the Fibonacci
sequence.
class Program
{
// using simple recursion
private static int fib1(int n)
{
if (n <= 1)
return 0;
if (n == 2)
return 1;
return fib1(n - 1) + fib1(n - 2);
}
Page 8 / 9
Programming Laboratories
Assignments 3
}
if (n < 1)
{
Console.WriteLine("Wrong value");
Console.ReadKey();
Environment.Exit(0);
}
int s1 = fib1(n);
Console.WriteLine(string.Format("Element {0,2} of Fibonacci sequence is
{1:###,##0}", n, s1));
int s2 = fib2(n);
Console.WriteLine(string.Format("Element {0,2} of Fibonacci sequence is
{1:###,##0}", n, s2));
Console.ReadKey();
}
}
Page 9 / 9