I have to make a program for my C class and im stuck on the part where i have to calculate the
variance with elements from my array. This is exactly what im suppose to do:
Reset the sum variable to 0
Using another for loop (which needs its own loop variable), run through all valid elements of the
array, starting from index 0. Inside the loop:
Use the sum variable to accumulate the numerator of the variance formula shown on page 1.
After the end of this loop, compute the variance by dividing the new sum by the
dataElementsToProcess parameter.
this is the entire function
bool GenerateStatistics(FILE *fp2, double myArray[], int dataElementsToProcess)
{
if (fp2 == NULL && myArray == NULL && dataElementsToProcess < 1)
{
return false;
}
else
{
int i = 0,o = 0;
int sum = 0, sum1 = 0;
double maxValue = 0;
double minValue = DBL_MAX;
double mean, variance, stdDeviation;
for (int i = 0; i < 600; i++)
{
sum += myArray[i];
if (myArray[i] > maxValue)
{
maxValue = myArray[i];
}
if (myArray[i] < minValue)
{
minValue = myArray[i]; }
}
mean = sum / dataElementsToProcess;
sum = 0;
for (int o = 0; o < 600; o++)
{
sum += myArray[o];
}
variance = sqrt(pow((sum - mean), 2) / (dataElementsToProcess));
stdDeviation = sqrt(variance);
fprintf(fp2, \"Reaction Rate Statistics:\ \ \");
fprintf(fp2, \"Number of experiments = %d\ \", dataElementsToProcess);
fprintf(fp2, \"Minimum rate\\t\\t= %f\ \", minValue);
fprintf(fp2, \"Maximum rate\\t\\t= %f\ \", maxValue);
fprintf(fp2, \"Arithmetic mean\\t\\t= %f\ \", mean);
fprintf(fp2, \"Variance\\t\\t= %f\ \", variance);
fprintf(fp2, \"Standard deviation\\t= %f\ \ \ \", stdDeviation);
}
return true;
}
Solution
Here in the function you have
average - mean
Noofelements - dataElementsTo Process
Variance formula is
V = summation over i=1 to n ((xi - mean )*(xi - mean))
So perform like this using a for loop over my_array
double V=0.0
for(int i=0;i<600;i++)
{
V=V+pow((my_Array[i]-mean),2)
}
Now calculate standard deviation using sqrt of variance
sd=0.0
sd=sqrt(V).
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
i need help to edit the two methods below so that i can have them wo.pdf
1. i need help to edit the two methods below so that i can have them work in the class provided
after the two methods.
Insert and Select methods below
Patient class below
then below is what the outcome of the data we add to the file we ill create when we get the two
methods above to work will look like patients.txt
A900:1234:Jimmy:Hawkins:Marietta:jhawk@yahoo.com:Cign.a A901:9999:Bill:Smith:
Acworth:bsmith@gmail.com: Aetna A902:8888: Teri:Smart:Atlanta:tsm@yahoo.com:BlueCross
A903:777:James: Roy:Acworth:jamesray@gmail.com:BlueCross A904:5555: Mary:Wilson:
Roswell:mwil@yahoo.com:cigna A905: 1111:Faith: Adams: Roswell:faith@gmail.com: Aetna
A906:1111:Jerry: Jones:Dallas:ij@cowboys.com:Cigna
A907:9090:Carrie:Slater:Marietta:cslat@gmail.com:Cigna A908:9898:Sara:Jefferson:
Dallas:sjeff@gmail.com:Cigna A909:1234:Debbie:Johnson:Marietta:djohn@hotmail.com:Cigna
A910:1111:Marth:stewart:Marietta:mstew@gmail.com: Aetna A911:4555: John:
Franca:Kennesaw: jfranco@hotmail.com:BlueCros
Solution
package pis;
import java.sql.*;
import java.util.ArrayList;
public class Patient {
private String id,name,dob,complaints,gender,referedby, contactno,mrvid;
private int age;
private ArrayList visits;
public Patient() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
2. }
public void setName(String name) {
this.name = name;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getComplaints() {
return complaints;
}
public void setComplaints(String complaints) {
this.complaints = complaints;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getReferedby() {
return referedby;
}
public void setReferedby(String referedby) {
this.referedby = referedby;
}
public String getContactno() {
return contactno;
}
public void setContactno(String contactno) {
this.contactno = contactno;
}
public String add() {
3. Connection con = null;
try {
con = Database.getConnection();
Statement st = con.createStatement();
// get patient id
ResultSet rs = st.executeQuery("select nvl(max(pid),0) + 1 from patients");
rs.next();
id = rs.getString(1);
rs.close();
PreparedStatement ps = con.prepareStatement("insert into patients values(?,?,?,?,?,?,null,?)");
ps.setString(1,id);
ps.setString(2,name);
ps.setString(3,dob);
ps.setString(4,gender);
ps.setString(5,contactno);
ps.setString(6,complaints);
ps.setString(7,referedby);
if ( ps.executeUpdate() == 1)
return id;
else
return null; // error
}
catch(Exception ex) {
System.out.println(ex.getMessage());
return null;
}
finally {
Database.close(con);
}
} // add()
public static boolean update(String pid, String contactno, String complaints) {
Connection con = null;
try {
con = Database.getConnection();
4. PreparedStatement ps = con.prepareStatement("update patients set contactno =?, complaints = ?
where pid = ?");
ps.setString(1,contactno);
ps.setString(2,complaints);
ps.setString(3,pid);
if ( ps.executeUpdate() == 1)
return true;
else
return false; // error
}
catch(Exception ex) {
System.out.println(ex.getMessage());
return false;
}
finally {
Database.close(con);
}
} // update()
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ArrayList getVisits() {
return visits;
}
public void setVisits(ArrayList visits) {
this.visits = visits;
}
public static Patient getAllDetails(String pid) {
// connect and get all details of the given patient
Connection con= null;
try{
5. con = Database.getConnection();
PreparedStatement ps = con.prepareStatement("select name,gender, dob, (sysdate - dob) / 365
age, complaints, contactno, referedby from patients where pid = ?");
ps.setString(1,pid);
ResultSet rs = ps.executeQuery();
if (! rs.next () )
return null;
// System.out.println("Patient Found");
// create Patient object and load data from row to object
Patient p = new Patient();
p.setId(pid);
p.setName( rs.getString("name"));
p.setContactno( rs.getString("contactno"));
p.setComplaints( rs.getString("complaints"));
p.setGender( rs.getString("gender"));
p.setDob( rs.getString("dob"));
p.setAge( rs.getInt("age"));
p.setReferedby( rs.getString("referedby"));
rs.close();
// Get details of visits of this patient
ps = con.prepareStatement("select vid, to_char(visitdate) vdate,complaint from visits where pid
= ? order by visitdate desc");
ps.setString(1,pid);
rs = ps.executeQuery();
ArrayList visits = new ArrayList();
Visit v;
while ( rs.next()) {
visits.add(new Visit(rs.getString("vid"), pid,rs.getString("complaint"),
rs.getString("vdate")));
}
rs.close();
p.setVisits(visits);
return p;
} // end of try
catch(Exception ex) {
9. this.visitdate = visitdate;
drugs = new ArrayList();
tests = new ArrayList();
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getComplaint() {
return complaint;
}
public void setComplaint(String complaint) {
this.complaint = complaint;
}
public String getVisitdate() {
return visitdate;
}
public void setVisitdate(String visitdate) {
this.visitdate = visitdate;
}
public ArrayList getDrugs() {
return drugs;
}
public void setDrugs(ArrayList drugs) {
this.drugs = drugs;
}
public ArrayList getTests() {
return tests;
}
public void setTests(ArrayList tests) {
this.tests = tests;
}
public String getVid() {
return vid;
10. }
public void setVid(String vid) {
this.vid = vid;
}
public static String add(String pid, String complaint, String drugnames[], String dosage[], String
nodays[], String tests[]) {
Connection con = null;
String vid;
try {
con = Database.getConnection();
// begin a transaction
con.setAutoCommit(false);
Statement st = con.createStatement();
// get visit id
ResultSet rs = st.executeQuery("select nvl(max(vid),0) + 1 from visits");
rs.next();
vid = rs.getString(1);
rs.close();
PreparedStatement ps = con.prepareStatement("insert into visits values(?,?,sysdate,?)");
ps.setString(1,vid);
ps.setString(2,pid);
ps.setString(3,complaint);
if ( ps.executeUpdate() != 1)
return null; // error
System.out.println("Visit added");
// insert into DRUGS
ps = con.prepareStatement("insert into drugs values(?,?,?,?)");
for ( int i = 0; i < 5; i ++) {
if ( drugnames[i].trim().length() == 0 ) continue;
ps.setString(1,vid);
ps.setString(2,drugnames[i]);
ps.setString(3,dosage[i]);
ps.setString(4,nodays[i]);
if ( ps.executeUpdate() == 0 )
throw new Exception("Could not insert drug details");
11. else
System.out.println("drug added");
}
// insert into TESTS
ps = con.prepareStatement("insert into tests values(?,?,null,null)");
for (int i = 0 ; i < 5 ; i ++) {
if ( tests[i].trim().length() == 0 ) continue;
ps.setString(1,vid);
ps.setString(2,tests[i]);
if ( ps.executeUpdate() == 0 )
throw new Exception("Could not insert test details");
else
System.out.println("Test added");
}
// update patients data
ps = con.prepareStatement("update patients set mrvid = ? where pid = ?");
ps.setString(1,vid);
ps.setString(2,pid);
ps.executeUpdate();
con.commit();
return vid;
}
catch(Exception ex) {
try {
con.rollback();
}
catch(Exception nex) {}
System.out.println(ex.getMessage());
return null;
}
finally {
Database.close(con);
}
} // add()
12. public static boolean updateTests(String vid, String testnames[], String testdates[], String
testresults[]) {
Connection con = null;
try {
con = Database.getConnection();
// begin a transaction
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement("update tests set testdate = ? , result = ? where
vid = ? and testname = ?");
for ( int i = 0 ; i < testnames.length ; i ++) {
if ( testdates[i].trim().length() > 0 && testresults[i].trim().length() > 0 ) {
ps.setString(1,testdates[i]);
ps.setString(2,testresults[i]);
ps.setString(3,vid);
ps.setString(4, testnames[i]);
if ( ps.executeUpdate() != 1) return false;
}
}
con.commit();
return true;
}
catch(Exception ex) {
try {
con.rollback();
}
catch(Exception nex) {}
System.out.println(ex.getMessage());
return false;
}
finally {
Database.close(con);
}
} // add()
13. public static Visit getAllDetails(String vid) {
// connect and get all details of the given visit
Connection con= null;
try{
con = Database.getConnection();
PreparedStatement ps = con.prepareStatement("select vid,to_char(visitdate) visitdate, complaint
from visits where vid = ?");
ps.setString(1,vid);
ResultSet rs = ps.executeQuery();
if (! rs.next () )
return null;
Visit v = new Visit( );
v.setVid(vid);
v.setVisitdate( rs.getString("visitdate"));
v.setComplaint( rs.getString("complaint"));
// Get details of drugs related to visit
ps = con.prepareStatement("select drugname, dosage, nodays from drugs where vid = ?");
ps.setString(1,vid);
rs = ps.executeQuery();
ArrayList drugs = new ArrayList();
while ( rs.next()) {
drugs.add(new Drug(vid, rs.getString("drugname"), rs.getString("dosage"),
rs.getInt("nodays")));
}
rs.close();
v.setDrugs(drugs);
// Get details of tests related to visit
ps = con.prepareStatement("select testname, nvl(to_char(testdate),' ') testdate, nvl(result,' ')
result from tests where vid = ?");
ps.setString(1,vid);
rs = ps.executeQuery();
ArrayList tests = new ArrayList();
while ( rs.next()) {
tests.add(new Test(vid, rs.getString("testname"), rs.getString("testdate"),
14. rs.getString("result")));
}
rs.close();
v.setTests(tests);
return v;
} // end of try
catch(Exception ex) {
System.out.println(ex.getMessage());
return null;
}
finally {
Database.close(con);
}
} // getAllDetails()
public static ArrayList getTests(String vid) {
Connection con= null;
try{
con = Database.getConnection();
// Get details of tests related to visit
PreparedStatement ps = con.prepareStatement("select testname, nvl(to_char(testdate),' ')
testdate, nvl(result,' ') result from tests where vid = ?");
ps.setString(1,vid);
ResultSet rs = ps.executeQuery();
ArrayList tests = new ArrayList();
while ( rs.next()) {
tests.add(new Test(vid, rs.getString("testname"), rs.getString("testdate"),
rs.getString("result")));
}
rs.close();
return tests;
} // end of try
15. catch(Exception ex) {
System.out.println(ex.getMessage());
return null;
}
finally {
Database.close(con);
}
} // getAllDetails()
}
New Patient
<%@include file="head.html"%>
New PatientPatient Name :
Date of birth : (dd-mon-yyyy)
Gender :Male
Female
Contact No. :Complaints : Refered By :
<%@include file="footer.html"%>