Adding new value to String array based on condition [closed] - c#

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 7 years ago.
Improve this question
String Array contains values like {'2','4','8'}
if 8 is present, want to add 999 to it,if string array contains 999 want to add 8 to it.
If both 8 and 999 are present in array no need to add any value to array.

Have a look at the following:
public string[] Add8or999(string[] source)
{
string[] output = source;
if (source.Contains("8") && source.Contains("999"))
{
// what to do here?
}
else if (source.Contains("8"))
{
output = new string[source.Length + 1];
for (int i = 0; i < source.Length; i++)
{
output[i] = source[i];
}
output[source.Length] = "999";
}
else if (source.Contains("999"))
{
output = new string[source.Length + 1];
for (int i = 0; i < source.Length; i++)
{
output[i] = source[i];
}
output[source.Length] = "8";
}
return output;
}
Basic Usage:
string[] s = Add8or999(new string[] {"8", "9", "10"});

Related

plusMinus function from Hacker Rank [closed]

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 1 year ago.
Improve this question
I'm trying to solve a task from HackerRank, but I'm not able to solve it. Any help is appreciated!
public static void plusMinus(List<int> arr)
{
float noOfPositive = 0;
float noOfNegative = 0;
float noOfZero = 0;
float length = arr.Count;
for(var i = 0; i < arr.Count; i++){
if(i > 0){
noOfPositive += 1;
}else if(i == 0){
noOfZero += 1;
}else{
noOfNegative += 1;
}
}
Console.WriteLine($"{noOfPositive/length}\n{noOfNegative/length}\n{noOfZero/length}");
input:
6
-4 3 -9 0 4 1
My Result:
0.8333333
0
0.1666667
Expected:
0.500000
0.333333
0.166667
You are checking the value of i not arr[i].
So your code should be:
for(var i = 0; i < arr.Count; i++){
if(arr[i] > 0){
noOfPositive += 1;
}else if(arr[i] == 0){
noOfZero += 1;
}else{
noOfNegative += 1;
}
}
And your expected values are wrong, you are putting in 7 items and expecting half of them to be positive so 3.5 items are positive?

How do you find the positive sum of an array that also has negative numbers? [closed]

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 2 years ago.
Improve this question
public static int PositiveSum(int[] arr)
{
int sum = 0;
for(int i = 0; i > arr.Length; i++)
{
sum+=arr[i];
}
return sum;
}
Example:
[1,-4,7,12] => 1 + 7 + 12 = 20
Note: if there is nothing to sum, the sum defaults to 0.
One way, with minimum changes to your code, is to add a condition inside the for loop that sums the values allowing only positive numbers to be added.
Something you must change is the condition in the loop, i > arr.Length will break the cycle immediately, even if Length is 0, it needs to be i < arr.Length.
public static int PositiveSum(int[] arr)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] > 0)
sum += arr[i];
}
return sum;
}
I would try filtering the arr to only contain positive numbers, then use sum on that new list:
public static int PositiveSum(int[] arr)
{
var pos = arr.Where(x => x > 0);
int sum = pos.Sum();
return sum;
}

How to replace the contents of an integer array with the index value at each index? [closed]

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
How do I replace the contents of an array of integers so that the value at each index is the index itself?
This is what I have tried so far:
var result = array;
for(int i = 0; i < array.Length; i++)
{
array.IndexOf(i);
Console.WriteLine(array.IndexOf(i));
}
return result;
Given an input array (0, 0 ,0), I am getting the following console output: 0 -1 -1. And the array output contents are (0, 0, 0) instead of (0, 1, 2).
your question is a bit confusing. But if you mean how to assign the index value to the arrays elements you could do this.
var result = array;
for(int i = 0; i < array.Length; i++)
{
array[i] = i;
Console.WriteLine(array.IndexOf(i));
}
return result;
The result will then be 0,1,2
Hope this helps.
I think the following code should help.
int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
for(int i = 0; i < myIntArray.Length; i++)
{
myIntArray[i] = i;
Console.WriteLine(myIntArray[i]);
}
correct me if i am wrong.

Array ints and replace the order of number I write [closed]

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 8 years ago.
Improve this question
I try Start the console and after i write 5 numbers is do exsepsion what wrong in code
what the console do is after I write 5 number for example 7,4,9,5,1 ---> 1,5,9,4,7
this what I wrote
static void Main(string[] args)
{
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length ; i++)
{
Console.Write("");
numbers[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < numbers.Length / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}
Console.Read();
You missed two things. First - in order to swap two array items, you need to store item somewhere. Otherwise you just replace one item with other. Second - arrays have indexes from 0 to Length - 1. So you need to subtract 1 from second item index:
for (int i = 0; i < numbers.Length / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}

C# Types of files [closed]

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 8 years ago.
Improve this question
Besides a .txt, where else i can insert data using C# ?
string file = GenerarPath(Environment.SpecialFolder.MyDocuments, "Pascal Triangle.txt");
var tw = new StreamWriter(file);
Console.WriteLine("\t\t Pascal Triangle in C#");
Console.WriteLine("\t________________________________________");
Console.Write("\nNumber of Rows: ");
int number = int.Parse(Console.ReadLine());
int x = number * 2;
for (int i = 0; i <= number; ++i, x -= 2)
{
for (int s = 0; s <= x; ++s)
tw.Write(" ");
for (int k = 0; k <= i; ++k)
//Console.Write(String.Format("{0,4:D}", formula(i, k)));
tw.Write(String.Format("{0,4:D}",pascal(i,k)));//formula(i, k)));
tw.WriteLine();
}
tw.Close();
Console.WriteLine("Finished");
Console.ReadKey();
Thank you
Almost anywhere. like databases (Sqlserver, access), word files, txt files as you have done.

Categories

Resources