I NEED HELP

I NEED HELP

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 856 Views - Last Post: 06 April 2008 - 10:10 AM Rate Topic: -----

#1 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

I NEED HELP

Posted 05 April 2008 - 05:03 PM

Hi,

i hardly can get a correct output solutions here. please help me it's urgent.

regards,
kckc314


/******************************************************************************
 *  Description:  Assign in all possible ways a digit to each of the letters  *
 *				that occur in the words ONE, TWO, SEVEN and NINE, so that:  *
 *				- different digits are assigned to different letters;	   *
 *				- the digit assigned to the first letter of each word	   *
 *				  is not zero;											  *
 *				- the equality ONE + ONE = TWO holds;					   *
 *				- SEVEN is a prime number;								  *
 *				- NINE is a perfect square.								 *
 *																			*
  ******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

int main(void) {

	int O, N, E, T, W, S, V, I, x, y, z;
	int i, j, k, l, m, n, o, p;
	unsigned int ONE, TWO, SEVEN, NINE, root;

	/* Assign a digit to each of the letters that occur in the words  *
	 * ONE, TWO, SEVEN and NINE. Different digits are assigned to	 *
	 * different letters. The digit assigned to the first letter of   *
	 * each word is not zero.										 */
	/*for (i = 100; i <= 999; i++) {
		O = i / 100;
		
		for (j = 100; j <= 999; j++) {
			N = j / 100;
			E = (i / 10) % 10;
			for (k = 100; k <= 999; k++) {
				T = k / 100;
				W = (k / 10) % 10;
				for (l = 100; l <= 999; l++) {
					S = l / 100;
					V = (l / 10) % 10;
					I = (j / 10) % 10;*/
					
		 for (i = 1; i <= 9; i++) {
			 O = i;
			 for (j = 1; j <= 9; j++) {
				 N = j;
				 for (k = 0; k <= 9; k++) {
					 E = k;
					 for (l = 1; l <= 9; l++) {
						 T = l;
						 for (m = 0; m <= 9; m++) {
							 W = m;
							 for (n = 1; n <= 9; n++) {
								 S = n;
								 for (o = 0; o <= 9; o++) {
									 V = o;
									 for (p = 0; p <= 9; p++) {
										 I = p;
		 
		 /* To verify that different digits are assigned to different	  *
		  * letters. Then evaluation is performed to assign into variable  *
		  * ONE and TWO.												   */
		 if ((O != N) && (N != E) && (O != E) && (T != W) && (W != O) && (T != O) && (S != E) && (E != V) && (V != N) &&
			 (S != V) && (S != N) && (N != I) && (I != E) && (S != T) && (S != W) && (S != O) && (E != T) && (E != W) && 
			 (V != T) && (V != W) && (V != O) && (N != T) && (N != W) && (I != O) && (I != T) && (I != W) && (I != S) && (I != V)) {
				  ONE = (O * 100) + (N * 10) + E;
				  TWO = ONE + ONE;
										  
					 /* To compute NINE is a perfect square with distinct digits.	*
					  * The lowest possible value for NINE is 1012, and the square   *
					  * root of 1012 is 31.81. To round it up is equal to 32.		*
					  * The highest possible value for NINE is 9897, and the		 *
					  * square root of 9897 is 99.48. To round it down is equal to   *
					  * 99. The loop will take from the lowest value 32 until the	*
					  * highest value 99 then it terminates. From the result break   *
					  * into single digit from the 4 digits number and assign them   *
					  * into letters N, I, N, E for comparison check later.		  */
					 for (y = 32; y <= 99; y++) {
						 N = (y * y) / 1000;
						 I = ((y * y) % 1000) / 100;
						 N = (((y * y) % 1000) % 100) / 10;
						 E = ((((y * y) % 1000) % 100) % 10) / 1;
						 if ((N != I) && (I != E) && (N != E) && (N != O) && (N != T) && (N != W) &&
							 (N != S) && (N != V) && (I != O) && (I != T) && (I != W) && (I != S) && 
							 (I != V) && (E != O) && (E != T) && (E != W) && (E != S) && (E != V))
											   
							 NINE = y * y;
					 }
					 
					 /* To compute SEVEN is a prime number with distinct digits.  */
					 SEVEN = (S * 10000) + (E * 1000) + (V * 100) + (E * 10) + N;
					 bool isPrime = true;
					 root = sqrt(SEVEN);
					 for (z = 2; z <= root; z++)
						 if (SEVEN % z == 0) {
							isPrime = false;
							break;
						 }
					 if (isPrime = true)
						printf("one = %u, two = %u, seven = %u and nine = %u is a solution.\n",
						ONE, TWO, SEVEN, NINE);
						}
	}
}
}
}
}
}
}
}
	system("PAUSE");
	return EXIT_SUCCESS;
}


