Admin2 12 Report post Posted September 21, 2007 (edited) 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+8Program output:Sila masukkan nilai a: 1Sila masukkan nilai b: 6Sila masukkan nilai c: 8VertexX = -3VertexY = -11 <-salah!! macam mana nak betulkan. jawapannya ialah -1Secara mathematicnya, Vertex bagi f(x)=x^2+6x+8 = (-3, 1)Ni bukan assignment!!! Edited September 21, 2007 by afat Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted September 21, 2007 vertexX = (-B)/(2*a);B tu declare kat mana? Quote Share this post Link to post Share on other sites
Paralys3r 0 Report post Posted September 22, 2007 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. Quote Share this post Link to post Share on other sites
Admin2 12 Report post Posted September 22, 2007 (edited) 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/2auntuk point y = f(-b/2a)kalau nak selesaikan soalan ni:cari vertex bagi f(x) = x^2+6x+8point x,-b/2a = -6/2(1) = -3point y,f(-b/2a) = (-3)^2+6(-3)+8 = -1 atau pun (-b/2a)^2+6(-b/2a)+8 = -1so, vertex = (-3, -1)macam mana nak coding dalam C pulak? fening2 Edited September 22, 2007 by afat Quote Share this post Link to post Share on other sites
mchammer 0 Report post Posted September 23, 2007 (edited) 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 September 23, 2007 by mchammer Quote Share this post Link to post Share on other sites
Admin2 12 Report post Posted September 24, 2007 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"); } Quote Share this post Link to post Share on other sites