954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Triangular number calculator

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!!

int main(int argc, char* argv[])
{
int Number;
int Index;
char Again;
  Index = 1;
  Again = 'y';
  while (Again == 'y')
  {
    Number = ReadIntPr("Enter number for testing: ");
    while (Index*(Index+1) < 2*Number)
    {
      Index = Index + 1; //'i' was used rather than index here
    }
    if  (Index*(Index+1) == 2*Number) //the correct 'is equal to' was not used
      WriteStringCr("Triangular");
    else
      WriteStringCr("Not triangular"); //the semicolon here was not included
    Again = ReadCharPr("Test another number (y/n)? ");
  }

  getchar();
  return 0;
}
afr02hrs
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Can anyone spot the semantic error in this triangular number calculator here?

ReadIntPr("Enter number for testing: ");
WriteStringCr("Triangular");
Again = ReadCharPr("Test another number (y/n)? ");

}


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?.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

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?

afr02hrs
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Index = 1;
should be inside the outer while loop.

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 
Index = 1; should be inside the outer while loop.

Perfect I just could not see it!! Works perfectly now. Thank you.

afr02hrs
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 
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.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
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.

afr02hrs
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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?.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

You keep editing your original question. Is is very confusing. You has changed you question three times.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Hi Edited the question once. Not changed in over an hour.

You keep editing your original question. Is is very confusing. You has changed you question three times.
afr02hrs
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 
Hi Edited the question once. Not changed in over an hour.


The first time you original question was about a specific line error that you had. You wrote:Again = ReadCharPr("Test another number (y/n)? "); //statement missing here?
Second time you question was edit again and also you included this:

while (Index*(Index+1) < 2*Number)
    {
      Index = Index + 1; //'i' was used rather than index here
    }
    if  (Index*(Index+1) == 2*Number) //the correct 'is equal to' was not used
      WriteStringCr("Triangular");
    else
      WriteStringCr("Not triangular"); //the semicolon here was not included
    Again = ReadCharPr("Test another number (y/n)? ");


And after that you edited the question to: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!!
No desire to start a war of words here. But like I say. You want help,
you will get help here, however editing post to different questions are
confusing.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
I guess I'm not understanding what your saying. Where did you change the Index = 1 to?.


Rereadvijayan's post. If the OP did what was suggested, it should be very clear where the assignment was placed:

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.

There's nothing wrong with initializing it every iteration; in fact the lack of doing so is what caused the problems.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You