I have been racking my brains to find what's wrong with my code...
been playing with it for an hour or so but it seems to still be wrong..
Please anyone can point out the mistake? Im so frustated right now.:yawn:
the answer to the question is 21124, but this program keeps giving me 21088
/*
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters
and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
*/
#include<stdio.h>
#include<string.h>
void initialize(int letters[]);
void completeArray(int letters[]);
int getTotal(int letters[]);
int hundred = strlen("hundred");
int thousand = strlen("thousand");
int and = strlen("and");
int main()
{
int letters[1001];
initialize(letters); //Input the ones that needs to be inputted
completeArray(letters); //Finish the array
printf("%d", getTotal(letters)); //Print the total of all the numbers inside the array
getchar();
return 0;
}
void initialize(int letters[])
{
letters[0] = 0; //Ignore the first index to make calculation simpler
letters[1] = strlen("one");
letters[2] = strlen("two");
letters[3] = strlen("three");
letters[4] = strlen("four");
letters[5] = strlen("five");
letters[6] = strlen("six");
letters[7] = strlen("seven");
letters[8] = strlen("eight");
letters[9] = strlen("nine");
letters[10] = strlen("ten");
letters[11] = strlen("eleven");
letters[12] = strlen("twelve");
letters[13] = strlen("thirteen");
letters[14] = strlen("fourteen");
letters[15] = strlen("fifteen");
letters[16] = strlen("sixteen");
letters[17] = strlen("seventeen");
letters[18] = strlen("eighteen");
letters[19] = strlen("nineteen");
letters[20] = strlen("twenty");
letters[30] = strlen("thirty");
letters[40] = strlen("forty");
letters[50] = strlen("fifty");
letters[60] = strlen("sixty");
letters[70] = strlen("seventy");
letters[80] = strlen("eighty");
letters[90] = strlen("ninety");
}
void completeArray(int letters[])
{
int i;
for(i=1; i<1000; i++)
{
if(i<20) //Number less than 20 is already there
{
continue;
}
else if(i < 100) //Calculating tens
{
if(i % 10 == 0)//x-ty are already there
{
continue;
}
else //get the tens + ones
{
letters[i] = letters[ (i/10)* 10 ] + letters[ i%10 ];
}
}
else //Calculating hundreds
{
if(i % 100 == 0)
{
letters[i] = letters[ i/100 ] + hundred; //Get the corresponding number at the front + hundred
}
else
{
//i consists of 3 numbers, say xyz
//letters[x] + hundred + and+ letters[y * 10] + letters[z]
letters[i] = letters[ i/100 ] + hundred + and + letters[ ((i % 100)/10)* 10 ] + letters[ i%10 ];
}
}
}
letters[1000] = letters[1] + thousand; //insert the 1000 manually.
}
int getTotal(int letters[])
{
int i;
int total = 0;
for(i=0; i<=1000; i++)
{
total += letters[i]; //Get total of all the numbers in the array
}
return total;
}