Triangular number calculator

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2007
Posts: 12
Reputation: afr02hrs is an unknown quantity at this point 
Solved Threads: 0
afr02hrs afr02hrs is offline Offline
Newbie Poster

Triangular number calculator

 
0
  #1
Mar 31st, 2007
Can anyone spot the semantic error in this triangular number calculator here? It correctly reports 21 as being a triangle number but incorrectly that 15 is not a triangle number. However if you input 15 as the first value entered it correctly says that 15 is a triangle number!!

  1. int main(int argc, char* argv[])
  2. {
  3. int Number;
  4. int Index;
  5. char Again;
  6. Index = 1;
  7. Again = 'y';
  8. while (Again == 'y')
  9. {
  10. Number = ReadIntPr("Enter number for testing: ");
  11. while (Index*(Index+1) < 2*Number)
  12. {
  13. Index = Index + 1; //'i' was used rather than index here
  14. }
  15. if (Index*(Index+1) == 2*Number) //the correct 'is equal to' was not used
  16. WriteStringCr("Triangular");
  17. else
  18. WriteStringCr("Not triangular"); //the semicolon here was not included
  19. Again = ReadCharPr("Test another number (y/n)? ");
  20. }
  21.  
  22. getchar();
  23. return 0;
  24. }
Last edited by afr02hrs; Mar 31st, 2007 at 12:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Triangular number calculator

 
0
  #2
Mar 31st, 2007
Originally Posted by afr02hrs View Post
Can anyone spot the semantic error in this triangular number calculator here?

  1. ReadIntPr("Enter number for testing: ");
  2. WriteStringCr("Triangular");
  3. Again = ReadCharPr("Test another number (y/n)? ");
  4.  
  5. }

Are you including the proper #defines at the beginning of the source file?.
Are you providing the proper prototypes for those functions.
Did you include those functions?.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: afr02hrs is an unknown quantity at this point 
Solved Threads: 0
afr02hrs afr02hrs is offline Offline
Newbie Poster

Re: Triangular number calculator

 
0
  #3
Mar 31st, 2007
Yes defines are correct. Prototypes are also unique to the library file that I am using. I wonder if there is a flaw in the logic of the design?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Triangular number calculator

 
0
  #4
Mar 31st, 2007
Index = 1;
should be inside the outer while loop.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: afr02hrs is an unknown quantity at this point 
Solved Threads: 0
afr02hrs afr02hrs is offline Offline
Newbie Poster

Re: Triangular number calculator

 
0
  #5
Mar 31st, 2007
Originally Posted by vijayan121 View Post
Index = 1;
should be inside the outer while loop.
Perfect I just could not see it!! Works perfectly now. Thank you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Triangular number calculator

 
0
  #6
Mar 31st, 2007
Originally Posted by vijayan121 View Post
Index = 1;
should be inside the outer while loop.
I'm afraid that is not correct. Index is a counter and is ok were is being declared.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: afr02hrs is an unknown quantity at this point 
Solved Threads: 0
afr02hrs afr02hrs is offline Offline
Newbie Poster

Re: Triangular number calculator

 
0
  #7
Mar 31st, 2007
Originally Posted by Aia View Post
I'm afraid that is not correct. Index is a counter and is ok were is being declared.
But moving Index = 1 does allow the program to correctly identify triangular numbers.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Triangular number calculator

 
0
  #8
Mar 31st, 2007
I guess I'm not understanding what your saying.
Where did you change the Index = 1 to?.

... Is it working now?.
Could you post your changes?.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Triangular number calculator

 
0
  #9
Mar 31st, 2007
You keep editing your original question. Is is very confusing. You has changed you question three times.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: afr02hrs is an unknown quantity at this point 
Solved Threads: 0
afr02hrs afr02hrs is offline Offline
Newbie Poster

Re: Triangular number calculator

 
0
  #10
Mar 31st, 2007
Hi Edited the question once. Not changed in over an hour.

Originally Posted by Aia View Post
You keep editing your original question. Is is very confusing. You has changed you question three times.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum


Views: 2621 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC