i think i wen bit over my head with this. i want to write first 70 fibonacci numbers into array and then to print it out. but i dunno if i got it right.
// if you don't understand something see here, use this site to look it up
// C++ headers
#include <iostream>
#include <string>
// good old C headers
#include <cstdlib>
// do not use "using namespace std;"
// it's bad practice they say, causes "namespace pollution"
// converts integer digit to character
char digittochar(constunsignedint digit)
{
switch (digit)
{
case 0: return'0';
case 1: return'1';
case 2: return'2';
case 3: return'3';
case 4: return'4';
case 5: return'5';
case 6: return'6';
case 7: return'7';
case 8: return'8';
case 9: return'9';
}
}
// convert an unsigned long integer to a C++ string
std::string ulitos(unsignedlongint number)
{
std::string result;
do
result.insert(result.begin(), digittochar((unsignedint)number % 10));
while (number /= 10);
return result;
}
std::string fibonacci(unsignedlongint howmany)
{
unsignedlongint
seed1=0,
seed2=1;
std::string result = ulitos(seed1) + ", " + ulitos(seed2) + ", ";
howmany -= 2; // because we already have the first two numbers
while (howmany--) // do this while howmany isn't zero
{
// what's the next number?
constunsignedlongint seed_temp = seed1 + seed2;
seed1 = seed2;
seed2 = seed_temp;
result = result + ulitos(seed_temp) + ", ";
}
return result;
}
int main(int argc, char **argv)
{
unsignedlongint howmany = 80;
// we can also specify howmany from commandline
if (argc == 2)
howmany = std::strtoul(argv[1], NULL, 10);
// who told you to use a C array?
// they're a leftover from C and they're hard to use.
std::string fibostring = fibonacci(howmany);
// print it
std::cout << fibostring.c_str() << std::endl;
system("PAUSE"); // if you use Windows...
// this is usually 0 but it's good practice not to take it for granted
return EXIT_SUCCESS;
}
// conclusion: if someone asks you to use C arrays they might as well ask you to use C instead of C++
// I think you can easily adapt this program to work with C arrays though
well yes, but i was ordered t ochange tho original program so that it writes int oarray and then prints it out. i have managed to sent every fibo number oto the formatt section, but in there i get a bit lost and i dunno waht number to send to write array section, and in write array i dont know how to make it write int oarray and then send the array back to main so that ot would print i out.
I'm sorry I can't specifically help you: the source code you have is too hard for me.
However:
1 2 3 4 5 6 7 8 9 10
void write_array(longlong num){
for (int i = 0; i < 80; i++){
// x[i] << num;
// the << operator shifts bits in this case, it does NOT have the meaning of "input"
// and so it doesn't save anything.
// I think you should use
x[i] = num;
}
gotolonglong fibo(int n); // I'm pretty sure this is not how goto is used.
}
i made sme changes to my code, its still incomplete, and nees some changes, but problem now is how do i get the int_64 int char array??
This code is still missing the print option for the array, il ttype it in later, when i get the antswerhow to oput int_64 ito char.