I don't understand why this loop starts at 8 and 18 when I output it, but when I try "i < 10 and j < 10", it starts at 2 and 2. Can someone explain this to me?
class Program
{
static void Main(string[] args)
{
for (int i = 2; i < 100; i++)
{
for (int j = 2; j < 100; j++)
{
Console.WriteLine("i = {0} and j = {1}", i, j);
}
}
}
}
Screenshot of my issue:
It is because you exceed the number of lines that your console is set to display at once.
The property is called Console.BufferHeight.
Try writing Console.WriteLine(Console.BufferHeight); to see the number of lines your console is set to display at once.
You can change it simply by writing setting it to your desired value, e.g. Console.BufferHeight = 20000; or changing the settings of the console by accessing the properties in the manner, that #Greg elaborated on.
Are you sure that it isn't starting at i = 2 and j = 2? You are running this loop so many times that your console output may not show that it starts at 2,2, even though it does. Try setting a breakpoint in the second for loop, I think you'll see it is starting at 2, 2. That's because the buffer size is too small.
Here's what you can do:
Run your project.
Right-click on the title bar-> Properties-> Layout.
Set your height to something really large.
You can refer to this question for more.
Related
I am new to Kattis and trying solve the challenge Jumbo Javelin with C# but I get run time error even though I am exiting my program with 0.
Here is my code:
using System;
namespace JumboJavelin
{
class Program
{
static void Main(string[] args)
{
int l = int.Parse(Console.ReadLine());
int sum = 0;
int loss = 1;
for (int n = 0; n <= 100; n++)
{
sum += l;
if((n + 1) % 2 == 0 && !n.Equals(0))
{
sum -= ((n + 1) / 2)*loss;
}
try
{
l = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
break;
}
}
Console.WriteLine(sum);
Environment.Exit(0);
}
}
}
Can someone please help me? I know my solution eventually outputs the wrong answer but right now I am trying to get rid of the run time error since it works perfectly fine on Visual Studio 2019.
All help is appreciated. Thanks
If you change the error your catch to Exception you will see that it is actually a wrong answer, so something goes wrong. You do exit your program with an exit code 0 at the bottom of your code, however, it throws an Exception somewhere else. Two things to consider:
First, the problem statement explains that as a first input, you get an integer N (this N indicates the amount of steel rods he has) and CAN BE between 1 and 100 (1 not inclusive hence, 1<N<=100). You now have a loop that always loops from 1 to 100. And the loop should loop from 1 up to and including whatever value N is. Try to see what you can do about that.
Second you read the input at the "tail" of the loop. This means that after the last cycle you do another read input. And that you do not read the first l. Try changing reading the input for l at the beginning of the loop. (and the first input you read in your Main, should not be for l (see point 1)).
See how far this gets you, is something is not clear please let me know.
As Kasper pointed out, n is dynamically provided on the first line; don't assume you have 100 lines to collect with for (int n = 0; n <= 100; n++).
There's no need to handle errors with the format; Kattis will always adhere to the format they specify. If something goes wrong parsing their input, there's no point trying to catch it; it's up to you to fix the misunderstanding. It's better to crash instead of catch so that you won't be fooled by Kattis telling you the failure was due to a wrong answer.
A simple approach is as follows:
Collect n from the first line of input; this is the preamble
Loop from 0 to n, collecting each line
Accumulate the values per line into a sum
Print sum - n + 1, the formula you can derive from their samples
using System;
class JumboJavelin
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += int.Parse(Console.ReadLine());
}
Console.WriteLine(sum - n + 1);
}
}
You can use this pattern on other problems. You won't always aggregate one result after n lines of input though; sometimes the input is a single line or you have to print something per test case. Some problems require you to split and parse each line on a delimiter.
Regardless of the case, focus on gathering input cleanly as a separate step from solving the problem; only combine the two if you're confident you can do it without confusion or if your solution is exceeding the time limit and you're sure it's correct otherwise.
Why doesnt this for loop work while the while loop does
for (int i = 0; i > 10; i++)
{
Console.WriteLine(i);
}
int j = 1;
while(j != 11)
{
Console.WriteLine(j);
j++;
}
It's easy to mix up comparers. A good way to remember for me is the heart. <3 because I know that's read as "Less than three".
It's easy to get confused when starting out, you just mixed up the condition.
As of why your for does not work is because it starts with i = 0 then checks if if 0 is greater than 10 which is not that's why it will not excute the loop body and terminates the loop.
In your while loop, initially j = 1 then while checks if 1 is not equal to 11, which is true so loop body will execute until j is not equal to 11.
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 5 years ago.
Improve this question
First of all, i stored 90 data in a list.
After that I want to separate those data to 30 data each set, which means that I have 3 set of data that consist 30 data each.
By using the code below, I get what I want but i don't really understand how's it works.
for (int i = 0; i < 3; i++)
{
for (int j = 0 + (i * 30); j < 30 + (i * 30); j++)
{
}
}
For what I understand is the first loop split the data in to 3 set and the second loops is for 30 data for each set.
Let's say if i have 1000 data now and i want to make it to 100 data each set , so i have 10 set data in total.
So,
for (int i = 0; i < 10; i++)
{
for (int j = 0 + (i * 100); j < 100 + (i * 100); j++)
{
}
}
Why do for (int i = 0; i < 10; i++) / for (int i = 0; i < 3; i++) come first instead of the second loops?
Is it that every time if I want to separate a brunch of data to a few set, the first loops
that i have to declare is how many set i want to data to be???
sometimes, it's better to refactor and have explaining variables ( or functions)
Also, added Console Output to help show what it's doing
int sets = 3;
int setSize = 30;
for (int i = 0; i < sets; i++)
{
Console.Write($"Set {i} : ");
int from = i * setSize;
for (int j = from; j < from + setSize; j++)
{
Console.Write($" {j}");
}
Console.WriteLine();
}
Is it that every time if I want to separate a brunch of data to a few set, the first loops that i have to declare is how many set i want to data to be???
Yes, it's the convenient way to do that,
This may show a more clear idea of this:
for (int i = 0; i < 3; i++)
{
// i is the page
for (int j = 30*(i); j < 30*(i+1); j++)
{
// 30 is the step and j is the offset
}
}
Sometimes This structure is called pagination
which the first loop defines the page and second inner loop defines the step and together they calculate the offset
Technically you can write your program any way you like, as long as it works.But remember, others may need to read your code (for example you left the company and others have taken up your post), and you code in a way that only YOU can understand, thats not a well written code.
There are two main criteria:
Readability: How easy is the code to read?
Writeability:How easy is the code to write?
Balancing the two criteria makes a better coding style.
In your case, breaking it into two for loops (first one indicates the number of sets, second one indicates number of entries) makes perfect sense and is easy to understand.
So, I have this for loop:
double spec = 0, tot = 0;
for (int i = 0; i < omega_algo.Length; i++)
{
if (omega_algo[i] > 0)
spec = Math.Sqrt(omega_algo[i]);
else
spec = 0;
tot += spec;
}
Where myArray.Length = 50.
I get an IndexOutOfRangeException while debugging and see that i is 50.
So, the for loop is entering when it shouldn't ( i < myArray.Length is false )!
This exception only occurrs ocasionally, which makes it even more weird.
Does someone have an explanation/fix for this? Am I missing something or could this be a weird Visual Studio bug?
EDIT:
I've edited the for loop to show the code.
No i is being incremented and omega_algo array is not changing at all.
EDIT:
Based on the comments below, I wrote a sample app, and your code should work as is.
If your array really does have a length of 50, then the value of i will never be 50. The only way this would be possible is if you are changing the value of i inside the loop.
Can you provide more code to show some context of how/where this is being used? How the array is being defined etc?
Your code should work if executed on a single thread. Do you have any thread or asynchrone jobs that are editing the array?
If so, just lock the array before accessing it and before editing it.
lock(myArray)
{
for (int i = 0; i < myArray.Length; i++)
{
int someVar = myArray[i]; //this is where exception is thrown when i=50
}
}
EDIT:
Since omega_algo array is not changing at all, this is not a threading issue.
i'm wondering i started programming 6 monthes ago but never noticed that cmd can't show all results and it seems that there is limit. for example try this.
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i);
}
you may not noticed at first but if you run this it only covers from 701 to 999 and you can't find 0 to 700, it seems that cmd only can show 299 lines and the previous result will be hide if you have more than that line. Am i correct? What is the problem? whats the reason for that?
Yes the command prompt has properties:
And under those properties you can adjust the default buffer higher or lower as needed:
You would need to set the buffer to the size of the loop to see all the entries.
http://msdn.microsoft.com/en-us/library/system.console.setbuffersize(v=vs.110).aspx
e.g.
Console.SetBufferSize(80, 1000);
You need to adjust the Console.BufferHeight property (see msdn).
Console.BufferHeight = 1200; //set the bufferheight to 1200 lines
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i);
}