151 lines
5.1 KiB
C#
151 lines
5.1 KiB
C#
using System.Net.Http.Headers;
|
|
|
|
namespace ripasso3;
|
|
|
|
class Program {
|
|
const int minNumero = 0;
|
|
const int maxNumero = 100;
|
|
static void Main(string[] args) {
|
|
int scelta;
|
|
int[] array = null; //Tecnica Segreta™ dei puntatori
|
|
bool opzione1 = false;
|
|
|
|
do {
|
|
Console.Clear();
|
|
Console.WriteLine("Scegliere un'opzione:");
|
|
Console.WriteLine("1. Crea un'insieme di numeri tra " + minNumero + " e " + maxNumero);
|
|
Console.WriteLine("2. Mostra il maggiore e il minore");
|
|
Console.WriteLine("3. Calcolo media");
|
|
Console.WriteLine("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
Console.Clear();
|
|
break;
|
|
case 1:
|
|
opzione1 = false;
|
|
Console.Clear();
|
|
array = CreaArray();
|
|
StampaArray(array);
|
|
opzione1 = true;
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
Console.Clear();
|
|
if(opzione1){
|
|
MaxMin(array);
|
|
}
|
|
else{
|
|
Console.WriteLine("È necessario eseguire l'opzione 1 prima di poter eseguire questa");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 3:
|
|
Console.Clear();
|
|
Media();
|
|
Pausa();
|
|
break;
|
|
|
|
default:
|
|
Console.WriteLine("Scelta non valida");
|
|
Pausa();
|
|
break;
|
|
}
|
|
}
|
|
while (scelta != 0);
|
|
}
|
|
|
|
static void Pausa() {
|
|
Console.WriteLine("Premere invio per continuare. . .");
|
|
Console.ReadLine();
|
|
Console.Clear();
|
|
}
|
|
static int[] CreaArray() {
|
|
int dimensione, i = 0;
|
|
string input;
|
|
|
|
do { //richiesta dimensione array
|
|
Console.Write("Quanti numeri vuoi inserire? ");
|
|
dimensione = Convert.ToInt32(Console.ReadLine());
|
|
if (dimensione <= 0) {
|
|
Console.WriteLine("Devi inserire almeno un numero");
|
|
}
|
|
}
|
|
while (dimensione <= 0);
|
|
|
|
int[] ritorno = new int[dimensione];
|
|
|
|
for (i = 0; i < dimensione; i++) {//creo un valore di default, così la sequenza non deve essere per forza completa
|
|
ritorno[i] = -1;
|
|
}
|
|
|
|
i = 0;//reset contatore
|
|
|
|
do{//richiesta numeri da mettere nell'array
|
|
Console.Clear();
|
|
Console.WriteLine("Hai ancora " + (dimensione - i) + " numeri da inserire");
|
|
Console.Write("Inserire un numero ([q] per uscire): ");
|
|
input=Console.ReadLine(); //non posso ancora fare il catch dell'eccezione se viene inserito un qualcosa che non sia un numero o q
|
|
if(input!="q"){
|
|
if(Convert.ToInt32(input) >= minNumero && Convert.ToInt32(input) <= maxNumero){
|
|
ritorno[i]=Convert.ToInt32(input);
|
|
i++;
|
|
}
|
|
else {
|
|
Console.WriteLine("Il numero deve essere compreso tra 0 e 100, estremi inclusi");
|
|
Pausa();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
while(input!="q" && i<dimensione);
|
|
|
|
return ritorno;
|
|
}
|
|
static void StampaArray(int[] p_array) {
|
|
for (int j = 0; j < p_array.Length; j++) {
|
|
if (p_array[j] != -1) {
|
|
Console.WriteLine("Elemento " + j + ": " + p_array[j]);
|
|
}
|
|
}
|
|
}
|
|
static void MaxMin(int[] p_array){
|
|
int temp = -1, max = maxNumero, min = minNumero;//temp è -1 perché è il valore di default dell'array quando è vuoto, e qualsiasi valore inserito sarà maggiore di questo
|
|
|
|
for (int i = 0; i < p_array.Length; i++){
|
|
if (p_array[i] != -1){
|
|
if(p_array[i] == maxNumero){//se ho già trovato il massimo valore inseribile
|
|
max = p_array[i];
|
|
break;//interrompo subito la ricerca
|
|
}
|
|
if (p_array[i] > temp){
|
|
max = p_array[i];
|
|
}
|
|
}
|
|
temp = p_array[i];
|
|
}
|
|
|
|
temp = maxNumero;//reset della variabile temporanea di appoggio, così che qualsiasi valore sia minore di questo
|
|
|
|
for (int i = 0; i < p_array.Length; i++){
|
|
if (p_array[i] != -1){
|
|
if(p_array[i] == minNumero){//se ho già trovato il minimo valore inseribile
|
|
min = p_array[i];
|
|
break;//interrompo subito la ricerca
|
|
}
|
|
if (p_array[i] < temp){
|
|
min = p_array[i];
|
|
}
|
|
}
|
|
temp = p_array[i];
|
|
}
|
|
Console.WriteLine("Il valore minimo è " + min + " mentre il valore massimo è " + max);
|
|
}
|
|
static void Media(){
|
|
|
|
}
|
|
} |