วันศุกร์ที่ 11 กันยายน พ.ศ. 2558

LAB 4 Generic 1-4


LAB 4 Generic 1 ( Calculate sum of integers from 1 to N )
    void setup(){
      int N = 10;
      println("N = "+N);
      sum_1_to_N(N);
    }

    void sum_1_to_N(int n){
      int sum = 0;
      while(n>0){
        if(n>1){
          print(n+"+");
        }else{
          print("1 = ");
        }
        sum = sum + n;
        n--;
      }
      println(sum);
      println("Sum 1 to N = "+sum);
    }

LAB 4 Generic 2 (  Multiplication table )
    void setup(){
      int start_num = 12;int end_num = 15;
      draw_multiplication_table(start_num,end_num);
    }

    void draw_multiplication_table(int current,int end_num){
      int n = 1;int result;
      while(current <= end_num){
        println("  *"+current+"* ");
        while(n <= 12){
          result = current*n;
          println(current+"X"+n+" = "+result);
          n++;
        }
        println(" ");
        n = 1;
        current ++;
      }
    }

LAB 4 Generic 3 ( Sum of prime numbers from 1 to N )
    void setup(){
      int end_num = 100;
      sum_prime_num(end_num);
    }

    void sum_prime_num(int end_num){
      int current = 2;int sum = 0;int n = 1;
      print("Prime number : ");
      while(current<=end_num){
        int ref_num = 2;boolean prime_num = true;
        while(ref_num<current && prime_num){
          if(current%ref_num==0)
            prime_num = false;
          ref_num++;
        }
        if(prime_num){
          sum += current;
          print(current+"  ");
          if(n%15 == 0)
            println(" ");
          n++;
        }
        current ++;
      }
      println(" ");
      println("Sum of Prime number = "+sum);
    }

LAB 4 Generic 4 ( Loan payment)
     void setup(){
      float loan = 5000;float interestRate = 12;int monthsTerm = 12;
      loan_cal(loan,interestRate,monthsTerm);
    }

    void loan_cal(float loan,float interestRate,int monthsTerm){
      float monthlyPayment;float effectiveInterest;float interest;float principal;
      float Balance;int n = 1;int term = 1;float totalInterest = 0;
      effectiveInterest = interestRate/(100*12);
      monthlyPayment = loan*(effectiveInterest/(1-pow(1+effectiveInterest,-monthsTerm)));  
      Balance = loan;
      println("Loan = "+loan);
      println("Interest rate = "+interestRate+"%");
      println("Loan term = "+monthsTerm+" mounth(s) ("+monthsTerm/12+" year(s))");
      println("Payment per mounth = "+monthlyPayment);
      println("Payment No. : Interest : Principal : Unpaid Balance : Total Interest to Date");
      while(term<=monthsTerm){
        interest = effectiveInterest*Balance;
        principal = monthlyPayment-interest;
        Balance = Balance-principal;
        totalInterest = totalInterest+interest;
        println("    "+round(term)+"            "+round(interest)+"         "+round(principal)+"           "+round(Balance)+"            "+round(totalInterest));    
        term++;
        if(n%12 == 0)
          println(" ");
        n++;
      }
    }

ปัญหาทีพบและวิธีแก้ :
 1) ประกาศตัวแปรซ้ำกัน 2 ที่ ( ลบคำสั่งประกาศตัวแปรให้เหลือ แค่ 1 คำสั่ง)
 2) ตัวแปรแบบ int ไม่สามารถรับค่า ที่เป็นทศนิยมได้ (กำหนดใช้ตัวแปรชนิด float แทน)
 3) loop while ค้างเพราะไม่ได้กำหนดคำสั่งบวกตัวแปรกำหนดจำนวนรอบ n (เพิ่มคำสั่ง n++; ไว้หลังสุด )
 4) เครื่องหมาย } หลังจบฟังก์ชั่นหายไป (เติมเคื่องหมาย } หลังฟังก์ชั่นที่หายไป )

ไม่มีความคิดเห็น:

แสดงความคิดเห็น