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 8 years ago.
Improve this question
using System;
using System.IO;
namespace Sudoku
{
class Game
{
private int[,] puzzle = new int[9, 9];
public void saveToFile()
{
StreamWriter str = new StreamWriter("SUDOKU.txt");
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
str.Write(puzzle[i, j] + " ");
}
str.Write("\t\n");
}
str.Close();
}
public void readFromFile()
{
clear();
StreamReader str = new StreamReader("SUDOKU.txt");
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
puzzle[i, j] = Convert.ToInt32(str.Read());
}
}
str.Close();
}
}
}
I can not download the data from the file.
Saving works fine and has a view of the txt file:
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 1 4 3 6 5 8 9 7
3 6 5 8 9 7 2 1 4
8 9 7 2 1 4 3 6 5
5 3 1 6 4 2 9 7 8
6 4 2 9 7 8 5 3 1
9 7 8 5 3 1 6 4 2
How it all written in my array 9x9 skipping all the gaps that would be all the data is written correctly?
Instead of using str.Read() which would require you to read single characters (or a buffer that you specified), try using str.Readline() to read a single line for each iteration of i.
public void readFromFile()
{
StreamReader str = new StreamReader("SUDOKU.txt");
for (int i = 0; i < 9; ++i)
{
string[] lineNumbers = str.ReadLine().Split(' ');
for (int j = 0; j < 9; ++j)
{
puzzle[i, j] = Convert.ToInt32(lineNumbers[j]);
}
}
str.Close();
}
This reads a single line at a time (each iteration of i), splits the line into lineNumbers by separating the current line by space characters. Each number on the current line can then be accessed by lineNumbers[j] within your inner loop (each iteration of j).
Related
So, I have this task where I have to find the competitors who won in a tie.
Basically the input is a txt file, which has the number of competitors and the number of competitions in it's first row. The second and third rows are the maximum and minimum points. The rows after the third represent competitors and they have the amount of points they won.
I have to find the number of competitors who if they won, they won in a tie and their indexes.
One input:
6 8
9 9 9 9 9 9 9 9
5 5 5 5 5 5 5 5
8 4 6 6 6 6 6 6
7 5 7 6 6 6 6 5
6 6 6 5 5 5 5 6
8 6 8 7 7 7 7 6
8 6 6 6 6 6 6 6
8 6 6 6 6 6 6 1
The first row is 6 and 8 which means there is 6 competitors and 8 competition, the maximum points are 9 the minimum points are 5.
In this case the output is:
5 1 3 4 5 6
In the first column the winning point is 8, number 1 4 5 and 6 got 8 points which means they tied and should be added to the output, in the second column the winning point is 6, number 3 4 5 6 got 6 points so 3 should be added to the output too, and so on...
I've got this so far:
static void Main(string[] args)
{
string[] fRow = Console.ReadLine().Split(' '); //splitting the first row to get N and M
int N = int.Parse(esor[0]);
int M = int.Parse(esor[1]);
int[] maxPoint = new int[M];
int[] minPoint = new int[M];
string[] maxP = new string[M];
string[] minP = new string[M];
maxP= Console.ReadLine().Split(' ');
minP = Console.ReadLine().Split(' ');
for (int i = 0; i < M; i++)
{
maxPoint[i] = Convert.ToInt32(maxP[i]);
minPoint[i] = Convert.ToInt32(minP[i]);
}
int[,] matrix = new int[N, M];
int[] s = new int[M];
int mCol = 0; int mRow = 0;
for (int i = 0; i < N; i++) //adding the points to the matrix
{
string[] row = Console.ReadLine().Split(' ');
for (int h = 0; h < N; h++)
{
s[h] = Convert.ToInt32(row[h]);
matrix[mCol, mRow] = s[h];
mCol++;
if (mCol==N)
{
mCol = 0;
mRow++;
}
}
}
int max = 0; //trying to find the greatest point in each column
int[] winPoint = new int[M];
for (int i = 0; i < M; i++)
{
for (int h = 0; h < N; h++)
{
if (matrix[i, h] > max)
{
max = matrix[i, h];
}
if (h == N-1)
{
h = 0;
}
}
}
}
And I got stuck here, I'm trying to find the greatest point in each column so they I could search for the same numbers in the colums and count the amount of ties, but I don't know how to do it. If someone could help me I'd really appreciate it, or if I should to it in a different way just tell me.
-> Please help with the implementation of a function
Basically I want to manipulate an image
Imagine that the image can be brake apart in pixels
-> each pixel of the image will be (for an easy example) "a number"
int[,] originalImage = new int[,]
{
{ 1,2,3 },
{ 4,5,6 }
};
And I need to double the size of the image x2 (or x3, x4, etc)
the pixel "1" when double the size will be copied on te right position, down and on the corner
int[,] expectedResult = new int[,]
{
{ 1,1,2,2,3,3 },
{ 1,1,2,2,3,3 },
{ 4,4,5,5,6,6 },
{ 4,4,5,5,6,6 }
};
How does the function can be implemented?
Multiply(int[,] originalImage, int multiply)
Note there are oodles of libraries that would do this in math or image processing. However, purely for academic purposes
Given
private static int[,] DoStuff(int[,] array,int size)
{
var sizeX = array.GetLength(0);
var sizeY = array.GetLength(1);
var newArray = new int[sizeX * size, sizeY * size];
for (var i = 0; i < newArray.GetLength(0); i++)
for (var j = 0; j < newArray.GetLength(1); j++)
newArray[i, j] = array[i / size, j / size];
return newArray;
}
Usage
int[,] originalImage =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
var newArray = DoStuff(originalImage,3);
for (var i = 0; i < newArray.GetLength(0); i++)
{
for (var j = 0; j < newArray.GetLength(1); j++)
Console.Write(newArray[i, j] + " ");
Console.WriteLine();
}
Output
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
I have a list of data that I have calculated (all in C#)
I want to display the result of the calculated data in 4 columns instead of one long row.
This is what I have in my prompt right now:
1
2
3
4 etc
This is what I want:
1 2 3 4
5 6 7 8 etc
How can I do that in C#?
private static void Print(int[] arr)
{
int counter = 0;
foreach (int item in arr)
{
if (counter == 4) // can be configured to desired columns
{
counter = 0;
Console.WriteLine();
}
counter++;
Console.Write(item + " ");
}
}
INPUT: {1,2,3,4,5,6,7,8,9,10,11,12,13}
OUTPUT:
1 2 3 4
5 6 7 8
9 10 11 12
13
Demo Fiddler: Here
This question already has answers here:
Randomize a List<T>
(28 answers)
Closed 6 years ago.
i have some list with 5 numbers (exp. 1 2 3 4 5) i want to order them in random ordering each time (page refresh) examples: (2 4 3 1 5) (1 3 5 4 2) (5 1 2 3 4)... code in C#, Thanks
var loadcards = (from card in db.GameCards
select card).Take(5).ToList();
foreach (var item in loadcards)
{
Response.Write("<script>alert('" + item.cardId + "');</script>");
}
Something like this:
int[] RandomizeOrder(int[] input)
{
Random RNG = new Random();
bool[] cellMap = new bool[input.Length];
int[] output = new int[input.Length];
for(int i = 0; i < input.Length; i++)
{
int index = RNG.Next(input.Length)
while(cellMap[index)
index = RNG.Next(input.Length);
cellMap[index] = true;
output[index] = input[i];
}
return output;
}
PS: You can remove the cellMap if none of the values is 0
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 8 years ago.
Improve this question
How can I do multiplication in hand form?
by hand form I mean:
Take two numbers as input,
then output in a text box,
the multiplication steps as it would be done by a school student.
so for the inputs 819 and 1358 I wish to output:
8 1 9
1 3 5 8
x_____________
8 1 9 0 0 0
5 4 5 7 0 0
1 0 9 5 0
1 4 5 5 2
+__________________
1 4 7 0 2 0 2
I can of-course get the final answer though the multiplication operation: (a*b)
but that will not let me display the steps.
This should do what you require.
Then modify like following
List<double> lst = new List<double>();
string strInput2 = txtInput2.Text;
for (int i = 0; i < strInput2.Length; i++)
{
double dbl = Convert.ToDouble(txtInput1.Text) * Convert.ToDouble(strInput2[strInput2.Length - (i + 1)].ToString());
string zeros = new String('0', i);
lst.Add(Convert.ToDouble(dbl + zeros));
//richTextBoxResult.Text += lst[i] + Environment.NewLine;
}
for (int i = lst.Count - 1; i >= 0; i--)
{
richTextBoxResult.Text += lst[i] + Environment.NewLine;
}
richTextBoxResult.Text += "________________" + Environment.NewLine;
richTextBoxResult.Text += lst.Sum();