Memory error NZEC when using C# on HackerEarth - c#

I am solving a question on HackerEarth. The test case passes successfully, but the rest of the cases show a NZEC error. I know NZEC stands for Non Zero Exit Code but I am not sure why this is happening here.
Any insight about why this happens and how I can optimize my code (preferably without changing the logic) will help.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int t = int.Parse(Console.ReadLine());
int[] arr = new int[3];
for(int m = 0; m < t; m++)
{
arr[m] = int.Parse(Console.ReadLine());
}
for (int n = 0; n < t; n++)
{
int i, j;
for (i = 0; i < arr[n]; i++)
{
for (j = 0; j < 2 * arr[n]; j++)
{
if (j <= i || j >= 2 * arr[n] - 1 - i)
{
Console.Write("*");
}
else
{
Console.Write("#");
}
}
Console.WriteLine("\n");
}
Console.WriteLine("\n");
}
}
}

Related

Spoj task Bonus: ARRAYSUB - subarraysZadanie runtime error (NZEC) {c#}

Good morning I ran into a problem because look at the Bonus task: ARRAYSUB - subarrays task
it gives me a runtime error (NZEC) although ideone is not showing any error https://ideone.com/1eiciI Thank you
using System;
using System.Linq;
namespace ConsoleApp17
{
internal class Program
{
private static void Main(string[] args)
{
int n = Convert.ToInt32((Console.ReadLine()));
int k;
int val = 0;
int[] arr = Console.ReadLine().Split().Select(int.Parse).ToArray();
k = Convert.ToInt32((Console.ReadLine()));
for (int i = 0; i < n - k + 1; i++)
{
val = arr[i];
for (int j = i; j < i + k; j++)
{
if (val < arr[j])
{
val = arr[j];
}
}
Console.Write(val + " ");
}
}
}
I try to solve task and get accpeted
That might help u
this type of error - when the program throws an exception. In this task, probably incorrectly loaded input or exceeding the range.
I propose:
int[] arr = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

A method to search Id inside a matrix -Trouble with output

I am new at programming and I am trying to create a method that allows me search Id inside a [10,4] matrix, however I don't get how to do it without using nested fors and also if and else statement. The problem is related to output, I know the structure isn't correct, but since I don't what else can be done I am trying make it as it is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace menu
{
class Program
{
enum header { id, name, surname, addres };
public static int id = 1;
static void Main(string[] args)
{
string[,] matrix = new string[10, 4];
insertStudent(matrix);
idSearch(matrix);
Console.ReadKey();
}
static int generateId()
{
return id++;
}
static void insertStudent(string[,] matrix)
{
int n = generateId();
matrix[n - 1, 0] = Convert.ToString(n);
for (int i = 1; i < matrix.GetLength(1); i++)
{
do
{
Console.WriteLine($"Insert {Enum.GetName(typeof(header), i)}");
matrix[n - 1, i] = Console.ReadLine();
}
while (String.IsNullOrEmpty(matrix[n - 1, i]));
}
}
static void idSearch(string[,] matrix)
{
int idChosen=0;
Console.WriteLine($"Insert ID you want to visualize:");
int.TryParse(Console.ReadLine(), out idChosen);
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
if (matrix[i, 0] == Convert.ToString(idChosen))
{
Console.WriteLine(matrix[i, j]);
}
else
{
Console.WriteLine("The chosen ID does not exist");
}
}
}
}
}
}
Right now you printing "The chosen ID does not exist" every time you check an index in your matrix. You want to move that statement to outside of your loop after you've already checked every index. Right now that check is really saying that your ID is not in that specific cell. I've altered your code slightly to reflect this. I also fixed your check to be on matrix[i,j] instead of matrix[i,0]
Also using a nested for loop is OK to use. I don't believe C# has any built in helper methods for searching multidimensional arrays.
bool found = false;
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
if (matrix[i, j] == Convert.ToString(idChosen))
{
//note that this will print your id
Console.WriteLine(matrix[i, j]);
//this would print where it found it
Console.WriteLine("Found at [" + i + "," + j + "]");
found = true;
}
}
}
if (!found)
{
Console.WriteLine("The chosen ID does not exist");
}

