Single linear regression analysis model error

Line 17 : "sumXY" redeclared as different kind of symbol

Line 20 : incompatible types when returning type ‘float (*)(float, float)’ but ‘float’ was expected

how to solve this error?

#include <stdio.h>
#include<math.h>
#include<stdlib.h>

float sumX, sumY, sumXY, sumsqX, xy, sum, n, b, a;
float a1, a2, a3, a4, a5, b1, b2, b3, b4, b5, c1, c2, c3, c4, c5, d1, d2, d3, d4, d5;

float a6, a7, a8, b6, b7, b8, c6, c7, c8, d6, d7, d8;


float sumf(float a, float b, float c, float m, float n, float e, float q, float z) {
    sum = a + b + c + m + n + e + q + z;

    return sum;
}

float sumXY(float p, float q) {

    sumXY = p * q;
    return sumXY;
}

int main()
{

    printf("Enter the values of n:\n\n");
    scanf("%f", &n);
    sumX = 0;
    printf("Enter the values of X:\n\n");
    scanf("%f", &a1);
    scanf("%f", &a2);
    scanf("%f", &a3);
    scanf("%f", &a4);
    scanf("%f", &a5);
    scanf("%f", &a6);
    scanf("%f", &a7);
    scanf("%f", &a8);
    sumX = sumf(a1, a2, a3, a4, a5, a6, a7, a8);

    sumY = 0;
    printf("Enter the values of Y:\n\n");
    scanf("%f", &b1);
    scanf("%f", &b2);
    scanf("%f", &b3);
    scanf("%f", &b4);
    scanf("%f", &b5);
    scanf("%f", &b6);
    scanf("%f", &b7);
    scanf("%f", &b8);
    sumY = sumf(b1, b2, b3, b4, b5, b6, b7, b8);

    c1 = sumXY(a1, a1);
    c2 = sumXY(a2, a2);
    c3 = sumXY(a3, a3);
    c4 = sumXY(a4, a4);
    c5 = sumXY(a5, a5);
    c6 = sumXY(a6, a6);
    c7 = sumXY(a7, a7);
    c8 = sumXY(a8, a8);
    sumsqX = sumf(c1, c2, c3, c4, c5, c6, c7, c8);

    d1 = sumXY(a1, b1);
    d2 = sumXY(a2, b2);
    d3 = sumXY(a3, b3);
    d4 = sumXY(a4, b4);
    d5 = sumXY(a5, b5);
    d6 = sumXY(a6, b6);
    d7 = sumXY(a7, b7);
    d8 = sumXY(a8, b8);
    xy = sumf(d1, d2, d3, d4, d5, d6, d7, d8);


    printf("\t-----------------------------------------------------\n");
    printf("\t\t\t*****Regression Table****\n");
    printf("\t\t\t.....................\n");


    printf("\n\tX\t\tY\tX^2\t\tXY\t\t\n");
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", a1, b1, c1, d1);
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", a2, b2, c2, d2);
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", a3, b3, c3, d3);
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", a4, b4, c4, d4);
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", a5, b5, c5, d5);
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", a6, b6, c6, d6);
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", a7, b7, c7, d7);
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", a8, b8, c8, d8);

    printf("\t=======================================================\n");
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", sumX, sumY, sumsqX, xy);
    printf("\t=======================================================\n\n");
    b = (sumXY(n, xy) - sumXY(sumX, sumY)) / (sumXY(n, sumsqX) - pow(sumX, 2));
    a = (sumY / n) - sumXY(b, sumX) / n;
    printf("The regression Co-Efficient is :\t%.2f\n\n", b);
    printf("The regression constant is :\t%.2f\n\n", a);
}

Upvotes: 0

Views: 54

Answers (1)

bruno
bruno

Reputation: 32596

as said in a remark you have

  float ..., sumXY, ...;

where you define sumXY as a global variable of type float

then

  float sumXY(float p, float q) {

define a function having the same name

In the rest of your program you do not use sumXY as a variable, so replace

 float sumX, sumY, sumXY, sumsqX, xy, sum, n, b, a;

by

 float sumX, sumY, sumsqX, xy, sum, n, b, a;

and

float sumXY(float p, float q) {

    sumXY = p * q;
    return sumXY;
}

by for instance

float sumXY(float p, float q) {
   return p * q;
}

even it is strange to define a function just to do a multiplication


Some other remarks :

  • I encourage you to check the return value of your scanf("%f", &xxx);, if the user do not enter a valid float the current variable and all the next will be unset but you will not know that
  • why do you use so much variables rather than arrays ? using array you can place your consecutive scanf in a loop and idem for the printf and do not have to give them separately in argument. Imagine also if the number of values is not anymore 8 but something else like 88 !
  • it is a bad idea to use global variables, they can be all local variables in main without any changes elsewhere

Here is a corrected and simplified version of your program, as you can see if the number of elements must not be 8 it is enough to change #define N 8 to a new value

#include <stdio.h>
#include<math.h>
#include<stdlib.h>

int enterValues(const char * msg, float v[], size_t n)
{
  puts(msg);

  for (size_t i = 0; i != n; ++i) {
    if (scanf("%f", &v[i]) != 1) {
      puts("invalid input");
      return 0;
    }
  }
  return 1;
}

float sumf(float a[], size_t n) {
  float sum = 0;

  for (size_t i = 0; i != n; ++i)
    sum += a[i];

  return sum;
}

float sumXY(float p, float q) {
  return p * q;
}

#define N 8

int main()
{
    float n;

    printf("Enter the values of n:\n\n");
    if (scanf("%f", &n) != 1) {
      puts("invalid input");
      return -1;
    }

    float a[N];

    if (!enterValues("Enter the values of X:\n", a, sizeof(a)/sizeof(a[0])))
      return -1;

    float sumX = sumf(a, N);

    float b[N];

    if (!enterValues("Enter the values of Y:\n", b, N))
      return -1;

    float sumY = sumf(b, N);

    float c[N];

    for (size_t i = 0; i != n; ++i)
      c[i] = sumXY(a[i], a[i]);

    float sumsqX = sumf(c, N);

    float d[N];

    for (size_t i = 0; i != n; ++i)
      d[i] = sumXY(a[i], b[i]);

    float xy = sumf(d, N);

    printf("\t-----------------------------------------------------\n");
    printf("\t\t\t*****Regression Table****\n");
    printf("\t\t\t.....................\n");


    printf("\n\tX\t\tY\tX^2\t\tXY\t\t\n");

    for (size_t i = 0; i != N; ++i) {
      printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", a[i], b[i], c[i], d[i]);
    }

    printf("\t=======================================================\n");
    printf("\n\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", sumX, sumY, sumsqX, xy);
    printf("\t=======================================================\n\n");

    float bb = (sumXY(n, xy) - sumXY(sumX, sumY)) / (sumXY(n, sumsqX) - pow(sumX, 2));
    float aa = (sumY / n) - sumXY(bb, sumX) / n;

    printf("The regression Co-Efficient is :\t%.2f\n\n", bb);
    printf("The regression constant is :\t%.2f\n\n", aa);

    return 0;
}

Upvotes: 1

Related Questions