C# console application reading user input [duplicate] - c#

This question already has answers here:
Console.ReadLine() max length?
(10 answers)
Closed 5 years ago.
I have c# console application. I want to read user input separated by space but my console application stops reading space separated input after count 87. what is the reason.
for example below is the count 87.I want to read 90 inputs separated by space. and it stops reading inputs after count 87.
1 1 1 1 1 1 1 12 22 22 22 22 22 222 222 222 222 222 222 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 31 22 22 22 22 22 22 22 22 22 2 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 2 2 2 2 22 22 22 22 22.
My console application code is;
string[] temp = Console.ReadLine().Split(' ');
int[] height = Array.ConvertAll(height_temp, Int32.Parse);
above as accoding to above input 90. height should be 90 but it gives me height 87. infact it stops reading inputs after count 87.

There is a character limit for Console.ReadLine(). It should be 256 characters if I am not mistaken. So, you can increase the limit like that;
Console.SetIn(new StreamReader(Console.OpenStandardInput(),
Console.InputEncoding,
false,
bufferSize: 1024));
string[] temp = Console.ReadLine().Split(' ');
int[] height = Array.ConvertAll(temp, Int32.Parse);

Related

Creat matrix numbers from 1 to 64 with only one loop how can I do that?

I want to create a method with signature:
void InitMatrixLinear(int[,] matrix)
but with only one loop I don't wanna create the same photo with two loops I need to make the same photo with only one loop how can I create this?
Like this I want to create:
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
Assuming the matrix passed in is 8x8 (since we want [1,2,...,64] as the elements):
for (int i = 0; i < 64; i++){
matrix[i%8,i/8] = i+1;
}
or
for (int i = 0; i < 64; i++){
matrix[i/8,i%8] = i+1;
}
Depending on the desired orientation of the matrix

Uniformly distributing hash of given properties

I am trying to distribute a set of items across number of buckets. I am looking for following properties:
Bucket assignment needs to be deterministic. In different runs same
input should end up in the same bucket.
Distribution of data between buckets should be uniform.
This should work for fairly small number of inputs (e.g. if I want
to distribute 50 inputs across 25 buckets ideally each bucket will
have 2 items).
First try was to generate md5 from input data and form bucket from first bytes of md5. I am not too satisfied with uniformity. It works well when input is large but not so well for small input. E.g. distributing 100 items across 64 buckets:
List<string> l = new List<string>();
for (int i = 0; i < 100; i++)
{
l.Add(string.Format("data{0}.txt", i));
}
int[] buckets = new int[64];
var md5 = MD5.Create();
foreach (string str in l)
{
{
byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(str));
uint bucket = BitConverter.ToUInt32(hash, 0) % 64;
buckets[bucket % 64]++;
}
}
Any suggestions what could I do to achieve higher uniformity? Thanks.
Leaving aside the efficiency of using MD5 for this purpose (see the discussion here and in the marked duplicate of that question), basically the answer is that what you have is what a uniform distribution really looks like.
That might seem counter-intuitive, but it's easily demonstrable either mathematically or by experiment.
As a kind of motivating example, consider the task of choosing exactly 64 numbers in the range 0-63. The odds that you will get one per bucket are very close to 0. There are 6464 possible sequences, of which 64! contain all 64 numbers. The odds of getting one of these sequence is about one in 3.1×1026. In fact, the odds of getting a sequence in which no element appears three times is less than one in a thousand (it's about .000658). So it's almost certain that a random uniform sample of 64 numbers in the range 0-63 will have some triplets, and it's pretty likely that there will be some quadruplet. If the sample is 100 numbers, those probabilities just get even bigger.
But the maths are not so easy to compute in general, so here I chose to illustrate by experiment :-), using random.org, which is a pretty reliable source of random numbers. I asked it for 100 numbers in the range 0-63, and counted them (using bash, so my "graph" is not as pretty as yours). Here are two runs:
First run:
Random numbers:
44 17 50 11 16 4 24 29 12 36
27 32 12 63 4 30 19 60 28 39
22 40 19 16 23 2 46 31 52 41
13 2 42 17 29 39 43 9 20 50
45 40 38 33 17 45 28 6 48 12
56 26 34 33 35 40 28 44 22 10
50 55 49 43 63 62 22 50 15 52
48 54 53 26 4 53 13 56 42 60
49 30 14 55 29 62 15 13 35 40
22 38 37 36 10 36 5 41 43 53
Counts:
X X X
X XX X X XX X X X X X
X X X XX XXX X X X XXX X XX XXXXXXXX XXX XX XX X XX
X XXX XXXXXXXXX XX XXX XXXXXXXXXXXXXXXXXXXXX XXX XXXXX X XX
----------------------------------------------------------------
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6
0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2
Second run:
Random numbers:
41 31 16 40 1 51 17 41 27 46
24 14 21 33 25 43 4 36 1 14
40 22 11 22 30 19 23 63 39 61
8 55 40 6 21 13 55 13 3 52
17 52 53 53 7 21 47 13 45 57
25 27 30 48 38 55 55 22 61 11
11 28 45 63 43 0 41 51 15 2
33 2 46 14 35 41 5 2 11 37
28 56 15 7 18 12 57 36 59 51
42 5 46 32 10 8 0 46 12 9
Counts:
X X X X
X X XX XX XX X X X
XXX X XX XXXXX X XX X XX X X X XX X XX XXX X X X X
XXXXXXXXXXXXXXXXXXXX XXXXX XX XXXX XXXXXXXXX XXXX XXX XXX X X X
----------------------------------------------------------------
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6
0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2
You could try this with your favourite random number generator, playing around with the size of the distribution. You'll get the same sort of shape.

