mardi 17 mars 2015

C++ getting rid of leading zeros in an array


I am having a problem getting rid of leading zeros when my Fibonacci program writes to the file. I understand that in my code I don't have anything that would remotely solve this problem, I am just looking for a way that I would go about doing this.



#include <iostream>
#include <ctime>
#include <fstream>

using namespace std;

int x [101] = {-1};
int y [101] = {1};
int z [101] = {0};

int main() {
for(int i = 0; i <= 100; i++){
x[i] = 0;
y[i] = 0;
z[i] = 0;
}
x[0] = -1;
y[0] = 1;
int j = 0;
ofstream push;
clock_t elapsed, start;

start = clock();

// Create // Open // Write Receipt
push.open("Fibonacci.txt");

do {

for (int i = 0; i <99 ; i++) {

if (j % 3 == 0) {
if(i == 0){
z[i] = 0;
}

z[i+1]=0;

z[i] += x[i] + y[i];

if (z[i] >= 10) {
z[i+1] += 1;
z[i] -= 10;
} else {
z[i+1] = 0;
}
} else if (j % 3 == 1) {
if(i == 0){
x[i] = 0;
}

x[i+1] = 0;
x[i] += z[i] + y[i];

if (x[i] >= 10) {
x[i+1] = 1;
x[i] -= 10;
} else {
x[i+1] = 0;
}
} else if (j % 3 == 2) {
if(i == 0){
y[i] = 0;
}

y[i+1]=0;
y[i] += x[i] + z[i];

if (y[i] >= 10) {
y[i+1] = 1;
y[i] -= 10;
} else {
y[i+1] = 0;
}
}
}

push << j+1 << ". " ;

do {
for(int i = 99; i >= 0;i--){
if(j % 3 == 0){
push << z[i];
} else if (j % 3 == 1){
push << x[i];
} else {
push << y[i];
}
}
} while (j < 477);

push << endl;
j++;
} while (j < 477);

// return elapsed time in milliseconds
elapsed = clock() - start;
push << endl << "Time elapsed: " << elapsed << endl << endl;
push.close();

system("pause");
return 0;
}



Aucun commentaire:

Enregistrer un commentaire