I'm trying to modify the code below so each first number in column is based on the row number.
int i, sum = 0;
for (int row = 0; row < 7; row++)
{
for (i = 1; i < 6; i++)
{
sum = sum + i;
Console.Write("{0} ", i);
}
Console.WriteLine(sum);
sum = 0;
}
Console.Read();
presents this in console:
1 2 3 4 5 15
1 2 3 4 5 15
1 2 3 4 5 15
1 2 3 4 5 15
1 2 3 4 5 15
1 2 3 4 5 15
1 2 3 4 5 15
But I'm trying get like this:
1 2 3 4 5 sum
2 3 4 5 6 sum
3 4 5 6 7 sum
4 5 6 7 8 sum
and so on..
......
....
Any idea on how to solve this?
Change the inner loop to be
for (int i = row + 1; i < row + 6; i++)
Replace
sum = sum + i;
Console.Write("{0} ", i);
enter code here
with
number = number + row
sum = sum + number;
Console.Write("{0} ", number);
This way you use the variable row to keep track of the starting number for the row.
If you don't need the console statements and just the sum, you can just use no inner loop and:
sum = Enumerable.Sum(Enumerable.Range(row+1, 5));
It's LINQ
or, if you need the console statement:
Enumerable.Range(row+1, 5).ToList().ForEach(c => sum += c; Console.Write(c));
Related
Got stuck on quite simple problem in my code. I need to count what I would call a nested sums of an array. Let's take as an example the array:
[1,2,2,3,6]
I want to sum them as:
0 + 1 = 1
1 + 2 = 3
1 + 2 + 2 = 5
1 + 2 + 2 + 3 = 8
1 + 2 + 2 + 3 + 6 = 14
sum = 1 + 3 + 5 + 8 + 14 = 31
Edit:
I tried to do it with stack, but it failed
int sums = 0;
Stack<int> sum = new Stack<int>();
for (int i = 0; i < queries.Length; i++)
{
sum.Push(queries[i]);
}
for (int i = 0; sum.Count != 0; i++)
{
if(i != 0)
{
sums += sum.Pop();
}
}
You can run this task in a single loop considering when you have an array of size 5, the first element is repeating 5 times, second element repeating 4 times and etc.
int[] arr = { 1, 2, 2, 3, 6 };
int mysum = 0;
for (int i = 0; i < arr.Length; i++)
mysum += (arr.Length - i) * arr[i];
Console.WriteLine(mysum);
Output:
31
Explanation:
1 = 1
1 + 2 = 3
1 + 2 + 2 = 5
1 + 2 + 2 + 3 = 8
1 + 2 + 2 + 3 + 6 = 14
========================
5*1 + 4*2 + 3*2 + 2*3 + 1*6 = 31
You can do this much more efficiently and more easily, by multiplying each value by its reversed index + 1
For example, using LINQ
int[] arr = { 1, 2, 2, 3, 6 };
var result = arr.Reverse().Select((val, i) => val * (i + 1)).Sum();
Note that .Reverse on an array (or other Collection<T>) does not actually move any items, it just reads them backwards. So this is therefore an O(n) operation, as opposed to your original solution which is O(n2 / 2)
dotnetfiddle
You can also do this procedurally, this is almost the same as #aminrd's answer.
int[] arr = { 1, 2, 2, 3, 6 };
var result = 0;
for (var i = arr.Length - 1; i >= 0; i--)
result += arr[i] * (i + 1);
Take advantage of Linq! .Sum() will add up everything in your collection. You run that twice, once per each slice, and once per each subtotal.
var input = new [] { 1, 2, 2, 3, 6 };
var totalSum = Enumerable.Range(1, input.Length).Sum(len => input[0..len].Sum());
// totalSum is 31
Enumerable.Range gets you a collection of numbers between (and including) 1 and 5 - the possible lengths of each slice of your sub arrays. You then use the range operator [0..#] to get increasingly larger slices.
Yes, this is not as clever as aminrd's solution - it's doing all the computations manually and you're performing many slices.
I have a text file that has:
1 2 3 4 0 5 6 7 8
How do I show the data in a 2D array of size [3,3]?
I'm new to C# and any help would be great!
I've tried the code below but it doesn't work:
int i = 0, j = 0;
int[,] result = new int[3, 3];
foreach (var row in input.Split('\n'))
{
j = 0;
foreach (var col in row.Trim().Split(' '))
{
result[i, j] = int.Parse(col.Trim());
j++;
}
i++;
}
Console.WriteLine(result);
divide by 3 and convert to integer to get row, use modulo 3 to get col.
j=0;
foreach (var col in input.Trim().Split(' '))
{
result[j/3, j%3] = int.Parse(col.Trim());
j++;
}
If all your 9 numbers are on the same line, then splitting by new line will not help. You can do:
foreach (var num in input.Split(' '))
{
result[i / 3, i % 3] = int.Parse(num.Trim());
i++;
}
because:
i i / 3 (div) i % 3 (mod)
0 0 0
1 0 1
2 0 2
3 1 0
4 1 1
5 1 2
6 2 0
7 2 1
8 2 2
Write a program that fills a 15-byte array with positive double-digit random numbers. in each number, the sum of the two digits is equal to 9.
Here's what I've done so far:
int one = 0;
int two = 0;
int[] arr = new int[15];
Random rnd = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = rnd.Next(10, 99);
one = arr[0] % 10;
two = arr[0] / 10;
if (arr[i] % 2 == 0 && one + two == 9)
Console.WriteLine(arr[i]);
}
The problem with your solution is that rnd.Next(10, 99) will not always produce number with the properties that you want. you should write a code that always works.
We know that sum of digits of a number should be 9. if we assume our two-digit number to be a*10+b where a and b are digits and a + b = 9, we can randomly generate a from 1 to 9.
Then we can calculate other digit b = 9 - a.
therefor our final result would be a*10 + 9 - a which will be simplified to a*9 + 9 where a is random number from 1 to 9, here are two examples.
a=7 then 7 * 9 + 9 = 72, 7 + 2 = 9
a=3 then 3 * 9 + 9 = 36, 3 + 6 = 9
note that a is in this range 1 <= a < 9
I'm struggling to create a multiplication table that would look like this:
1 x 9 + 2 = 11
12 x 9 + 3 = 111
123 x 9 + 4 = 1111
......
123456 x 9 + 8 = 11111111
Currently I managed do to this:
#region MTABLE
for (int i = 2; i <= 8; i++)
{
int number = 1 * 9 + i;
Console.WriteLine("{0} X {1} + {2} = {3} ", 1, 9, i, number);
}
Console.ReadKey();
#endregion
And output that I get now:
1 X 9 + 2 = 11
1 X 9 + 3 = 12
1 X 9 + 4 = 13
1 X 9 + 5 = 14
1 X 9 + 6 = 15
1 X 9 + 7 = 16
1 X 9 + 8 = 17
The problem is that I don't know how to add number to 1 so next will be 12 and than next one 123...
If someone can give me any advice how to continue.
Concatenating a digit to a number can also be performed with multiplying with 10 and adding the digit:
int firstPart = 1;
for (int i = 2; i <= 8; i++)
{
int number = firstPart * 9 + i;
Console.WriteLine("{0} X {1} + {2} = {3} ", firstPart, 9, i, number);
firstPart = firstPart * 10 + i;
}
The first part of your formula (which is now set to 1) should be a variable outside the for loop scope and of type string. Each time you concatenate the i to this and then you do a Int.Parse of the string so you can multiply with it.
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 6 years ago.
Improve this question
I have a school project and I need to complete it but its not working. I have to do a nested for loop in c# , I have managed to do the earlier projects which had the result :
1 2 3 6
1 2 3 6
1 2 3 6
1 2 3 6
static void Main(string[] args)
{
for (int row = 0; row < 4; row++)
{
int x = 1;
int y = 0;
for (int column = 0; column < 3; column++)
{
Console.Write(x);
y = y + x;
x = x + 1;
}
Console.Write(y);
Console.WriteLine();
}
Console.ReadLine();
}
but now i stuck with the second project which must give an output :
1 2 3 6
2 3 4 9
3 4 5 12
5 6 7 18
please help anyone !!
for (var x = 1; x < 5; x++)
{
if (x == 4) x = 5;
Console.Write("{0} ",x);
for (var y = 1; y < 4; y++)
Console.Write("{0} ", y < 3 ? x + y : 3 * (x + 1));
Console.WriteLine();
}
EXPLANATION:
required output is:
1 2 3 6
2 3 4 9
3 4 5 12
5 6 7 18
So here are four rows and four columns
first column in each row is 1,2,3,5, so outer loop in x goes from 1 to 4, and first statement changes the last value of 4 to a 5.
The 2nd & 3rd columns are always just the first column incremented by one, then by one again, and finally, last 4th column is 3 times 1 + the first column. So inner loop in y just goes 1, 2, 3, and, for 2nd and 3rd column, prints the first column plus the y value (1, 2) for 2nd and 3rd column and three times 1 plus the first column for the last (4th) column.
So each row is, effectively
x, x+1, x+2, 3*(x+1)
and each row starts with x one greater than the row before it, except we skip row 4
The expression y < 3 ? x + y : 3 * (x + 1) is a ternary which has three parts, a Boolean condition, followed by a question mark (?), the value to generate when the Boolean is true, followed by a colon (:) and then the value to generate when the Boolean is false. So it reads like this:
if y is less than 3, output x+y, else output 3 times (x+1)
Another simpler option (which only uses one loop), might be:
for (var x = 2; x < 7; x++)
{
if (x == 5) continue; // skip row 4
Console.WriteLine("{0} {1} {2} {3} ",x-1, x, x+1, 3*x);
}
Well, here is the solution for you. But please try to understand it and next time you wont ask us to solve your homework.
int p;
for (int x = 1; x < 6; x++)
{
if (x !== 4)
{
for (int y = x; y < x + 3; y++)
{
p += y;
Console.Write(y);
}
Console.Write(p);
Console.WriteLine();
p = 0;
}
}
Console.ReadLine();
I made it a little different, so you can play with it.