0% found this document useful (0 votes)
6 views5 pages

LP Assignments 2 Sol

Ex2 de c#

Uploaded by

fernandafortesch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

LP Assignments 2 Sol

Ex2 de c#

Uploaded by

fernandafortesch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Laboratories

Assignment 2

1) Build an e-mail address

A company decided to standardize the e-mail address of its employees. The new e-mail address
is built with the first letter of each word of the name except the family name, in lower case,
followed by a dot, then followed by the family name with only the first letter in upper case, then
the character @, and then the domain of the company.

Write a method that receives, as parameters, the full name of the employee and the company
domain, and returns the e-mail as defined above.

Example:

string name = “John Charles Smith Brown”;

string domain = “company.pt”;

string address = GetEmail( name, domain);

address should have the value [email protected]


class Program
{
private static string GetEmail(string name, string domain)
{
string email = "";
string[] names = name.Split(' ');
for (int i = 0; i < names.Length - 1; i++)
{
email += names[i].Substring(0, 1).ToLower();
}
email += ".";
string fname = names[names.Length - 1];
email += fname.Substring(0, 1).ToUpper() + fname.Substring(1).ToLower();
email += "@" + domain;
return email;
}
static void Main(string[] args)
{
string name = "John Charles Smith Brown";
string domain = "company.pt";
string address = GetEmail(name, domain);
Console.WriteLine(address);
Console.ReadKey();
}
}

2) Find the prime numbers smaller than 10.000, using the sieve of Erastothenes.

It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting
with the first prime number, 2. The multiples of a given prime are generated as a sequence of
numbers starting from that prime, with constant difference between them that is equal to that

Page 1 / 5
Programming Laboratories
Assignment 2

prime. This is the sieve's key distinction from using trial division to sequentially test each
candidate number for divisibility by each prime.

Create a boolean array with 10.000 elements. Each elements indicates if the corresponding
number (index) is a prime or not.

Initialize all the elements of the array to True. Next change to False all the multiples of 2. Next, do
the same with all the multiples of 3, etc.

Finally the elements of the array that are True indicate the numbers that are prime, because they
are not multiples of any number.
class Program
{
const int n = 10000;
static void Main(string[] args)
{
Boolean[] p = new Boolean[n];
for (int i = 0; i < n; i++)
p[i] = true;
for (int ix = 2; ix < n / 2; ix++)
{
if (p[ix])
{
for (int x = ix + ix; x < n; x += ix)
{
p[x] = false;
}
}
}
for (int i = 1; i < n; i++)
if (p[i])
Console.Write(i + " ");
Console.ReadKey();

}
}

3) Print ascending sequences

Write a method that receives as a parameter an array of double, and prints the ascending
sequences, one per line. Finally, it should write the number of sequences found.

An ascending sequence is a sequence of numbers so that each number is greater than the
previous one. So, the sequence id finished when a number is smaller than the previous one.

Example: given the array 7 8 12 4 2 5 8 5 6, the method should print:


7 8 12
4
2 5 8
5 6

4 sequences have been found

Page 2 / 5
Programming Laboratories
Assignment 2

class Program
{
static void AscSeq(double[] v)
{
int nseq = 0;
if (v.Length > 0) // is array not empty?
{
Console.Write(v[0] + " "); // prints the first number
nseq++; // first sequence
for (int i = 1; i < v.Length; i++)
{
if (v[i] < v[i - 1]) // smaller than previous?
{
nseq++; // another sequence
Console.WriteLine(); // new line
}
Console.Write(v[i] + " ");
}
Console.WriteLine();
}
Console.WriteLine(nseq + “ sequences have been found”);
}
static void Main(string[] args)
{
double[] v = { 2.2, 6, 5.5, 4, 7.1, 8, 3, 9.4, 6, 4, 3, 2, 6, 7 };
AscSeq(v);
Console.WriteLine();
double[] v0 = { }; // special case: empty array
AscSeq(v0);
Console.WriteLine();
double[] v1 = { 99 }; // special case: only one element
AscSeq(v1);
Console.ReadKey();
}
}

4) Operations with matrices (rectangular arrays)

Write a method that receives as parameters two rectangular matrices, and returns the matrix that
is their sum.

Write a method that receives as parameters two rectangular matrices of the appropriate
dimensions, and returns their product matrix.

class Program
{
static double[,] Sum(double[,] m1, double[,] m2)
{
int nl = m1.GetUpperBound(0)+1; // number of lines
int nc = m1.GetUpperBound(1)+1; // number of columns
double[,] sum = new double[nl, nc]; // result array
if (nl != m2.GetUpperBound(0)+1 || nc != m2.GetUpperBound(1) + 1)
return null; // not the same size?
for (int i = 0; i < nl; i++)
{
for (int j = 0; j < nc; j++)
{
sum[i, j] = m1[i, j] + m2[i, j];

Page 3 / 5
Programming Laboratories
Assignment 2

}
}
return sum;
}
static double[,] Prod(double[,] m1, double[,] m2)
{
int nl = m1.GetUpperBound(0) + 1; // number of lines of result array
int nc = m2.GetUpperBound(1) + 1; // number of columns of result array
double[,] prod = new double[nl, nc]; // result array
if (m1.GetUpperBound(1) != m2.GetUpperBound(0))
return null; // array have the correct dimensions?
for (int i = 0; i < nl; i++)
{
for (int j = 0; j < nc; j++)
{
prod[i, j] = 0;
for (int k = 0; k < m1.GetUpperBound(1) + 1; k++)
{
prod[i, j] += m1[i, k] * m2[k, j];
}
}
}
return prod;
}
static void Main(string[] args)
{
double[,] m1 = new double[,]
{
{1,3,5,7 },
{2,4,6,8 },
{1,2,3,4 }
};
double[,] m2 = new double[,]
{
{11,13,15,17 },
{22,24,26,28 },
{31,32,33,34 }
};
double[,] m3 = new double[,]
{
{1,2 },
{2,3 },
{3,4 },
{4,5 }
};
double[,] res = Sum(m1, m2);
for (int i = 0; i <= m1.GetUpperBound(0); i++)
{
for (int j = 0; j <= m1.GetUpperBound(1); j++)
{
Console.Write("{0,10:# ###}", res[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine();
res = Prod(m1, m3);
for (int i = 0; i <= m1.GetUpperBound(0); i++)
{
for (int j = 0; j <= m3.GetUpperBound(1); j++)
{
Console.Write("{0,10:# ###}", res[i, j]);

Page 4 / 5
Programming Laboratories
Assignment 2

}
Console.WriteLine();
}
Console.ReadKey();
}
}

Page 5 / 5

You might also like