UP | HOME

Exercise 3 solutions

Table of Contents


Python

e03.py

from math import log, pi

# set up constants

Msmall = 47.0
Mbig = 67.0
rho = 1.038
c = 3.7
K = 0.0054
Tw = 100.0
Ty = 70.0

whichEgg = bool(raw_input('Is the egg large (1) or small (0)? '))

if (whichEgg == 0):
        M=Msmall
else:
        M=Mbig

print 'enter the initial temperature of the egg'
To = float(raw_input('reminder 4.0 for fridge, 20.0 for room: '))

t = (((M**(2.0/3.0))*c*(rho**(1.0/3.0)))/(K*(pi**2)*((4.0*pi/3.0)**(2.0/3.0)))) * log(0.76 * ((To-Tw)/(Ty-Tw)))

print "time taken to cook the egg is: %.3f seconds (%d minutes, %d seconds)" % (t, t/60.0, t%60.0)

MATLAB / Octave

e03.m

% set up constants

Msmall = 47.0;
Mbig = 67.0;
rho = 1.038;
c = 3.7;
K = 0.0054;
Tw = 100.0;
Ty = 70.0;

whichEgg = input('Is the egg large (1) or small (0)? ');

if (whichEgg == 0)
        M=Msmall;
else
        M=Mbig;
end;

disp(['enter the initial temperature of the egg']);
To = input('reminder 4.0 for fridge, 20.0 for room: ');

t = (((M^(2.0/3.0))*c*(rho^(1.0/3.0)))/(K*(pi^2)*((4.0*pi/3.0)^(2.0/3.0)))) * log(0.76 * ((To-Tw)/(Ty-Tw)));

disp(['time taken to cook the egg is: ',num2str(t),' seconds (',num2str(floor(t/60.0)),' minutes, ',num2str(floor(mod(t,60))),' seconds)']);

R

e03.R

# set up constants

Msmall <- 47.0
Mbig <- 67.0
rho <- 1.038
c <- 3.7
K <- 0.0054
Tw <- 100.0
Ty <- 70.0

whichEgg <- as.numeric(readline("Is the egg large (1) or small (0)? "))

if (whichEgg == 0) {
	M <- Msmall
} else {
	M <- Mbig
}

cat("enter the initial temperature of the egg\n")
To <- as.numeric(readline("reminder 4.0 for fridge, 20.0 for room: "))

t <- (((M**(2.0/3.0))*c*(rho**(1.0/3.0)))/(K*(pi**2)*((4.0*pi/3.0)**(2.0/3.0)))) * log(0.76 * ((To-Tw)/(Ty-Tw)))

cat("time taken to cook the egg is:",t,"seconds (",as.integer(t/60),"minutes,",as.integer(t%%60),"seconds)\n")

C

e03.c

// compile with: gcc -o e03 e03.c

#include <stdio.h>
#include <math.h>

int main(int argc, char *argv[]) {

  float Msmall = 47.0;
  float Mbig = 67.0;
  float rho = 1.038;
  float c = 3.7;
  float K = 0.0054;
  float Tw = 100.0;
  float Ty = 70.0;

  printf("Is the egg large (1) or small (0)? ");
  int whichEgg;
  scanf("%d", &whichEgg);

  float M;
  if (whichEgg == 0) {
        M = Msmall;
  }
  else {
        M = Mbig;
  }

  printf("enter the initial temperature of the egg\n");
  printf("reminder 4.0 for fridge, 20.0 for room: ");
  float To;
  scanf("%f", &To);

  float pi = 3.14159;
  float t = (((pow(M,(2.0/3.0)))*c*(pow(rho,(1.0/3.0))))/(K*(pi*pi)*(pow((4.0*pi/3.0),(2.0/3.0))))) * log(0.76 * ((To-Tw)/(Ty-Tw)));

  printf("time taken to cook the egg is: %.2f seconds (%d minutes, %d seconds)\n", t, (int) (t/60), (int)t%60);

  return 0;
}

Paul Gribble | fall 2014
This work is licensed under a Creative Commons Attribution 4.0 International License
Creative Commons License