int hight = 0;
int highest = 0;
int highestindex = 0;
int main()
{
// game loop
while (1) {
for (int i = 0; i < 8; i++) {
if (hight > highest){
highestindex = i;
highest = hight;}
cin >> hight; cin.ignore();
}
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
cout << highest << endl; // The index of the mountain to fire on.
}
}
I read a few tutorials online for javascript beginners (functions, loops, arraysā¦ basic stuff), wanted to try some exercices to train before I go any further. So here I am andā¦ Iām getting so frustrated because we canāt see the solutions.
I read the exercice and Iām lostā¦ I have no idea what to do.
After spending 20 minutes on scratching my head I finaly saw the āhintsā on the leftā¦ they talk about stdinstdoutstderrā¦ Are we suppose to use these words in our solution ? (How Iām I suppose to know these? Iāve never heard of them before )
I read the algorithm & pseudo-code which I believe is the āsolutionā but I have to write it down with proper code and I donāt even know where I have to write itā¦ Is it inside the for that is already there ? or make a new one underneath ?
I tried doing things with typeof, tostringā¦ When I write these down outside the for it says undefined (I guess itās because of the āscopeā ?) when I write it down inside the for itās fine.
I am a complete begginer, even with the last hint I canāt understand what to do. If you guys got any hint for meā¦ <3
(trying to do it in javascript)
Edit : Ok so I checked the answers above to get some answers, saw that I had to write the code around whatās already writtenā¦ managed to complete the exercice but with no joy because I didnāt understand it on my ownā¦ feels Weird will try other exercises. <3
// game loop
while (true)
{
int max = 1000;
int number = 0;
for (int i = 0; i < 8; i++)
{
int mountainH = int.Parse(Console.ReadLine()); // represents the height of one mountain.
if (max > mountainH) {
max = mountainH;
number = i;
}
}
Console.WriteLine(number);
}
}
Hello, my Code does not work and I dont know why.:
import sys
import math
while True:
moutain_h = 0
highest_mountain_h = 0
highest_mountain_index = 0
for i in range(8):
mountain_h = int(input()) # represents the height of one mountain.
if mountain_h > highest_mountain_h :
highest_mountain_index = i
Highest_mountain_h = mountain_h
print(i)
int main()
{
int max;
// game loop
while (1) {
max = 0;
for(int i = 0; i < 8; i++){
int mountainH;
count >> mountainH; cin.ignore();
if(mountainH > max){
max = i;
}
}
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
cout << to_string(max) << endl; // The index of the mountain to fire on.
}
}
import java.util.*;
import java.io.*;
import java.math.*;
/**
* The while loop represents the game.
* Each iteration represents a turn of the game
* where you are given inputs (the heights of the mountains)
* and where you have to print an output (the index of the mountain to fire on)
* The inputs you are given are automatically updated according to your last actions.
**/
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
// game loop
while (true) {
int max=0;
int index=0;
for (int i = 0; i < 8; i++) {
int mountainH = in.nextInt();
System.err.println("mountainH est"+ mountainH);
if (mountainH==9)
{
// removing this if block makes it work
index=i;
System.err.println("breaking");
break;
}
if (mountainH>max) {
max= mountainH;
index=i;
}
}
System.err.println("broke out");
// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");
System.out.println(index); // The index of the mountain to fire on.
}
}
}
i remove the part of comparing the mountainH to 9 it works
I find this extremely confusing. It took me long enough to figure out why the game loop wasnāt happening at a standard framerate of between 30, 60 or 100 times per second. When I finally figured out ā or did I? ā what a āturnā is supposed to mean, I wrote this code:
while (true)
{
List<int> mountains = new List<int>();
for (int i = 0; i < 8; i++)
{
int mountainH = int.Parse(Console.ReadLine()); // represents the height of one mountain.
mountains.Add(mountainH);
}
mountains.Sort();
Console.Error.Write("sorted" + mountains.Count + "," + mountains[mountains.Count-1]);
Console.WriteLine(mountains.Count-1); // The index of the mountain to fire on.
}
It does not work. It never shoots down mountain of height 9, despite the fact that said mountain is lying right at mountains[mountains.Count-1] after I sort it. I think thereās a bug somewhere in your engine code thatās ignoring the new, sorted indices and using some fictional indices that we canāt see under the hood.
(as a side note, I did clear the mountains list between āturnsā initially as well, but with the sort and the weirdly infrequent iteration of the while loop, itās pointless)
Did you not see my sort command? By the unusual logic of writing the indices to destroy mountains, the last index of that list should and does always have the highest mountain. Also, with the loop being called once per āturnā, it doesnāt matter. I was trying all options to see how malleable the interface was.
Have you? Writing the index of the highest mountain is what youāre supposed to do. When I sort them, the index of the highest mountain is always 7. I even checked that with the debugger.
Look, Iām sorry to be blunt to the point of being typical ornery software engineer-ish, but at the very least, there is a fundamental communication problem with the way these puzzles are described to the end user. There is zero explanation of where these magic indices come from, where they are stored, why the indices I use in my list donāt map to them properly, or what data connects Console.Writeline(index) to the actual mountain.
This would be an absurdly simple problem to solve in real C#, in a real Unity environment, yet in this venue itās giving me, an admittedly dense in certain ways, but still experienced programmer, a lot of trouble. Itās making up weird rules and hiding code, and IMO, nothing good can come from that in the long game. In a framerate loop, I would not use a sort for each frame (although the default comparer is Quicksort, which is good), but a test environment like this should be able to understand what Iām trying to do.
I totally understand you get confused on your first steps on the platform while being an experienced programmer.
Have you, by any chance, seen the hints on the top left menu?
Now, about your code:
if you sort a list of integers, the index of the highest integer will always be the last one (size - 1). However, the puzzle asks you the index of the highest mountain as they are given to you, not after you sort them.
Does it help? Iāll be happy to answer any question you might have.