Sunteți pe pagina 1din 10

ACTIVITĂȚI 5

TABLOURI

1. Minim, maxim, media unui șir


2. Tablouri în scară
3. Șiruri de caractere (string)

1. Bucla foreach

1.

Consultați Cursul 07

1
Notițe TABLOURI ACTIVITĂȚI 5

Minim, maxim, media unui șir

a) Introduceți și rulați programul de mai jos. Explicați modul de


1 introducere a valorilor unui șir.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cursul07.ex1
{
class Program
{
static void Main(string[] args)
{
const int N = 9;

int[] note;
// note = new int[]{10,9,9,7,5,10,9,8,5};
note = new int[N]{10,9,9,7,5,10,9,8,5};
int min, max, suma;
float media;
/*
note[0] = 10;
note[1] = 9;
note[2] = 9;
note[3] = 7;
note[4] = 5;
note[5] = 10;
note[6] = 9;
note[7] = 8;
note[8] = 5;
*/
min = max = note[0];
suma = 0;

for (int i = 0; i < N; i++)


{
if (note[i] < min)
min = note[i];
if (note[i] > max)
max = note[i];

2
ACTIVITĂȚI 5 TABLOURI Notițe

suma = suma + note[i];


}
media = suma / N;
Console.WriteLine("min={0}, max={1}, media={2:0.00}",
min, max, media);

Console.ReadKey();
}
}
}

b) Modificați programul astfel încât valorile elementelor șirului șă fie introduse de


la tastatură.

c) Adăugați un șir care să conțină creditele asociate notelor și să care să calculeze


și să afișeze și media ponderată

d) Adăugați linia de program prin care programul afișează un titlu sugestiv la


începutul execuției. Introduceți comentariile informative la început de program
sursa. Adăugați programul la Portofoliul de programe.

Tablouri în scară

2 a) Introduceți și rulați programul de mai jos.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cursul07.ex2
{
class Program
{
static void Main(string[] args)
{

int[][] pasageri = new int[7][]; //matrice in


scara
pasageri[0] = new int[4] { 20, 14, 8, 9 }; // linia 0
(ziua 1) 4 curse
pasageri[1] = new int[4] { 14, 7, 14, 8 }; // linia 1
(ziua 2, 4 curse, nr pasageri)
pasageri[2] = new int[4] { 17, 12, 9, 19 };
pasageri[3] = new int[4] { 13, 15, 18, 14 };

3
Notițe TABLOURI ACTIVITĂȚI 5

pasageri[4] = new int[4] { 20, 19, 18, 20 };


pasageri[5] = new int[2] { 10, 5 };
pasageri[6] = new int[2] { 7, 9 };

for (int i = 0; i < 5; i++)


{
Console.WriteLine("Ziua {0}", i + 1);
for (int j = 0; j < 4; j++)
{
Console.WriteLine("Cursa {0} a avut {1}
pasageri", j + 1, pasageri[i][j]);
}
Console.WriteLine("\n");
}
for (int i = 5; i < 7; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine("In ziua {0}, cursa {1} a
avut {2} pasageri", i + 1, j + 1, pasageri[i][j]);
}
Console.WriteLine();
}

Console.ReadKey();
}
}
}

b) Explicați structura de tablou în scară a variabilei int[][] pasageri = new


int[7][].

c) Care este semnificația liniilor în matricea int[][] pasageri ? dar a elementelor


acestor linii ?

d) Adăugați linia de program prin care programul afișează un titlu sugestiv la


începutul execuției. Introduceți comentariile informative la început de program
sursa. Adăugați programul la Portofoliul de programe.

Șiruri de caractere (string)

3 a) Introduceți și rulați programul de mai jos.

sing System;
using System.Collections.Generic;

4
ACTIVITĂȚI 5 TABLOURI Notițe

