When I write C# code return the integer array,why it just output 3? it cannot random number.Please help me, thanks!
static void Main(string[] args)
{
Console.WriteLine(3);
}
static int[] RandomArray(int items)
{
int[] array = new int[items];
Random rand = new Random();
for (int i = 0; i < items; i++)
{
array[i] = rand.Next(100, 200);
}
return array;
}
static void Main(string[] args)
{
var output = RandomArray(10);
for (int i = 0; i < output.Length; i++)
{
Console.WriteLine(output[i]);
}
Console.WriteLine();
}
static int[] RandomArray(int items)
{
int[] array = new int[items];
Random rand = new Random();
for (int i = 0; i < items; i++)
{
array[i] = rand.Next(100, 200);
}
return array;
}
In output variable, you will get array. You can use it further.
You can do this.
The problem is you were just writing 3 to the console
private static Random rand = new Random();
public static void Main(string[] args)
{
foreach(var item in RandomArray(3))
Console.WriteLine(item);
}
static int[] RandomArray(int items)
{
int[] array = new int[items];
for (int i = 0; i < items; i++)
array[i] = rand.Next(100, 200);
return array;
}
Demo Here
Random Class
Console.WriteLine Method
Main is the entry point in a C# application. Main is called by the common language runtime (CLR) at the start of your program's execution. In other words, Main is called automatically. RandomArray is also a method. However, it is not an Entry point and therefore it will not be called automatically. You have to call RandomArray from main in order for the method to execute.
Right now your program is effectively just:
static void Main(string[] args)
{
Console.WriteLine(3);
}
The rest of the code is just sitting around doing nothing.
In order to fix this problem you need to call RandomArray from Main
static void Main(string[] args)
{
Console.WriteLine(3);
RandomArray(5);
}
And if you also want to output more data to the console then you can do something like:
static int[] RandomArray(int items)
{
int[] array = new int[items];
Random rand = new Random();
for (int i = 0; i < items; i++)
{
array[i] = rand.Next(100, 200);
Console.WriteLine($"{array[i]} inserted into array");
}
return array;
}
Presumably you also want to do something with the return value of RandomArray. Modify Main to assign the return value to a variable. Then you can do something with the result:
static void Main(string[] args)
{
Console.WriteLine(3);
int[] result = RandomArray(5);
foreach (var element in result)
{
Console.WriteLine(element);
}
}
Related
This question already has answers here:
Making an array of random ints
(2 answers)
Closed last month.
I have just started to learn coding and was hoping someone with a little more skill would help me out a bit.
Here is the code and when i run it the displayarray is coming up as just zeros and then the sum of the array is also zero I just cant grasp why thats happening so it would be awesome if someone could help me out.
namespace random_array_2._0
{
class Program
{
static void Main(string[] args)
{
int[] array;
int sum = 0;
int arraySum;
array = CreateArray();
DisplayArray(array);
arraySum = SumTheArray(array, sum);
}
static int[] CreateArray()
{
Random Array = new Random();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(Array.Next(1, 100));
}
return new int[10];
}
static void DisplayArray(int[] array)
{
Console.WriteLine();
Console.WriteLine("Sorted array in ASC order");
Array.Sort(array);
foreach (int i in array)
{
Console.Write(i + " ");
}
}
static int SumTheArray (int[] array, int sum)
{
for (int i = 0; i < array.Length; i++)
{
sum += array[i];
}
Console.WriteLine($"all together equals to: {sum}");
Console.ReadKey();
return sum;
}
}
}
Your CreateArray function is printing 10 random numbers and then returning a new array. The default value for int is 0, so this array contains 10 zeros.
You should first create the array and then populate it with random values:
static int[] CreateArray()
{
Random Array = new Random();
var a = new int[10];
for (int i = 0; i < a.Length; i++)
{
a[i] = Array.Next(1, 100);
}
return a;
}
There you go:
static int[] CreateArray()
{
Random randomNumberGenerator = new Random();
int[] array = new int[10];
for (int i = 0; i < array.Length; i++)
{
int randomNumber = randomNumberGenerator.Next(1, 100);
array[i] = randomNumber;
}
return array;
}
My advise to you is to stop using Console.WriteLine statements and start using the debugger. Console.WriteLine often lies; the debugger tends to never lie.
Your CreateArray function is not storing anything into Array, and it's returning an array with 10 zeroes (default value when none in given).
Try with
static int[] CreateArray()
{
Random rnd = new Random();
int[] Array = new int[10];
for (int i = 0; i < 10; i++)
{
Array[i] = rnd.Next(1, 100);
}
return Array;
}
My main goal is to create a Method, where it is possible to enter a number, out of which a Method will choose some other numbers (according to the purpose) and combine them in Array.
I need this Array and its value to be flexible, so I decided to create a new variable, that is within the scope for both Container() and Main() methods. Then I assigned a value from Container() to optainer, but it didn't work (foreach doesn't display all numbers from Container(), only the first one). Where is my problem?
static int[] optainer;
static void Container()
{
uint numb = uint.Parse(Console.ReadLine());
for (int i = 1000000; i >= 1; i--)
{
if (numb % i == 0)
{
optainer = new int[] { i };
}
}
}
static void Main(string[] args)
{
Console.WriteLine("Enter num. from 1 to 1 000 000");
Container();
foreach (int iw in optainer)
{
Console.WriteLine(iw);
}
// Expected: few numbers according to the condition; Real result: 1
```
You have always only one element in optainer,
this line is the error
optainer = new int[] { i }; you create always a new array with only one item and the last is always 1.
you can change in this way
static List<int> optainer = new List<int>();
static void Main(string[] args)
{
Console.WriteLine("Enter num. from 1 to 1 000 000");
Container();
foreach (int iw in optainer)
{
Console.WriteLine(iw);
}
}
static void Container()
{
uint numb = uint.Parse(Console.ReadLine());
for (int i = 1000000; i >= 1; i--)
{
if (numb % i == 0)
{
optainer.Add(i);
}
}
}
I'm sure there's a sexier way to do this, but try this:
static void Container()
{
uint numb = uint.Parse(Console.ReadLine());
for (int i = 1000000; i >= 1; i--)
{
if (numb % i == 0)
{
int size = optainer.Length + 1;
Array.Resize(ref optainer, size);
optainer[size - 1] = i;
}
}
}
Everytime you write
optainer = new int[] { i };
you create a new list(you overwrite the old one) you would have to append
to the array. Therefore you would need to know the size of the array.
To save memory you should use something more dynamic like lists.
Here is an explanation how to add a value:
Adding values to a C# array
Goal
A C# program that picks a random number from 1 to 6 and stores it
Question
Which way to store: the entire sequence or an array of 6 elements with increments to the n-1 index for every hit of n?
Code
using System;
namespace DiceProbabilityCalc
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[] instanceCount = new int[1000];
for (int i = 0; i < 999; i++)
{
int num = rnd.Next(1, 7);
}
}
}
}
Thanks to #PeterSmith and #Martheen for solving the question in the comments: The array instanceCount can be used to store the number of instances instead of the entire sequence. This is the resulting code.
using System;
namespace DiceProbabilityCalc
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[] instanceCount = new int[6];
for (int i = 0; i < 999; i++)
{
int num = rnd.Next(1, 7);
instanceCount[num - 1]++;
}
for (int j = 0; j < instanceCount.Length; j++)
{
Console.WriteLine(instanceCount[j]);
}
}
}
}
I've just recently started learning to code C# to hopefully get a job someday. I'm trying to get 3 randomly generated points in a 5x5 grid. For some reason when I try to run it it just auto crashes. For testing purposes I added a Console.WriteLine and Console.ReadKey to try and see the output but it still auto closed immediately. Is there any reason why this shouldn't be working? Thanks for any help :D
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
}
public void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
Console.WriteLine(AiB[0]);
Console.ReadKey();
}
}
}
your method is not called. that is why it auto closes.
nothing is done in your program.
class Program
{
static void Main(string[] args)
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
Console.WriteLine(AiB[0]);
Console.ReadKey();
}
}
now the output is:
complete code:
class Program
{
static void Main(string[] args)
{
CompB();
}
public static void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
Console.WriteLine(AiB[0]);
Console.ReadKey();
}
}
This is how i found out that the method was not used:
Before:
After:
As already called out above you are missing the CompB();from the main method .
One more I just looked into the for loop in line 18 it starts from in the i=1 but you are doing
System.Console.WriteLine(AiB[0]);
you should try out something like
System.Console.WriteLine(AiB[1]);
static void Main(string[] args)
{`enter code here`
CompB();
}
public static void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
System.Console.WriteLine(AiB[1]);
System.Console.ReadKey();
}
You need to call your method CompB() from the main method. Then you have to write out all the values int the array.
class Program
{
static void Main(string[] args)
{
CompB();
}
public static void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
//Write all values:
for (int i = 0; i < 6; i++)
{
System.Console.WriteLine("Value of {0}: {1}", i, AiB[i]);
}
System.Console.ReadKey();
}
}
Getting back into the swing of things after focusing on other assignments.
could someone tell me why I get an error for this.
static void Main(string[] args)
{
Console.WriteLine("% unique numbers");
Random rnd = new Random();
for (int count = 0; count <= 5; count++)
{
DiceR(); // ------ Error here -----
}
}
public void DiceR()
{
Random rnd = new Random();
Console.WriteLine(rnd.Next(1, 6));
}
}
make it static
static public void DiceR()
{
Random rnd = new Random();
Console.WriteLine(rnd.Next(1, 6));
}