I have a 2 dimensional list that includes student numbers where each row represents a different lecture. I want to find out if a student takes more than one lecture and if so turn it into an adjacency matrix.
List look like this:
18011011 18011024 18011055
18011022 18011024 18011034
18011011 18011023 18011045 18011100
And matrix should be like this:
0 1 1
1 0 0
1 0 0
Code:
for (int i = 0; i < Yollar.Count; i++)
{
for (int j = 0; j < Yollar[i].Count; j++)
{
for (int k = i + 1; k < Yollar.Count; k++)
{
for (int p = 0; p < Yollar[k].Count; p++)
{
if (Yollar[i][j] == Yollar[k][p])
{
AdjancencyMatrix[i, p] = 1;
}
}
}
}
}
for(int i = 0; i < Yollar.Count; i++)
{
for(int j = 0; j < Yollar[i].Count; j++)
{
for (int k =i +1; k < Yollar.Count; k++)
{
for(int p = 0; p < Yollar[k].Count; p++)
{
if (Yollar[i][j] == Yollar[k][p])
{
AdjancencyMatrix[i, k] = 1;
AdjancencyMatrix[k, i] = 1;
}
}
}
}
}
I figured out. I only need lecture indexes so if you use only row indexes and changed the AdjacencyMatrix indexes and problem solved
Related
While printing this jagged array I am getting the no of rows to be 1 less than expected. It should start with 0th index to (h-1)th index creating a total of h rows. What am I doing wrong?
h is the no. of rows.
int h = int.Parse(Console.ReadLine());
int[][] arr = new int[h][];
for(int i = 0; i < h; ++i)
{
arr[i] = new int[i+1];
}
for(int i = 0; i < h; i++)
{
Console.WriteLine();
for(int j = 0; j < i; j++)
{
Console.Write(arr[i][j] + " ");
}
}
That's because your inner for-loop has the condition j < i. If i is 0 in the first pass, the inner for-loop will not be passed.
Try it with
for(int j = 0; j < arr[i].Length; j++)
{
Console.Write(arr[i][j] + " ");
}
The arrays have a growing list of elements, starting with 1 so if you want to scan all the items:
for(int i = 0; i < h; i++)
{
Console.WriteLine();
for(int j = 0; j < (i + 1); j++)
{
Console.Write(arr[i][j] + " ");
}
}
Shouldn't it be:
new int[i];
Instead of:
new int[i+1]
Or is it h - 1? Just change that index of the array.
Or you need j < arr[i].Length
I'm a new student having a bit of trouble with this assignment, but the powerpoint notes and other online guides don't seem to help. If anyone can give me a pointer it would be much appreciated!
private static int[,] GenerateTT(int size)
{
int[,] table = new int[size,size];
for (int i = 1; i < size+1; i++)
{
for (int j = 1; j < i+1; j++)
{
table[i-1, j-1] = i * j;
}
}
return table;
}
private static void DisplayTT(int[,] table)
{
Console.WriteLine();
Console.WriteLine("Here is the times table for that size:");
Console.WriteLine();
for (int i = 1; i <= table.Length; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("{0}\t", table[i-1, j-1]);
}
Console.WriteLine("\n");
}
Console.WriteLine();
}
The output is supposed to be like this (if you enter 4 for example):
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
But what I'm getting is this, plus it crashes at DisplayTT(table):
1
2 4
3 6 9
4 8 12 16
here's the relevant part of the Main method if it helps.
int size = GetValue("Please enter the size (4-20) of the times table: ", 4, 20);
Console.WriteLine();
int[,] table = GenerateTT(size);
DisplayTT(table);
You can use GetLength(X) Property for multi dimensional arrays where X is index of dimension.
for (int i = 0; i < table.GetLength(0); i++)
{
for (int j = 0; j < table.GetLength(1); j++)
{
Console.Write("{0}\t", table[i, j]);
}
Console.WriteLine("\n");
}
You should change the inner loop hi-bound:
private static int[,] GenerateTT(int size)
{
int[,] table = new int[size,size];
for (int i = 1; i < size+1; i++)
{
for (int j = 1; j < i+1; j++) // <-- change i+1 to size+1
{
table[i-1, j-1] = i * j;
}
}
return table;
}
I'd rather keep the loops start from zero:
private static int[,] GenerateTT(int size)
{
int[,] table = new int[size,size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
table[i, j] = (i+1) * (j+1);
}
}
return table;
}
The following code sorts rows by the first element using bubble method.
I can't change it to counting sort.
public void SortStack(double[,] n)
{
for (int i = 0; i < n.GetLength(0) - 1; i++)
{
for (int j = i; j < n.GetLength(0); j++)
{
if (n[i, 0] > n[j, 0])
{
for (int k = 0; k < n.GetLength(1); k++)
{
var temp = n[i, k];
n[i, k] = n[j, k];
n[j, k] = temp;
}
}
}
}
}
Please help.
As you do bubble sort based on first element of each row. you should do counting sort like that too. so you just need to count first item of each row.
private static int[,] CountingSort2D(int[,] arr)
{
// find the max number by first item of each row
int max = arr[0, 0];
for (int i = 0; i < arr.GetLength(0); i++)
{
if (arr[i, 0] > max) max = arr[i, 0];
}
// we want to get count of first items of each row. thus we need 1d array.
int[] counts = new int[max + 1];
// do the counting. again on first index of each row
for (int i = 0; i < arr.GetLength(0); i++)
{
counts[arr[i, 0]]++;
}
for (int i = 1; i < counts.Length; i++)
{
counts[i] += counts[i - 1];
}
// result is sorted array
int[,] result = new int[arr.GetLength(0), arr.GetLength(1)];
for (int i = arr.GetLength(0) - 1; i >= 0; i--)
{
counts[arr[i, 0]]--;
// now we need to copy columns too. thus we need another loop
for (int j = 0; j < arr.GetLength(1); j++)
{
result[counts[arr[i, 0]], j] = arr[i, j];
}
}
return result;
}
Here is the test.
static void Main()
{
Random rand = new Random();
int[,] arr = new int[3,3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i, j] = rand.Next(0, 100);
}
}
int[,] newarr = CountingSort2D(arr);
for (int i = 0; i < arr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
for (int i = 0; i < newarr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < newarr.GetLength(1); j++)
{
Console.Write(newarr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
}
Example Output:
{ 49,66,8 },{ 33,39,64 },{ 65,52,76 } // Original array
{ 33,39,64 },{ 49,66,8 },{ 65,52,76 } // Sorted array
Ok, so lets say i have two arrays like these;
int[] wow = new int[50];
for (int j = 0; j < wow.Length; j++)
{
wow[j] = j + 1;
}
int[] wew = new int[50];
for (int i = 0; i < wew.Length; i++)
{
wew[i] = i + 10;
}
and i want to print them like;
1 , 11
2 , 12
3 , 13
for (int j = 0; j < wow.Length; j++)
{
wow1[j] = j + 1;
wow2[j] = j + 10;
//print wow1 & wow2 here.
Console.WriteLine("{0},{1}", wow1[j], wow2[j]);
}
Note that in your two loops, i is no different with j, they are essentially the same!
How about using two for-loops?
for(int i = 0; i < wow.Length;i++)
{
for(int j = 0; j < wew.Length;i++)
{
//Print
Console.WriteLine("{0} , {1}", wow[i].ToString(), wew[j].ToString());
}
}
Try this code in case both arrays are the same length
for (int i=0; i<wew.Length; i++)
{
Console.WriteLine(wow[i] + ", " + wew[i]);
}
If the length is different more logic is needed
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testing_random
{
class Program
{
static void Main(string[] args)
{
int n = 4;
int[,] a = new int[n,n];//declaring the matrix
Random o = new Random();
a[0,0] = o.Next(n);
for (int i = 1; i < n; i++)//filling the first line
{
int d = 1;
while (d != 0)
{
a[i,0] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i,0] == a[j,0])
d++;
}
}
for (int i = 1; i < n; i++)//filing the first column
{
int d = 1;
while (d != 0)
{
a[0, i] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[0, i] == a[0, j])
d++;
}
}
for (int k = 1; k < n; k++)//filling the rest of the matrix
for (int i = 1; i < n; i++)
{
int d = 1;
while (d != 0)
{
a[i, k] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i, k] == a[j, k])
d++;
for (int j = 0; j < k; j++)
if (a[i, k] == a[i, j])
d++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write("{0} ", a[i, j]);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
The output should be a matrix of 4*4 where each column and each line contains each number once.
The problem is when i run the code, not every time i get an output, i think the problem is not every set of first line and column can give a matrix as required the i get into an unending loop.
what i want to do is limit the running time of the application to 100 ms per example,so if the matrix is not filled,the program restarts
What piece of code am i missing?
replace the while( d != 0 ) with a loop which counts up to a certain very large maximum number of iterations. (Try 1000, 100000, whatever.)
Are you trying to randomly insert numbers between 1-4 in the first line of the array? If so there is a much easier way to do it.
You can generate the 4 numbers to be inserted into the array and then just look through the first line of the array and set each value.
Random rnd = new Random();
var randomNumbers = Enumerable.Range(1, 4).OrderBy(i => rnd.Next()).ToArray();
for (int i = 0; i < n; i++)
{
a[i, 0] = randomNumbers[i];
}