0
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]);
        }
} 
4
Contributors
6
Replies
8
Views
4 Years
Discussion Span
Last Post by WaltP
Featured Replies
  • 1
    WaltP 2,905   4 Years Ago

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

0

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.

0
#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;
} 
1

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!

This topic has been dead for over six months. 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.