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
Hey guys I am a beginner at coding and I'm doing a linear search in c# and can't figure out how to make it show in what array the number was found when doing the search. It just makes it say that it's on the last array. Here's my code:
Random Generator = new Random();
int[] array = new int[100];
int count = 0;
for (int i = 0; i < array.Length; i++)
{
array[i] = Generator.Next(1, 100);
count++;
}
Console.WriteLine("Write a number between 1-100 and see if it's in a array and in what array.");
int guess = Convert.ToInt32(Console.ReadLine());
bool exists = array.Contains(guess);
if (exists)
{
Console.WriteLine("Your number {0} is in the {1}th Array.", guess, count);
}
else
{
Console.WriteLine("Your number {0} does not match any number in any of the Arrays.", guess);
}
Console.ReadKey();
There's something wrong with my count int, but I don't know what to do. Thanks in advance.
I think you are looking for Array.IndexOf
Int index = Array.IndexOf(amountOfArrays,guess);
If (index == -1)
{
// not found
}
else
{
// found
}
Count++ will be the last. How would you expect it to be anything else?
Related
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 1 year ago.
Improve this question
I am new to c#, but have been coding in java before. I downloaded visual studio code and the c# extension, and it seems to be working when I do Console.WriteLine("Example"); yet when I have a for loop it seems to not run it. I have a really simple sum calculator, but it doesn't work:
static void Main(string[] args)
{
int sum = 0;
Console.Write("Started Program");
Console.WriteLine("...");
for (int i = 0; i < 10; i++)
{
Console.Write("Choose numbers you want to add: ");
int num = Console.Read();
sum = sum + num;
}
Console.WriteLine(sum);
}
Its probably passing the .read input as ASCII characters instead of the actual int ... try changing;
int num = Console.Read();
to
int num = Int32.Parse(Console.ReadLine());
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 1 year ago.
Improve this question
for(int i = 1; i != amount+1; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Append(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Append(Convert.ToDecimal(Console.ReadLine()));
}
(This section is what is affected)
It gets the inputs from the user (I.E. it gets the names and grades, though it doesn't add to the array. I am new to C#, coming from python. Do you guys have any idea how I could fix this issue? TIA
Are you looking for something like this?
using System.Collections.Generic;
int amount = 5;
List<string> people = new List<string>();
List<decimal> grades = new List<decimal>();
for (int i = 0; i < amount; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Add(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Add(Convert.ToDecimal(Console.ReadLine()));
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to count sum of the numbers found in a string, not digits. For example, there are string = "abc12df34", and the answer must be 46 (12+34), not 10. Also maybe negative numbers for string = "abc10gf-5h1" answer must be 6. I can not understand how to implement this.
RegEx approach:
string input = "abc10gf-5h1";
int result = Regex.Matches(input, "-?[0-9]+").Cast<Match>().Sum(x =>int.Parse(x.Value));
While the above answer is elegant, it's not really something to understand what to do or how to approach the problem. Here is another, more explicit solution.
In words, you iterate through your string, collect digits as long as there are any, if you find a nondigit, your number is finished, you convert the number to integer and sum it up, clear the number string. The same you do if you find the end of the string. On the next found digit you start collecting digits again.
This algorithm will fail on any number larger than than 10 digits in multiple ways (as the other answer will also), but this is just for demonstration anyway.
string input = "abc10gf-5-1h1";
var number = new char[10];
int numberlength = 0;
int pos = 0;
int sum = 0;
while (pos < input.Length)
{
char c = input[pos++];
if (char.IsDigit(c))
{
number[numberlength++]=c;
}
else
{
if (numberlength > 0)
{
sum += int.Parse(new String(number, 0, numberlength));
numberlength = 0;
}
if (c=='-')
number[numberlength++]=c;
}
}
if (numberlength > 0)
sum += int.Parse(new String(number, 0, numberlength));
Console.WriteLine(sum);
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 6 years ago.
Improve this question
In my organization, users must generate a password from numbers only (keypads are used to access), minimum length is 8 numbers. How can I make sure the password the user generats is not too weak (using c# on server procossing password change request), applying the following rule:
3 following numbers (even a part of password) are not sequential or repeated (9451238401 or 543555784)
The regular expression is:
^((?!(?<ch>.)\k<ch>\k<ch>)(?!012|123|234|345|456|567|678|789|890)[0-9]){8,}$
The (?!(?<ch>.)\k<ch>\k<ch>) will check for the same character repeated thrice. Note that for the various contiguous sequences I had to put them in a list of possible sequences, (?!012|123|234|345|456|567|678|789|890). [0-9] is the character that will be accepted as valid. The {8,} is for the minimum length.
If you want a general-purpose approach which tells you the number of repeated, ascending and descending digits:
static void checkStrength(string text, out int maxRepeats, out int maxAscending, out int maxDescending)
{
maxRepeats = 0;
maxAscending = 0;
maxDescending = 0;
int currRepeats = 0;
int currAscending = 0;
int currDescending = 0;
for (int i = 1; i < text.Length; ++i)
{
char curr = text[i];
char prev = text[i-1];
if (curr - prev == -1)
maxDescending = Math.Max(maxDescending, ++currDescending);
else
currDescending = 1;
if (curr - prev == 1)
maxAscending = Math.Max(maxAscending, ++currAscending);
else
currAscending = 1;
if (curr == prev)
maxRepeats = Math.Max(maxRepeats, ++currRepeats);
else
currRepeats = 1;
}
}
You would have to call this and then do what you want with the results:
int maxRepeats, maxAscending, maxDescending;
checkStrength(text, out maxRepeats, out maxAscending, out maxDescending);
if (maxRepeats > REPEAT_LIMIT || maxAscending > ASCENDING_LIMIT || maxDescending > DESCENDING_LIMIT)
{
// ... report error or whatever
}
If you don't need to vary the allowed number of repeated or ascending digits, then xanatos' regex is clearly by far the shortest code. This code is only needed if you need to vary the allowed counts at runtime.
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 trying to do a Prime Number finder but cant see why it's not working.
When i run the debug test nothing show in the console. Could someone check it and tell me what i do wrong?
List<int> primes = new List<int>();
primes.Add(2);
primes.Add(3);
int maxPrime = 11; //The maximum found Primes
int primeCount = primes.Count; //Current Number of Primes
int num = 4; //Current Number
int x = 0; //
int curPrime = primes[x];
while (primeCount < maxPrime)
{
if (x != primeCount)
{
if (num % primes[x] == 0)
{
num++;
x = 0;
}
else
{
x++;
}
}
else
{
primes.Add(num);
primeCount=primes.Count;
x = 0;
}
}
primes.ForEach(i => Console.Write("{0}\t", i));
You have an infinite loop.
Since you never modify primeCount or maxPrime, this will always be true:
while (primeCount < maxPrime)
In order to end that loop, you need to modify one of those two values in such a way that the condition will evaluate to false.
(Note: There appear to also be other bugs/problems in the code aside from this. For example, num = num++; doesn't do what you probably think it does.)