Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Well I've started doing a project on C#, and it has object (class called CLA). CLA creates a set(list).
I will do function on List<Cla> and which needs HighValue, LowValue and so on. But ArgumentOutOfRangeException got in here.
Can i call method in constructor? Is this code correct?
class Program
{
static void Main(string[] args)
{
List<Cla> ClaList = new List<Cla>();
ClaList.Add(new Cla("a", 10.3, "Priznak1", 1));
ClaList.Add(new Cla("b", 3.3, "Priznak1", 1));
ClaList.Add(new Cla("c", 7.3, "Priznak1", 1));
ClaList.Add(new Cla("d", 9.3, "Priznak1", 1));
ClaList.Add(new Cla("e", 8.3, "Priznak1", 1));
CritListSmall NewTest = new CritListSmall(ClaList);
Console.WriteLine("ddd {0}", NewTest.HighValue);
}
}
class CritListSmall
{
public double HighValue;
public double LowValue;
public CritListSmall(List<Cla> p)
{
HighValue = p[0].ObjectValue;
LowValue = p[0].ObjectValue;
int NumberElements;
NumberElements = p.Count;
for (int i = 1; i <= NumberElements; i++)
{
double m = p[i].ObjectValue; //Exception occurs here
if (HighValue < m)
HighValue = m;
}
for (int l = 1; l <= NumberElements; l++)
{
double n = p[l].ObjectValue; //and here
if (LowValue > n)
LowValue = n;
}
}
public class Cla
{
public string ObjectName;
public double ObjectValue;
public string PriznakName;
public int ClassNumber;
public Cla(string on, double ov, string pn, int cn)
{
ObjectName = on;
ObjectValue = ov;
PriznakName = pn;
ClassNumber = cn;
}
public double CritValue;
public double ExpValue;
public bool Outlier;
public double AbsoluteValue;
}
}
In C# collections have zero-based indexes. It means if you have 5 items in list, their indexes will be 0,1,2,3,4. So instead of
for(int i = 1; i <= NumberElements; i++)
you should use
for(int i = 0; i < NumberElements; i++)
^ ^
from zero and less than count of items
In C# array indexes start at 0, not 1, so you need to change your loops to be this:
for (int i = 0; i < NumberElements; i++)
And:
for (int l = 0; l < NumberElements; l++)
for(int i = 1; i <= NumberElements; i++)
That is your problem. It must be:
for(int i = 0; i < NumberElements; i++)
for(int l = 0; l < NumberElements; l++)
Why? NumberElements is the count of p, for example 10. But the index is from 0 to 9.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
It gives a 1503 error, but I can’t fix it. It gives an error from Array.Sort(
valuePerWeight, weights, values); in the values variable. How can I fix it? I tried to write values in front of weights but it doesn’t work
class Program
{
static void Main(string[] args)
{
int[] data = ReadDataFromFile("items.txt");
int capacity = data[0];
int[] weights = new int[data.Length - 1];
int[] values = new int[data.Length - 1];
for (int i = 1; i < data.Length; i++)
{
weights[i - 1] = data[i];
values[i - 1] = data[++i];
}
int[] selectedItems = GreedyKnapsack(capacity, weights, values);
int totalValue = 0;
Console.WriteLine("Selected items:");
for (int i = 0; i < selectedItems.Length; i++)
{
if (selectedItems[i] == 1)
{
Console.WriteLine($"Weight: {weights[i]}, Value: {values[i]}");
totalValue += values[i];
}
}
Console.WriteLine($"Total value: {totalValue}");
}
it gives an error in array.sort
static int[] GreedyKnapsack(int capacity, int[] weights, int[] values)
{
int n = weights.Length;
int[] selected = new int[n];
double[] valuePerWeight = new double[n];
for (int i = 0; i < n; i++)
{
valuePerWeight[i] = (double)values[i] / weights[i];
}
Array.Sort(
valuePerWeight, weights, values);
int weight = 0;
for (int i = n - 1; i >= 0; i--)
{
if (weight + weights[i] <= capacity)
{
selected[i] = 1;
weight += weights[i];
}
}
return selected;
}
}
}
You are getting CS1503 because you are passing an array of int Values when you should be passing an IComparer<int>.
From you question, it´s not clear what's the objective of your method.
If you want to sort valuePerWeight, weights and values, you can just sort them seperately:
Array.Sort(valuePerWeight)
Array.Sort(weights)
Array.Sort(values)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I made a two dimensional array in c # with random numbers but I want the numbers are not repeated
For example, a successful output giving the input 4 (x), 4 (y), 15 (maxElem), would be:
14|8|1|7
3|13|2|4
2|6|12|8
10|9|4|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TwoDimensionArray
{
class Program
{
static void Main(string[] args)
{
int x = 4;
int y = 4;
int[,] z = new int[x, y];
Random r = new Random();
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
z[i, j] = r.Next(1, 15);
}
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (i == 3 && j == 3)
{
Console.Write(" ");
break;
}
else
{
if (z[i, j] > 9)
Console.Write(z[i, j] + " ");
else
Console.Write(z[i, j] + " ");
}
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
Generate an array containing the valid numbers, then use a shuffle algorithm to randomize the order in the array, and then finally populate your two-dimensional array by retrieving in order the values from your shuffled array.
Try doing it like this:
int x = 4;
int y = 4;
int[,] z = new int[x, y];
Random r = new Random();
var values =
Enumerable
.Range(1, x * y)
.OrderBy(n => r.Next())
.ToArray();
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
z[i, j] = values[i * 4 + j];
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm currently doing an assignment where I need to find the errors in a program that is preventing it from executing. Its a comparison sort program where there are two sorting methods being compared, Merge Sort and Counting Sort. Its giving me the "No overload for (insert method), takes 1 arguments" message, but I'm unable to find or fix what's giving it the errors, any general idea or fixes to make the comparison work would be appreciated.
using System;
using System.Collections;
namespace Sort
{
class Program
{
static void Main(string[] args)
{
Random rdm = new Random();
ArrayList data = new ArrayList();
ArrayList dataB = new ArrayList();
int size = 50000;
int[] dta = new int[size];
int[] dta50 = new int[50];
int randomNum;
DateTime startTime, endTime;
int i;
// Show both methods worked
for (i = 0; i < 50; i++)
{
randomNum = rdm.Next(100);
data.Add(randomNum);
dta50[i] = randomNum;
}
dataB = CountingSort(data);
PrintArray("Merge Sort:", dataB);
MergeSort(dta50);
Console.WriteLine();
Console.WriteLine("\nCounting Sort:");
Console.WriteLine();
for (int x = 0; x < 50; x++)
Console.Write(dta50[x] + " ");
Console.WriteLine();
Console.WriteLine("\nCounting Sort\tMerge Sort");
for (int n = 1; n < 20; n++) // run 20 times
{
data.Clear();
for (i = 0; i < size; i++)
{
randomNum = rdm.Next(size);
data.Add(randomNum);
dta[i] = randomNum;
}
// Counting Sorting
startTime = DateTime.Now;
CountingSort(data);
endTime = DateTime.Now;
TimeSpan howlong = endTime.Subtract(startTime);
Console.Write(" {0}ms", howlong.Milliseconds);
// Merge Sorting
startTime = DateTime.Now;
MergeSort(dta);
endTime = DateTime.Now;
TimeSpan howlong1 = endTime.Subtract(startTime);
Console.WriteLine("\t\t{0}ms", howlong1.Milliseconds);
}
Console.ReadLine();
}
public static void MergeSort(int[] input, int left, int right)
{
if (left < right)
{
int middle = (left + right) / 2;
MergeSort(input, left, middle);
MergeSort(input, middle + 1, right);
//Merge
int[] leftArray = new int[middle - left + 1];
int[] rightArray = new int[right - middle];
Array.Copy(input, left, leftArray, 0, middle - left + 1);
Array.Copy(input, middle + 1, rightArray, 0, right - middle);
int i = 0;
int j = 0;
for (int k = left; k < right + 1; k++)
{
if (i == leftArray.Length)
{
input[k] = rightArray[j];
j++;
}
else if (j == rightArray.Length)
{
input[k] = leftArray[i];
i++;
}
else if (leftArray[i] <= rightArray[j])
{
input[k] = leftArray[i];
i++;
}
else
{
input[k] = rightArray[j];
j++;
}
}
}
}
private static int[] CountingSort(int[] arr, int min, int max)
{
int[] count = new int[max - min + 1];
int z = 0;
for (int i = 0; i < count.Length; i++) { count[i] = 0; }
for (int i = 0; i < arr.Length; i++) { count[arr[i] - min]++; }
for (int i = min; i <= max; i++)
{
while (count[i - min]-- > 0)
{
arr[z] = i;
z++;
}
}
return arr;
}
static void PrintArray(string title, ArrayList dt)
{
int cc = 0;
Console.WriteLine("\n" + title + "\n");
foreach (int item in dt)
{
if (cc < 10)
Console.Write("{0,4}", item);
else
{ Console.WriteLine(); cc = 0; }
}
}
}
}
Look at your functions:
public static void MergeSort(int[] input, int left, int right)
private static int[] CountingSort(int[] arr, int min, int max)
They each expect three values when you invoke them. Look at how you invoke them:
MergeSort(dta50);
CountingSort(data);
MergeSort(dta);
You're supplying one value, not three.
Elsewhere you invoke them differently:
MergeSort(input, left, middle);
MergeSort(input, middle + 1, right);
In those cases you supply three values.
You need to supply the functions with the values it requires. Otherwise the code has no way of knowing what to do.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Example:
2,3,0,1,-5,10,11,12
The largest length of positive numbers is 3.
I have the code to check the array but do not understand how exactly i can make an int to hold the length and than restart if for example the sequence stops.
int counter = 0;
int longestCounter = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] > 0) counter++;
else
{
if ( counter > longestCounter ) longestCounter = counter;
counter = 0;
}
}
if ( counter > longestCounter ) longestCounter = counter;
Just adding this answer as an alternative. You can simply use a for-loop like this (very similar to the other answers, just more compact):
int longestLen = 0, currentLen = 0;
for(int i = 0; i < array.Length; i++) {
currentLen = array[i] > 0 ? currentLen + 1 : 0;
longestLen = Math.Max(currentLen, longestLen);
}
Console.WriteLine(longestLen); // 3
Or Linq, like this:
int longestLen = array.Aggregate(
new { c = 0, m = 0 },
(x, n) => new { c = n = (n > 0 ? x.c + 1 : 0), m = Math.Max(n, x.m) },
x => x.m);
Console.WriteLine(longestLen); // 3
// similar to the "Sliding Windows Algorithm" in "Programming Pearls"
int i, current, maxSoFar;
current = 0;
maxSoFar = 0;
for (i = 0; i < a.Length; i++)
{
if (a[i] > 0)
{
current = current + 1;
if (current > maxSoFar)
{
maxSoFar = current;
}
} else {
current = 0;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
This is how my class is designed:
class MergeSort
{
int[] mArray;
public MergeSort(int[] A)
{
mArray = A;
}
void Merge(int[] A,int p ,int q, int r)
{
int n1 = q - p + r;
int i = 0, j = 0;
int n2 = r - q;
int[] left = new int[n1+1];
int[] right = new int[n2+1];
for ( i = 0; i < n1; i++)
left[i] = A[p + i - 1];
for ( i = 0; i < n2; i++)
right[i] = A[q + i];
left[n1] = Int32.MaxValue;
right[n2] = Int32.MaxValue;
i = j = 0;
for (int k = p; k < r; k++)
{
if (left[i] <= right[j])
{
A[k] = left[i];
i++;
}
else
{
A[k] = right[j];
j++;
}
}
}
public void Merge_Sort(int[] A, int p, int r)
{
int q = 0;
mArray = A;
if(p<r)
{
q = (p+r)/2;
Merge_Sort(A,p,q);
Merge_Sort(A, q + 1, r);
Merge(A,p,q,r);
}
}
public string show()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mArray.Length; i++)
{
sb.Append(mArray[i].ToString() + " ");
}
return sb.ToString();
}
}
And this is how i go about sorting:
int[] arr = { 10, 12, 5, 6, 30, 1, 11, 120, 12 };
MergeSort ms = new MergeSort(arr);
ms.Merge_Sort(arr, 0, arr.Length );
MessageBox.Show(ms.show());
But i keep getting the error Index was outside the bounds of the array. on many places in my Merge function. I have tried to implement the algorithm as stated in the book by CLRS(Coremen). But i keep getting this error, i have been at it since 3 days - please help.
You've an error in line left[i] = A[p + i - 1]; because you're passing wrong values for your parameters. Indeed, in that line, p is zero. In the first execution of loop, having i=0 the resulting position in the A array is -1, that causes the IndexOutOfRange exception, as shown below: