how to calculate totals and minimum spanning trees using arrays - c#

I'm working on a C# assignment in which i have to create a minimum spanning tree through the use of arrays. My code is made up of three arrays which track the nodes and whether or not they have been reached. I have to find the smallest total for all of the randomly generated links to be added up to visit all of the nodes. However, for some reason it is not quite working. The totals being produced are not correct and just wondered if anyone could help me work out why.I believe the error must be between the calculating of the total or how the minimum value is being decided or possible both however i am unsure.
So far i have tried changing the way it is calculated to involve a sum variable to store the values seperately. Ive attempted to add another if statement that would stop the min value from being anything above 98 ( the void value is 99) i have also tried to alter the earlier code to see if how i am testing the values in the SP array are the desired ones. still no results
int n = 5; //n = number of values
int m = 50; //m = max value in arra
int VoidValue = 99; // if i = j value = void value
int Total = 0; //Total value for spanning tree
int sum = 0; //Sum for total value for spanning tree
Random Rand = new Random(); //Create randomise value
int[,] c = new int[n + 1, n + 1]; //Cost array
int[] SP = new int[n + 1]; //Spanned array
int[,] AD = new int[n + 1, n + 1]; //Adjacency array
for (int i = 1; i <= n; i++)
{
SP[i] = 0;
for (int j = 1; j <= n; j++)
{
if (i == j)
{
c[i, j] = VoidValue; // give void spaces the value of 99
AD[i, j] = 0;
}
else
{
c[i, j] = Rand.Next(1, m); // Populate the array with randomised values
AD[i, j] = 0;
}
}
}
//Output all arrays to screen
Console.WriteLine("Cost Array: At the beginning");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
{
Console.Write("c[" + i + "," + j + "] = " + "- " + " ");
}
else
{
Console.Write("c[" + i + "," + j + "] = " + c[i, j].ToString("00") + " ");
}
}
Console.WriteLine();
}
Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("Spanned Array : At the beginning");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
Console.Write("S[" + i + "] = " + SP[i].ToString("00") + " ");
Console.WriteLine();
}
Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("Adjacency Array : At the beginning");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
{
Console.Write("A[" + i + "," + j + "] = " + "- " + " ");
}
else
{
Console.Write("A[" + i + "," + j + "] = " + AD[i, j].ToString("00") + " ");
}
}
Console.WriteLine();
}
Console.ReadLine();
// Random Starting Point
int startPoint = Rand.Next(1, n + 1);
Console.WriteLine("Start at node " + startPoint);
SP[startPoint] = 1;
//Check the spanned array
Console.WriteLine("");
Console.WriteLine("Spanned Array : After the starting point has been chosen ");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
Console.Write("S[" + i + "] = " + SP[i].ToString("00") + " ");
Console.WriteLine();
}
Console.ReadLine();
// Find minimum vallue link, repeatedly follow these links until spanned array is full
for (int p = 1; p < n; p++) // For every value of the spanned array
{
int MinValue = VoidValue, MinValuei = 0, MinValuej = 0; // declare variables
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++) // I and J are for both variables of the cost and adjacent array
if (i != j)
{
if (SP[i] == 1) //Spanned node
{
if (SP[j] == 0) //Unspanned node
{
if (c[i, j] < MinValue)
{
MinValue = c[i, j];
MinValuei = i;
MinValuej = j;
}
}
}
}
AD[MinValuei, MinValuej] = 1;
SP[MinValuej] = 1;
}
Console.WriteLine("");
Console.WriteLine("The min value is: " + MinValue);
Total = Total + MinValue;
Console.WriteLine("");
Console.WriteLine("The total is: " + Total);
}
//Finally output spanned and adjacent arrays
Console.WriteLine("");
Console.WriteLine("Spanned Array: After spanning tree");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
Console.Write("S[" + i + "] = " + SP[i].ToString("00") + " ");
Console.WriteLine();
}
Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("Adjacency Array : After spanning tree");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
{
Console.Write("A[" + i + "," + j + "] = " + "- " + " ");
}
else
{
Console.Write("A[" + i + "," + j + "] = " + AD[i, j].ToString("00") + " ");
}
}
Console.WriteLine();
}
Console.ReadLine();
//Output total value for the spanning tree
Console.WriteLine("");
Console.WriteLine("The total value for the spanning tree: " + Total);
Console.WriteLine("");
Console.ReadLine();

Related

Two dimensional array feeding

