C
C#3y ago
daniel1

❔ tough homework 2

" A series of random numbers is called "balanced" when the amount of positive numbers in the series is equal to the amount of negative numbers in it. Write a receiving program that generates and displays a series of 30 random integers from the domain 20- to 20. The program must check whether the series is a balanced series or not. An appropriate message must be displayed."
13 Replies
daniel1
daniel1OP3y ago
this is the task my qustion is how do i check all of thoes numbers at the same time and am i supposed to use a "random" command or a "readline" command
Becquerel
Becquerel3y ago
have you use for and foreach loops before?
daniel1
daniel1OP3y ago
i used for not foreach
Becquerel
Becquerel3y ago
you will need to use Random, yeah
daniel1
daniel1OP3y ago
ok
Becquerel
Becquerel3y ago
so, make a List<int>, use a for loop to fill it up with 30 numbers from Random
daniel1
daniel1OP3y ago
for (int i=1;i<=30;i++)
Becquerel
Becquerel3y ago
then go back over the list in another loop, keeping count of if each number is positive or negative
daniel1
daniel1OP3y ago
does this mean it can geanerete 30 integers or 29 ?
Becquerel
Becquerel3y ago
i forget, i don't use for-loops often i think it will, but for (int i = 0; i < 30; i++) is more common i think
daniel1
daniel1OP3y ago
alright so static void Main(string[] args) { int num; for (int i = 0; i <= 30; i++) { Random rnd = new Random(); num = rnd.Next(-20, 20); } } } } i know i have to use "if: " but i dont know how i am supposed to use if or wait static void Main(string[] args) { int num,postivecount=0,neagtivecount=0; for (int i = 0; i <= 30; i++) { Random rnd = new Random(); num = rnd.Next(-20, 20); if (num > 0) { postivecount++; } else { neagtivecount++; } } } } } i still have a problem what do i do if i get a zero so i have to write 21 insted of 20 but i dont need to write -21 insted of -20 ? i have a slow brain and didnt understand what was writen down below maxvalue ok great thx static void Main(string[] args) { int num,postivecount=0,neagtivecount=0; for (int i = 0; i <= 30; i++) { Random rnd = new Random(); num = rnd.Next(-20, 21); if (num > 0) { postivecount++; } if (num < 0) { neagtivecount++; } if (neagtivecount == postivecount) { Console.WriteLine("we have a equal number"); } else { Console.WriteLine("we dont have equal numbers"); } } } } } you think this will do ?
TheRanger
TheRanger3y ago
put the Random instance out of the loop no need to initialize it 31 times
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server