This post has been edited by kckc314: 05 April 2008 - 05:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I NEED HELP

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: I NEED HELP

Posted 05 April 2008 - 05:35 PM

No offense, but what a stupid homework assignment.

If I understand what I read correctly you must give 'O' 'N' 'E' values so that ONE + ONE = TWO which are some values.

Have you penned an algorithm yet? There is no point discussing this any further if the core idea of how you are going to do this isn't down, on paper preferably.

For the sake of argument let's assume that it has to be 0-9. Which means that is only 30 different combinations for ONE and TWO.

Do the digits multiply against the others in the same word or are they added?

This post has been edited by KYA: 05 April 2008 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: I NEED HELP

Posted 05 April 2008 - 05:54 PM

View PostKYA, on 5 Apr, 2008 - 05:35 PM, said:

No offense, but what a stupid homework assignment.

If I understand what I read correctly you must give 'O' 'N' 'E' values so that ONE + ONE = TWO which are some values.

Have you penned an algorithm yet? There is no point discussing this any further if the core idea of how you are going to do this isn't down, on paper preferably.

For the sake of argument let's assume that it has to be 0-9. Which means that is only 30 different combinations for ONE and TWO.

Do the digits multiply against the others in the same word or are they added?





what algorithm you are mentioning here? in fact there is no algorithm need to be used in this program. i have included quite clear comments and information in the code. the output solution must be different digits assigned to different letters and also fulfill the requirements. SEVEN is a prime number, NINE is a perfect square and ONE + ONE = TWO holds.
kckc314
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: I NEED HELP

Posted 05 April 2008 - 06:03 PM

When I say algorithm I mean a way to compute the numbers. There is a formula that can be used and applied to all numbers/letters for this program. Without that we are shooting in the dark.

Quote

what algorithm you are mentioning here? in fact there is no algorithm need to be used in this program. i have included quite clear comments and information in the code. the output solution must be different digits assigned to different letters and also fulfill the requirements. SEVEN is a prime number, NINE is a perfect square and ONE + ONE = TWO holds.


You have a for loop running 1000 times in the first segment of code and I don't think you know why. Are the numbers added to multiplied together? How do you go about doing that etc... Planning is more important then actual code imho.
Was This Post Helpful? 0
  • +
  • -

#5 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: I NEED HELP

Posted 05 April 2008 - 06:26 PM

View PostKYA, on 5 Apr, 2008 - 06:03 PM, said:

When I say algorithm I mean a way to compute the numbers. There is a formula that can be used and applied to all numbers/letters for this program. Without that we are shooting in the dark.

Quote

what algorithm you are mentioning here? in fact there is no algorithm need to be used in this program. i have included quite clear comments and information in the code. the output solution must be different digits assigned to different letters and also fulfill the requirements. SEVEN is a prime number, NINE is a perfect square and ONE + ONE = TWO holds.


You have a for loop running 1000 times in the first segment of code and I don't think you know why. Are the numbers added to multiplied together? How do you go about doing that etc... Planning is more important then actual code imho.



Don't look at the first part of the 4 nested for loop. I use them for testing purposes only as you can see i have commented out. basically they are same as the second part 8 nested for loop. the first part is only to convert into shorter for loop.

when you see ONE = (O * 100) + (N * 10) + E means to evaluate the value to assign into ONE as 'O', 'N', 'E' is only stand for a single digit. as i need to evaluate 'O' into 100, 'N' into 10 plus E. the algorithm was used in my code is only to filter out what value is equal to a single digit for comparison use with different letters. they are not somehow a special formula that can apply to all digits/letters as you mentioned. If it really there is a formula i hope you can tell me. as far as i concern, the way i use for SEVEN and NINE should be correct, but don't know why the output is not correct.
kckc314
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: I NEED HELP

Posted 05 April 2008 - 07:47 PM

That's what I am talking about. You say 'O' must evaluate into 100, 'N' into 10, etc... but the instructions say to have each letter be the equivalent of one digit. A digit is 0-9.
Was This Post Helpful? 0
  • +
  • -

#7 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: I NEED HELP

Posted 05 April 2008 - 08:58 PM

View PostKYA, on 5 Apr, 2008 - 07:47 PM, said:

That's what I am talking about. You say 'O' must evaluate into 100, 'N' into 10, etc... but the instructions say to have each letter be the equivalent of one digit. A digit is 0-9.





