C# reset many integer variables quicker [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is there a way to reset these variables quicker like at the time of declaration?
Declaration:
int numa1, numa2, numa3, numd1, numd2, numd3;
Reset afterwards:
numa1 = 0;
.
.
.
numd3 = 0;
Because I will change these variables, but then I need to reset them as 0, OK?
Replying the comments below:
Sorry, I didn't change "Reset afterward" to "Initializes afterward". Someone else did that.
Sorry, but you cannot change the values of the variables like:
numa1, numa2, numa3, numd1, numd2, numd3 = 0;
I tried and I received Error 1, 2, 4.

By quicker if you mean faster, I donot think there is any better alternative.
If you mean reducing some lines,
You can choose either
int numa1=0, numa2=0, numa3=0, numd1=0, numd2=0, numd3 = 0;
or
int numa1, numa2, numa3, numd1, numd2, numd3 = 0;
numa1= numa2= numa3= numd1= numd2= numd3;

I guess that you should have used two arrays, I never had a function that used so many variables with such names (sequential numbers).
If you'd use arrays, Your code will look like this :
int[] numa = new [] {0,0,0,0};
int[] numd = new [] {0,0,0,0};
But this is up to you.
For more information about arrays

Related

Working with predefined arrays [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am creating an own generic collection class in C#, and Im working on an array that stores a list that should have an predefined length (It should start with a length of 2). Now I have no idea on how to make it start with a length of 2. For example, if I create an array:
private int[] numbers = new int[20];
How do I predefined it length so it starts with a length of 2?
I see no reason to do that. However you can create arrays with length 2 ->
private int[] numbers = new int[2];.
Also, you can use List<T> to do just that and forget about memory management? :-)
Just use one of the build in classes. They will hadle everything for you avoiding errors.
List<int> numbers = new List<int>();
numbers.Add(123);
int i = numbers[0];
int[] asArray = numbers.ToArray();

How to round up the number in c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I round up values like this:
1.001 => 2
3.3 => 4
Means if the number has fractional part than i want the smallest integer number greater than the number ?
I used Math.Ceiling() but is not helping. How can i do this ?
Math.Ceiling will work. can you tell what its not working ? in term of any errors or returned result.
var returnVal=Math.Ceiling(yourValue);
Use Math.Ceiling() method.
Returns the smallest integer greater than or equal to the specified
number.
var i = Math.Ceiling(1.001);
var j = Math.Ceiling(3.3);
Console.WriteLine(i);
Console.WriteLine(j);
Output:
2
4
Math.Ceiling(value);
Should work.
double x;
x = Math.Ceiling(5.2) ;//Result; 6
x = Math.Ceiling(5.7) ;//Result; 6
x = Math.Ceiling(-5.2) ;//Result;-5
x = Math.Ceiling(-5.7) ;//Result;-5
This is a simple example. How can't you use it? Maybe you miss to assign a variable to
Math.Ceiling();

Convert double array to list [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
txt file that contains several columns of numbers, every column is saved in a double array, what I want to do is get the average of a specific column, but to do that I have to convert the array to a list and then began to calculate. I have this code so far:
List<double> 1 = new List<double>(NumSepaERG);
List<double> 12 = NumSepaERG.ToList();
But i get and error of Invalid Expression term double
Variable names can't start with a numeric character. Change to something like:
List<double> list1 = new List<double>(NumSepaERG);
but you can compute an average using Linq without converting to a list:
double average = NumSepaERG.Average();
If NumSepaERG is a jagged array (an array of arrays), the syntax would be:
double average = NumSepaERG[i].Average();
where i is between 0 and the number of arrays - 1;
I think it's a syntax error, you can't have numbers as variable names. You actually don't even need to make it a list.
double average = NumSepERG.Average();

MasterMind in C# Console Application [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an assignment from my school in which we have to make a MasterMind game in C# in a console application.
So far I managed to do the border (with help from friends), an introductory tune (beep) when the game starts, and the user input to insert the numbers.
The problem is when the user ends the game, the game doesn't stop to accept inputs from the user and obviously crashes.
I also have an error in the high score method "use of unassigned local variable".
score = ptsguesses * ptsTime;
Where are ptsguesses and ptsTime initialized? Nowhere, obviously.
You probably want to set ptsguesses and ptsTime before calculating the score.
the use of unassigned value is probably this one:
static void highscore()
{
{
byte ptsguesses,ptsTime, userGuesses, timeTaken;
int score; <<------
change it to int score = 0;
also, ptsguesses,ptstime,userguesses and timetaken have never been initialised.
you might want to try to pass those arguments to your highscore() method.
something like
static void highscore(byte ptsguesses, byte ptsTime, byte userGuesses, byte timeTaken)
you'd have to call the highscore() method then, and pass the actual values to the method. that way if u try to run highscore(), you will have something to actually calculate.

How to increment a integer variable itself [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How to increment a integer variable itself in C#?
i need something the variables as
integer1
integer2
integer3
integer4
integer5
In a for loop when it is looped i want the variables gets incremented. How can i get?
Dynamic identifier names are not possible in C# and any other .NET language.
You can use an array or generic list instead - these will hold a collection of your type and allow you to iterate over it.
var intArr = new int[5];
var intList = new List<int>(5);
foreach(var num in intList)
{
// do something with num
}
What you really want is an array:
var ints = new int[5]; // declares an array of five integers, ints[0] to ints[4]
for (int i = 0; i < 5; i++) {
// do something with ints[i]
}
For a more in-depth introduction, have a look at the following tutorial on MSDN. It's for an old C# version, but the concepts still apply:
Arrays Tutorial
You can't change variable names dynamically in C#.

Categories

Resources