i need help with one exercise using cycles
Jack wanted to plant hyacinths. Hyacinth bulbs of N different colours were available on the market, with several bulbs of each colour. Jack bought all the bulbs offered, hoping to make a garden of hyacinths of all different colours. Then, without considering what he was doing, he mixed the bulbs. But the bulbs of hyacinths of different colours are exactly the same, and Jack got confused. It's good that he at least remembered how many bulbs of the different colours he had bought. How many least bulbs should Jack plant to be sure that there will be at least one hyacinth of each colour in his garden? Write a lilies program to calculate this number. We assume that one hyacinth will grow from each bulb.
Login
From the first line of the standard input, the number of different coloured hyacinths is entered. The number of colours is not greater than 7. From the second line, # positive integers are entered, separated by one space, the number of bulbs of different colours, and the number of bulbs of one colour is not greater than 20.
Exit
On the single line of standard output, the program should output the required minimum number of bulbs to be planted.
EXAMPLE
Login
3
9 6 8
Exit
18
if anyone can help, cant understand how make it shorter without writing so many console readlines and also i know that the minimum number could be found by Math.Max(of the number) + Math.Min(of the numbers) + their amount
i need it for sunday so please hurry up
95 Replies
The instructions only talk about two lines of input. What $code do you have written so far?
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/i got a lot of versions of it because i cant understand how to use cyckles in this exersice properly
and have to write a lot of console readlines so i need to declare seven variables and i cant understand how can i make when i write three to be declared only three variables such as three colours and the amount of bulbs of each colour
Have you learned about arrays?
no im a newbiew
but i think its about length
or index
Yeah. Arrays have a length and are accessed by an index. Arrays will be the key to this exercise
yes but
how to write it
Where is this exercise from? Is it a learning course or just exercises? Either way, Microsoft has a lot of documentation available; like this page for arrays https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/arrays
Arrays - C#
Store multiple variables of the same type in an array data structure in C#. Declare an array by specifying a type or specify Object to store any type.
Just focus on the Single-dimensional arrays for this exercise
yesa thx
got it
but
it doesnt let me to use arrays
What doesn't?
i cant
write it with arrays it doesnt let me and writes that i need other version
Can you show me code or any errors you have? I can't tell what's going wrong without seeing what you're doing
when i write int [] N = [5, 4 , 3]
ok wait
no wait it wrote something else
int[] N = [int a = int.Parse(Console.ReadLine), int b = int.Parse(Console.ReadLine), int c = int.Parse(Console.ReadLine), int d = int.Parse(Console.ReadLine), int e = int.Parse(Console.ReadLine), int f = int.Parse(Console.ReadLine), int g = int.Parse(Console.ReadLine)];
i meant the N is how many colours so if i write 3 in the console i can write three variables who are read from the consoleint[] N = [int a = int.Parse(Console.ReadLine())];
doesn't work, you're trying to declare a variable while initializing the array.
You could do something like
int [] N = [int.Parse(Console.ReadLine())];
with as many comma separated int.Parse
bits as you like
yes but
it still doesnt let me and writes it as a mistake
What is the error?
it writes that its currenlty in preview or unsupported
no wait
its fixed
Okay, that syntax was something recently added in C# 12. It sounds like your program just needed to use that version
Before, you would have to write it like this
yes but how should i continue the code
because i cant use if or switch
What do you need to do next?
on the first line i have to write int variable which says how many colours
and its max seven
and after that on the second line how many bubls of each colour
and the exit is the minimum amount of bulbs of each colour so jack could be sure that he will plant of each colour
Right. It sounds like you're able to read the first line for how many colors. Is that right?
yes
but i have to write from each index of the console so its login only the index which i wrote
and i cant understand how to make if i write 3 on the first line the console reads three variables
You mentioned "cycles" earlier. Do you know what a "loop" is?
yes
i need loop i think
but do i have to use while
A while loop is fine
Since you're trying to do something a specific number of times, a for loop would be better in my opinion
yes but do i have to write while ( N ==N[1))
When do you want to stop looping?
no i cant understand how to do with with while
the construction
i made another
but with many console readlines
I see you gave up on the loop
but i cant make the loop
It sounds like you're not sure what the condition of the loop should be
no i know that its after the initialization
but i cant use it in this exercise
Because of the instructors?
no because of the first line and doesnt fit
The first line gives you the number of unique hyacinths, right? Then you need to read that many numbers for the count?
yes
but how can i declare that command
so it can count that many times
Do you know any other types of loops other than "while"?
yes mainly for
How does a for loop work?
with
condition initialization
and un update like i++
Right. Can you show me how to write a for loop that loops 5 times?
okay i will try
but
there should be a variable
i dont know how to make it loop five times because i need something else
What do you mean?
i dont understand how to loop five times
Can you show me any for loop?
yes
for ( int i =1; i <= 1000; i++)
{
Console.WriteLine("i + " ");
}
i meant
Great! You wrote a loop that loops 1000 times.. How could you change it to loop
N
times?if i change the one thousand with N
yes but
it doesnt read variables only
once i declare it and to its number
So loops are really good for accessing arrays. You can use your loop variable
i
as an index, like array[i]
You can setup an empty array, then loop through it to read inputs and assign themyes but
what should be in the loop
in the initialization
Rename your array at the top real quick, you're using the name
i
twice and it's giving you an erroryes but should it be i = 1
On line 1 you declare
int[] i = new int[]
then on line 15 you have int i = 1
You can't use the same name for two different variablesyes i fixed it
but i meant
how can i make it to dont use many math.max and math.min
There are easy ways but maybe it would be good in the long run to try using the loop you wrote. Inside your loop try doing
Console.WriteLine(array[i]);
(I don't know what you renamed the array to) to see how it accesses each element in the arrayyes but its making me to write all of the indexes
int[] a = new int[]
{
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
};
int N = int.Parse(Console.ReadLine());
for ( int i = 1; i <= N; i++)
{
Console.WriteLine(a[i]);
}
Right, I just want you to see how it works. Because right now there's a bug in that loop
yes shouldnt be i removed because 1 is for the first index
The first item in an array is at index zero,
a[0]
in this case, but your i
is starting at oneyes but how can i fix the loop
because i cant put a formula in it
and if i use if it will be the same
What do you mean? You can set
i
to 0 instead of 1 the same way you changed 1000 to Nyes but i have to write in the console above seven variables
If you want it to console write seven times, yes
but how to change it
To what?
to three times
or two
int N = int.Parse(Console.ReadLine());
Type "3" for this inputi already did but nothing happen
int N = int.Parse(Console.ReadLine());
int[] a = new int[]
{
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
};
for ( int i = 0; i <= N; i++)
{
Console.WriteLine(a[i]);
}
What was the output?
two three
it reads and the other readlines in the array
Did you input
1
2
3
?
i inputed
3
1
2
3
4
5
6
7
the output is
1
2
3
4
That looks almost perfect. You see the output gave you four numbers even though N = 3
"Off by one" errors are pretty common when you're first getting started
then what should i do
i < N
instead of i <= N
. Since a[3]
gives you the fourth item in the array, you don't want i
to reach 3yes got it
and how to make it
the console.readlines from the array to be ridden only three or a certain
amount
So you have a loop that goes N times and you want to Console.ReadLine N times?
yes
Right now you're doing Console.WriteLine N times. How would you do ReadLine instead?
maybe Console.ReadLine
i meant
Console.WriteLine(a[1,2,3]);
What does
a[1,2,3]
do?its taking the chosen indexes from the array
no wait its invalid
to write three times Console.WriteLine(a[]) with different indexes
I made it with
minvalue
Nice! How's the project looking now?
I used the indexes with many math max and math min
but the other one was with min value, for and sum