0

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;
}
4
Contributors
11
Replies
12
Views
10 Years
Discussion Span
Last Post by John A
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?.

0

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?

0

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

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

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.

0

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.

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

0

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

0

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.

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.

0

I guess I'm not understanding what your saying.
Where did you change the Index = 1 to?.

Reread vijayan'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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.