0

I have a question. Basicly, I want to calculate a formula. I want to use this formula for transform the numbers in the first array to second array. For example;

My first array is; {178, 693, 1346, 2484, 2848, 3470, 3820}

My second array is; {110, 290, 530, 920, 1300, 1890, 2050}

I want a formula like this: y = a + bx + cx^2 + d*x^3

When i give my first array as an input to this function, it should give me second array as and answer. For example;

110 = a + b*178 + c*178^2 + d*178^3

As you can see, I want my computer to calculate a, b, c and d numbers. The computer should give me the answer as shown below;

'Your numbers are: a=325, b=-258, c=-3 d=412'

I know, this is mathematically impossible to calculate exact a,b,c,d numbers for transformation because there is none. So I only wanted aprox. function. What is this mean? If a function can calculate outputs with %90 accuracy, this is enough for me.

Long story short, I wrote a code for this in c++ (visual studio) but the app crashed imediately. Can you please specify my problem and help me?

#include "iostream"
#include "stdio.h"

long x[] = {178, 693, 1346, 2484, 2848, 3470, 3820};
long y[] = {110, 290, 530, 920, 1300, 1890, 2050};
long y_deneme[7];
long a = -9999999;
long b = -9999999;
long c = -9999999;
long d = -9999999;
long a_tutan[2];
long b_tutan[2];
long c_tutan[2];
long d_tutan[2];
long y_tutan[2][7];
long a_secilen = 0;
long b_secilen = 0;
long c_secilen = 0;
long d_secilen = 0;
long y_secilen[7];
int y_0_dizi_sayac = 0;
int y_1_dizi_sayac = 0;
int tutan_deneme_sayac = 0;
int katsayi_index = 0;

int main() {
printf("Katsayilar deneniyor. a = %ld, b = %ld, c = %ld, d = %ld \n", a, b, c, d);
for (a = -9999999; a < 9999999; a ++) {
    for (int i = 0; i < 7; i ++) {
        y_deneme[i] = a + (b * x[i]) + (c * x[i] * x[i]) + (d * x[i] * x[i] * x[i]);
    }
    for (int i = 0; i < 7; i ++) {
        if (y_deneme[i] <= (y[i] + y[i] / 10) || y_deneme[i] >= (y[i] - y[i] / 10)) {
            tutan_deneme_sayac++;
        }
    }
    if (tutan_deneme_sayac == 6) {
        a_tutan[katsayi_index] = a;
        b_tutan[katsayi_index] = b;
        c_tutan[katsayi_index] = c;
        d_tutan[katsayi_index] = d;
        printf("Tutan degerler var. a = %ld, b = %ld, c = %ld, d = %ld \n", a_tutan[katsayi_index], b_tutan[katsayi_index], c_tutan[katsayi_index], d_tutan[katsayi_index]);
        for (int i = 0; i < 7; i++) {
            y_tutan[katsayi_index][i] = y_deneme[i];
        }
        katsayi_index++;
    }
    tutan_deneme_sayac = 0;
    if (katsayi_index > 1) {
        for (int i = 0; i < 7; i++) {
            if (y[i] - y_tutan[0][i] < y[i] - y_tutan[1][i]) {
                y_0_dizi_sayac++;
            }
            else {
                y_1_dizi_sayac++;
            }
        }
        if (y_0_dizi_sayac < y_1_dizi_sayac) {
            for (int i = 0; i < 7; i++) {
                y_secilen[i] = y_tutan[0][i];
            }
            a_secilen = a_tutan[0];
            b_secilen = b_tutan[0];
            c_secilen = c_tutan[0];
            d_secilen = d_tutan[0];
            printf("Secilen degerler degisti. a = %ld, b = %ld, c = %ld, d = %ld \n", a_secilen, b_secilen, c_secilen, d_secilen);
        }
        else {
            for (int i = 0; i < 7; i++) {
                y_secilen[i] = y_tutan[1][i];
            }
            a_secilen = a_tutan[1];
            b_secilen = b_tutan[1];
            c_secilen = c_tutan[1];
            d_secilen = d_tutan[1];
            printf("Secilen degerler degisti. a = %ld, b = %ld, c = %ld, d = %ld \n", a_secilen, b_secilen, c_secilen, d_secilen);
        }
    }
}
b++;
if (b > 9999999) {
    b = -9999999;
    c++;
}
if (c > 9999999) {
    c = -9999999;
    d++;
}
if (d > 9999999) {
    printf("Programin sonu geldi. \n");
    printf("En yakin a = %ld, b = %ld, c = %ld, d = %ld \n", a_secilen, b_secilen, c_secilen, d_secilen);
    printf("En yakin y0 = %ld, y1 = %ld, y2 = %ld, y3 = %ld, y4 = %ld, y5 = %ld, y6 = %ld \n", y_secilen[0], y_secilen[1], y_secilen[2], y_secilen[3], y_secilen[4], y_secilen[5], y_secilen[6]);
    printf("Gerçek y0 = %ld, y1 = %ld, y2 = %ld, y3 = %ld, y4 = %ld, y5 = %ld, y6 = %ld", y[0], y[1], y[2], y[3], y[4], y[5], y[6]);
}
}
  • 1
    What type of crash? What was the error msg? What line of code? What are the variables being accessed on that line of code? What were the variable values? All these can easily be obtained from the debugger and would bring you much closer to understanding your problem. – kaylum Mar 25 '20 at 20:21
  • I'll point the new user @Halit to this stackoverflow question that explains how he might be able to [use a debugger to help him solve his code problem](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Try reading that and if you still can't figure out what's happening you could try rephrasing your question to be a little more specific. Good luck! – JJF Mar 25 '20 at 20:27
  • I downloaded, compiled, built, and ran your source code [under `gdb`]. It did _not_ crash. I got output: `Katsayilar deneniyor. a = -9999999, b = -9999999, c = -9999999, d = -9999999`. So, _no_ crash, but probably not the correct output. – Craig Estey Mar 25 '20 at 20:47
  • 2
    BTW, this math problem is called [polynomial regression](https://en.wikipedia.org/wiki/Polynomial_regression). – Bob__ Mar 25 '20 at 21:16
  • multiplication has higher precedence than addition, no need for parentheses around them like that – phuclv Mar 26 '20 at 02:19

0 Answers0