A program is required that allows saving in a vector the years entered by a user according to the criterion if the year is a leap year or not.
Design a console application that allows a user to enter n number of years and then classify them as leap year or NOT leap year. Your program must ask how many years you want to enter, for example, if the user enters 20, it must allow entering 20 years, remember that you must classify them by leap year or NOT leap year. According to your validation of leap year or Not, declare one vector to store leap years and another vector to store non-leap years. Finally visualize on the screen the 2 vectors, with the years classified
My code:
static void Main(string[] args)
{
int año, op, i;
int[] anoBisiesto;
int[] anoNoBisiesto;
Console.WriteLine("Parcial #2 - Jesus Palma");
Console.WriteLine("------------------------");
Console.WriteLine(" ");
Console.WriteLine("Clasificación de año bisiesto y no bisiesto. \n");
Console.Write("¿Cuántos años desea ingresar para clasificar?: ");
op = Int32.Parse(Console.ReadLine());
Console.Write("\n");
for (i = 0; i < op; i++)
{
anoBisiesto = new int[op];
anoNoBisiesto = new int[op];
Console.Write("Ingrese un año: ");
año = Int32.Parse(Console.ReadLine());
if (año % 4 == 0 && (año % 100 != 0 || año % 400 == 0))
{
Console.WriteLine("El año " + año + " Si es bisiesto \n");
anoBisiesto[op] = año;
}
else
{
Console.WriteLine("El año " + año + " No es bisiesto \n");
anoNoBisiesto[op] = año;
}
}
for (i = 0; i < op; i++)
{
Console.WriteLine("DATOS DEL VECTOR: ");
Console.WriteLine("--------------------- \n");
Console.Write("" + anoBisiesto[op] + " ");
}
}
But when printing the data that I saved in the array, it tells me "Local variable not assigned"
You have to take the variable out of the for and initialize it, because it may not enter the for and it would never be initialized and that is the error that can occur and the visual studio marks you.
The answer was quite simple, for the dimension of the array, when "How many years" we want to enter is displayed, they are stored in the variable
op
, then, I equal itnumeros
withop
, and the Array (Array) I declare it as an integer in the following way:int[] añosBisiestos = new int[numeros];
and the otherint[] añosNoBisiestos = new int[numeros];
, what this does is that the LeapYears and NonLeapYears Arrays have as dimension the number of years to be entered, and, if it is a leap year,añosBisiestos[i] = año;
the year is stored inside the arrayañosBisiesto
and inside this, thei
to indicate the position of the year, for example: LeapYears[0], LeapYears[1], etc, and at the end, to display the data stored in the array, a cycle identical to the first is made and the array is printed:Console.Write("[" + añosBisiestos[i] + "] \n");
I propose an alternative, you could use the IsLeapYear function that belongs to the DateTime class , this function returns true if the year is a leap year and false otherwise. And also use Linq .
So far, what we have done is declare an array of years which will save us all the years that we enter, regardless of whether or not it is a leap year.
We will now use Linq to determine which years are leap years and which years are not.
Here what is done is a Linq query , which will go through all the years of the original array and will ask if each year is a leap year, returning an array with all the leap years.
Subsequently, through a foreach , all the elements of the leap array are traversed and they are printed on the console.
For non-leap years it would be similar:
This time the Linq query will return the non-leap and we print them using a foreach as well .
To use Linq you must put in the usings: