calculate and display the square root of each value in NumArray then store it in an array called SquareNumArray.
hi guys what is wrong with this code? 

#include <stdio.h>
int main()
{
    int numArrays[] = {2, 67, 23, 5, 7, 34, 14, 4, 8, 62};
    int SqareNumArray[];
    int i;

    for(i=0; i<10; i++)
    {
    SquareNumArray[i]=sqrt(numArrays[i]);
    printf("%d", SquareNumArray[i]);
        }
} 

Recommended Answers

All 6 Replies

According to my compiler the following things are wrong with your code:

bla.c: In function 'main':
bla.c:5:5: error: array size missing in 'SqareNumArray'
bla.c:9:1: error: 'SquareNumArray' undeclared (first use in this function)
bla.c:9:1: note: each undeclared identifier is reported only once for each function it appears in
bla.c:9:1: warning: implicit declaration of function 'sqrt' [-Wimplicit-function-declaration]
bla.c:9:19: warning: incompatible implicit declaration of built-in function 'sqrt' [enabled by default]
bla.c:5:5: warning: unused variable 'SqareNumArray' [-Wunused-variable]
bla.c:12:1: warning: control reaches end of non-void function [-Wreturn-type]

Though I have to say: you could have easily found that out by trying to compile the code yourself.

As far as semantics are concerned another thing that might be wrong is that you're casting the result of sqrt to int, so the result won't be the actual square root. That may or may not be intentional though. You'll know that better than I do.

Style-wise it's wrong that you hardcoded the number 10 as the upper bound for the for-loop. Magic numbers are bad.

#include <stdio.h>
int main()
{
    int numArrays[] = {2, 67, 23, 5, 7, 34, 14, 4, 8, 62};
    int SqareNumArray[10];
    int i;

    for(i=0; i<10; i++)
    {
    numArrays[i]=sqrt(numArrays[i]);
    SqareNumArray[i]=numArrays[i]
    printf("%d", SquareNumArray[i]);
        }
        return 0;
} 
SqareNumArray[i]=numArrays[i]
printf("%d", SquareNumArray[i]);

thats it?

Fix the spelling mistakes before posting the code

When asking for help, EXPLAIN what problem you are having. Do not expect us to try to figure out what it does wrong -- tell us!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.