0% found this document useful (0 votes)
28 views

Laborator 2 Structuri Fundamentale de Control. Structuri de Date: Vectori Unidimensionali

The document discusses 4 programming exercises in C# involving vectors and arrays: 1. Calculating statistics (counts, averages) of student grades sorted into intervals and calculating the average for a user-specified interval. 2. Splitting a string into words and counting the occurrences of a searched word. 3. Calculating and displaying Fibonacci numbers up to a user-specified limit. 4. Copying elements from two input arrays into a third array, sorting the destination array, and reversing the values in the destination array.

Uploaded by

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

Laborator 2 Structuri Fundamentale de Control. Structuri de Date: Vectori Unidimensionali

The document discusses 4 programming exercises in C# involving vectors and arrays: 1. Calculating statistics (counts, averages) of student grades sorted into intervals and calculating the average for a user-specified interval. 2. Splitting a string into words and counting the occurrences of a searched word. 3. Calculating and displaying Fibonacci numbers up to a user-specified limit. 4. Copying elements from two input arrays into a third array, sorting the destination array, and reversing the values in the destination array.

Uploaded by

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

LABORATOR 2

C#

STRUCTURI FUNDAMENTALE DE CONTROL.


STRUCTURI DE DATE: VECTORI UNIDIMENSIONALI
1. Se initializeaza un vector cu note ale studentilor. Sa se calculeze
numarul de note pe fiecare interval 1-4, 4-5, 5-6, 6-7, 7-8, 8-9, 9-10.
Sa se calculeze media notelor intre 1 si 10. Sa se calculeze media
pentru un interval specificat de utilizator
static void Main(string[] args)
{
int i, note1_4 = 0, note4_5 = 0, note5_6 = 0, note6_7 = 0, note7_8 = 0, note8_9 = 0, n
ote9_10 = 0, note_imposibile = 0;
float[] note = { 3.7F, 4.5F, 8, 10, 9.2F, 2, 2, 7, 6, 9, 5, 4, 3, 1, 5.7F, 6.3F, 10, 10, 1, 7, 6.
8F, 7, 9, 2, 4, 8, 10, 8.5F, 7, 8, 12 };
float medie = 0;
for (i = 0; i < note.Length; i++)
{
if (note[i] >= 1 && note[i] <= 4)
{
note1_4 = note1_4 + 1;
continue;
}
else if (note[i] > 4 && note[i] <= 5)
{
note4_5 = note4_5 + 1;
continue;
}
else if (note[i] > 5 && note[i] <= 6)
{
note5_6 = note5_6 + 1;
continue;
}
else if (note[i] > 6 && note[i] <= 7)
{
note6_7 = note6_7 + 1;
continue;
}
else if (note[i] > 7 && note[i] <= 8)
{
note7_8 = note7_8 + 1;
continue;
}
else if (note[i] > 8 && note[i] <= 9)
{
note8_9 = note8_9 + 1;
continue;
}
else if (note[i] > 9 && note[i] <= 10)
{
note9_10 = note9_10 + 1;
continue;
}
else
{
note_imposibile = note_imposibile + 1;

continue;

}
medie = calculeazaMedie(note);
Console.WriteLine("Note intre 1-4 : " + note1_4);
Console.WriteLine("Note intre 4-5 : " + note4_5);
Console.WriteLine("Note intre 5-6 : " + note5_6);
Console.WriteLine("Note intre 6-7 : " + note6_7);
Console.WriteLine("Note intre 7-8 : " + note7_8);
Console.WriteLine("Note intre 8-9 : " + note8_9);
Console.WriteLine("Note intre 9-10 : " + note9_10);
Console.WriteLine("Note imposibile : " + note_imposibile);
Console.WriteLine("Media notelor intre 1 si 10 varianta 1 este: " + medie);
medie = calculeazaMedie_v2(note);
Console.WriteLine("Media notelor intre 1 si 10 varianta 2 este: " + medie);
medie = calculeazaMedie_v3(8, 10, note);
Console.WriteLine("Media notelor intre 8 si 10 este: " + medie);
Console.ReadLine();
}
static float calculeazaMedie(float[] temp_vector)
{
float medie = 0;
int count = 0;
float suma_elemente = 0;
foreach (float f in temp_vector)
{
if (f >= 1 && f <= 10)
{
count++;
suma_elemente = suma_elemente + f;
}
}
medie = suma_elemente / count;
return medie;
}
// metoda 2 for
static float calculeazaMedie_v2(float[] temp_vector)
{
float medie = 0;
int count = 0;
float suma_elemente = 0;
for (int i = 0; i < temp_vector.Length; i++)
{
if (temp_vector[i] >= 1 && temp_vector[i] <= 10)
{
count++;
suma_elemente = suma_elemente + temp_vector[i];
}
}
medie = suma_elemente / count;
return medie;
}
static float calculeazaMedie_v3(int limita_minima, int limita_maxima, float[] tempVector)
{
int count = 0;
float medie = 0;
float suma = 0;

foreach (float f in tempVector)


{
if (f >= limita_minima && f < limita_maxima)
{
count++;
suma = suma + f;
}
}
medie = suma / count;
return medie;
}

2. Impartirea unui string in cuvinte. Cautarea numarului de aparitii ale


unui cuvant in cadrul unui string
class Program
{
static void Main(string[] args)
{
# region declaratii_si_initializari_variabile
string s = string.Empty; // Declararea unui string
string cuvant_cautat = string.Empty;
Console.Write("Introduceti stringul : ");
int numar_aparitii_cuvant = 0;
#endregion
s = Console.ReadLine(); // Citirea stringului si salvarea in s'
string[] cuvinte = s.Split(' ');
Console.Write("Introduceti cuvantul cautat: ");
cuvant_cautat = Console.ReadLine();
// 'cuvinte' reprezinta un vector de stringuri
// stringul 's' va f impartit in substringuri la intalnirea unui caracter din setcaractere
// fecare substring va f salvat in vectorul cuvinte
int i = cuvinte.Length; //Dimensiunea vectorului cuvinte
Console.WriteLine("The total number of words in the entered string : " + i);
Console.WriteLine("Cuvantul: " + cuvant_cautat + " a aparut de: " + numar_aparitii_cu
vant);
Console.ReadLine();
}
static int cautaCuvant(string cuvant, string[] vector_cuvinte)
{
int numar_aparitii = 0;
foreach (string temp_string in vector_cuvinte)
{
if (formateazaCuvant(temp_string) == cuvant)
{
numar_aparitii++;
}
}
return numar_aparitii;
}
static string formateazaCuvant(string cuvant)
{
string cuvantFinal = cuvant;
if (cuvant.EndsWith(","))
{
cuvantFinal = cuvant.Remove(cuvant.Length - 1);
}

return cuvantFinal;

3. Sa se calculeze si sa se afiseze numerele din seria Fibonnaci


static void Main(string[] args)
{
int frst = 1, second = 1, third, no, count = 0;
long sum = 2;
Console.Write("Enter the number uptill which you want the fbonacci numbers :");
no = int.Parse(Console.ReadLine());
if (no >= 45)
{
Console.WriteLine("\n Numarul nu trebuie sa depaseasca 45 : " + sum);
}
else
{
Console.Write("Fibonacci Series : 1 1");
do
{
third = frst + second;
Console.Write(" " + third);
frst = second;
second = third;
count = count + 1;
sum = sum + third;
}
while ((count + 3) <= no);
Console.WriteLine("\nSum of all fbonacci digits : " + sum);
}
}

4. Sa se copie elementele a doi vectori intr-un al treilea vector. Sa se


ordoneze vectorul destinatie. Sa se inverseze valorile vectorului
destinatie
static void Main(string[] args)

int[] A = { 127, 157, 240, 550, 510 };


int[] B = { 275, 157, 750, 255, 150, 209, 1, 4, 7 };
int CLength = (A.Length + B.Length);
int[] C = new int[CLength];
int contor_C = 0;
int[] Vector_Destinatie = new int[CLength];
# region metoda 1
for (int i = 0; i < A.Length; i++)
{
C[i] = A[i];
contor_C = i;
}
contor_C = contor_C + 1;
for (int j = 0; j < B.Length; j++)
{
C[contor_C] = B[j];
contor_C = contor_C + 1;
}
#endregion
#region metoda 2
Array.Sort(C);
Array.Copy(A, 0, Vector_Destinatie, 0, A.Length);
Array.Copy(B, 0, Vector_Destinatie, A.Length, B.Length);
#endregion
#region diferite metode din clasa vector
Array.Sort(Vector_Destinatie);
int pozitie_numar = Array.IndexOf(Vector_Destinatie, 550);
pozitie_numar = Array.LastIndexOf(Vector_Destinatie, 550);
pozitie_numar = Array.BinarySearch(Vector_Destinatie, 550);
Array.Reverse(Vector_Destinatie);
#endregion
Console.ReadLine();

You might also like