C# input matrix from text file crash [duplicate]

This question already has answers here:
Splitting string based on variable number of white spaces
(2 answers)
Closed 6 years ago.
here is my source code at the moment..
CODE:
static void InputValues()
{
int row, col;
string[] words;
matrixName = fileIn.ReadLine();
words = fileIn.ReadLine().Split(' ');
dimenOne = int.Parse(words[0]);
dimenTwo = int.Parse(words[1]);
matrix = new int[dimenOne+1, dimenTwo+1];
for (row = 1; row <= dimenOne; row++)
{
words = fileIn.ReadLine().Split(' ');
for (col = 1; col <= dimenTwo; col++)
{
matrix[row, col] = int.Parse(words[col-1]);
}
}
}
My program will crash after it reads in the first value of 45 after
matrix[row, col] = int.Parse(words[col-1]); there are 3 spaces between values in the text file which is posted below. How do i populate the 2-d array without crashing?
TXT FILE
3
Matrix One
5 7
45 38 5 56 18 34 4
87 56 23 41 75 87 97
45 97 86 7 6 8 85
67 6 79 65 41 37 4
7 76 57 68 8 78 2
Matrix Two
6 8
45 38 5 56 18 34 4 30
87 56 23 41 75 87 97 49
45 97 86 7 6 8 85 77
67 6 79 65 41 37 4 53
7 76 57 68 8 78 2 14
21 18 46 99 17 3 11 73
Matrix Three
6 6
45 38 5 56 18 34
87 56 23 41 75 87
45 97 86 7 6 8
67 6 79 65 41 37
7 76 57 68 8 78
21 18 46 99 17 3
Either test if you can convert the value to an integer (using TryParse) or better use a regular expression to parse the input string. Your problem is that the split function returns more results than you expect (can easily be seen if you set a breakpoint after words = filein....)
If you have a variable number of spaces in your lines, you should eliminate them.
words = fileIn.ReadLine()
.Split(' ')
.Where(x => !string.IsNullOrWhiteSpace(x))
.ToArray();

Selection Sort trouble with indexes [duplicate]