need to do an arr[n,n] which will do a result as [0,0] = 0 [0,1] = 1 [0,2] = 3 [0,3] = 6 [1,1] = 0 [1,2] = 2 [1,3] = 5 [2,2] = 0 [2,3] = 3 [3,3] = 0
Trying to feed that arr with two for cycles. Anyway, can't figure out how to set conditions to generate it as I want it.
Any hint is welcome.
I tried to feed an array with two for loops, where I tried to sum values. So many errors occur when I start typing code.
E: Need to solve last if (else if (i > j && j == 0)), problem is out of bounds. Any idea? Don't want to shuffle everything. Need to calculate [1-4,0] value.
== [4,0] = 5, [3,0] = 9, [2,0] = 12, [1,0] = 14
Console.Write("Zadejte n: ");
int n = Convert.ToInt32(Console.ReadLine()) ;
int[,] array_prava = new int[n, n];
int[,] array_leva = new int[n, n];
int pulka = n / 2;
for (int i = 0; i < n; i++) // array_prava
{
for (int j = 0; j < n; j++)
{
if (i == j)
{
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j];
Console.WriteLine("[" + i + "," + j + "] " + array_prava[i, j]);
}
else if (j > i)
{
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j - 1] + i;
Console.WriteLine("11[" + i + "," + j + "] " + array_prava[i, j]);
}
else if (i > j && j == 0){
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j - 1] + i;
Console.WriteLine("22[" + i + "," + 0 + "] " + array_prava[i, 0]);
}
}
// Console.Write("----------------------------\n");
}
any hint is welcome
Got it.
for (int i = 0; i < n; i++) // array_prava
{
for (int j = 0; j < n; j++)
{
if (i == j)
{
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j];
Console.WriteLine("[" + i + "," + j + "] " + array_prava[i, j]);
}
else if (j > i)
{
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j - 1] + i;
Console.WriteLine("11[" + i + "," + 0 + "] " + array_prava[i, 0]);
}
}
}

Displaying an arrays index number

I need help with C#
I'm currently trying to find the minimum value in a 2D array. I have managed to do this however, after I have found the minimum value I want the corresponding values index number (Where it is stored in my 2D array) to be output. For example, I have a 2D and a 1D array. Once the minimum value has been discovered the index value for the 2D array needs to be changed in the 1D array.
The 2d array is c[i,j]
and the 1D array is a[i]
so how would i be able to display the j number in my c array in my a array. For example, if my minimum value was at c[1,5] I want the value in a[5] to be changed from 0 to 1. Any help would be great thanks!
P.S. if ive made this sound really confusing I apologise I'm new to this !
int n = 5, m = 10, MinValue = 100, MaxValue = 1, Total = 0, Sum = 0; //n = number of values m = max value in array MinValue = Lowest number in array
Random Rand = new Random();
int[] A = new int[n + 1];
for (int i = 1; i < n+1; i++) //Reached and unreached array - creation
{
A[i] = 0;
}
A[1] = 1;
int[,] c = new int[n + 1, n + 1]; //Create array
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
c[i, j] = Rand.Next(1, m); //Randomise the array
if (i == j)
{
c[i, j] = 99; // give void spaces the value of 99
}
if (c[i, j] > MaxValue && c[i, j] < 99)
{
MaxValue = c[i, j]; // max value takes the highest value (that isnt 99)
}
}
}
for (int p = 1; p <= n; p++)
{
for (int i = 1; i <= n+1; i++)
{
for (int j = 1; i <= n+1; i++)
{
if (c[i, j] <= MinValue)
{
if (A[i] == 1)
{
if (A[i] == 0)
{
Total = Sum + MinValue;
Sum = Total;
A[i] = 1;
}
}
}
}
}
}
Console.WriteLine("Total Value = " + Total);
Console.WriteLine("");
Console.WriteLine("The tracking array - what has been reached and what hasn't"); // Output reaching array
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
Console.WriteLine("A[" + i + "] = " + A[i].ToString("00") + " ");
//Output the array to screen
}
Console.WriteLine("");
Console.WriteLine("The link length array"); // Output link length array
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
Console.Write("c[" + i + "," + j + "] = " + c[i, j].ToString("00") + " ");
}
Console.WriteLine();
//Output the array to screen
}
Console.WriteLine("");
Console.ReadLine();

My code won't do the sum; only the elements of the matrix appear and then nothing happens

namespace Suma_diagonala_secundara
{
class Program
{
static void Main(string[] args)
{
int n, i, j, s = 0;
Console.Write("n= ");
n = Convert.ToInt32(Console.ReadLine());
int[,] tab = new int[n, n];
for(i=0;i<n;i++)
for (j = 0; j < n; j++)
{
Console.Write("tab[{0}][{1}]= ", i + 1, j + 1);
tab[i, j] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nElementele matricii sunt: ");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
Console.Write("{0} ", tab[i, j]);
Console.WriteLine("");
}
Console.WriteLine("Suma elementelor de pe diagonala secundara este: ");
for (i = 0; i < n; i++)
{
s = s + tab[i, n - i + 1];
}
Console.ReadKey();
}
}
}
In your loop
for (i = 0; i < n; i++)
{
s = s + tab[i, n - i + 1];
}
you are accessing the array out of bounds, since n - 0 + 1 = n + 1 is larger than n - 1 (the largest index in tab).
What you actually want is (note the parentheses)
for (i = 0; i < n; i++)
{
s = s + tab[i, n - (i + 1)];
}
The following line:
s = s + tab[i, n - i + 1];
Is throwing an IndexOutOfRangeException because you are requesting an index higher than the array capacity.
The correct loop code is (with decrement instead increment)
for (i = 0; i < n; i++)
{
s = s + tab[i, n - i - 1];
}

