How to fix my system out of memory exception? - c#

I am trying to create a program that will give me every single digit between 1 and 55 but 6 times in a row.
That is;
1-17-25-45-49-51
55-54-53-52-51-50
Note, two numbers can never repeat in a row.
I've tested this program on a smaller scale and so far it does as intended. However, if I run it full scale, as the code posted bellow shows, it runs into an "out of memory exception". My guess is, its because of the memory having to hold such big numbers while building the ArrayList and file consecutively.
How do I go about fixing this?
class MainClass
{
public static void Main (string[] args)
{
ArrayList newWords= new ArrayList();
System.Console.WriteLine ("Please enter word to prepend ");
int wordCount = 0;
string numbers;
string word = Console.ReadLine ();
string word2 = word;
string separator = "-";
System.Console.WriteLine ("Please enter location of where you would like the new text file to be saved: ");
string newFileDestination = Console.ReadLine ();
TextWriter writeToNewFile = new StreamWriter (newFileDestination);
for(int a = 1; a < 56; a++){
for(int b = 1; b < 56; b++){
for(int c = 1; c < 56; c++){
for(int d = 1; d < 56; d++){
for(int e = 1; e < 56; e++){
for(int f = 1; f < 56; f++){
if(a != b && a != c && a != c && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
word = word2;
numbers = string.Concat(a, separator, b, separator, c, separator, d, separator, e, separator, f);
word += numbers;
newWords.Add (word);
}
}
}
}
}
}
}
foreach(string line in newWords){
writeToNewFile.WriteLine(line);
Console.WriteLine (line);
wordCount++;
}
writeToNewFile.Close ();
Console.WriteLine (wordCount);
Console.WriteLine ("Press any key to exit.");
System.Console.ReadKey ();
}
}
}

Without trying to fix the rest of your code, the basic principle you need to follow is to write out the numbers as you get them, instead of cumulating them in a list. You can get rid of the ArrayList altogether, and the accompanying foreach loop.
The relevant part would look like this:
TextWriter writeToNewFile = new StreamWriter (newFileDestination);
for(int a = 1; a < 56; a++){
for(int b = 1; b < 56; b++){
for(int c = 1; c < 56; c++){
for(int d = 1; d < 56; d++){
for(int e = 1; e < 56; e++){
for(int f = 1; f < 56; f++){
if(a != b && a != c && a != c && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
word = word2;
numbers = string.Concat(a, separator, b, separator, c, separator, d, separator, e, separator, f);
word += numbers;
writeToNewFile.WriteLine(word);
Console.WriteLine (word);
wordCount++;
}
}
}
}
}
}
}
But, be warned. Your code will take a long time to complete. You just won't get the out of memory error. You may run out of hard disk space though :)

Related

finding middle position in C# [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 5 years ago.
Improve this question
Hello I have tried to follow learning on codeasy.net, I have reached chapter 4 where it asks this:
Write a program that reads from the console three numbers each from the new line and then outputs the middle by the value of these three numbers.
Example:
>54
>4456
>2
54
I have tried all the code I know as a beginner and it is still wrong. My current code is this:
using System;
namespace ConsoleInput
{
public class TheMiddle
{
public static void Main(string[] args)
{
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
if (a < b && c < b)
Console.WriteLine(a);
if (a < b && b < c)
Console.WriteLine(b);
if (c < b && a < c)
Console.WriteLine(c);
if (a < b && c > b )
Console.WriteLine(a);
else
if (a > b && c > a)
Console.WriteLine(a);
if (a > b && b > c)
Console.WriteLine(b);
if (c > b && b > a)
Console.WriteLine(a);
if (b > a && c > a)
Console.WriteLine(c);
}
}
}
You need to make more comparisons. There are 3 cases.
if((a <= b && b <= c) || (c <= b && b <= a))
Console.WriteLine(b);
if((b <= a && a <= c) || (c <= a && a <= b))
Console.WriteLine(a);
if((a <= c && c <= b) || (b <= c && c <= a))
Console.WriteLine(c);
Basically the number is in the middle of the other two and you have to check the case where the the other two numbers are on either side. So for example b is in the middle if it's between a and c in that order, or between c and a in that order.
using System;
namespace ConsoleInput
{
public class TheMiddle
{
public static void Main(string[] args)
{
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
int result;
if (a < b)
{
if (c < a)
result = a;
else if (c > b)
result = b;
else
result = c;
}
else
{
if (c < b)
result = b;
else if (c > a)
result = a;
else
result = c;
}
Console.WriteLine(result);
}
}
}
this is the answer.
You can do this just by sorting your values and taking middle:
var list = new List<int>();
list.Add(a);
list.Add(b);
list.Add(c);
list.Sort();
var middleValue = list[list.Count/2];

Windows Phone 8.1 comparing Control.Content giving wrong result

I'm experiencing a really weird problem I have the following piece of code :
for (int i = 0; i < Board.Length - 2; i++)
{
var a = Board[i].Content;
var b = Board[i + 1].Content;
var c = Board[i + 2].Content;
if (a == b && a == c &&
(string) a != string.Empty && a != null)
{
MessageDialog msd = new MessageDialog("test");
await msd.ShowAsync();
}
}
Where Board is an array of buttons and a,b,c have the same value of "1". However when comparing them in the if statement they all give false ? The other statements where I check if the string is empty or null give value of true.
You are performing a reference equality comparison instead of a value equality comparison. Your code is equivelent to the following:
for (int i = 0; i < Board.Length - 2; i++)
{
object a = Board[i].Content;
object b = Board[i + 1].Content;
object c = Board[i + 2].Content;
if (a == b && a == c &&
(string) a != string.Empty && a != null)
{
MessageDialog msd = new MessageDialog("test");
await msd.ShowAsync();
}
}
This means that a == b is being resolved as <object> == <object> rather than <string> == <string>, which results in a comparison equivelent to Object.ReferenceEquals(a, b). To get value equality, you should immediately cast a, b and c. Now that a is a string, you can also use String.IsNullOrEmpty instead of manually checking both:
for (int i = 0; i < Board.Length - 2; i++)
{
string a = (string)Board[i].Content;
string b = (string)Board[i + 1].Content;
string c = (string)Board[i + 2].Content;
if (a == b && a == c && !String.IsNullOrEmpty(a))
{
MessageDialog msd = new MessageDialog("test");
await msd.ShowAsync();
}
}

