Jump to content
Admin2

Masalah Rumus Math Dalam C

Recommended Posts

Assalamualaikum, selamat berpose kepada muslimin dan muslimat sekalian. Dah lama tak aktif kat bahagian programming ni. Kini aku kembali aktif dengan C. Aku sedang test buat program untuk calculate rumus Vertex (Titik maximum atau minimum) point. Masalahnya rumus aku tu tak jadi. So boleh siapa fixkan sikit kenapa boleh ada ralat.

 
#include <stdio.h>

void main()


{

    int a, b, c;
        
    int vertexX, vertexY; //variable for VertexX = X point, vertexY = Y point

    printf("\n*************************************\n");
    printf("\n*** Program copyrighted by Arafat ***\n");
    printf("\n*************************************\n");

    printf("nPlease enter the value for a: ");
    scanf("%d", &a);
                printf("\n");
    
    printf("nPlease enter the value for b: ");
    scanf("%d", &b);
    printf("\n");
    
    printf("nPlease enter the value for c: ");
    scanf("%d", &c);
    printf("\n");

    
    vertexX = (-B)/(2*a);
    vertexY = (a) * ((vertexX)^2) + (b*(vertexX)) + ©;     \\ Bahagian ini ada ralat...
    printf("\nXvertex is: %d", vertexX);
    printf("\nYvertex is: %d", vertexY);

    printf("\n");
    
    

}

Contoh soalan.

f(x)=x^2+6x+8

Program output:

Sila masukkan nilai a: 1

Sila masukkan nilai b: 6

Sila masukkan nilai c: 8

VertexX = -3

VertexY = -11 <-salah!! macam mana nak betulkan. jawapannya ialah -1

Secara mathematicnya, Vertex bagi f(x)=x^2+6x+8 = (-3, 1)

Ni bukan assignment!!!

Edited by afat

Share this post


Link to post
Share on other sites

be careful with your math vs C operators. ^ operator dalam C bermaksud bitwise xor manakala ^ dalam math awak adalah kuasa dua. So replace

((vertexX)^2)

dalam code awak dengan double multiply atau gunakan pow() function... atau apa2lah fungsi yang berkenaan.

Share this post


Link to post
Share on other sites

sorry B tu sebenarnya b. ni saya ubah balik.. Paralys3r boleh tunjukkan skit tak function pow() tu? saya ni baru lagi dalam C ni. macam mana nak taruk function kuasa 2? kat kuasa dua tu ke yang ada ralat?

#include <stdio.h>

void main()


{

    int a, b, c;
        
    int vertexX, vertexY; //variable for VertexX = X point, vertexY = Y point

    printf("\n*************************************\n");
    printf("\n*** Program Find vertex (Titik Max or Min***\n");
    printf("\n*************************************\n");

    printf("Please enter the value for a: ");
    scanf("%d", &a);
                printf("\n");
    
    printf("Please enter the value for b: ");
    scanf("%d", &b);
    printf("\n");
    
    printf("Please enter the value for c: ");
    scanf("%d", &c);
    printf("\n");

    
    vertexX = (-b)/(2*a);
    vertexY = (a) * ((vertexX)^2) + (b*(vertexX)) + c;     // Bahagian ini ada ralat...
    printf("\nXvertex is: %d", vertexX);
    printf("\nYvertex is: %d", vertexY);

    printf("\n"); 
    
    

}
ataupun ubah macam kat bawah ni ke? tapi lagi la tak dapat jawapan. semuanya kosong
#include <stdio.h>
#include <math.h>

void main()


{

    int a, b, c;
        
    double vertexX, vertexY; //variable for VertexX = X point, vertexY = Y point

    printf("\n*************************************\n");
    printf("\n*** Program Find vertex x,y                  ***\n");
    printf("\n*************************************\n");

    printf("Please enter the value for a: ");
    scanf("%d", &a);
                printf("\n");
    
    printf("Please enter the value for b: ");
    scanf("%d", &b);
    printf("\n");
    
    printf("Please enter the value for c: ");
    scanf("%d", &c);
    printf("\n");

    
    vertexX = (-b)/(2*a);
    vertexY = (a) * (pow(vertexX, 2)) + (b*(vertexX)) + c;     // Bahagian ini ada ralat...
    printf("\nXvertex is: %d", vertexX);
    printf("\nYvertex is: %d", vertexY);

    printf("\n");
    
    

}

Recall balik math:

Rumus untuk cari vertex dalam math ialah:

untuk point x = -b/2a

untuk point y = f(-b/2a)

kalau nak selesaikan soalan ni:

cari vertex bagi f(x) = x^2+6x+8

point x,

-b/2a = -6/2(1) = -3

point y,

f(-b/2a) = (-3)^2+6(-3)+8 = -1 atau pun (-b/2a)^2+6(-b/2a)+8 = -1

so, vertex = (-3, -1)

macam mana nak coding dalam C pulak? fening2

Edited by afat

Share this post


Link to post
Share on other sites

printf("\nXvertex is: %d", vertexX);
printf("\nYvertex is: %d", vertexY);
vertexX & vertexY are declared as double. So tukar %d jadi %f
printf("\nXvertex is: %f", vertexX);
printf("\nYvertex is: %f", vertexY);

Edited by mchammer

Share this post


Link to post
Share on other sites

oooo jadi la.. thanx2 tukar %d ke %f. trima kasih mchammer. trima kasih sume..

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

void main()


{

    int a, b, c;
        
    double vertexX, vertexY; //variable for VertexX = X point, vertexY = Y point

    printf("\n*************************************\n");
    printf("\n*** Program Find vertex x,y                  ***\n");
    printf("\n*************************************\n");

    printf("Please enter the value for a: ");
    scanf("%d", &a);
                printf("\n");
    
    printf("Please enter the value for b: ");
    scanf("%d", &b);
    printf("\n");
    
    printf("Please enter the value for c: ");
    scanf("%d", &c);
    printf("\n");

    
    vertexX = (-b)/(2*a);
    vertexY = (a) * (pow(vertexX, 2)) + (b*(vertexX)) + c;     // fixed
    printf("\nXvertex is: %f", vertexX);  //fixed
    printf("\nYvertex is: %f", vertexY); //fixed

    printf("\n");
    
    

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...