Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Exercise 1: By using the sequential search algorithm, write

C# code to search for an element of an integer array of 10


elements.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10] { 23, 2, 3, 34, 6,1,24,45,78,8}; //data set
int pos,target;
Console.Write("Enter value to find:");
target = int.Parse(Console.ReadLine());
pos = seqsearch(arr, target, 10);
if (pos != -1)
Console.WriteLine("The target item was found at location:{0}", pos);
else
Console.WriteLine("The target item was not found in the list.\n");
Console.ReadLine();

}
///sequential search
static int seqsearch(int[] dataset, int target, int n)
{
int found = 0;
int i;
int pos = -1;
for (i = 0; i < n && found != 1; i++)
if (target == dataset[i]) { pos = i; found = 1; }

return pos;
}
}
}

Exercise 2: Modify the C# code in exercise 1 in order to


search for an element of the array using binary search
algorithm.

Solution:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Csharp_exercises

class Program

static void Main(string[] args)

int[] arr = new int[10] { 23, 2, 3, 34, 6, 1, 24, 45, 78, 8 }; //unsorted data set
int pos, target;

Console.Write("Enter value to find:");

target = int.Parse(Console.ReadLine());

pos = binsearch(arr, 23, 10);

if (pos != -1)

Console.WriteLine("The target item was found at location:{0}", pos);

else
Console.WriteLine("The target item was not found in the list.\n");

Console.ReadLine();

///binary search

static int binsearch(int[] dataset,int target, int l,int u){

insertsort(dataset,dataset.Length);//make sure the list sorted


while(u>=l){

int mid=(l+u)/2;

if(target==dataset[mid]) return mid;

else if(target>dataset[mid]) l=mid+1;

else if(target<dataset[mid]) u=mid-1;


}

return -1;
}

static void insertsort(int[] dataset, int n)

int i, j;

for (i = 1; i < n; i++)

int pick_item = dataset[i];

int inserted = 0;

for (j = i - 1; j >= 0 && inserted != 1; )

if (pick_item < dataset[j])

{
dataset[j + 1] = dataset[j];

j--;

dataset[j + 1] = pick_item;

else inserted = 1;

}
}
}

}
}

4.02 - Search in array


Objetive
Create a program that says if a data belongs in a list that was
previously created.

The steps to take are:


- Ask the user how many data will he enter.
- Reserve space for that amount of numbers (floating point).
- Request the data to the user
- Later, repeat:
* Ask the user for a number (execution ends when he enters
"end" instead of a number).
* Say if that number is listed or not.

Must be done in pairs. but you must provide a single source file,
containing the names of both programmers in a comment.

Solution
?
1 using System;
2 public class Exercise052
{
3 public static void Main()
4 {
5 Console.Write("Cuantos datos reservo: ");
6 int repeticiones = Convert.ToInt32(Console.ReadLine());
7 float numero;
float[] listaNumeros = new float[repeticiones];
8
9 for(int i=1; i<=repeticiones; i++)
10 {
11 Console.Write("Dime numero {0} para guardar en la lista: ", i);
12 listaNumeros[i] = Convert.ToSingle(Console.ReadLine());
13 }
14
15 Console.Write("Que numero comprueba en la lista? ");
numero = Convert.ToSingle(Console.ReadLine());
16
17 while(Console.ReadLine() != "end")
18 {
19 for(int i=1; i<=repeticiones; i++)
20 {
if (listaNumeros[i] == numero)
21 Console.WriteLine("El número {0} existe en la lista",
22 numero );
23
24 }
25 }
26
27 }
}
28
29
30
31
32

4.04 - Array of positive and negative


numbers
Objetive
Create a C# program to ask the user for 10 real numbers and
display the average of the positive ones and the average of the
negative ones.

Solution
?
1 using System;
2 public class ArrayOfPositiveAndNegative
3 {
4 public static void Main()
{
5 float[] numbers = new float[10];
6 float totalPositive = 0.0f;
7 float totalNegative = 0.0f;
8 int countPositive = 0;
int countNegative = 0;
9
10
11 for (int i=0;i < 10;i++)
{
12
Console.Write("Number {0}= ",i+1);
13 numbers[i] = Convert.ToSingle( Console.ReadLine());
14 }
15
16 for (int i=0;i < 10;i++)
17 {
//Negative
18 if (numbers[i] < 0)
19 {
20 totalNegative = totalNegative + numbers[i];
21 countNegative++;
22 }
23
//Positive
24 if (numbers[i] > 0)
25 {
26 totalPositive = totalPositive + numbers[i];
27 countPositive++;
}
28 }
29
30 Console.WriteLine("Average numbers negatives = {0}",totalNegative /
31 countNegative);
32 Console.WriteLine("Average numbers positives = {0}",totalPositive /
33 countPositive);
}
34 }
35
36
37
38
39
40

You might also like