Difference_of_squares.c

I am not sure why my code isn’t passing the test. I will paste it below.

#include “difference_of_squares.h”

int difference_of_squares(int n)
{
int sum = 0;
int sum_of_squares;
int square_of_sum = 0;
int diff;

for (int i = 1; i <= n; i++){
    sum += i;
} 
sum_of_squares = sum * sum;

for (int j = 1; j<=n; j++){
    square_of_sum += j*j;
}
diff = sum_of_squares - square_of_sum;
return diff;

}

Also sharing the screenshot of why it’s failing.

Do you understand what the error message is saying? Take a look at the file “difference_of_squares.h”.

Note, when you share code without using a codeblock, the code will get mangled.

No I am not sure what the errors mean. I’m very new to C.

Ok I will share screenshots henceforth. Thank you.

Please don’t use screenshots. Please use codeblocks.

```
your code here
```
1 Like

No I am not sure what the errors mean. I’m very new to C.

In that case, since there’s no learning mode in the C track, you probably should do the exercises alongside some tutorial or course.

A good reference might also come in handy.

In a typical C program, there are two files, a header (usually ending in .h) and a source file (usually ending in .c).

A header file declares the interface for your program: the functions, names and data structures you’re going to use. A source file implements this interface. The source file must #include the header file at the beginning.

So, you should always have some declaration of functions in the .h file and the functions you declare in the .h file should be defined in the .c file and must have the same overall structure as it was declared in the .h file. So, for instance, if you declare a function to get two ints and return a double, you can’t implement it as taking two chars and returning a float.

2 Likes

This was very helpful. I think I know what to do now. Much appreciated.

1 Like