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.

Why does the 3rd Loop become random numbers? C Programming

+4 votes
asked Nov 1 by Jing Dao Tan (160 points)
/*
Output should reflect as follows:
Number of Engine Size: 5
  x Values: Corresponding Values
      1.5 , 110
      2.0 , 130
      2.4 , 245
      3.0 , 290
      3.6 , 310

1st Round: m = 11.996, c = 4.34, MSE = 53845
2nd Round: m = 22.145, c = 7.993, MSE = 38827.381
3th Round: m = 30.732, c = 11.066, MSE = 28091.485
4th Round: m = 37.999, c = 13.648, MSE = 20415.483
*/

#include <stdio.h>
#include <math.h>
#define MAXSIZE 100
float x[MAXSIZE], y[MAXSIZE], pred_y[MAXSIZE];
double m, c, Dm, Dc, MSE;

void predy (float m, float c, int times)
{
  int i;
  for (i = 0; i < times; i++)
    {
      pred_y[i] = m * x[i] + c;
    }
}

float calc_dm (int times)
{
  int i;
  float sum, Dm;
  double dm[MAXSIZE];
  for (i = 0; i < times; i++)
    {
      dm[i] = x[i] * (y[i] - pred_y[i]);
    }
  for (i = 0; i < times; i++)
    {
      sum += dm[i];
    }
  Dm = (-2 / (float)times) * sum;
  return Dm;
}

float calc_dc (int times)
{
  int i;
  float sum, Dc;
  double dc[MAXSIZE];
  for (i = 0; i < times; i++)
    {
      dc[i] = (y[i] - pred_y[i]);
    }
  for (i = 0; i < times; i++)
    {
      sum += dc[i];
    }
  Dc = (-2 / (float)times) * sum;
  return Dc;
}

float calc_MSE (int times)
{
  int i;
  double e[MAXSIZE];
  double sum = 0.0;
  for (i = 0; i < times; i++)
    {
      e[i] = (y[i] - pred_y[i]) * (y[i] - pred_y[i]);
    }
  for (i = 0; i < times; i++)
    {
      sum = sum + e[i];
    }
  MSE = sum / 5.0;
  return MSE;
}

int main() {
  int i, times,j;
  double pred_y[MAXSIZE];
  float n = 0.01;
  printf("Enter the number of Engine Size to be calculalted: ");
  scanf("%d", &times);
  for (i = 0; i < times; i ++)
    {
      printf("Enter the Engine Size for #%d: ", i + 1);
      scanf("%f", &x[i]);
      printf("Enter the corresponding CO2 Emission: ");
      scanf("%f", &y[i]);
    }
  
  for (j = 0; j < 5 ; j++)
  {
  predy(m, c, times);
  Dm = calc_dm(times);
  Dc = calc_dc(times);
  MSE = calc_MSE(times);
  m = m - n * Dm;
  c = c - n * Dc;
  m = round(m * 1000)/ 1000;
  c = round(c * 1000)/ 1000;
  MSE = round(MSE * 1000)/ 1000;
  printf ("Value of M: %.3f\n", m);
  printf ("Value of C: %.3f\n", c);
  printf ("Value of MSE: %.3f\n", MSE);
    }
  return 0;
}

1 Answer

0 votes
answered Nov 4 by Peter Minarik (63,140 points)

I'm not sure about your calculations, but line 81 is definitely wrong as it is prone to read memory garbage. It should be this instead:

for (j = 0; j < times ; j++)

Actually, if you're calculating 5 consecutive predictions, your code may be fine.

I have no idea what you're calculating. Maybe your formulas are wrong? DC, DM, MSE? They mean nothing to me.

commented Nov 4 by MIKEY (150 points)
I am thinking why do u need these formulas for?
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.
...