0% found this document useful (0 votes)
47 views15 pages

Using Using Using Using Using Namespace Class Static Void String

This document contains the code for a menu-driven program for working with arrays in C#. The program displays a menu with 14 options for performing operations on an integer array like filling it randomly, showing the array, finding the largest value, etc. It gets user input to select a menu option and calls the appropriate method to perform the selected operation, then returns to the main menu.

Uploaded by

Ainhoa Mol
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)
47 views15 pages

Using Using Using Using Using Namespace Class Static Void String

This document contains the code for a menu-driven program for working with arrays in C#. The program displays a menu with 14 options for performing operations on an integer array like filling it randomly, showing the array, finding the largest value, etc. It gets user input to select a menu option and calls the appropriate method to perform the selected operation, then returns to the main menu.

Uploaded by

Ainhoa Mol
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/ 15

...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.

cs 1
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace ConsoleApp1
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 Menu();
14 }
15 static void Menu()
16 {
17 string opcio;
18 int numMenu;
19 bool numvalid=false;
20 do
21 {
22 Console.Clear();
23 Console.ForegroundColor = ConsoleColor.Yellow;
24 Console.WriteLine("\n\n");
25 Console.WriteLine("\t\t\t
\t***************************************************");
26 Console.ForegroundColor = ConsoleColor.Blue;
27 Console.WriteLine("\t\t\t\t** MENÚ TAULES 8A
**");
28 Console.ForegroundColor = ConsoleColor.Yellow;
29 Console.WriteLine("\t\t\t
\t***************************************************");
30 Console.ForegroundColor = ConsoleColor.Yellow;
31 Console.WriteLine("\t\t\t\t** TRIA ENTRE UNA DE LES
OPCIONS **");
32 Console.WriteLine("\t\t\t\t** ** ** ** ** ** * * ** **
** ** ** **\n ");
33 Console.WriteLine("\t\t\t\t** 1.- MOSTRA TAULA
**");
34 Console.WriteLine("\t\t\t\t** 2.- MOSTRA FINS A N
ELEMENTS **");
35 Console.WriteLine("\t\t\t\t** 3.- OMPLIR ALEATORI
**");
36 Console.WriteLine("\t\t\t\t** 4.- OMPLIR FINS A N I
ALEATORI **");
37 Console.WriteLine("\t\t\t\t** 5.- OMPLIR AMB LIM_INF
I LIM_SUP **");
38 Console.WriteLine("\t\t\t\t** 6.- OMPLIR TAULA DES
DE TECLAT **");
39 Console.WriteLine("\t\t\t\t** 7.- AFEGIR UN VALOR
**");
40 Console.WriteLine("\t\t\t\t** 8.- TORNAR UNA STRING
**");
41 Console.WriteLine("\t\t\t\t** 9.- CALCULAR MITJANA
**");
42 Console.WriteLine("\t\t\t\t** 10.- VALOR MÉS GRAN
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 2
**");
43 Console.WriteLine("\t\t\t\t** 11.- POSICIÓ DEL VALOR
MÉS GRAN **");
44 Console.WriteLine("\t\t\t\t** 12.- HI ÉS O NO HI ÉS?
**");
45 Console.WriteLine("\t\t\t\t** 13.- ON ES TROBA?
**");
46 Console.WriteLine("\t\t\t\t** 14.- ELIMINAR VALOR
**");
47 Console.WriteLine("\t\t\t\t**
**");
48 Console.WriteLine("\t\t\t\t**
**");
49 Console.ForegroundColor = ConsoleColor.Blue;
50 Console.WriteLine("\t\t\t\t\t ** PRESS 0 TO EXIT **
");
51 Console.ForegroundColor = ConsoleColor.Yellow;
52 Console.WriteLine("\t\t\t
\t***************************************************");
53 Console.WriteLine("\t\t\t
\t***************************************************");
54 Console.Write("\t\t\t\t");
55
56 do
57 {
58 opcio = Console.ReadLine();
59 numvalid = CompruebaInt(opcio);
60 }
61 while (!numvalid);
62
63 numMenu = int.Parse(opcio);
64
65 if (numMenu >= (0) && numMenu <= 14)
66 Operacio(numMenu);
67 else
68 {
69 Console.WriteLine("\n\n");
70 Console.ForegroundColor = ConsoleColor.DarkRed;
71 Console.WriteLine("\t\t\t\t OOPSS!! Aquesta opció No és
vàlida ");
72 Console.Write("\t\t\t\t");
73 Console.ReadKey();
74 }
75 }
76 while (numMenu != 0);
77 }
78 static int Operacio(int opcio)
79 {
80 int[] myarray = new int[10];
81 int n, inf, sup, nElements = 0, amplada;
82 int posActual = 0,posInicial=0;
83 string introAmplada = "amplada", introElements = "elements",
introValors = "valor", introPosicio = "posicio";
84 switch (opcio)
85 {
86 case 0:
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 3
87 char resp;
88 Console.WriteLine("\t\t\t\t vols sortir S/N?");
89 Console.WriteLine("\n");
90 resp = Char.ToUpper(Console.ReadKey().KeyChar);
91 if (resp == 'S')
92 {
93 Console.Write("\t\t\t\t");
94 Console.WriteLine("\t\t adiositoooo!!!!\n\n");
95 Console.Write("\t\t\t\t");
96 System.Environment.Exit(0);
97 }
98 break;
99
100 case 1:
101 Console.Clear();
102
103 Console.WriteLine("\n\n");
104 Console.WriteLine("\t\t\t
\t**************************************************");
105 Console.WriteLine("\t\t\t\t* MOSTRA TAULA
*");
106 Console.WriteLine("\t\t\t
\t**************************************************");
107
108 myarray = new int[ DemanaValor(introAmplada)];
109 MostraTaula(myarray);
110
111 Console.WriteLine("\t\t\t
\t**************************************************");
112 Console.Write("\t\t\t\t");
113 Console.ReadKey();
114 break;
115
116 case 2:
117 Console.Clear();
118
119 Console.WriteLine("\n\n");
120 Console.WriteLine("\t\t\t
\t**************************************************");
121 Console.WriteLine("\t\t\t\t* MOSTRAR TAULA FINS A
N ELEMENTS *");
122 Console.WriteLine("\t\t\t
\t**************************************************");
123
124 myarray = new int[DemanaValor(introAmplada)];
125 nElements = DemanaValor(introElements);
126
127 MostraTaula(myarray, nElements);
128
129 Console.WriteLine("\t\t\t
\t**************************************************");
130 Console.Write("\t\t\t\t");
131 Console.ReadKey();
132 break;
133
134 case 3:
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 4
135 Console.Clear();
136
137 Console.WriteLine("\n\n");
138 Console.WriteLine("\t\t\t
\t**************************************************");
139 Console.WriteLine("\t\t\t\t* OMPLIR ALEATORI
*");
140 Console.WriteLine("\t\t\t
\t**************************************************");
141
142 myarray = new int[amplada = DemanaValor(introAmplada)];
143
144 OmpleAleatori(myarray);
145
146 Console.WriteLine("\t\t\t
\t**************************************************");
147 Console.Write("\t\t\t\t");
148 Console.ReadKey();
149 break;
150
151 case 4:
152 Console.Clear();
153
154 Console.WriteLine("\n\n");
155 Console.WriteLine("\t\t\t
\t**************************************************");
156 Console.WriteLine("\t\t\t\t* OMPLIR FINS A N I
ALEATORI *");
157 Console.WriteLine("\t\t\t
\t**************************************************");
158
159 myarray = new int[DemanaValor(introAmplada)];
160 nElements = DemanaValor(introElements);
161
162 OmpleAleatori(myarray, nElements);
163
164 Console.WriteLine("\t\t\t
\t**************************************************");
165 Console.Write("\t\t\t\t");
166 Console.ReadKey();
167 break;
168
169 case 5:
170 Console.Clear();
171
172 Console.WriteLine("\n\n");
173 Console.WriteLine("\t\t\t
\t**************************************************");
174 Console.WriteLine("\t\t\t\t* OMPLIR AMB LIM_INF I
LIM_SUP *");
175 Console.WriteLine("\t\t\t
\t**************************************************");
176
177 myarray = new int[DemanaValor(introAmplada)];
178 nElements = DemanaValor(introElements);
179
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 5
180 Console.Write("\t\t\t\t Valor mínim? ");
181 inf = Convert.ToInt32(Console.ReadLine());
182 Console.Write("\n");
183 do
184 {
185 Console.Write("\t\t\t\t Valor màxim? ");
186 sup = Convert.ToInt32(Console.ReadLine());
187 Console.Write("\n");
188 if (sup < inf)
189 Console.Write("No pot ser més petit que el valor
inferior, torna a escriure-ho!");
190 } while (sup < inf);
191
192 OmpleAleatori(myarray, nElements, inf, sup);
193
194 Console.WriteLine("\t\t\t
\t**************************************************");
195 Console.Write("\t\t\t\t");
196 Console.ReadKey();
197 break;
198
199 case 6:
200 Console.Clear();
201
202 Console.WriteLine("\n\n");
203 Console.WriteLine("\t\t\t
\t**************************************************");
204 Console.WriteLine("\t\t\t\t* OMPLIR TAULA DESDE
TECLAT *");
205 Console.WriteLine("\t\t\t
\t**************************************************");
206
207 myarray = new int[DemanaValor(introAmplada)];
208 nElements = OmplirTeclat(myarray);
209
210 if (nElements == myarray.Length)
211 {
212 Console.WriteLine("\t\t\t\tarray plena!no hi caben més
valors!");
213 }
214 MostraTaula(myarray, nElements);
215
216 Console.WriteLine("\t\t\t
\t**************************************************");
217 Console.Write("\t\t\t\t");
218 Console.ReadKey();
219 break;
220
221 case 7:
222 Console.Clear();
223
224 Console.WriteLine("\n\n");
225 Console.WriteLine("\t\t\t
\t**************************************************");
226 Console.WriteLine("\t\t\t\t* AFEGIR UN VALOR
*");
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 6
227 Console.WriteLine("\t\t\t
\t**************************************************");
228
229 myarray = new int[DemanaValor(introAmplada)];
230 int valor;
231 bool espot = false;
232
233 do
234 {
235 valor = DemanaValor(introValors);
236 //Console.WriteLine("\t\t\t\tintrodueix un valor a a
la taula");
237 //Console.Write("\t\t\t\t");
238 //valor = int.Parse(Console.ReadLine());
239 espot = AfegirValor(myarray, ref nElements, valor);
240
241 } while (espot && valor!=-99);
242
243 if (!espot) Console.WriteLine("\t\t\t\ttaula plena");
244 else if(valor==-99) Console.WriteLine("\t\t\t\t VALOR
INVÀLID");
245
246 MostraTaula(myarray);
247
248 Console.WriteLine("\t\t\t
\t**************************************************");
249 Console.Write("\t\t\t\t");
250 Console.ReadKey();
251 break;
252
253 case 8:
254 Console.Clear();
255
256 Console.WriteLine("\n\n");
257 Console.WriteLine("\t\t\t
\t**************************************************");
258 Console.WriteLine("\t\t\t\t* TORNAR UNA STRING
*");
259 Console.WriteLine("\t\t\t
\t**************************************************");
260
261 myarray = new int[DemanaValor(introAmplada)];
262
263 do
264 {
265 Console.WriteLine("\t\t\t\tintrodueix un valor a a la
taula");
266 Console.Write("\t\t\t\t");
267 valor = int.Parse(Console.ReadLine());
268 espot = AfegirValor(myarray, ref nElements, valor);
269
270 } while (espot);
271
272 if (!espot) Console.WriteLine("\t\t\t\ttaula plena");
273
274 string array = TornaString(myarray, ref nElements);
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 7
275 Console.WriteLine(array);
276
277 Console.WriteLine("\t\t\t
\t**************************************************");
278 Console.Write("\t\t\t\t");
279 Console.ReadKey();
280 break;
281
282 case 9:
283 Console.Clear();
284
285 Console.WriteLine("\n\n");
286 Console.WriteLine("\t\t\t
\t**************************************************");
287 Console.WriteLine("\t\t\t\t* CALCULA LA MITJANA
*");
288 Console.WriteLine("\t\t\t
\t**************************************************");
289
290 myarray = new int[DemanaValor(introAmplada)];
291
292 Console.WriteLine("\t\t\t\t Intodueix els valors a dins de
l'array, acaba amb -1");
293 do
294 {
295 valor = int.Parse(Console.ReadLine());
296 espot = AfegirValor(myarray, ref nElements, valor);
297
298 } while (espot && valor != -1);
299
300 if (!espot) Console.WriteLine("\t\t\t\ttaula plena");
301
302 Console.WriteLine($"\t\t\t\tla mitjana del array és:
{Mitjana(myarray, ref nElements)}");
303
304 Console.WriteLine("\t\t\t
\t**************************************************");
305 Console.Write("\t\t\t\t");
306 Console.ReadKey();
307 break;
308
309 case 10:
310 Console.Clear();
311
312 Console.WriteLine("\n\n");
313 Console.WriteLine("\t\t\t
\t**************************************************");
314 Console.WriteLine("\t\t\t\t* VALOR MÉS GRAN
*");
315 Console.WriteLine("\t\t\t
\t**************************************************");
316
317 myarray = new int[DemanaValor(introAmplada)];
318
319 Console.WriteLine("\t\t\t\tIntodueix els valors a dins de
l'array, acaba amb -1");
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 8
320 do
321 {
322 Console.Write("\t\t\t\t");
323 valor = int.Parse(Console.ReadLine());
324 espot = AfegirValor(myarray, ref nElements, valor);
325
326 } while (espot && valor != -1);
327
328 if (!espot) Console.WriteLine("\t\t\t\ttaula plena");
329
330 MostraTaula(myarray);
331
332 Console.WriteLine($"\t\t\t\tEl valor més gran és: {MesGran
(myarray, ref nElements)}");
333
334 Console.WriteLine("\t\t\t
\t**************************************************");
335 Console.Write("\t\t\t\t");
336 Console.ReadKey();
337 break;
338
339 case 11:
340 Console.Clear();
341
342 Console.WriteLine("\n\n");
343 Console.WriteLine("\t\t\t
\t**************************************************");
344 Console.WriteLine("\t\t\t\t* POSICIÓ DEL VALOR
MÉS GRAN *");
345 Console.WriteLine("\t\t\t
\t**************************************************");
346
347 myarray = new int[DemanaValor(introAmplada)];
348
349 Console.WriteLine("\t\t\t\tIntodueix els valors a dins de
l'array, acaba amb -1");
350 do
351 {
352 Console.Write("\t\t\t\t");
353 valor = int.Parse(Console.ReadLine());
354 espot = AfegirValor(myarray, ref nElements, valor);
355
356 } while (espot && valor != -1);
357
358 if (!espot) Console.WriteLine("\t\t\t\ttaula plena");
359
360 Console.WriteLine($"\t\t\t\tEl valor més gran és: {MesGran
(myarray, ref nElements)}\n" +
361 $"\t\t\t\tEs troba en la posició: taula[{ BuscaGran
(myarray, ref nElements)}]");
362
363 Console.WriteLine("\t\t\t
\t**************************************************");
364 Console.Write("\t\t\t\t");
365 Console.ReadKey();
366 break;
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 9
367
368 case 12:
369 Console.Clear();
370
371 Console.WriteLine("\n\n");
372 Console.WriteLine("\t\t\t
\t**************************************************");
373 Console.WriteLine("\t\t\t\t* HI ÉS O NO HI
ÉS? *");
374 Console.WriteLine("\t\t\t
\t**************************************************");
375
376 myarray = new int[DemanaValor(introAmplada)];
377
378 Console.WriteLine("\t\t\t\tIntodueix els valors a dins de
l'array, acaba amb -1");
379 do
380 {
381 Console.Write("\t\t\t\t");
382 valor = int.Parse(Console.ReadLine());
383 espot = AfegirValor(myarray, ref nElements, valor);
384
385 } while (espot && valor != -1);
386
387 if (!espot) Console.WriteLine("\t\t\t\ttaula plena");
388
389
390 int valorBuscar = DemanaValor(introValors);
391 posInicial = DemanaValor(introPosicio);
392 //Console.WriteLine("\t\t\t\tquin valor vols buscar?");
393 //Console.Write("\t\t\t\t");
394 //int valorBuscar= int.Parse(Console.ReadLine());
395
396 if (BuscaValor(myarray, ref nElements, valorBuscar))
397
398 Console.WriteLine($"\t\t\t\tEl valor:{valorBuscar} no
trobat!");
399
400 Console.WriteLine("\t\t\t
\t**************************************************");
401 Console.Write("\t\t\t\t");
402 Console.ReadKey();
403 break;
404
405 case 13:
406 Console.Clear();
407
408 Console.WriteLine("\n\n");
409 Console.WriteLine("\t\t\t
\t**************************************************");
410 Console.WriteLine("\t\t\t\t* 13.- ON ES
TROBA? *");
411 Console.WriteLine("\t\t\t
\t**************************************************");
412
413
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 10
414 myarray = new int[DemanaValor(introAmplada)];
415
416 Console.WriteLine("\t\t\t\tIntodueix els valors a dins de
l'array, acaba amb -1");
417 do
418 {
419 Console.Write("\t\t\t\t");
420 valor = int.Parse(Console.ReadLine());
421 espot = AfegirValor(myarray, ref nElements, valor);
422
423 } while (espot && valor != -1);
424
425 if (!espot) Console.WriteLine("\t\t\t\ttaula plena");
426
427 Console.WriteLine("\t\t\t\tquin valor vols buscar?");
428 Console.Write("\t\t\t\t");
429 valorBuscar = int.Parse(Console.ReadLine());
430
431 Console.WriteLine("\t\t\t\t A partir de quina posicio?");
432 Console.Write("\t\t\t\t");
433 posInicial = int.Parse(Console.ReadLine());
434
435
436
437 if (BuscaValor(myarray, ref nElements, valorBuscar))
438 {
439 posActual = TornaPosicio(myarray, ref nElements,
valorBuscar, posInicial);
440 Console.WriteLine($"\t\t\t\tEl valor:{valorBuscar} es
troba en la posició: taula[{posActual}]");
441 }
442
443 else
444 Console.WriteLine($"\t\t\t\tEl valor:{valorBuscar} no
trobat!");
445
446 Console.WriteLine("\t\t\t
\t**************************************************");
447 Console.Write("\t\t\t\t");
448 Console.ReadKey();
449 break;
450
451 case 14:
452 Console.Clear();
453
454 Console.WriteLine("\n\n");
455 Console.WriteLine("\t\t\t
\t**************************************************");
456 Console.WriteLine("\t\t\t\t* 14.- ELIMINAR
VALOR *");
457 Console.WriteLine("\t\t\t
\t**************************************************");
458
459 myarray = new int[DemanaValor(introAmplada)];
460
461 Console.WriteLine("\t\t\t\tIntodueix els valors a dins de
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 11
l'array, acaba amb -1");
462 do
463 {
464 Console.Write("\t\t\t\t");
465 valor = int.Parse(Console.ReadLine());
466 espot = AfegirValor(myarray, ref nElements, valor);
467
468 } while (espot && valor != -1);
469
470 if (!espot) Console.WriteLine("\t\t\t\ttaula plena");
471
472 Console.WriteLine("\t\t\t\tquin valor vols eliminar?");
473 Console.Write("\t\t\t\t");
474 valorBuscar = int.Parse(Console.ReadLine());
475
476 if (BuscaValor(myarray, ref nElements, valorBuscar))
477 {
478 posActual = TornaPosicio(myarray, ref nElements,
valorBuscar, posInicial);
479
480 EliminaPerPosicio(myarray, ref nElements, posActual);
481 Console.WriteLine($"\t\t\t\tEl valor:{ valorBuscar }
es troba en la posicio: taula[{ posActual }]" +
482 $"ha sigut eliminat");
483 MostraTaula(myarray, nElements);
484
485 }
486 else
487 Console.WriteLine($"\t\t\t\tEl valor:{valorBuscar} no
trobat!");
488
489 Console.WriteLine("\t\t\t
\t**************************************************");
490 Console.Write("\t\t\t\t");
491 Console.ReadKey();
492 break;
493 }
494 return opcio;
495 }
496 static bool CompruebaInt(string linea)
497 {
498 bool valid = true;
499 int comparacio = 0;
500 int n = 2147483647;
501 linea.Trim();
502 if (linea.Length > 0 && linea.Length <= 10)
503 {
504 for (int i = 0; i < linea.Length && valid; i++)
505 {
506 if (!("0123456789".Contains(linea[i])))
507 valid = false;
508 }
509 if (linea.Length == 10 && valid)
510 {
511 comparacio = linea.CompareTo(n.ToString());
512 }
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 12
513 if (valid && comparacio <= 0)
514 return true;
515 else
516 return false;
517 }
518 else
519 return false;
520 }
521 static void MostraTaula(int[] taula)
522 {
523 for (int i = 0; i < taula.Length; i++)
524 Console.WriteLine("\t\t\t\tTaula[{0}] = {1}\n", i, taula[i]);
525 }
526 static void MostraTaula(int[] taula, int n)
527 {
528 for (int i = 0; i < n; i++)
529 Console.WriteLine("\t\t\t\tTaula[{0}] = {1}\n", i, taula[i]);
530 }
531 static void OmpleAleatori(int[] taula)
532 {
533 Random aleatori = new Random();
534 for (int i = 0; i < taula.Length; i++)
535 {
536 taula[i] = aleatori.Next(0, 100);
537 Console.WriteLine("\t\t\t\tTaula[{0}] = {1}\n", i, taula[i]);
538 }
539 }
540 static void OmpleAleatori(int[] taula, int n)
541 {
542 Random aleatori = new Random();
543 for (int i = 0; i < n; i++)
544 {
545 taula[i] = aleatori.Next(0, 100);
546 Console.WriteLine("\t\t\t\tTaula[{0}] = {1}\n", i, taula[i]);
547 }
548 }
549 static void OmpleAleatori(int[] taula, int n, int limInf, int limSup)
550 {
551 Random aleatori = new Random();
552 for (int i = 0; i < n; i++)
553 {
554 taula[i] = aleatori.Next(limInf, limSup);
555 Console.WriteLine("\t\t\t\tTaula[{0}] = {1}\n", i, taula[i]);
556 }
557 }
558 static int OmplirTeclat(int[] taula)
559 {
560 int nElements = 0;
561
562 Console.WriteLine("\t\t\t\tEntra valors a la taula, acaba amb -1
");
563 Console.Write("\t\t\t\t");
564 int valor = int.Parse(Console.ReadLine());
565
566 for (int i = 0; i < taula.Length && valor != -1; i++)
567 {
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 13
568 taula[i] = valor;
569 nElements++;
570 Console.Write("\t\t\t\t");
571 valor = int.Parse(Console.ReadLine());
572 }
573 return nElements;
574 }
575 static bool AfegirValor(int[] taula, ref int nElem, int valor)
576 {
577 bool afegir = true;
578 if (nElem < taula.Length)
579 {
580 taula[nElem] = valor;
581 nElem++;
582 }
583 else afegir = false;
584
585 return afegir;
586 }
587 static string TornaString(int[] taula, ref int nElem)
588 {
589 string res = "Mostrant taula en format string:\n";
590 Console.Write("\t\t\t\t");
591 for (int i = 0; i < nElem; i++)
592 {
593 res += "\t\t\t\tTaula[" + i + "] = " + taula[i] + "\n";
594 }
595 return res;
596 }
597 static double Mitjana(int[] taula, ref int nElem)
598 {
599 double mitja = 0;
600
601 for (int i = 0; i < nElem; i++)
602 {
603 mitja += taula[i];
604 }
605 mitja /= taula.Length;
606 return mitja;
607 }
608 static int MesGran(int[] taula, ref int nElem)
609 {
610 int max = taula[0];
611
612 for (int i = 0; i < nElem; i++)
613 {
614 if (taula[i] > max)
615 max = taula[i];
616 }
617 return max;
618 }
619 static int BuscaGran(int[] taula, ref int nElem)
620 {
621 int max = taula[0];
622 int pos = 0;
623
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 14
624 for (int i = 0; i < nElem; i++)
625 {
626 if (taula[i] > max)
627 {
628 max = taula[i];
629 pos = i;
630 }
631 }
632 return pos;
633 }
634 static bool BuscaValor(int[] taula, ref int nElem,int valor)
635 {
636 bool trobat = false;
637 for (int i = 0; i < nElem; i++)
638 {
639 if (taula[i] == valor)
640 trobat=true;
641 }
642 return trobat;
643 }
644 static int DemanaValor(string introDada)
645 {
646 int valor=0;
647 Console.Write("\t\t\t\tIntrodueix {0}: ", introDada);
648 introDada = Console.ReadLine();
649
650 if (CompruebaInt(introDada))
651 valor = int.Parse(introDada);
652
653 return valor;
654 }
655 static int TornaPosicio(int[] taula, ref int nElem, int valor,int
posInicial)
656 {
657 int posTornada=0;
658 for (int i = posInicial; i < nElem; i++)
659 {
660 if (taula[i] == valor)
661 posTornada = taula[i];
662 }
663 return posTornada;
664 }
665 static void EliminaPerPosicio(int[] taula, ref int nElem, int posicio)
666 {
667 if (posicio >= 0 && posicio < nElem)
668 {
669 taula[posicio] = taula[nElem - 1];
670 nElem--;
671 }
672 //int posicioBorrar = nElem - 1;
673 //for (int i = posicioBorrar; i < nElem - 1; i++)
674 // taula[i] = taula[i + 1];
675 //nElem--;
676 }
677 }
678 }
...oa\source\repos\LLISTA_8A_TABLAS\ConsoleApp1\Program.cs 15
679
680

You might also like