0

let suppose I have

int coin[5] = {25, 10, 5, 1, 3};

printf("%i\n", sizeof(coin));

Please, I am not understanding why this is giving me an output of

20

I am still doing pset1 for the greedy program; To find my arround this I had divided this result by 5. Is this the appropriate way of finding the size of an array?

2

An int is 32 bits (4 bytes) on the appliance. The size of a 5-int array is 20 bytes (5 ints * 4 bytes each). The sizeof() operator is used to calculate the size of any datatype in bytes. For more information, you may read this wikipedia article!

2
  • Thanks, so this mean i will have a different output if I was using long long – crack64 Jun 25 '14 at 20:19
  • Why don't you try it? ;) – kzidane Jun 25 '14 at 20:43
1

What are you hoping to get from using sizeof()? I'm unsure why you have such an array for greedy at all. Might it be easier to use an integer counter that increments every time you calculate a coin?

1
  • Yeah, I later on realized that I was complicating my life with array for this pset. and that was what was delaying me. but in the process i learned something. – crack64 Jun 26 '14 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .