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)
26 Replies
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)
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
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 intoahh okay will try thanks
The result of the integer division is then converted to floating point but by then it's too late
ahhh okay
i changed it all to double but its telling me i cant change int to double?
its giving the suggestion to put (int)inside the []
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
The size of the array is an int
You need to make the contents of the array double
i thought putting double at the beginning and new double shoudlve done it, what else do i need to do?
Make the size an int
stracka
ah like this
Well, I wouldn't write it like that
Just declare it as an int instead of double
oh in the original declare okay
int stracka = int.Parse
ahh okay😁
Better to use TryParse but that's the next level
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
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
ah i see, yeah i knew the first two but not about an array needing to be a whole number, thanks.
Is that an exam?
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
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.