When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
155 messages ยท Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
#include> iosteam>
#include "InfInt.h"
#include <chrono>
using namespace std;
using namespace std::chrono;
int main()
{
const int runLimit{4};
time_point<steady_clock> start = steady_clock::now();
time_point<steady_clock> endTime = runLimit * 1s + start; // calculate end time
int n = 0;
do
{
int term1 = 0;
int term2 = 1;
int nextTerm;
int i = 2;
while (i = n)
{
i++;
nextTerm = term1 + term2;
term1 = term2;
term2 = nextTerm;
n = i;
}
}
while (steady_clock::now() < endTime);
cout << n << "\n";
}
```Requested by: dragonslayer0531#0000
i = n assigns the value of n to i, not equality check
your while loop condition
also, what are you trying to do with that do-while?
i feel like this could be simplified
So just since it's your first time, a little advice: Don't use using namespace XYZ;. It's bad practice
Issues can arise that are a pita to figure out what the cause is
yeah, that's just solving one issue in your code
oh...
try something like this to calc fibb:
long long first = 0;
long long second = 1;
while (...) {
long long next = first + second;
first = second;
second = next;
}
std::cout << second << std::endl;
where the ... is the condition you want to continue running your application
sure, just switch around the do-while
do {
long long next = first + second;
first = second;
second = next;
} while (...);
what's happening here is that you're checking to make sure i == n for your inner for loop, which will always evaluate to false
since you set i = 2 always and n is initially 0. Since you never enter the inner loop of your code, n = i is never called, thus n is always 0 and you can only evaluate the while loop's condition to false
also could u explain in easier detail sorry but confusing
your debugger did what?
it skipped the inner part
and went down to while at the bottom
skipped while (i=n)
So you initially set n to 0, correct? Well, when you do while (i == n), i is equal to 2, right?
And n will only ever change if the body of that inner-most while loop is entered
yeah
So because i == n initially evaluates to false, n will never change, i will always be 2 when you evaluate the while condition, so i == n will always be false
so this does the sequence portion, meaning calculates the values at said subscript and i need it for my code
correct
it doesn't keep track of the subscript, that would be on you to do
it just runs and keeps calculating the next value
so like half my problems are fixed with ==
oh okay
Some are, some aren't
there are fundamental flaws in the logic of your code above
i'd recommend thinking about how you'd solve it mathematically, then translate it to code. you'll notice you only need one loop
well i would but ive been trying since morning of today and yesterday
which i couldve spent studying for othr courses
rn im just tryna finish the assignment
and i did read the textbook that goes over the basics of these individually but im not a reader
welcome to college. learn to read ๐
i chose my course cuz of what i wanted to do... not cuz i wanted to learn to code (coding class is mandatory even if u didnt choose it)
wrong still
why do you have that inner while loop?
you didn't answer the question. in your mind, what is the reason for your inner while loop?
while (i == n);
so basically
so i left the while
as is
and i prefer switch/ if else/ for instead of do while cuz i have used them a bit more (since i starteed this sept)
but ik theyre basically similar
especialy the for
still didn't answer my question
why while(i == n);
i'm trying to follow the reasoning behind your code
in my mind i thought of it as: do the "do" portion if i ==n and i left it at that because then it would always be true... (this is only if i assigned n as i in the prior line)
honestly even i cant understand
so that while isn't associated with the do statement.
it is
the do-while's while loop is the one after the } of the do block
i would go through the do portion first, and make n == i and then have the while statement, which would cause it to go in a loop
i think that makes more sense
if you can't understand the lines of code you've written, you shouldn't write them ๐


when was doing the coding portion, using the template, i didnt factor in that the template is setup so that i didnt need to add a while
but no, that while inside the do block will actually never execute (since i == n will always be false)
cuz the timer does it for me
what
no because i is initialized at 2 and n is at 0, so when it first executes, it does the "do" portion and i increases by 1, but it still isnt = n... so after that it drops dead
meaning terminate
either both n and i had to start at 0 or 2
right...?
the inner while stops, but not the do while, yeah
well i didnt factor the outside
but yeah
is that my problem there
i got rid of the while
correct, no need for n
ok
just as another quick word of advice for reading and understanding code, single letter variable names aren't useful
that appears fine to me, though I'd have to run it to verify
hmm okay
print second
do:
cout << i << " " << second << "\n";
verify that i has the correct second
(where i is the subscript of fibbonacci and second is the value at the subscript)
ahh okay
yeah, that appears wrong
you're having the long long wrap around
do you need to see how many you can calculate, or the actual number at subscript i?
that should be fine then
hm okay
so the value of second is what then?
cuz it isnt the actual number right
wait is it wrong because long long cant comprehend the massive value of the subscript and so it outputs junk BUT otherwise the subscript itself is right
It's because it's larger than the maximum value of a long long, so it's overflowed and wraps back around
F(163912554) is an absolutely massive number
or can i not use that
i have no clue what infint is
not a clue what the header is. feel free to try it, though it'll definitely slow down your code
(which is expected)
your prof may expect you to use infint's types instead of long long
because it can actually support numbers big enough to hold F(n)
well he didnt expect us to use infint, rather he didnt specify
up to you on what to use then
infint will likely slow your code down to what they have
sure, just don't get marked down because second isn't actually correct
(because of integer overflow)
ima ask my friends, and then adjust
ty
@pure pollen Has your question been resolved? If so, run !solved :)
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity