I need to introduce two lists of 15 numbers, each one in an array, to later calculate the sum of each array and compare them with each other. I have used the following code:
using System;
using System.Linq;
namespace Ejercicio_24_alt
{
class Program
{
static void Main(string[] args)
{
//VARIABLES
int[] array1 = new int[15];
int[] array2 = new int[15];
int suma1 = 0, suma2 = 0;
Console.WriteLine("Introduce dos lista de 15 valores: ");
Console.WriteLine("Introduce los datos de la lista 1: ");
loop(array1, suma1);
Console.WriteLine("Ahora introduce los datos de la lista 2:");
loop(array2, suma2);
if (suma1 > suma2) Console.WriteLine($"La suma de la lista 1 es mayor a la suma de la lista 2.");
else Console.WriteLine($"La suma de la lista 2 es mayor a la suma de la lista 1.");
}
static int loop(int[] array, int suma)
{
for (int i = 0; i < 15; i++)
{
Console.WriteLine($"Número {i + 1}: ");
int num = int.Parse(Console.ReadLine());
array.SetValue(num, i);
//Alternativamente
// array[i]=int.Parse(Console.ReadLine());
}
return suma = array.Sum();
}
}
}
I have used it alternatively array[i]=int.Parse(Console.ReadLine());
but in one way or another it always returns 0 in the sum of each array. In fact, if it is done Console.WriteLine(suma1);
after declaring in the Main, the loop method prints 0.
The problem is that you validate
if (suma1 > suma2)
, but your values forsuma1
ysuma2
are always 0, so that's why it always prints 0.Now, your method
static int loop(int[] array, int suma)
is a static method that returns a valueint
that is the value of the sum of the values in the array. But unfortunately you execute the method but you don't receive the value of the sum.Fix it by assigning it the already defined variables of
suma1
andsuma2
. Something like:I recommend you make use of checkpoints to debug your code so you can identify the problems you have by testing line by line.
In programming there is a principle that in essence says that a method or a class should only do what corresponds to it and nothing else, your loop method should only focus on filling the array since this is its only responsibility, so the sum of the array should not be done inside this method, so your loop method would look like:
As you can see, this new loop method changed in its implementation, now it does not return any value and only receives the array of numbers as a parameter.
So your original code could look like:
Here you call the loop method to fill both arrays, and later you would get the sum of these arrays (you could well make a method whose only responsibility is to calculate the sum of the elements of an array, I didn't do it that way but it's a good idea). You would then ask if the sum of the elements of list 1 is greater than the sum of the elements of list 2, if the sum of the elements of list 2 is greater than the sum of the elements of list 1, or if Both sums are equal.
Making each method or class compliant with the single responsibility principle ensures greater code readability, greater reusability, easier code maintenance, and many other benefits.