Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
The task is:

The input consists of the integer N. 1 ≤ N ≤ 10^18.
If N is both a square number and a triangular number, output Both.
If N is a square number only, output Square.
If N is a triangular number only, output Triangular.
If N is neither a square number nor a triangular number, output Neither.


However my teacher say that i am wrong

What I have tried:

C++
long long int a, c, i, f, y, z;

scanf("%lld", &a);

if ((a >= 1) && (a <= pow(10,18))){

long long int e = 0;

for (f=1; e<a; f++){

    e = f * f;

    if (e == a){
        y = 1;
    }
}

long long int d = 0;

for (i=1; d<a; i++){
    d = d + i;

    if (d == a){
        z = 1;
    }
}

if (y == 1 && z == 1){
    printf("Both");
}else if (y == 1){
    printf("Square");
   }else if (z == 1){
        printf("Triangular");
        }else {
            printf("Neither");
        }
}

return 0;
Posted
Updated 17-Mar-20 3:27am
v4
Comments
Patrice T 16-Mar-20 15:02pm    
Advice: post code that we can compile and run.

Make certain you initialize your variables.

That would have been evident by using a debugger.

long long int a = 0;
long long int i = 0;
long long int f = 0;
long long int y = 0;
long long int z = 0;


i tried with 49 (squared)
I tried with 378 (triangle number) and it works
 
Share this answer
 
Comments
Maciej Los 16-Mar-20 13:18pm    
Seems it should be obvious, but sometimes it's not. Even if there is a lot articles about Uninitialized variable (on Wikipedia)[^]
Test it.
Run it with various values that you know fit into each category, some that fit in none, and some that fit in multiple categories. See what it does.

Then when you find something that gives the wrong output use the debugger to follow through your code and find out why.

That's part of your task: to design a solution, implement it in code, test it, and debug any problems. Give it a try: it's worth getting used to the whole process early as it's far simpler to learn on simple code like this that it will be on the much more complicated examples you will get later!
 
Share this answer
 
Quote:
My program is wrong, why?

First thing, when you show a piece of code, it is a good idea to make complete enough so that we can compile and run it.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
The main difference with the debugger is that it initialize all variables with 0, even if not initialized in code, contrary to non debugged programs with do not have default value.
You can often have a variable declared in 1 place and used in another place.
C++
long long int a, c, i, f, y, z;

During debugging, you can initialize the variables with fancy value so that when you see that value being used while debugger, you know you missed something.
C++
long long int a=-12345, c=-12345, i=-12345, f=-12345, y=-12345, z=-12345;
 
Share this answer
 
You need to debug your code.

A common way is to seperate your code into small and clean functions with parameters and return the results. It is called testing.

For instance:

C++
const char *shape(int y, int z) {
 if (y == 1 && z == 1){
    return("Both");
}else if (y == 1){
    return("Square");
   }else if (z == 1){
        return("Triangular");
        }else {
            return("Neither");
        }
}

return NULL;
}
You can test it like:
C++
char *s = shape( 1,1);
if( strcmp(s, "Both") {
 //log error
  printf("Both test unsucesful");
}
This helps to identify bugs and so shows you what works and what not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900