Misunderstanding With If and Else

I'm new to C# and would like some help!
I am working on some code that allows the user to find out if specific cows on his farm aren't producing enough milk.
On line 75 the if statement is supposed to print out the cows that 'are not good enough' or Tell the user that everything is OK. But instead it permanently try's to print the Bad Cows.
Console.WriteLine("How many cows are in your herd?");
int CowNum = int.Parse(Console.ReadLine());
int Temp;
double TempD;
string TempS;
int MinimumVal = 6;
int MDIR = 4;
string[] BadList = new string[CowNum];
int[] Counter = new int[CowNum];
double[] Total = new double[CowNum];
string[] Days = new string[7] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
string[] CowID = new string[CowNum];
double[,] CowYield = new double[CowNum, 7];
Random r = new Random();
for (int n = 0; n < CowNum; n++) // Sets Cow ID's
{
Console.WriteLine("What is the ID of Cow: " + (n + 1) + "?" );
TempS = (Console.ReadLine());
CowID[n] = TempS;
}
for (int n = 0; n < CowNum; n++) // Sets the Yield of each cow to a certin day
for (int x = 0; x < Days.GetLength(0); x++)
{
Console.WriteLine("What was the Yeild for Cow: " + CowID[n] + " on " + Days[x] + "?");
TempD = double.Parse(Console.ReadLine());
CowYield[n, x] = TempD;
if (TempD < MinimumVal)
{
Counter[n] = Counter[n] + 1;
Console.WriteLine(Counter[n]);
}
//Console.WriteLine("What was the Yeild for Cow: " + CowID[n] + " on " + Days[x] + "?"); //Randomly Generated 'Saves Time'
//Temp = r.Next(0, 20);
//Console.WriteLine(Temp);
//CowYield[n, x] = Temp;
}
for (int n = 0; n < CowNum; n++)
for (int x = 0; x < Days.GetLength(0); x++)
Total[n] = Total[n] + CowYield[n, x];
for (int n = 0; n < CowNum; n++)
{
if (Counter[n] > MDIR)
{
BadList[n] = CowID[n];
Console.WriteLine("asd" + BadList[n]);
}
}
int index = Array.IndexOf(Total, Total.Max()); // Gets index of Highest producing cow
TempS = CowID[index];
Console.WriteLine("\nThe Highest producing cow is Cow: " + TempS + ". with a whopping " + Total[index] + "L of Milk!\n");
if (BadList.GetLength(0) > 0)
{
Console.WriteLine(BadList.GetLength(0));
for (int n = 0; n < BadList.GetLength(0); n++)
{
Console.WriteLine(BadList[n]);
}
}
else
{
Console.WriteLine("None of your cows had less than 6L of milk for four or more days in a row!");
}
Console.ReadLine();
}
In short your code fails beause
(For ease of all of us, heres a simplified one)
string[] thing = new string[200]
if (thing.Length>0)
{ Console.WriteLine("All wrong");}
else
{ Console.WriteLine("OK");}
Your Length of thing is always 200 because you made it that big. So output is always "All wrong"
However if you had
List<String> thing = new List<String>();
//process list here, and use thing.Add(badcow)
if (thing.Count() >0 )
{ Console.WriteLine("All wrong");}
else
{ Console.WriteLine("OK");}
then it will work because there maybe 0 entries in the list. It may produce both answers and will pick as you expected.

its showing error arrays dont have to many dimension,index out of bound;

It's showing arrays don't have to many dimensions.
string[][] a = new string[10][];
int bound0 = a.GetUpperBound(0);
int bound1 = a.GetUpperBound(1);
for (int i = 1; i <= bound0; i++)
{
for (int x = bound0; x >1; x--)
{
Console.Write Line(" Page-- " + i + " -- PAGE " + bound0);
bound0--;
x--;
i++;
}
}
Actually I have done this in using for loops requirement is to use array in my app here is for loop:
for (int i = 1; i < bound0; i++)
{
for (int j = bound0; j > 1; j--)
{
Console.WriteLine(" Page-- " + i + " -- PAGE " + bound0);
bound0--;
j--;
i++;
string[][] is an Array of String Arrays.In other words: jagged array.Not two dimensional array.If you want two dimensional array use:
string[,] array = new string[10,10];
Also see the documentation

Categories