count vowels, consonants and numeric

What's wrong with my code? If I enter aaa, it returns 3 in the vowels row, but if I enter abc, it return 3 also in the vowels row.
By the way, it's a windows form .
txtInputString.SelectionStart = 0;
txtInputString.SelectionLength = txtInputString.Text.Length;
txtInputString.Focus();
int vowelCount = 0, consonants = 0, nonNumeric = 0;
int count = txtInputString.TextLength;
for (int i = 0; i < count; i++)
{
if ((txtInputString.Text.Contains('a') == true) || (txtInputString.Text.Contains('e') == true) || (txtInputString.Text.Contains('i')== true) || (txtInputString.Text.Contains('o')==true) || (txtInputString.Text.Contains('u')==true))
{
vowelCount++;
}
else if ((txtInputString.Text.Contains('b') == true) || txtInputString.Text.Contains('c') || txtInputString.Text.Contains("d") || txtInputString.Text.Contains("f") || txtInputString.Text.Contains("g"))
{
consonants++;
}
else
{
nonNumeric++;
}
}
txtVowel.Text = vowelCount.ToString() + "";
txtConsonant.Text = consonants.ToString() + "";
txtNonNumeric.Text = nonNumeric.ToString();
Try that :
txtInputString.SelectionStart = 0;
txtInputString.SelectionLength = txtInputString.TextLength;
txtInputString.Focus();
int vowelCount = 0, consonants = 0, nonNumeric = 0;
int count = txtInputString.TextLength;
for (int i = 0; i < count; i++)
{
char c = txtInputString.Text.ElementAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
vowelCount++;
else if (c == 'b' || c =='c' || c == 'd' || c == 'f')
consonants++;
else
nonNumeric++;
}
txtVowel.Text = vowelCount.ToString() + "";
txtConsonant.Text = consonants.ToString() + "";
txtNonNumeric.Text = nonNumeric.ToString();
you can make this a lot more simple by loading up all your consonants and vowels beforehand and applying some LINQ:
string consonants = "bcdfghjklmnpqrstvwxyz";
string vowels = "aeiou";
int vowelCount = 0, consonantCount = 0, nonNumericCount = 0;
var input = "alsdkghanivhusrvndb"; //some input
foreach (char t in input)
{
if (consonants.Any(c => c == t))
consonantCount++;
else if (vowels.Any(c => c == t))
vowelCount++;
else
nonNumericCount++;
}

How to Convert Javascript Code to C# Code

I have a Javascript method. I wrote on C# but it dosen't work.
Javascript Code
var __AM = 65521;
function cc(a) {
var c = 1, b = 0, d, e;
for (e = 0; e < a.length; e++) {
d = a.charCodeAt(e);
c = (c + d) % __AM;
b = (b + c) % __AM;
}
return b << 16 | c;
}
My written C# Code
private string CC(string a)
{
var __AM = 65521;
int e;
long d;
long c = 1, b = 0;
for (e = 0; e < a.Length; e++)
{
var p = Encoding.Unicode.GetBytes(a[e].ToString());
d = Convert.ToInt32(p.First());
c = (c + d) % __AM;
b = (b + c) % __AM;
}
return b.ToString() + c.ToString();
}
JS Test
cc("4JipHEz53sU1406413803");
Result: 1132332429
C# Test
CC("4JipHEz53sU1406413803");
Result: 172781421
How Can I get JS value in C#?
This code works:
private string cc(string a)
{
var __AM = 65521;
int e;
long d;
long c = 1, b = 0;
for (e = 0; e < a.Length; e++)
{
d = (int)a[e];
c = (c + d) % __AM;
b = (b + c) % __AM;
}
return (b << 16 | c).ToString();
}

Converting C# code to Java code

I have to convert this little piece of C# code to Java:
const int AM = 65521;
int GetCCSufix(string a)
{
int c = 1, b = 0, d, e;
var chars = a.ToCharArray();
for(e =0; e< chars.Length; e ++)
{
d = chars[e];
c = (c + d) % AM;
b = (b + c) % AM;
}
return b << 16 | c;
}
And I made it this:
private int getSuffix(String a) {
int constant = 65521;
int c = 1;
int b = 0;
int d = 0;
int e = 0;
for(e = 0; e < a.length(); e++){
d = a.charAt(e);
c = (c + d) % constant;
b = (b + c) % constant;
}
return b << 16 | c;
}
However, this doesn't seem to give me the same output as the C# code. What am i doing wrong?
I did a verbatim translation of the original code, see if this gives the correct result. What values are you using for testing, that give different results?
private static final int AM = 65521;
int getCCSuffix(String a) {
int c = 1, b = 0, d = 0, e;
char[] chars = a.toCharArray();
for (e = 0; e < chars.length; e++) {
d = chars[e];
c = (c + d) % AM;
b = (b + c) % AM;
}
return b << 16 | c;
}

Categories