243 lines
9.3 KiB
C#
243 lines
9.3 KiB
C#
namespace array_funzioni;
|
|
|
|
class Program
|
|
{
|
|
const int dimensione=5;
|
|
static void Main(string[] args)
|
|
{
|
|
Console.Clear();
|
|
//dichiarazione e inizializzazione variabili
|
|
int scelta=0;
|
|
bool opzione1=false;
|
|
bool opzione5=false;
|
|
string input="";
|
|
int i=0;
|
|
int p=0;
|
|
int d=0;
|
|
int numeri=0;
|
|
int somma=0;
|
|
int media=0;
|
|
const int divisibile=7;
|
|
int[] insieme1=new int[dimensione];
|
|
for (int j=0; j<dimensione; j++){
|
|
insieme1[j]=0;
|
|
}
|
|
int[] pari=new int[dimensione];
|
|
for (int j=0; j<dimensione; j++){
|
|
pari[j]=0;
|
|
}
|
|
int[] dispari=new int[dimensione];
|
|
for (int j=0; j<dimensione; j++){
|
|
dispari[j]=0;
|
|
}
|
|
int[] insiemeDoppio=new int[dimensione*2];
|
|
for (int j=0; j<dimensione*2; j++){
|
|
insiemeDoppio[j]=0;
|
|
}
|
|
int[] insieme2=new int[dimensione];
|
|
for (int j=0; j<dimensione; j++){
|
|
insieme2[j]=0;
|
|
}
|
|
int[] insieme3=new int[dimensione];
|
|
for (int j=0; j<dimensione; j++){
|
|
insieme3[j]=0;
|
|
}
|
|
int[] intersezione=new int[dimensione*2];
|
|
for (int j=0; j<dimensione*2; j++){
|
|
intersezione[j]=0;
|
|
}
|
|
|
|
|
|
//menù
|
|
do{
|
|
Console.WriteLine("Scegliere un'opzione:");
|
|
Console.WriteLine("1. Crea un'insieme di numeri");
|
|
Console.WriteLine("2. Mostra insieme di numeri");
|
|
Console.WriteLine("3. Calcolo media dei numeri");
|
|
Console.WriteLine("4. Mostra multipli di " +divisibile);
|
|
Console.WriteLine("5. Crea due insiemi di numeri, divisi tra pari e dispari");
|
|
Console.WriteLine("6. Mostra l'insieme dei numeri pari e dispari");
|
|
Console.WriteLine("7. Intersezione di due insiemi");
|
|
Console.WriteLine("8. Due insiemi mischiati");
|
|
Console.WriteLine("9. Altri due insiemi mischiati diversamente");
|
|
Console.WriteLine("10. Ancora altri due insiemi mischiati");
|
|
Console.WriteLine("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
scelta=Convert.ToInt32(Console.ReadLine());
|
|
|
|
|
|
switch (scelta){
|
|
case 0:
|
|
Console.Clear();
|
|
break;
|
|
case 1:
|
|
Console.Clear();
|
|
i=0;
|
|
do{
|
|
Console.Write("Inserire un numero, massimo " +dimensione+ " numeri ([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"){
|
|
insieme1[i]=Convert.ToInt32(input);
|
|
}
|
|
i++;
|
|
}
|
|
while (input!="q" && i<dimensione);
|
|
opzione1=true;//traccia eseguita
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
Console.Clear();
|
|
if (opzione1==false){
|
|
Console.WriteLine("Errore: è necessario creare l'insieme di numeri prima di stamparlo");
|
|
}
|
|
else{
|
|
StampaArray(insieme1);
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 3:
|
|
Console.Clear();
|
|
if (opzione1==false){
|
|
Console.WriteLine("Errore: è necessario creare l'insieme di numeri prima di calcolarne la media");
|
|
}
|
|
else{
|
|
for (int j=0; j<dimensione; j++){
|
|
if (insieme1[j]!=0){
|
|
numeri++;
|
|
}
|
|
}
|
|
for (int j=0; j<dimensione; j++){
|
|
somma=somma+insieme1[j];
|
|
}
|
|
media=somma/numeri;
|
|
Console.WriteLine("Media: " +media);
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 4:
|
|
Console.Clear();
|
|
if (opzione1==false){
|
|
Console.WriteLine("Errore: è necessario creare l'insieme di numeri prima di individuare i suoi multipli di " +divisibile);
|
|
}
|
|
else{
|
|
for (int j=0; j<dimensione; j++){
|
|
if (insieme1[j]%2==0 && insieme1[j]%divisibile==0 && insieme1[j]!=0){
|
|
Console.WriteLine("Elemento " +j+ ": " +insieme1[j]);
|
|
}
|
|
}
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 5:
|
|
Console.Clear();
|
|
//reset per permettere esecuzioni consecutive
|
|
i=0;
|
|
p=0;
|
|
d=0;
|
|
for (int j=0; j<dimensione; j++){
|
|
pari[j]=0;
|
|
}
|
|
for (int j=0; j<dimensione; j++){
|
|
dispari[j]=0;
|
|
}
|
|
//inserimento e logica
|
|
do{
|
|
Console.Write("Inserire un numero, massimo " +dimensione*2+ " numeri ([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
|
|
//catch del tentativo di indirizzare fuori dall'array
|
|
if (p>=dimensione && Convert.ToInt32(input)%2==0){
|
|
Console.WriteLine("E' stato inserito il numero massimo di numeri pari, riprovare.");
|
|
}
|
|
else if (d>=dimensione && Convert.ToInt32(input)%2!=0){
|
|
Console.WriteLine("E' stato inserito il numero massimo di numeri dispari, riprovare.");
|
|
}
|
|
//assegnazione negli array
|
|
else {
|
|
if (input!="q"){//non si è interrotto l'inserimento
|
|
if(Convert.ToInt32(input)%2==0){//se pari
|
|
pari[p]=Convert.ToInt32(input);
|
|
p++;
|
|
}
|
|
else{
|
|
dispari[d]=Convert.ToInt32(input);
|
|
d++;
|
|
}
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
while (input!="q" && i<dimensione*2);
|
|
|
|
//output
|
|
Console.WriteLine("Numeri pari:");
|
|
for (int j=0; j<dimensione; j++){
|
|
if (pari[j]!=0){
|
|
Console.WriteLine("Elemento " +j+ ": " +pari[j]);
|
|
}
|
|
}
|
|
Console.WriteLine("Numeri dispari:");
|
|
for (int j=0; j<dimensione; j++){
|
|
if (dispari[j]!=0){
|
|
Console.WriteLine("Elemento " +j+ ": " +dispari[j]);
|
|
}
|
|
}
|
|
opzione5=true;
|
|
Pausa();
|
|
break;
|
|
case 6:
|
|
Console.Clear();
|
|
if (opzione5==false){
|
|
Console.WriteLine("Errore: è necessario creare l'insieme dei numeri pari e dispari prima di stamparne l'unione");
|
|
}
|
|
else{
|
|
for (int j=0; j<dimensione*2; j++){
|
|
insiemeDoppio[j]=0;
|
|
}
|
|
insiemeDoppio=Unione(pari,dispari);
|
|
StampaArray(insiemeDoppio);
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 7:
|
|
Console.Clear();
|
|
intersezione=Intersezione(insieme2, insieme3);
|
|
break;
|
|
case 8:
|
|
Console.Clear();
|
|
break;
|
|
case 9:
|
|
Console.Clear();
|
|
break;
|
|
case 10:
|
|
Console.Clear();
|
|
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 void StampaArray(int [] p_insieme) {
|
|
for (int j=0; j<p_insieme.Length; j++){
|
|
Console.WriteLine("Elemento " +j+ ": " +p_insieme[j]);
|
|
}
|
|
}
|
|
static int[] Unione(int[] p_array1, int[] p_array2){
|
|
int [] ritorno = new int[dimensione*2];
|
|
for (int i=0; i<dimensione; i++){
|
|
ritorno[i]=p_array1[i];
|
|
}
|
|
for (int i=0; i<dimensione; i++){
|
|
ritorno[i+dimensione]=p_array2[i];
|
|
}
|
|
return ritorno;
|
|
}
|
|
}
|