❔ 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
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
have you use
for
and foreach
loops before?i used for
not foreach
you will need to use
Random
, yeahok
so, make a List<int>, use a for loop to fill it up with 30 numbers from
Random
for (int i=1;i<=30;i++)
then go back over the list in another loop, keeping count of if each number is positive or negative
does this mean it can geanerete 30 integers or 29 ?
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 thinkalright
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 ?
put the Random instance out of the loop
no need to initialize it 31 times
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.