เราสามารถใช้วิธีการพัฒนาซอฟต์แวร์เพื่อแก้สมการเชิงเส้นของตัวแปรหนึ่งตัวในภาษาซีโปรแกรมได้
ความต้องการ
- สมการควรอยู่ในรูป ax+b=0
- a และ b เป็นอินพุต เราต้องหาค่าของ x
บทวิเคราะห์
ที่นี่
- ข้อมูล อินพุต คือ ค่า a,b .
- ผลลัพธ์ คือ x ค่า .
อัลกอริทึม
อ้างถึงอัลกอริทึมที่ระบุด้านล่างเพื่อค้นหาคำตอบของสมการเชิงเส้น
Step 1. Start Step 2. Read a,b values Step 3. Call function Jump to step 5 Step 4. Print result Step 5:
- i. if(a == 0)
- Print value of c cannot be predicted
- Else
- Compute c=-b/a
- Return c
โปรแกรม
ต่อไปนี้เป็นโปรแกรม C เพื่อหาคำตอบของสมการเชิงเส้น -
#include <stdio.h>
#include <string.h>
float solve(float a, float b){
float c;
if(a == 0){
printf("value of c cannot be predicted\n");
}else{
c = -b / a;
}
return c;
}
int main(){
float a, b, c;
printf("\n enter a,b values: ");
scanf("%f%f", &a, &b);
c = solve(a, b);
printf("\n linear eq of one variable in the form of ax+b = 0, if a=%f,b=%f,then x= %f",a,b,c);
return 0;
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
enter a,b values: 4 8 linear eq of one variable in the form of ax+b = 0, if a=4.000000, b=8.000000, then x= -2.000000