This question already has an answer here:
Selection Sort trouble with indexes
(1 answer)
Closed 6 years ago.
Actually I'm dealing with CodeAbbey problem, so I don't want answer as code, but explenation about that, what I am doing wrong. http://www.codeabbey.com/index/task_view/selection-sort
My Selection Sort actually works without any problems, but I don't know why I do not get proper indexes (when sorting works!). I.e. for input data:5 1 3 6 2 4 7 9 8 0 I got it sorted to 0 1 2 3 4 5 6 7 8 9, as I wished.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SelectionSort
{
class Program
{
static void Main(string[] args)
{
int howMany = int.Parse(Console.ReadLine()); //length of array
List<int> Base = new List<int>(Array.ConvertAll(Console.ReadLine().Split(), int.Parse)); //input to array (i.e. 5 1 3 6 2 4 7 9 8 0 => { 5, 1, 3, 6, 2, 4, 7, 9, 8, 0 })
List<int> Output = new List<int>(); // list to store sorted array
string[] ans = new string[howMany]; // array for storing answers
int loops = Base.Count();
for(int i = 0; i != loops; i++)
{
int topID = 0, topValue = 0;
for(int j = 0; j != Base.Count(); j++)
{
if (j == 0)
{
topID = 0;
topValue = Base[0];
}
else
{
if(topValue < Base[j])
{
topValue = Base[j];
topID = j;
}
}
}
ans[i] = topID.ToString(); //after looping through array save topID to answer array
Output.Add(Base[topID]); //add topValue to output
Base.RemoveAt(topID); //remove topValue with index topID from list
}
//Output.Reverse(); // Writing on stdout
//foreach(var s in Output) // sorted array
//{ //
// Console.Write(s + " "); // It works without any problems
//} //
//Console.ReadLine(); //
foreach(var s in ans)
{
Console.Write(s + " "); // write on stdout stored indexes
}
Console.ReadLine();
}
}
}
I.e. for such a test data:
124
144 146 4 121 106 142 153 168 122 42 135 127 126 16 193 52 29 161 186 83 152 72 51 125 37 116 187 133 183 132 80 53 185 129 7 189 98 128 32 33 56 157 49 50 10 77 11 196 160 162 68 43 14 181 112 113 94 100 165 79 172 159 156 57 9 6 66 86 17 63 46 178 130 88 192 124 105 182 34 18 76 155 24 89 123 12 179 109 188 13 40 5 163 45 27 85 103 93 69 58 25 81 145 92 30 138 154 177 158 140 91 171 139 67 175 184 120 8 54 147 84 174 95 55
I got it sorted to:
4 5 6 7 8 9 10 11 12 13 14 16 17 18 24 25 27 29 30 32 33 34 37 40 42 43 45 46 49 50 51 52 53 54 55 56 57 58 63 66 67 68 69 72 76 77 79 80 81 83 84 85 86 88 89 91 92 93 94 95 98 100 103 105 106 109 112 113 116 120 121 122 123 124 125 126 127 128 129 130 132 133 135 138 139 140 142 144 145 146 147 152 153 154 155 156 157 158 159 160 161 162 163 165 168 171 172 174 175 177 178 179 181 182 183 184 185 186 187 188 189 192 193 196
and I got such an indexes:
47 14 72 34 84 25 17 29 107 25 69 46 76 63 94 100 105 52 96 7 49 76 41 15 39 47 86 33 46 61 82 6 15 87 1 75 0 3 76 77 75 5 16 16 46 18 20 5 5 11 42 48 3 1 64 10 26 25 44 1 36 48 25 12 58 23 45 49 50 37 32 28 41 49 5 43 8 21 16 29 5 35 16 37 21 22 32 18 11 34 33 7 3 4 9 8 15 22 10 1 18 3 12 5 4 17 2 14 14 10 9 8 1 4 7 6 3 2 2 4 1 1 1 0
when I was supposed to get these:
47 14 74 35 88 26 18 32 115 28 77 53 86 71 107 28 74 60 86 7 58 92 49 17 48 61 28 41 62 81 60 6 20 88 1 6 0 5 74 53 5 10 27 29 72 33 37 11 12 23 12 53 8 3 32 25 55 54 0 4 11 41 57 36 14 56 28 56 8 5 12 8 41 35 19 19 30 14 45 29 21 28 35 37 5 32 19 11 19 30 26 31 15 22 21 28 3 11 19 9 6 9 11 5 15 7 16 3 3 10 12 3 3 8 1 6 6 3 4 3 1 0 1
What am I doing wrong?
Greetings
You should be swapping topValue with the last unsorted element instead of removing it. I guess you could remove it after swapping if you wanted. But if you don't remove it and you properly ignore the sorted elements, the original collection will be sorted at the end, so you won't have to make any Add() or Reverse() calls or create a new collection.

month wise reports by date

Is it possible to get this report format in sql. I tried various ways but no luck..any light on this would help me a lot.. I can get name, tickets from table but how can date wise report in sql. I dont want use any reports
NAME 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 (date)
smrithi 10 20 34 45 55 55 66 77 33 44 55 56 44 66 77 88 55 22 33 11 44 99 77 88 (tickets)
XYZ 10 20 34 45 55 55 66 77 33 44 55 56 44 66 77 88 55 22 33 11 44 99 77 88
this is what I tried ..
SELECT *
FROM
( SELECT CAST(DAY(t_date_time_issued) AS VARCHAR(4)) AS SaleDay,
CAST(MONTH(t_date_time_issued) AS VARCHAR(4)) AS SaleYear
FROM dbo.tickets) as ts
PIVOT
(
count(t_reference)
FOR SaleDay IN ( [1],[2],[3],[4],[5],[6],[7],[8],[9],
[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],
[22],[23],[24] ) ) AS pvt
Try something like this:
SELECT Name, [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24]
FROM
(
SELECT Name,
CAST(DAY(t_date_time_issued) AS VARCHAR(4)) AS SaleDay,
t_reference
FROM dbo.tickets) AS ts
PIVOT
(
count(t_reference)
FOR SaleDay IN ( [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24] )
) AS pvt
http://sqlfiddle.com/#!6/052ff/5

Categories

Resources