first of all, i use for loops to assign a digit to different letters. then i use if statement to verify to ensure different letters have a different digit. meaning that if O = 1 in the word of ONE, then the O also equal to 1 in the word of TWO. if N = 2 in the word of ONE then N also equal to 2 in the word of SEVEN and NINE. then same thing happens for the rest of the letters. if they all are not equal after the test, the i evaluate it by ONE = (O * 100) + (N * 10) + E reason being the output must appear as a number. From the number you may find different letters represented by different digits. Not to offence, i wonder did you really look at my code and try to understand the requirements from the instructions in the header. there are also comments being included within the code.
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: I NEED HELP

Posted 05 April 2008 - 09:13 PM

OK. I have throughly reread your code. I think part of the problem is the way its written. Why not define a separate function that does the massive amount of != checking for better program readability?

When you say you're not getting the correct output, what do you mean exactly? Letters having the same number or the sqrt or other calculations not performing correctly? With this information you can pinpoint where the logic is going askew.

edit: I originally misread your code--I have a better understanding of what is required now.

This post has been edited by KYA: 05 April 2008 - 09:14 PM

Was This Post Helpful? 0
  • +
  • -

#9 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: I NEED HELP

Posted 05 April 2008 - 10:03 PM

View PostKYA, on 5 Apr, 2008 - 09:13 PM, said:

OK. I have throughly reread your code. I think part of the problem is the way its written. Why not define a separate function that does the massive amount of != checking for better program readability?

When you say you're not getting the correct output, what do you mean exactly? Letters having the same number or the sqrt or other calculations not performing correctly? With this information you can pinpoint where the logic is going askew.

edit: I originally misread your code--I have a better understanding of what is required now.



thanks it's glad you get there finally.

FYI, the correct solution should be one = 236, two = 472, seven = 56963 and nine = 3136 is a solution.

from here you may notice that each letter has the same digit in different word. the bottom line is different digits are assigned to different letters.

However, the problem is, if you run my program, it just keep on appearing multiple lines of solution and won't stop. if you take a look the part that i wrote to filter ONE, TWO, NINE and SEVEN, by looking at the output, it seems i am unable to filter out the solution. i have spent much time on it but still couldn't figure it out. hope you can help.
Was This Post Helpful? 0
  • +
  • -

#10 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: I NEED HELP

Posted 06 April 2008 - 01:42 AM

View Postkckc314, on 5 Apr, 2008 - 10:03 PM, said:

View PostKYA, on 5 Apr, 2008 - 09:13 PM, said:

OK. I have throughly reread your code. I think part of the problem is the way its written. Why not define a separate function that does the massive amount of != checking for better program readability?

When you say you're not getting the correct output, what do you mean exactly? Letters having the same number or the sqrt or other calculations not performing correctly? With this information you can pinpoint where the logic is going askew.

edit: I originally misread your code--I have a better understanding of what is required now.



thanks it's glad you get there finally.

FYI, the correct solution should be one = 236, two = 472, seven = 56963 and nine = 3136 is a solution.

from here you may notice that each letter has the same digit in different word. the bottom line is different digits are assigned to different letters.

However, the problem is, if you run my program, it just keep on appearing multiple lines of solution and won't stop. if you take a look the part that i wrote to filter ONE, TWO, NINE and SEVEN, by looking at the output, it seems i am unable to filter out the solution. i have spent much time on it but still couldn't figure it out. hope you can help.



hi KYA, awaiting your valuable solution!!!!!!
Was This Post Helpful? 0
  • +
  • -

#11 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: I NEED HELP

Posted 06 April 2008 - 01:56 AM

Issue 1:

You never actually verify if TWO = ONE + ONE

ONE = (O * 100) + (N * 10) + E;
TWO = ONE + ONE;

  /* Add me! */
if(TWO != (T * 100) + (W * 10) + O)
  break;




I'm looking for anything else, but that did greatly decrease what was printed.
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: I NEED HELP

Posted 06 April 2008 - 01:59 AM

im wondering how you got this to compile

This post has been edited by KYA: 06 April 2008 - 02:10 AM

Was This Post Helpful? 0
  • +
  • -

#13 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: I NEED HELP

Posted 06 April 2008 - 02:13 AM

Issue 2:

I just noticed this, but how you check if seven is prime is incorrect...
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: I NEED HELP

Posted 06 April 2008 - 02:17 AM

There is also an ambiguous call to sqrt(SEVEN)

SEVEN is defined as an unsigned int, but sqrt requires a float or double to process
Was This Post Helpful? 0
  • +
  • -

#15 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: I NEED HELP

Posted 06 April 2008 - 02:22 AM

View PostKYA, on 6 Apr, 2008 - 02:17 AM, said:

There is also an ambiguous call to sqrt(SEVEN)

SEVEN is defined as an unsigned int, but sqrt requires a float or double to process


Umm... No. The value is automatically converted to a double (c++) or a float ©. For the return value, the value is automatically cast to a unsinged int as well; however, the compiler does warn you about this.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2