jeudi 2 février 2017

How do I store an array backwards and manipulate it?

I'm pretty confused. I have to simply create an array but backwards. For example, if I have the number 7925 normally it would be stored as such:

Index: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9...]

Value: [7, 9, 2, 5, 0, 0, 0, 0, 0, 0...]

But, I need to achieve this;

Index: [...9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Value: [...0, 0, 0, 0, 0, 0, 7, 9, 2, 5]

//The only catch is I can't use vectors or reverse or anything fancy. Just a simple raw array. I only need to be able to accept numbers up to 250 from 0.

My teacher gave me this hint to store the number. But again, it can take any number.

7925 % 10 = 5;

792 / 10 = 79;

79 % 10 = 9;

7 / 10 = 0;

I'm guessing I need some type of while loop?

So now for the code I have!

bigint.hpp

const int MAX_INT = 250;

class bigint
{
public:
bigint();
bigint(int);
}

bigint.cpp

#include "bigint.hpp"

bigint::bigint(int num) : bigint() <- This initializes everything to zero, it works!
{
//
//This is where I am confused!
//
}

Then simply in main I have;

//#include files

main()
{
bigint b1(7925);
//print array in backwards order to make sure it worked
//print array normally
}

Expected output:

0 | 0 | 0 | 0 | 0 | 7 | 9 | 2 | 5 | <- Index 0

7925

Thanks for any tips :D

Aucun commentaire:

Enregistrer un commentaire