Вы находитесь на странице: 1из 4

ECE 431 – Computer Programming Assignment 1 (7th Oct 2015)

Groups EE2413A, EE2413E, EE2413H, EE2413G, EE2423D

1. Write a program that calculate the time taken for


the capacitor to be 90%, 95% and 99% charged.
The capacitor is not charged initially i.e. Vc=0 at
t=0. The battery V is a 15V battery, resistor R is
270KΩ and capacitor C is 100μF. Use the following
formula to calculate the time taken for the
capacitor to charge up, where Vc is the voltage
across the capacitor C.
Give your answer in a suitable table form.
http://www.electronics-tutorials.ws/rc/rc_1.html
t

Vc  V (1  e RC
)
SAMPLE PROGRAM
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main(){ float r=270000, c=0.000100;
cout<<fixed<<setprecision(2);
cout<<"Voltage supply = 15V"<<endl;
cout<<"Resistor R = 270K and capacitor C = 100microF"<<endl;
cout<<"\nTime to charge the capacitor :-"<<endl;
cout<<"Percentage\tTime"<<endl;
cout<<" 90%\t\t"<<(-r*c)*log(0.1)<<endl;
cout<<" 95%\t\t"<<(-r*c)*log(0.05)<<endl;
cout<<" 99%\t\t"<<(-r*c)*log(0.009)<<endl; //should be log(0.01)
return 0; }

SAMPLE OUTPUT
Voltage supply = 15V
Resistor R = 270K and capacitor C = 100microF

Time to charge the capacitor:-


Percentage Time
90% 62.17
95% 80.88
99% 127.18

NOTE: The capacitor cannot be 100% charged, the output only estimates for 99%
charged and the charging time may be from 127 to around 190 seconds.

2. Write a menu driven program that convert:-


a. A decimal number to an 8-bit binary number
b. An 8-bit binary number to decimal number

ECE 431 – Mohd Uzir Kamaluddin / October 2015


The user will choose the conversion type and then enter the appropriate value. Use
switch…case construct for the menu.
http://programmingknowledgeblog.blogspot.my/2013/05/c-program-for-converting-
decimals-to.html

SAMPLE PROGRAM
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main(){
int input;
int dc, m, rm, i=1, sum=0;
int bin, dec = 0, rem, num, base = 1;
cout<<"Pls choose the conversion:-"<<endl;
cout<<"1. Decimal to Binary conversion."<<endl;
cout<<"2. Binary Decimal to conversion."<<" Choice (1/2): ";
cin>>input;
switch(input){
case 1: cout<<"\nEnter the decimal to be converted: ";
cin>>dc;
m=dc;
while (dc>0) { rm=dc%2;
sum=sum + (i*rm);
dc=dc/2;
i=i*10; }
cout<<"The binary equivalent of "<<m<<" is: "<<sum<<endl;
break;
case 2: cout << "\nEnter the binary number (1s and 0s): ";
cin >> num;
bin = num;
while (num > 0) {
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10; }
cout << "The decimal equivalent of " << bin << " is: " << dec << endl;
break;
default: cout<<"\nInvalid choice."<<endl; }
return 0; }

SAMPLE OUTPUTS
Pls choose the conversion:-
1. Decimal to Binary conversion.
2. Binary Decimal to conversion. Choice (1/2): 1

Enter the decimal to be converted: 253


The binary equivalent of 253 is: 11111101

ECE 431 – Mohd Uzir Kamaluddin / October 2015


--------------------------------------------------------------------------------------------
Pls choose the conversion:-
1. Decimal to Binary conversion.
2. Binary Decimal to conversion. Choice (1/2): 2

Enter the binary number (1s and 0s): 10110111


The decimal equivalent of 10110111 is: 183

NOTE:
The decimal and binary number entered by user is not checked for validity.
Choice other than 1 or 2 will result in ‘Invalid choice’ printed out.

3. Based on Newton’s method, write a program to calculate the root(s) of the equation:-
2x3 - 3x2 + 2x - 6 = 0. Newton’s formula for finding the roots of an equation is:-
f ( xi )
xi 1  xi 
f '( xí )
where f’(xi) is the derivative of function f(x) evaluated at x=xi. The program display the
equation and ask the user to enter a guess i.e. the starting value of x. The program
then plots the roots from the guessed value until it converges in table form.
Use while…do loop for the iteration process.
https://www.youtube.com/watch?v=1uN8cBGVpfs

SAMPLE PROGRAM
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{ float x, xn, f, df; int y=0;
cout<<fixed<<setprecision(3);
cout << "Function: 2x^3 - 3x^2 + 2x - 6 = 0" <<endl;
cout << "\nEnter guess value of x: "; //prompts user to input starting value
cin >> x; //assigns inputted starting value to x
cout<<"\nx\txn"<<endl; //header
while(y<15)
{ f = ((2*x*x*x)-(3*x*x)+(2*x)-6); //function to solve
df = ((6*x*x)-(6*x)+2); //derivative of function
xn=x-(f/df); //Newton's method
cout<<x<<"\t"<<xn<<endl;
if(xn==x)break; //check if solution has arrived
x=xn; //use new x value for next iteration
y++; }
cout<<"\nThe root equals to "<<x<<" and is found after "<<y<<" iterations."<<endl;
return 0;}

ECE 431 – Mohd Uzir Kamaluddin / October 2015


SAMPLE OUTPUTS
Function: 2x^3 - 3x^2 + 2x - 6 = 0

Enter guess value of x: 1

x xn
1.000 3.500
3.500 2.583
2.583 2.070
2.070 1.872
1.872 1.842
1.842 1.842
1.842 1.842
1.842 1.842

The root equals to 1.842 and is found after 7 iterations.

------------------------------------------------------------------------------------------
Function: 2x^3 - 3x^2 + 2x - 6 = 0

Enter guess value of x: 25

x xn
25.000 16.833
16.833 11.388
11.388 7.762
7.762 5.351
5.351 3.761
3.761 2.743
2.743 2.150
2.150 1.894
1.894 1.843
1.843 1.842
1.842 1.842
1.842 1.842

The root equals to 1.842 and is found after 11 iterations.

ECE 431 – Mohd Uzir Kamaluddin / October 2015

Вам также может понравиться