help a noob

hello guys i need help im a noob. im doing this program where i ask how many routes, then i ask how many meters the routes are, and then i anwser them what the route is in km. the problem im having is dividing every number by 1000 which im trying to do. it just becoms zero. here is the code and how the program looks like when it runs (its in swedish)
No description
No description
26 Replies
Jimmacle
Jimmacle8mo ago
the problem is you're doing integer division if you need to have a fraction you have to cast the number to float or double (or use floats/doubles to begin with)
FernandoBorges
FernandoBorgesOP8mo ago
im using double, right? . im using it when dividng by 1000 but im guessing ur saying i have to use it in the array as well
Jimmacle
Jimmacle8mo ago
basically, if both sides of the / expression are integers then it will do integer division doesn't matter what type you're storing the result into
FernandoBorges
FernandoBorgesOP8mo ago
ahh okay will try thanks
mtreit
mtreit8mo ago
The result of the integer division is then converted to floating point but by then it's too late
FernandoBorges
FernandoBorgesOP8mo ago
ahhh okay
FernandoBorges
FernandoBorgesOP8mo ago
i changed it all to double but its telling me i cant change int to double?
No description
FernandoBorges
FernandoBorgesOP8mo ago
its giving the suggestion to put (int)inside the []
FernandoBorges
FernandoBorgesOP8mo ago
No description
FernandoBorges
FernandoBorgesOP8mo ago
this is how its supposed to look an example its not only a double problem because if i do just like 2000 3000 etc it still gives me zero so im guessing im not identefying every digit ? i guessing i+1 is the wrong here but idk what else to do there
mtreit
mtreit8mo ago
The size of the array is an int You need to make the contents of the array double
FernandoBorges
FernandoBorgesOP8mo ago
i thought putting double at the beginning and new double shoudlve done it, what else do i need to do?
mtreit
mtreit8mo ago
Make the size an int stracka
FernandoBorges
FernandoBorgesOP8mo ago
No description
FernandoBorges
FernandoBorgesOP8mo ago
ah like this
mtreit
mtreit8mo ago
Well, I wouldn't write it like that Just declare it as an int instead of double
FernandoBorges
FernandoBorgesOP8mo ago
oh in the original declare okay
mtreit
mtreit8mo ago
int stracka = int.Parse
FernandoBorges
FernandoBorgesOP8mo ago
ahh okay😁
mtreit
mtreit8mo ago
Better to use TryParse but that's the next level
FernandoBorges
FernandoBorgesOP8mo ago
haha yeah. its still giving me zero. how do i identify each given number and divide it? this is how i have tried. i was thinking that it divides the first one given and then when i++ it does the next one but im obviously doing it wrong
No description
No description
mtreit
mtreit8mo ago
Basically int is used for things that are always whole numbers. Double or float is used for things that can be fractional. An array length must be a whole number. You can't have an array of length 3.345
FernandoBorges
FernandoBorgesOP8mo ago
ah i see, yeah i knew the first two but not about an array needing to be a whole number, thanks.
Buddy
Buddy8mo ago
Is that an exam?
FernandoBorges
FernandoBorgesOP8mo ago
no:kekw: i started programming like a few weeks back in school and my teacher makes powerpoints with questions at the end but it does looks like an exam lol so this question was on the powerpoint of arrays
Stars
Stars8mo ago
I don't know why anyone care about number type. In this code snippet, the array named strackorimeter has never been assigned any value at any index. strackorimeter[i + 1] always equals to 0. This array has no sense in this code, change to a double variable and do the division.
Want results from more Discord servers?
Add your server