using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cursul07.ex4
{
class Program
{
static void Main(string[] args)
{
// ex1
string str1="Acesta este un string";
string str2=string.Copy(str1);
string str3 = "Acesta este un alt string";

for (int i = 0; i < str1.Length; i++)


Console.Write(str1[i]);
Console.WriteLine();

if (str1 == str2)
Console.WriteLine("str1=str2");
else
Console.WriteLine("str1 !=str2");

if (str1 == str3)
Console.WriteLine("str1=str3");
else
Console.WriteLine("str1 !=str3");

int result = str1.CompareTo(str3);


if(result ==0)
Console.WriteLine("str1 este egal cu str3");
else
Console.WriteLine("str1 si str3 sunt diferite");

string s = str3.Substring(12, 13);


Console.WriteLine(s);

// ex2
string S = "Acesta este un string";
string subsirS = "est";
int j,k;
j = S.IndexOf(subsirS);
k = S.LastIndexOf(subsirS);
Console.WriteLine("Primul index este {0}",j);
Console.WriteLine("Ultimul index este {0}", k);

// ex3
string [ ] myStringArray;
char[ ] charArray = { '-' };
string myString="This-is-my-string";

5
Notițe TABLOURI ACTIVITĂȚI 5

myStringArray=myString.Split(charArray,4);
foreach (string ss in myStringArray)
{
Console.WriteLine(ss);
}

Console.ReadKey();
}
}
}

b) Observați efectul metodelor specifice string-urilor Copy(..), CompareTo(..),


Substring(..). Ce este Length ?

c) Ce reprezintă valorile returnate de metodele IndexOf(..),LastIndexOf(..)


?

d) În programul de mai sus, ce reprezintă variabila myStringArray ? Care este


efectul apelării metodei Split(..,..) ?

e) Adăugați linia de program prin care programul afișează un titlu sugestiv la


începutul execuției. Introduceți comentariile informative la început de program
sursa. Adăugați programul la Portofoliul de programe.

6
ACTIVITĂȚI 5 TABLOURI Notițe

Bucla foreach

1 a) Introduceți și rulați programul următor. Explicați funcționarea buclei


foreach.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cursul07.ex3
{
class Program
{
static void Main(string[] args)
{
{
int sum = 0;
int[] n = new int[20];

for (int i = 0; i < n.Length; i++)


n[i] = i * i;

foreach (int x in n)
{
Console.WriteLine("valoarea este: " + x);
sum += x;
}

Console.WriteLine("Suma este {0}", sum);


}
Console.ReadKey();
}
}
}

b) Care este diferența între foreach și instrucțiunea for ?

c) Ce reprezintă valoarea afișată a variabilei sum ?

d) Adăugați linia de program prin care programul afișează un titlu sugestiv la


începutul execuției. Introduceți comentariile informative la început de program
sursă. Adăugați programul la Portofoliul de programe.

7
Notițe TABLOURI ACTIVITĂȚI 5

Ap1

1

8
ACTIVITĂȚI 5 TABLOURI Notițe

Declaring Methods with Parameters


To pass information necessary for our method we use the parameters list. As
was already mentioned, we must place it between the brackets following the
method name, in method the declaration:

static <return_type> <method_name>(<parameters_list>)


{
// Method's body
}

The parameters list <parameters_list> is a list with zero or more declarations


of variables, separated by a comma, so that they will be used for the
implementation of the method’s logic:

<parameters_list> = [<type1> <name1>[, <typei> <namei>]],


where i = 2, 3, …

When we create a method, and we need certain information to develop the


particular algorithm, we choose that variable from the list, which is of type
<typei> and so we use it by its name <namei>.
The parameters from the list can be of any type. They can be primitive types
(int, double, …) or object types (for example string or array – int[],
double[], string[], …).

Rules to Name a Method


It is recommended, when declare a method, to follow the rules for method naming
suggested by Microsoft:
- The name of a method must start with capital letter.
- The PascalCase rule must be applied, i.e. each new word, that concatenates
so to form the method name, must start with capital letter.
- It is recommended that the method name must consist of verb, or verb and
noun.
Note that these rules are not mandatory, but recommendable. If we aim our C# code
to follow the style of all good programmers over the globe, we must use Microsoft’s
code convention.
Here some examples for well named methods:

Print
GetName
PlayMusic
SetUserName
And some examples for bad named methods:

Abc11

9
Notițe TABLOURI ACTIVITĂȚI 5

Yellow___Black
foo
_Bar
It is very important that the method name describes the method’s purpose. All behind
this idea is that when a person that is not familiar with our program reads the method
name, they can easily understand what that method does, without the need to look at
the method’s source code.

To name a method it is good to follow these rules:


- Method name must describe the method’s purpose.
- Method name must begin with capital letter.
- The PascalCase rule must be applied.
- The method name must consist of verb, or verb and
noun.

10

S-ar putea să vă placă și