Skip to content

Instantly share code, notes, and snippets.

@jpolivra
Created October 20, 2023 21:43
Show Gist options
  • Save jpolivra/9f83da26c4f653b5f7efb826f4e13c7e to your computer and use it in GitHub Desktop.
Save jpolivra/9f83da26c4f653b5f7efb826f4e13c7e to your computer and use it in GitHub Desktop.
Vector class with default/custom initializer length. Vector methods: Generate - Populate the vector; List - List vector values; Insert - Update vector position with provided value; ExtractValue - Extract specific position value; ExtractEdges - Extract lower and higher number value present into vector.
using System;
namespace abstract_data_types;
class Program
{
class Vector {
private int[] _V;
private Random random = new Random();
public Vector()
{
_V = new int[10];
}
public Vector(int N)
{
_V = new int[N];
}
public void Generate()
{
for(int i = 0; i < _V.Length; i++)
{
_V[i] = random.Next();
}
}
public void List()
{
foreach(int x in _V){
Console.WriteLine(x);
}
}
public void Insert(int Position, int Value)
{
if(Position >= _V.Length)
{
Console.WriteLine("Invalid position!");
}
else
{
_V[Position] = Value;
}
}
public void ExtractValue(int Position)
{
if(Position >= _V.Length)
{
Console.WriteLine("Invalid position!");
}
else
{
Console.WriteLine($"Extracted value: {_V[Position]}");
}
}
public void ExtractEdges()
{
int LowerEdge = _V[0];
int HigherEdge = _V[0];
for(int i = 1; i < _V.Length; i++)
{
if(_V[i] < LowerEdge)
{
LowerEdge = _V[i];
}
if(_V[i] > HigherEdge)
{
HigherEdge = _V[i];
}
}
Console.WriteLine($"Lower edge equal: {LowerEdge}");
Console.WriteLine($"Higher edge equal: {HigherEdge}");
}
}
static void Main(string[] args)
{
Vector V = new Vector();
V.Generate();
V.List();
V.ExtractEdges();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment