0

I am generating random numbers from -1000 to 1000 (the numbers are written in numbers.txt), but I don't know how to write the number of positives, negatives, and zeroes into a new .txt file (can you help me how to do this?).

The output should look like this:

The number of all numbers: 100
The number of positive numbers: 65 (for example)
The number of negative numbers: 3
The number of zeroes: 1

    namespace ConsoleApp81
    {
        class Program
        {
            static void Generate(string nazev, int pocet, int odd, int doo)  
            {
                Random nahoda = new Random();
                StreamWriter sw = new StreamWriter(nazev, false, Encoding.Unicode);
    
                for (int i = 0; i < 100; i++)
                {
                    sw.WriteLine(nahoda.Next(-1000, 1001));
                }
                sw.WriteLine(nahoda);
                sw.Close();   
            }
            static void Main(string[] args)
            {
                Generate("numbers.txt", 100, -1000, 1000);
            }         
        }
    }
Connor Low
  • 5,900
  • 3
  • 31
  • 52
kurec
  • 1
  • 4
  • 2
    Please edit your question to include your code, rather than attaching it as an image – stuartd Feb 11 '21 at 15:50
  • 1
    If you want to output the count of each type, then you're going to have to actually count them by looking at each random number generated: i.e. whether if the number is positive, negative, or indeed zero. – stuartd Feb 11 '21 at 15:54
  • There are multiple step needed here. Read a file. line by line. To a list of string. Convert those string to int. For each int test to know it it's positive negative or 0. On what step do you have an issue? – Drag and Drop Feb 11 '21 at 15:54
  • 1
    What research have you done so far? What have solutions have you done? What is the actual problem you are asking about? Counting how many numbers fulfill some criteria? How to write the result to a text file? How to read numbers from a text file? – JonasH Feb 11 '21 at 15:55
  • Side note, you don't do anything with your parameters `pocet` `odd` and `doo`. I think you meant to use those in your `for` loop and in `nahoda.Next` – tnw Feb 11 '21 at 15:56
  • 1
    [How to read a File into List?](https://stackoverflow.com/questions/6904401/how-best-to-read-a-file-into-liststring), [How to convert List to List?](https://stackoverflow.com/questions/6201306/how-to-convert-liststring-to-listint), [How do I check if a number is positive or negative in C#?](https://stackoverflow.com/questions/4099366/how-do-i-check-if-a-number-is-positive-or-negative-in-c), [almost everything you need](https://stackoverflow.com/questions/55548821/way-to-extract-positive-and-negative-numbers-from-one-array-to-another-two-posi) – Drag and Drop Feb 11 '21 at 15:59
  • 1
    Use [ReadLines](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readlines?view=net-5.0) to read the file. Use [int.TryParse()](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0) to convert each line to an integer (possibly). Use simple variables to track total number, negative, zero or positive with an `if...else if` block. – Idle_Mind Feb 11 '21 at 16:00

0 Answers0