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
I have List which contains:(without remain column data)
Recive Pay Remain
---------------------------------------------
2 0 0
0 4 0
3 0 0
4 0 0
And I have initialRecieve=1 variable
How can I calculate the Remain Column in C# to obtain the below result?
Recive Pay Remain
---------------------------------------------
2 0 3 (initialRecieve+Recive-Pay)
0 4 -1 (Remain+Recive-Pay)
3 0 2 (Remain+Recive-Pay)
4 0 6 (Remain+Recive-Pay)
I create a simple console app depends what you need
class doc{
public int Pay{get;set;}
public int Receive{get;set;}
}
public class Program
{
public static void Main(string[] args)
{
List<doc> lst = new List<doc>();
lst.Add(new doc(){Receive=2,Pay=0});
lst.Add(new doc(){Receive=0,Pay=4});
lst.Add(new doc(){Receive=3,Pay=0});
lst.Add(new doc(){Receive=4,Pay=0});
int remain = 1; // initialRecieve=1
var result = (from line in lst
select new {
Receive = line.Receive,
Pay = line.Pay,
Remain = (remain = remain + line.Receive - line.Pay)
}).ToList();
foreach(var item in result){
Console.WriteLine(item);
}
}
}
And the result would be:
{ Receive = 2, Pay = 0, Remain = 3 }
{ Receive = 0, Pay = 4, Remain = -1 }
{ Receive = 3, Pay = 0, Remain = 2 }
{ Receive = 4, Pay = 0, Remain = 6 }
Check it on DotNetFiddle
Simply do something like the below:
for (var i = 0; i < DataTable.Rows.Count; i++)
{
if (i != 0)
{
DataTable.Rows[i][2] = (DataTable.Rows[i - 1][2] + DataTableRows[i][0]) - DataTable.Rows[i][1];
}
else if (i == 0) DataTable.Rows[i][2] = (InitialRecieve + DataTable.Rows[i][0]) - DataTable.Rows[i][1];
}
That should calculate what you need with the data in memory.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Question:
Have the function ClosestEnemy(arr) take the array of numbers stored in arr and from the position in the array where a 1 is, return the number of spaces either left or right you must move to reach an enemy which is represented by a 2. For example: if arr is [0, 0, 1, 0, 0, 2, 0, 2] then your program should return 3 because the closest enemy (2) is 3 spaces away from the 1. The array will contain any number of 0's and 2's, but only a single 1. It may not contain any 2's at all as well, where in that case your program should return a 0.
My problem Console.Readline() and return.I dont know proper use.
I get to this errors, I put a comment line on the code
CS1503 C# Argument 1: cannot convert from 'int' to 'int[]'
CS0165 C# Use of unassigned local variable 'result'
examples:
Input: new int[] {1, 0, 0, 0, 2, 2, 2}
Output: 4
Input: new int[] {2, 0, 0, 0, 2, 2, 1, 0}
Output: 1
public static int ClosestEnemy1(int[] arr)
{
int result;
int counterright = 0;
int counterleft = 0;
int locationleft = 0;
int location1 = 0;
int locationright = 0;
int rightclosest;
int leftclosest;
int i;
// code goes here
for(i = 0; i < arr.Length; i++)
{
if (arr[i] == 1)
{
location1 = i;//1'in konumu belirlendi
}
}
//sağa kontrol et
while (arr[i]!= '\0' )//saga bak
{
i = location1;
i++;
if (arr[i] == 2)
{
counterright = 1;
locationright = i;
break;
}
}
for (i = location1; i >= 0; i--)
{
if (arr[i] == 2)
{
counterleft = 1;
locationleft = i;
break;
}
}
if(counterright == 0 && counterleft == 0)
{
result=0;
}
if(counterright == 0 && counterleft == 1)
{
result = location1 - locationleft;
}
if(counterright == 1 && counterleft == 0)
{
result = locationright - location1;
}
if(counterright == 1 && counterleft == 1)
{
leftclosest = location1 - locationleft;
rightclosest = locationright - location1;
if (leftclosest > rightclosest)
{
result = leftclosest;
}
else
{
result= rightclosest;
}
}
return result; //it's error!!!!!!!
}
static void Main()
{
// keep this function call here
Console.WriteLine(ClosestEnemy1(Convert.ToInt32(Console.ReadLine())));//it's errorr!!!!
}
Try these corrections in your code:
1) chnage input type to string : public static int ClosestEnemy1(string inputString)
, make sure you leave the comment for user to enter input like this: 1, 0, 0, 0, 2, 2, 2
2) inside the function make int arr[] by String.Split():
string[] stringNumbers = inputString .Split(',');
int[] arr = Array.ConvertAll(stringNumbers, s => int.Parse(s));
3) change this line :
int result=0; to assign the default value to result .
4) assign i=0 or what you think better before while, becuse the last assigned value for i is 7 and it will has an error.
//sağa kontrol et
i = 0;
while (arr[i] != '\0')//saga bak
5) in the main() function after you get console result, put a Console.ReadKey(); to you see the result.
I'm currently stuck on this problem the goal is to be able to print out a pyramid depending on the input number.
Ex: input = 3
It should look like this:
1
23
I'm able to make it work with some numbers but it doesn't work with other inputs.
The current logic that I follow is by dividing the input by 2 then add the remainder to the next one to be divided.
Example input: 10
10 / 2 = 5 remainder: 0
5 / 2 = 2 remainder: 1
2 + 1 (previous remainder)/ 2 = 1 remainder: 1
1
Whereas it would look like this:
5
2
2
1
The rules of the pyramid that I'm trying to make only needs to have one or two differences per row.
That's why I'll be needing to deduct 1 from 5 and add it to the next one:
4
3
2
1
Thus having pyramid like this: 10
****
***
**
*
The problem is this approach isn't applicable on other inputs and I'm having a hard time on finding a different approach for this.
This code works for your example, although it might be improved to detect whenever a number is not suitable to print a piramid.
static void Main(string[] args)
{
while (true)
{
Console.Write("Enter a number:");
int input = int.Parse(Console.ReadLine());
List<List<string>> piramid = new List<List<string>>();
int rowNumber = 1, dotsCount = 0;
while (dotsCount < input)
{
List<string> row = new List<string>();
for (int i = 1; i <= rowNumber && dotsCount < input; i++)
{
row.Add("*");
dotsCount++;
}
piramid.Add(row);
rowNumber++;
}
Console.WriteLine("Piramid");
Print(piramid);
Console.WriteLine("Piramid (inverted)");
PrintInverted(piramid);
}
}
static void Print(List<List<string>> piramid)
{
foreach (var item in piramid)
{
item.ForEach(Console.Write);
Console.Write("\n");
}
}
static void PrintInverted(List<List<string>> piramid)
{
for (int i = piramid.Count - 1; i >= 0; i--)
{
List<string> item = piramid[i];
item.ForEach(Console.Write);
Console.Write("\n");
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
im generating a list of numbers such as 100 101 102 103 110 111 112 130 131 132 and i want to remove numbers that are sequential till the next number but with a specific tolerance...
so with a tolerance of 3 for example my output will be
100 102
110 112
130 132
the tolerance value must be adjustable...
ADRS is my list of int's,
ADRSC is my list of strings after being cleaned
for (int v = 0; v <= ADRS.Count - 2; v++)
{
if (v == 0) //adds first number
ADRSC.Add(ADRS[v].ToString());
if (ADRS[v]-(int.Parse(ADRSC.Last())) > 3)
{
ADRSC.Add(ADRS[v].ToString());
}
}
in short:
i want to remove the numbers inbetween, that have a difference of under 3
This worked
adrCache = Nums.First();
for (int x=1; x < Nums.Count-2; x++)
{
if ((Nums[x+1] - adrCache) <= 4)
{
adrCache = Nums[x+1];
Nums[x] = 0; //ill then just remove all zeros
}
else {
adrCache = Nums[x];
}
}
Here's a shot at what you're looking for, but the question is a little unclear to me:
public static List<int> RemoveSequential(List<int> items, int tolerance)
{
if (items == null || items.Count < 2) return items;
var result = new List<int> {items.First()};
var sequenceCount = 0;
for(int i = 1; i < items.Count; i++)
{
if (Math.Abs(items[i] - items[i - 1]) == 1)
{
sequenceCount++;
if (sequenceCount == tolerance - 1)
{
result.Add(items[i]);
}
}
else
{
sequenceCount = 0;
result.Add(items[i]);
}
}
return result;
}
Usage would look like:
public static void Main(string[] args)
{
var input = new List<int> {100, 101, 102, 103, 110, 111, 112, 130, 131, 132};
var output = RemoveSequential(input, 3);
Console.WriteLine($"Input: {string.Join(", ", input)}");
Console.WriteLine($"Output: {string.Join(", ", output)}");
GetKeyFromUser("\nDone! Press any key to exit...");
}
Output
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 4 years ago.
Improve this question
I have a integer array that shows the nodes of a path : {1,2,3,4,5}
1 --> 2 --> 3 --> 4 --> 5
But some nodes of this path can be optional.
For example node 4 is optional.
So, I can use this path 1 --> 2 --> 3 --> 4 --> 5
or this path 1 --> 2 --> 3 --> 5 to reach my destination.
I want to produce all path combinations.
//ProduceCombinations(int[] path,int[] possibleNodes)
{1*,2,3,4*,5*}
12345
-2345
123-5
-23-5
1234-
-234-
123--
-23--
12345
-2345
123-5
-23-5
1234-
-234-
123--
-23--
Here you go:
static void Main(string[] args)
{
int[] pathNodes = new int[] {1,2,3,4,5};
int[] optionalNodes = new int[] { 1, 4, 5 };
List<int[]> combies = ProduceCombinations(pathNodes, optionalNodes);
}
public static List<int[]> ProduceCombinations(int[] PathNodes, int[] OptionalNodes)
{
List<int[]> results = new List<int[]>();
results.Add((int[])PathNodes.Clone());
int index = 0;
for (int j = 0; j < OptionalNodes.Length; j++)
{
while (PathNodes[index] < OptionalNodes[j]) index++;
int lenght = results.Count;
for(int i = 0; i < lenght; i++)
{
var newSol = (int[])results[i].Clone();
newSol[index] = 0;
results.Add(newSol);
}
}
return results;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to solve this puzzle with a more elegant code in C#.
using System;
public class Program
{
public static void Main()
{
int numero = 12345678;
int check = CountHoles(numero);
Console.WriteLine(check);
}
public static int CountHoles(int num){
int roles = 0;
for (int i=0; i < num.ToString().Length; i++ ){
string ver = "";
ver = num.ToString().Substring(i,1);
if (ver.Contains("0") || ver.Contains("4") || ver.Contains("6") || ver.Contains("9")){
roles++;
}
if (ver.Contains("8")){
roles = roles+2;
}
}
return roles;
}
}
.Net Fiddle: https://dotnetfiddle.net/3s8ucy
Thanks :)
I like to use dictionary maps in place of if-statements. You can use LINQ to get an array of digits from the string and then another LINQ statement to sum the results (or you can combine the two into a single LINQ statement).
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
private static Dictionary<int, int> _holeMap = new Dictionary<int, int>
{
{ 0, 1 },
{ 1, 0 },
{ 2, 0 },
{ 3, 0 },
{ 4, 1 },
{ 5, 0 },
{ 6, 1 },
{ 7, 0 },
{ 8, 2 },
{ 9, 1 }
};
public static void Main()
{
int numero = 12345678;
int check = CountHoles(numero);
Console.WriteLine(check);
}
public static int CountHoles(int num){
var digits = num
.ToString()
.Select(c => int.Parse(c.ToString()));
return digits.Sum(d => _holeMap[d]);
}
}
https://dotnetfiddle.net/gI5f02
You do not need to convert the number to string, you can improve method CountHoles like this:
int holes = 0;
do{
float tmp = (float) num / 10;
num /= 10;
int ver = (tmp - num) * 10;
if (ver == 0 || ver == 4 || ver == 6 || ver == 9) holes++;
if (ver == 8) holes += 2;
}while (num != 0)
return roles;