How to make a matrix multiplication in a task

I am unable unable to get the product of matrices in a task in order to run it
in several instances. I think the task is not properly doing the multiplication and calling several 0's.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _010029428_Multiplicacion
{
class Program
{
static void Main(string[] args)
{
int i, j, m, n;
Random rnd = new Random((int)DateTime.Now.Ticks & 0x000fff);
Console.WriteLine("Ingrese el numero de renglones y columnas: ");
m = Convert.ToInt32(Console.ReadLine());
n = Convert.ToInt32(Console.ReadLine());
int[,] a = new int[m, n];
Console.WriteLine("first Matrix");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
a[i, j] = rnd.Next(1, 4);
}
}
Console.WriteLine("First matrix is: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(a[i, j] + "\t");
}
Console.WriteLine();
}
int[,] b = new int[m, n];
Console.WriteLine("Second Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
b[i, j] = rnd.Next(1, 4);
}
}
Console.WriteLine("The second matrix is:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(b[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("The result is :");
int[,] c = new int[m, n];
var multiplicacion = new Task(() =>
{
for (j = 0; j < n; j++)
{
c[i, j] = 0;
for (int k = 0; k < n; k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
});
for (i = 0; i < m; i++)
{
multiplicacion.Start();
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
}
}
}
}
The code is using multiplicacion.start() to run it asynchronously
Not sure what the problem may be
There are several problems.
First: You cannot create just one task and run it multiple times at once. New Task() must be created for each value of "i".
Second: You cannot use global variable ("j") and mutate it in a task that runs asynchronously, without proper synchronization. This would result in program crash or wrong result. Each task must have it's own (local) variable ("jj") if it needs to change it. Read-only variables (i, a, b, c, m, n) are usually safe to use, but it is better to pass them as arguments.
Third: You must wait for all tasks to finish before you write result to screen.
Four: Running task asynchronously has significant performance overhead. So unless you matrices are really huge (probably at least hundreads of rows and columns), calculating result in several asynchronous tasks is probably much slower than simple synchronous calculation on main thread.
Example of how to make it work follows. Code is still ugly and not wery good, but should work and give you some starting point for further improvements.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _010029428_Multiplicacion
{
class Program
{
static void Main(string[] args)
{
int i, j, m, n;
Random rnd = new Random((int)DateTime.Now.Ticks & 0x000fff);
Console.WriteLine("Ingrese el numero de renglones y columnas: ");
m = Convert.ToInt32(Console.ReadLine());
n = Convert.ToInt32(Console.ReadLine());
int[,] a = new int[m, n];
Console.WriteLine("first Matrix");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
a[i, j] = rnd.Next(1, 4);
}
}
Console.WriteLine("First matrix is: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(a[i, j] + "\t");
}
Console.WriteLine();
}
int[,] b = new int[m, n];
Console.WriteLine("Second Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
b[i, j] = rnd.Next(1, 4);
}
}
Console.WriteLine("The second matrix is:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(b[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("The result is :");
int[,] c = new int[m, n];
Task[] tasks = new Task[m];
for (i = 0; i < m; i++)
{
var multiplicacion = new Task((parameter) =>
{
int ii = (int)parameter;
for (int jj = 0; jj < n; jj++)
{
c[ii, jj] = 0;
for (int k = 0; k < n; k++)
{
c[ii, jj] += a[ii, k] * b[k, jj];
}
}
},
i);
tasks[i] = multiplicacion;
multiplicacion.Start();
}
Task.WaitAll(tasks);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Insertion sort algorithm c#

I am trying to implement the Insertion sort in one of my programs. What I have been trying to create is a sorting program (in both ascending or descending order) However I have tried with algorithms such as quicksort and merge-sort, I am really new to c# and coding. The problem I am facing here is the fact that my files includes both a string of code, as well as a double/integer (example: 75.350, 74.430, Thursday, Friday) and since this algorithm is designed for integers. Is there a way to convert it please?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
StreamReader sh1Open = new StreamReader("c:..\\Files\\SH1_Open.txt");
string Sh1OpenString = sh1Open.ReadToEnd();
int[] x = { Convert.ToInt32(Sh1OpenString) };
int j;
int temp;
for (int i = 1; i < x.Length; i++)
{
j = i - 1;
while (j >= 0 && x[j]>x[j+1])
{
temp = x[j];
x[j] = x[j + 1];
x[j + 1] = temp;
j = j - 1;
}
}
for (int i = 0; i < x.Length; i++)
{
Console.WriteLine(x[i]);
}
Console.ReadKey();
}
}
}
The best way is probably using generic method with IComparable constraint.
T[] InsertionSort(T[] x) where T : IComparable<T>
{
for (int i = 0; i < x.Length-1; i++)
{
int j = i+1;
while (j>0)
{
if (x[j-1].CompareTo(x[j]) > 1)
{
T temp = x[j-1];
x[j - 1] = x[j];
x[j] = temp;
}
j--;
}
}
return x;
}
or using algorithm from http://www.codecodex.com/wiki/Insertion_sort
static void InsertSort(IComparable[] array)
{
int i, j;
for (i = 1; i < array.Length; i++)
{
IComparable value = array[i];
j = i - 1;
while ((j >= 0) && (array[j].CompareTo(value) > 0))
{
array[j + 1] = array[j];
j=j-1;
}
array[j + 1] = value;
}
}
Also, there is probably a bug in this line:
int[] x = { Convert.ToInt32(Sh1OpenString) };
because you're trying to convert whole file to one integer.
Since in C# the comparison operators are defined for strings you could just replace all relevant int variables with string variables. The algorithm should run the same (albeit a bit slower)

