Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to solve this simple programm text

+1 vote
asked Jun 15, 2023 by Herbert Lensch (130 points)
Hello, I think something is wrong with this _sqrt(z) Operation, also the x number isnt used in the main function? Somebody a little fix/hint

#include <stdio.h>

static void_sqrt(int x)
{
 x = x*x;   
}

int main(int argc, char *argv[])

{
   if (2==argc){
       int z;
       sscanf(argv[1], "%d", &z);
       
       _sqrt(z);
   
    printf("%d/n", z);
}
    return 0;
}

Herbert

1 Answer

0 votes
answered Jun 18, 2023 by Peter Minarik (86,240 points)

Your function is called _sqrt(), which suggests the square root of a value. However, your function calculates the square of a number instead. This is misleading.

Also, there is no space between the return value and the name of the function. This should be like this:

static void _sqrt(int)

This function calculates the square, but it is not communicated to the caller in any way. The easiest way would be to return the calculated value, such as

static int square(int x)
{
    return x * x;
}

In your main() function you call _sqrt(z), but you never store the returned value and you print out in the next line the value of z, that is, the input of your _sqrt(z) instruction. After the above suggestion, this could be easily fixed.

One more thing, the new line character is '\n', not "/n".

So all of this put together would result in something, like below:

#include <stdio.h>

static int square(int x)
{
    return x * x;
}

int main(int argc, char * argv[])

{
    if (argc != 2)
        return -1;
        
    int z;
    sscanf(argv[1], "%d", &z);
    printf("%d\n", square(z));

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...