I have a nested for loop in which 'j' represents the line number and 'i' represents the multiples of 3 below 1000.
I want it to display the line number then the multiple. For example:
1 3
2 6
3 9
4 12
...
333 999
right through to multiple 999
but it just dosent display the right line numbers and the multiples keep looping until the line numbers reach 999
Console.BufferHeight = 4000;
for (int j = 1; j < 1000; j++)
{
for (int i = 3; i < 1000; i++)
{
Console.WriteLine(j + " " + i);
}
}
Console.ReadLine();
for (int i = 1, j = 3; j < 1000; i++, j += 3)
{
Console.WriteLine(string.Format("{0} - {1}", i.ToString(), j.ToString()));
}
It will print starting at
1 - 3
2 - 6
up to
332 - 996
333 - 999
Do you really need nested loops for this problem? A single should be enough, if I understand your right.
Console.BufferHeight = 4000;
for (int j = 1; j < 1000; j++)
{
Console.WriteLine(j + " " + (j * 3));
}
Console.ReadLine();
You don't need nested loop for that. You can just use following code.
Console.BufferHeight = 4000;
for (int j = 1; j < 1000; j++)
{
Console.WriteLine(j + " " + j*3);
}
Console.ReadLine();
Are you just trying to do something like this, or did I miss the point entirely?
for (int i = 1; i < 1000; i++)
{
Console.WriteLine(string.Format("{0}: {1}", i, (i * 3)));
}
Well, you're looping from i = 3 to i = 999 inside of a loop from j = 1 to j = 999. This will result in 995,004 lines being written tot he console.
You want something like this:
for( int i = 1; i <= 1000; ++i )
{
Console.WriteLine( "{0} {1}", i, i * 3 );
}
Related
I can not have infinite points stored in the computer, but what I mean by that is the max value of Int64 (long).
I could use a nested loop, but that would take an eternity to go to the next line, so I found another way.
All the points whose x and y values add upto n are on a diagonal.
(0,0) - 0
(1,0),(0,1) - 1
(2,0),(1,1),(0,2) - 2
(all of these are diagonals)
So we could iterate through all values of n, from 0 to max value of long, that would give us all the points on all the diagonals.
So I wrote the code to do this.
public void Main()
{
for(long i = 0; i < Int64.MaxValue; i++)
{
long[] a = new long[(i + 1)];
long[] b = new long[(i + 1)];
for(long j = 0; j <= i; j++)
{
a[j] = j;
b[j] = (i - a[j]);
}
f(a,b);
}
}
public void f(long[] a, long[] b)
{
string toPrint = "";
for(int i = 0; i < a.Length; i++)
{
toPrint += "(" + a[i] + "," + b[i] + "),";
}
Console.Write(toPrint + "\n\n");
}
It does the job for a grid of 2 dimensions, but I want it to work for n dimensions, the same idea applies
I need an algorithm that prints 3 lines, first is for numbers from 1-20 divisble by 2, second is for divisble by 3, third is for divisble by 4.
for (int i = 1;i <20 ; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
}
i can't print the result in 3 seperate lines
You want this code:
for(int j = 2; j <= 4; j++)
{
for(int i = 1; i <= 20; i++)
{
if (i % j == 0)
Console.Write("{0} ", i);
}
Console.WriteLine();
}
Think why this is a correct and elegant way to do your task.
Basically i'm trying to multiply each element of the first array with each element of the second array and then store it all at the end as a total. I'm pretty new to coding so just trying to learn and this one really has me stuck. This is the example of what it should eventually do.
ExampleArray1 = 5,6,7,8
ExampleArray2 = 2,3,4
(5*2)+(5*3)+(5*4) + (6*2)+(6*3)+(6*4) + (7*2)+(7*3)+(7*4) + (8*2)+(8*3)+(8*4) = 234
My code
int[] firstArray = { 5, 6, 7, 8 };
int[] secondArray = { 2, 3, 4 };
int[] thirdArray = new int[firstArray.Length * secondArray.Length];
for (int i = 0; i < firstArray.Length; i++)
for (int j = 0; j < secondArray.Length; j++)
{
thirdArray[i * firstArray.Length + j] = firstArray[i] * secondArray[j];
Console.WriteLine(thirdArray[i * firstArray.Length + j]);
}
You dont need a third array, you can just sum the results
var total = 0;
for (int i = 0; i < firstArray.Length; i++)
for (int j = 0; j < secondArray.Length; j++)
{
total += (firstArray[i] * secondArray[j]);
}
Console.WriteLine(total);
However you forgot to minus one form the length, in the third array index.
i.e to get the index you need i * (firstArray.Length - 1) + j
int[] thirdArray = new int[firstArray.Length * secondArray.Length];
for (int i = 0; i < firstArray.Length; i++)
for (int j = 0; j < secondArray.Length; j++)
{
thirdArray[i * (firstArray.Length - 1) + j] = firstArray[i] * secondArray[j];
}
Console.WriteLine(thirdArray.Sum());
You can apply some basic algebra to simplify this:
var total = 0;
var array1Total = 0;
var array2Total = 0;
for (int i = 0; i < firstArray.Length; i++)
{
array1Total += firstArray[i];
}
for (int j = 0; j < secondArray.Length; j++)
{
array2Total += secondArray[j];
}
total = array1Total * array2Total;
Console.WriteLine(total);
The reason is that if you expand (x0+x1+x2+x3...)*(y0+y1+y2+...) then you can see that you will multiply x0 by each of the y, then multiply x1 by each of the y, and so on. So you'll get each of the elements in the first brackets multiplied by each of the elements in the second brackets.
While this may not seem much different it will be considerably better for large arrays. if the length of your arrays are m and n then by the nested loops method you'll have m*n loop iterations. With the above technique you will have m+n. For small values this isn't a big deal. If you have arrays of thousands of items then the above will be significantly faster.
Of course, there is an even easier way to do the above:
var total = firstArray.Sum()*secondArray.Sum();
I want to print the following pattern:-
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
But I am getting the following output:-
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Here is the Code:
static void Main(string[] args)
{
int spacelimit = 13, num = 1, n = 5;
for(int i = 1; i<=n; i++)
{
for (int space = spacelimit; space >= i; space--)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("{0,3:D} ",num++);
}
spacelimit = spacelimit - 3;
Console.WriteLine();
}
Console.ReadKey();
}
What I am doing wrong with the spaces? I am unable to do it.
This will do the trick
int spacelimit = 13, num = 1, n = 5;
for(int i = 1; i <= n; i++)
{
for(int space = spacelimit; space >= i; space--)
{
Console.Write(" ");
}
for(int k = 1; k <= i; k++)
{
Console.Write("{0,2:D} ", num++);
}
spacelimit = spacelimit - 2;
Console.WriteLine();
}
Console.ReadKey();
I have just changed 3 to 2 i.e. spacelimit - 2 and {0,2:D}
Yes changing spacelimit also solves the issue with trailing spaces but this solution worked as expected ... please have a look on the image.
If we make space>= i-3 in for loop, as shown below, it works fine. Please check. Thanks.
int spacelimit = 13, num = 1, n = 5;
for (int i = 1; i <= n; i++)
{
for (int space = spacelimit; space >= i - 3; space--) // HERE, I MADE i-3
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("{0,3:D} ", num++);
}
spacelimit = spacelimit - 3;
Console.WriteLine();
}
Console.ReadKey();
Set the initial value of spacelimit to 16.
Hi i am new to programming and i currently have this code:
namespace Patterns
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 4; i++)//'rows'
{
for (int h = 1; h <= 9 - (i*2)+1; h++)
{
Console.Write("#");
}
Console.WriteLine("\n" );
}
}
}
}
This produces this output:
########
######
####
##
the number of hashes is correct as i am going from 8, 6, 4, 2 but i need to add an extra space every time i go onto a new line. How do i make it so the output is as follows?
########
######
####
##
Thanks,
Umer
From your code you could modify it to do the following in the inner for loop:
for (int j = 0; j < i - 1; j++) {
Console.Write(" ");
}
for (int h = 1; h <= 9 - (i*2)+1; h++) {
Console.Write("#");
}
Console.WriteLine("\n" );
As a note you should probably use StringBuilder to do this as I believe it is quite inefficient to constantly call Console.WriteLine.
The code could be modified further:
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 4; i++) {
for (int j = 0; j < i - 1; j++) {
sb.append(" ");
}
for (int h = 1; h <= 9 - (i*2)+1; h++) {
sb.append("#");
}
sb.append("\n" );
}
Console.WriteLine(sb.toString());
Introduce variables, start your rows at 0 and repeat the string for each row number.
This can also be applied to the string printing the hashes:
static void Main(string[] args)
{
int rows = 4;
int columns = 9;
for (int i = 0; i < rows; i++)
{
// Print a string with `i` spaces.
Console.Write(new String(' ', i));
int hashes = columns - ((i + 1) * 2) + 1;
Console.Write(new String('#', hashes));
Console.WriteLine();
}
}
Basically, just add space in front of your hash characters.
######## Row 1 (i=1), 0 Space
###### Row 2 (i=2), 1 Space
#### Row 3 (i=3), 2 Spaces
## Row 4 (i=4), 3 Spaces
In this case, you need "i-1" spaces for each rows. (Actually, it's (8 - charater count) / 2) and character count was 9 - (i*2) + 1, so ( 8 - 9 + i * 2 - 1 ) / 2 = (i * 2 - 2) / 2 = i - 1 )
So just make loop to add spaces before print hash chracters.
namespace Patterns
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 4; i++)//'rows'
{
for (int j = 0; j < i -1; j++) {
Console.Write(" ");
}
for (int h = 1; h <= 9 - (i*2)+1; h++)
{
Console.Write("#");
}
Console.WriteLine("\n" );
}
}
}
}
You could do something like this:
for (int i = 1; i <= 4; i++)//'rows'
{
for (int h = 1; h <= 9 - (i*2)+1; h++)
{
Console.Write("#");
}
Console.WriteLine("\n" );
for (int y = i; y > 0; y--)
{
Console.Write(" ");
}
}