Where can I find C# code for console application designs. The star pyramid for example [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I want to know if there's a website that provides code star designs for my console application. For example I want a code that can output the pyramid using for loops:
*
***
*****
*********
Or code that can output the half-life logo using for loops.
It doesn't matter where the code was created, as long as I can understand the for loop it is ok.
using System;
using System.Collections.Generic;
using System.Text;
namespace Pyramid
{
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("Enter the Height of the Pyramid: ");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
for (int j = n; j >= i; j--)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("*");
}
for (int m = 2; m <= i; m++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} Console.Read();
}
}
}
int height = 5;
for (int count = 1; count <= height; count++)
Console.WriteLine(new String('*', count * 2 - 1).PadLeft(height + count));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PyramidUsingForLoops
{
class StarPyramid
{
static void Main(string[] args)
{
int Row = 5;
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Row-(i+1); j++)
{
Console.Write(" ");
}
for (int k = 0; k < 2*i+1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
namespace Program
{
class Program
{
static void Main(string[] args)
{
// print triangle
int n = 5;
/* three phrase:
* first: find first location of the line
* second: print increasing
* third: print decreasing */
int k=n;
for (int i = 0; i <n; i++) //print n line
{
// first
for (int j = 1; j <= k; j++) Console.Write(" ");
// second
for (int j = 1; j <= i; j++) Console.Write(j);
// third
for (int j = i + 1; j >= 1; j--) Console.Write(j);
k--;
Console.WriteLine();
}
}
}
}
int rowCount = 5;
for (int i = 0; i < rowCount; i++)
{
Console.Write(new string(' ', rowCount - i - 1));
Console.Write(new string('*', 2 * i + 1));
Console.WriteLine();
}
Regarding your comment to the question: you're saying that one can do "some complex stuff" with the for-loop you're trying to discover. I have to say: you seem to be on the wrong track. The for-loop has always the same simple structure:
for (Type variable = startvalue; condition; action)
{
// Do stuff
}
The "complex" stuff is to sometimes find out what condition is or what action to take. condition may be anything that evaluates to boolean and action may be anything too.
So the "complex" stuff has nothing to do with the structure of the for-loop itself. You could also write the following:
for (int i = 0; DateTime.Now < new DateTime(2009, 12, 31); i++)
{
Console.WriteLine("I've iterated {0} times before 31st December 2009!", i);
}
The condition does not even consider the i variable, but still it is a valid for-loop.

Categories

Resources