I am have been trying to do some problem-solving practice using C# and also new to C#. So here is the problem, given the array arr= [1,1,0,-1,-1] there are 5 elements, two positive, two negative and one zero digits. Their ratios would be 2/5=0.400000 for two positive digit numbers, 2/5 = 0.400000 for two negative numbers and 1/5 = 0.200000 for 1 zero. It should be printed as
0.400000
0.400000
0.200000
What I am trying to do actually is, count all negative number from an array and divide it by the total number of elements in that array. Same case for positive and zeros in that array. And finally, it would print out the 3 results. Here is what I have tried. I am not getting the desired output.
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the plusMinus function below.
static void plusMinus(int[] arr) {
int[] positiveArray = new int[arr.Length];
int[] negativeArray = new int[arr.Length];
int[] zeroArray = new int[arr.Length];
for(int i=0; i <arr.Length; i++){
if(arr[i]>0){
arr[i]=positiveArray[i];
}else if(arr[i]<0){
arr[i] = negativeArray[i];
}else if(arr[i] == 0){
arr[i] = zeroArray[i];
}
}
float postive = (float)positiveArray.Length/arr.Length;
float negative = (float)negativeArray.Length/arr.Length;
float zero = (float)zeroArray.Length/arr.Length;
Console.WriteLine(postive);
Console.WriteLine(negative);
Console.WriteLine(zero);
}
static void Main(string[] args) {
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp))
;
plusMinus(arr);
}
}
Output example:
