C
C#5mo ago
MIRO

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
Vi Ness
Vi Ness5mo ago
The instructions only talk about two lines of input. What $code do you have written so far?
MODiX
MODiX5mo ago
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/
MIRO
MIRO5mo ago
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
Vi Ness
Vi Ness5mo ago
Have you learned about arrays?
MIRO
MIRO5mo ago
no im a newbiew but i think its about length or index
Vi Ness
Vi Ness5mo ago
Yeah. Arrays have a length and are accessed by an index. Arrays will be the key to this exercise
MIRO
MIRO5mo ago
yes but how to write it
Vi Ness
Vi Ness5mo ago
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.
Vi Ness
Vi Ness5mo ago
Just focus on the Single-dimensional arrays for this exercise
MIRO
MIRO5mo ago
yesa thx got it but it doesnt let me to use arrays
Vi Ness
Vi Ness5mo ago
What doesn't?
MIRO
MIRO5mo ago
i cant write it with arrays it doesnt let me and writes that i need other version
Vi Ness
Vi Ness5mo ago
Can you show me code or any errors you have? I can't tell what's going wrong without seeing what you're doing
MIRO
MIRO5mo ago
when i write int [] N = [5, 4 , 3] ok wait
MIRO
MIRO5mo ago
MIRO
MIRO5mo ago
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 console
Vi Ness
Vi Ness5mo ago
int[] 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
int[] N =
[
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
// etc..
];
int[] N =
[
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
// etc..
];
MIRO
MIRO5mo ago
yes but
MIRO
MIRO5mo ago
No description
MIRO
MIRO5mo ago
it still doesnt let me and writes it as a mistake
Vi Ness
Vi Ness5mo ago
What is the error?
MIRO
MIRO5mo ago
it writes that its currenlty in preview or unsupported no wait its fixed
Vi Ness
Vi Ness5mo ago
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
int[] N = new int[]
{
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
// etc..
};
int[] N = new int[]
{
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
int.Parse(Console.ReadLine()),
// etc..
};
MIRO
MIRO5mo ago
yes but how should i continue the code because i cant use if or switch
Vi Ness
Vi Ness5mo ago
What do you need to do next?
MIRO
MIRO5mo ago
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
Vi Ness
Vi Ness5mo ago
Right. It sounds like you're able to read the first line for how many colors. Is that right?
MIRO
MIRO5mo ago
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
Vi Ness
Vi Ness5mo ago
You mentioned "cycles" earlier. Do you know what a "loop" is?
MIRO
MIRO5mo ago
yes i need loop i think but do i have to use while
Vi Ness
Vi Ness5mo ago
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
MIRO
MIRO5mo ago
yes but do i have to write while ( N ==N[1))
Vi Ness
Vi Ness5mo ago
When do you want to stop looping?
MIRO
MIRO5mo ago
no i cant understand how to do with with while the construction i made another but with many console readlines
MIRO
MIRO5mo ago
No description
Vi Ness
Vi Ness5mo ago
I see you gave up on the loop
MIRO
MIRO5mo ago
but i cant make the loop
Vi Ness
Vi Ness5mo ago
It sounds like you're not sure what the condition of the loop should be
MIRO
MIRO5mo ago
no i know that its after the initialization but i cant use it in this exercise
Vi Ness
Vi Ness5mo ago
Because of the instructors?
MIRO
MIRO5mo ago
no because of the first line and doesnt fit
Vi Ness
Vi Ness5mo ago
The first line gives you the number of unique hyacinths, right? Then you need to read that many numbers for the count?
MIRO
MIRO5mo ago
yes but how can i declare that command so it can count that many times
Vi Ness
Vi Ness5mo ago
Do you know any other types of loops other than "while"?
MIRO
MIRO5mo ago
yes mainly for
Vi Ness
Vi Ness5mo ago
How does a for loop work?
MIRO
MIRO5mo ago
with condition initialization and un update like i++
Vi Ness
Vi Ness5mo ago
Right. Can you show me how to write a for loop that loops 5 times?
MIRO
MIRO5mo ago
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
Vi Ness
Vi Ness5mo ago
What do you mean?
MIRO
MIRO5mo ago
i dont understand how to loop five times
Vi Ness
Vi Ness5mo ago
Can you show me any for loop?
MIRO
MIRO5mo ago
yes for ( int i =1; i <= 1000; i++) { Console.WriteLine("i + " "); } i meant
Vi Ness
Vi Ness5mo ago
Great! You wrote a loop that loops 1000 times.. How could you change it to loop N times?
MIRO
MIRO5mo ago
if i change the one thousand with N
Vi Ness
Vi Ness5mo ago
Nod
MIRO
MIRO5mo ago
yes but it doesnt read variables only once i declare it and to its number
Vi Ness
Vi Ness5mo ago
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 them
MIRO
MIRO5mo ago
yes but what should be in the loop
MIRO
MIRO5mo ago
No description
MIRO
MIRO5mo ago
in the initialization
Vi Ness
Vi Ness5mo ago
Rename your array at the top real quick, you're using the name i twice and it's giving you an error
MIRO
MIRO5mo ago
yes but should it be i = 1
Vi Ness
Vi Ness5mo ago
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 variables
MIRO
MIRO5mo ago
yes i fixed it but i meant how can i make it to dont use many math.max and math.min
Vi Ness
Vi Ness5mo ago
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 array
MIRO
MIRO5mo ago
yes 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]); }
Vi Ness
Vi Ness5mo ago
Right, I just want you to see how it works. Because right now there's a bug in that loop
MIRO
MIRO5mo ago
yes shouldnt be i removed because 1 is for the first index
Vi Ness
Vi Ness5mo ago
The first item in an array is at index zero, a[0] in this case, but your i is starting at one
MIRO
MIRO5mo ago
yes 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
Vi Ness
Vi Ness5mo ago
What do you mean? You can set i to 0 instead of 1 the same way you changed 1000 to N
MIRO
MIRO5mo ago
yes but i have to write in the console above seven variables
Vi Ness
Vi Ness5mo ago
If you want it to console write seven times, yes
MIRO
MIRO5mo ago
but how to change it
Vi Ness
Vi Ness5mo ago
To what?
MIRO
MIRO5mo ago
to three times or two
Vi Ness
Vi Ness5mo ago
int N = int.Parse(Console.ReadLine()); Type "3" for this input
MIRO
MIRO5mo ago
i 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]); }
Vi Ness
Vi Ness5mo ago
What was the output?
MIRO
MIRO5mo ago
two three it reads and the other readlines in the array
Vi Ness
Vi Ness5mo ago
Did you input 1 2 3 ?
MIRO
MIRO5mo ago
i inputed 3 1 2 3 4 5 6 7 the output is 1 2 3 4
Vi Ness
Vi Ness5mo ago
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
MIRO
MIRO5mo ago
then what should i do
Vi Ness
Vi Ness5mo ago
i < N instead of i <= N. Since a[3] gives you the fourth item in the array, you don't want i to reach 3
MIRO
MIRO5mo ago
yes got it and how to make it the console.readlines from the array to be ridden only three or a certain amount
Vi Ness
Vi Ness5mo ago
So you have a loop that goes N times and you want to Console.ReadLine N times?
MIRO
MIRO5mo ago
yes
Vi Ness
Vi Ness5mo ago
Right now you're doing Console.WriteLine N times. How would you do ReadLine instead?
MIRO
MIRO5mo ago
maybe Console.ReadLine i meant Console.WriteLine(a[1,2,3]);
Vi Ness
Vi Ness5mo ago
What does a[1,2,3] do?
MIRO
MIRO5mo ago
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
Vi Ness
Vi Ness5mo ago
Nice! How's the project looking now?
MIRO
MIRO5mo ago
I used the indexes with many math max and math min but the other one was with min value, for and sum