Skip to content

Instantly share code, notes, and snippets.

@jpolivra
Created October 16, 2023 22:01
Show Gist options
  • Save jpolivra/2a11be30c70a45272a6d703a5677bd60 to your computer and use it in GitHub Desktop.
Save jpolivra/2a11be30c70a45272a6d703a5677bd60 to your computer and use it in GitHub Desktop.
Abstract data types: vectors.
using System;
public class AbstractDataTypes
{
public static void Main(string[] args)
{
int[] Vector = new int[5];
for(int i = 0; i < 5; i++)
{
Console.Write($"Insert the {i + 1}o element of the vector: ");
Vector[i] = int.Parse(Console.ReadLine());
}
int Add;
int Count;
Add = AddEven(Vector);
Count = CountOdd(Vector);
Console.WriteLine();
Console.WriteLine($"Addition of even numbers: {Add}");
Console.WriteLine($"Amount of odd numbers: {Count}");
Console.ReadKey();
}
// Functions
static int AddEven(int[] V)
{
int S = 0;
for(int i = 0; i < V.Length; i++)
{
if(V[i] % 2 == 0)
S += V[i];
}
return S;
}
static int CountOdd(int[] V)
{
int Q = 0;
for(int i = 0; i < V.Length; i++)
{
if(V[i] % 2 != 0)
Q += V[i];
}
return Q;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment