writing into array

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int long long fibo(int n);
string output_formatted_string(long long num);
void write_array(long long num);
long long char x[80];

int main (void) {
int n= 0;
cout << "Enter a number: ";
cin >> n;
string s = output_formatted_string(fibo(n));
for (int i = 0; i<80; i++){
cout << x[i] << endl;
}
system("PAUSE");
return 0;
}

long long fibo(int n)
{
if (n <2)
return 1;
long long temp1 = 1;
long long temp2= 2;
long long total = 0;
while (n-- > 1)
{
total = temp1 + temp2;
temp2 = temp1;
temp1 = total;
output_formatted_string(total);
}
}

#define GROUP_SEP ','
#define GROUP_SIZE 3

string output_formatted_string(long long num){
stringstream temp, out;
temp << num;
string s = temp.str();

int n = s.size() % GROUP_SIZE;
int i =0;
if(n > 0 && s.size() > GROUP_SIZE){
out << s.substr(i, n) << GROUP_SEP;
i += n;
}

n = s.size() / GROUP_SIZE -1;
while (n-- > 0){
out << s.substr(i, GROUP_SIZE) << GROUP_SEP;
i += GROUP_SIZE;
}
out << s.substr(i);
write_array(out);
return out.str();
}
void write_array(long long num){
	for (int i = 0; i < 80; i++){
		x[i] << num;
	}
	goto long long fibo(int n);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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(const unsigned int 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(unsigned long int number)
{
	std::string result;

	do
		result.insert(result.begin(), digittochar((unsigned int)number % 10));
	while (number /= 10);

	return result;
}

std::string fibonacci(unsigned long int howmany)
{
	unsigned long int
		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?
		const unsigned long int seed_temp = seed1 + seed2;

		seed1 = seed2;
		seed2 = seed_temp;

		result = result + ulitos(seed_temp) + ", ";		
	}

	return result;
}

int main(int argc, char **argv)
{
	unsigned long int 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.
Last edited on
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(long long 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;
	}
	goto long long fibo(int n); // I'm pretty sure this is not how goto is used.
}
yea i jsut tought about x[i] = num;

and i cant remember how i can go back to fibo. oh and btw my code has loads of unnessesary lines in them, i just need to find them.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int long long fibo(int n);
string output_formatted_string(long long num);
char x[70];
int main (void) {
int n= 0;
cout << "Enter a number: ";
cin >> n;
string s = output_formatted_string(fibo(n));
cout << "Fibo(" << n <<"( = " << s << endl;
system("PAUSE");
return 0;
}

long long fibo(int n)
{
int i = 0;
if (n <2)
return 1;
long long temp1 = 1;
long long temp2= 2;
long long total = 0;
while (n-- > 1)
{
total = temp1 + temp2;
temp2 = temp1;
temp1 = total;
x[i] = total;
i++;
}
return total;
}

#define GROUP_SEP ','
#define GROUP_SIZE 3

string output_formatted_string(long long num){
stringstream temp, out;
temp << num;
string s = temp.str();
for(int d = 0; d <= 70; d++){
int n = s.size() % GROUP_SIZE;
int i =0;
if(n > 0 && s.size() > GROUP_SIZE){
out << s.substr(i, n) << GROUP_SEP;
i += n;
}

n = s.size() / GROUP_SIZE -1;
while (n-- > 0){
out << s.substr(i, GROUP_SIZE) << GROUP_SEP;
i += GROUP_SIZE;
}
out << s.substr(i);
x[d] = out;
}
return out.str();
}
Topic archived. No new replies allowed.