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;
}

Edited 5 Years Ago by efronefron: additional information

At line 100 this letters[ ((i % 100)/10)* 10 ] + letters[ i%10 ] can be replaced with letters[ i % 100 ] Think about say 345, this is working out the number of letters in 45, you method adds the number of letter in 40 (forty) and 5 (five) however if you are calculating letters in 345 you have already calculated letters in 1 - 99 so you can just use the number of letter in 45(forty five).

This may also fix you problem because now consider 317, you code calculates number of letters in 10(ten) and number of letters in 7(seven) which is 8 but you actually need number of letters in 17(seventeen) or 9.

OMG !!!!

Thank you so much!!!!

My mind is now so clear now that this problem is over lol

thanks

This question has already been answered. Start a new discussion instead.