Difference between revisions of "CSC103 Lab 4 Solution 2011"

From dftwiki3
Jump to: navigation, search
(Output)
(Code)
 
Line 72: Line 72:
 
       rice = rice * 2;  // we put twice as many grains on the next square
 
       rice = rice * 2;  // we put twice as many grains on the next square
 
}  
 
}  
 +
 +
 +
// ---------------------------------------
 +
// Figure out the weight of the grains
  
 
var weightOneGrain = 0.000025; //grams.  
 
var weightOneGrain = 0.000025; //grams.  
 
                       // Answer obtained from  
 
                       // Answer obtained from  
 
                       // http://wiki.answers.com/Q/How_much_does_a_grain_of_rice_weigh
 
                       // http://wiki.answers.com/Q/How_much_does_a_grain_of_rice_weigh
var rice = 1;
+
 
 +
rice = 1; // 1 grain on the first square...
 +
 
 +
document.write( "<P>" );
 
for ( k = 1; k <= 64; k = k+1 ) {
 
for ( k = 1; k <= 64; k = k+1 ) {
 
       var totalWeight = rice * weightOneGrain / 1000; // because 1000 kilos in a ton  
 
       var totalWeight = rice * weightOneGrain / 1000; // because 1000 kilos in a ton  
       document.write( "The number of grains of rice on Square #" + k + " = " + rice + "grains, " );
+
       document.write( "The number of grains of rice on Square #" + k + " = " + rice + " grains, " );
 
       document.write( " for a total weight of " + totalWeight + " tons. <br>" );
 
       document.write( " for a total weight of " + totalWeight + " tons. <br>" );
 
       rice = rice * 2;  // we put twice as many grains on the next square
 
       rice = rice * 2;  // we put twice as many grains on the next square

Latest revision as of 11:20, 28 February 2011

--D. Thiebaut 11:16, 28 February 2011 (EST)


Solution Program for Lab #4

Code

<code>
<html><head><title>CSC103 Javascript Program</title></head>
<body>
<h1>Solutions to Lab #4</h1>
<script language="JavaScript" type="text/javascript"><!--

//Enter your code below this line:

//============ SOLUTION FOR LAB #4  CSC103 ====================

// -------------------------
// Exercise 2.2.1
// -------------------------
var firstName = "Carol";
var lastName  = "Christ";
var Class = "A lot of class";
var Box = "1111";

document.write( "<UL>" );  // unnumbered lis
document.write( "<LI> Name: " + firstName + " " + lastName );
document.write( "<LI> Class: "  + Class );
document.write( "<LI> Box: " + Box );
document.write( "</UL><P>" );


// -------------------------
// Exercise 2.4.1
// -------------------------
var x = 3;
var y = 9;
var z = x + y;

document.write( "x = " + x + "<br>" );
document.write( "y = " + y + "<br>" );
document.write( "z = " + x + " + " + y + " = " + z + "<P>" );

// -------------------------
// Exercise 2.5
// -------------------------

// print all the numbers between 10 and 20, inclusive

document.write( "All the numbers between 10 and 20 are: " );
var  i;
for ( i = 10; i <= 20; i = i+1 ) {
       document.write( i + " "  );
}
document.write( "<P>" );

// compute the sum of all the numbers between 1 and 10, inclusive

document.write( "The sum of all the numbers between 1 and 10 is : " );
var sum = 0;
for ( i = 1; i <= 10; i = i+1 ) {
       sum = sum + i;
       //document.write( "i = " + i + " sum = " + sum + "<br>"  );
}
document.write( sum + "<P>" );

// -------------------------
// Exercise 2.6
// -------------------------
var k;
var rice = 1;
for ( k = 1; k <= 64; k = k+1 ) {
      document.write( "The number of grains of rice on Square #" + k + " = " + rice + "<br>" );
      rice = rice * 2;  // we put twice as many grains on the next square
} 


// ---------------------------------------
// Figure out the weight of the grains 

var weightOneGrain = 0.000025; //grams. 
                      // Answer obtained from 
                      // http://wiki.answers.com/Q/How_much_does_a_grain_of_rice_weigh

rice = 1; // 1 grain on the first square...

document.write( "<P>" );
for ( k = 1; k <= 64; k = k+1 ) {
      var totalWeight = rice * weightOneGrain / 1000; // because 1000 kilos in a ton 
      document.write( "The number of grains of rice on Square #" + k + " = " + rice + " grains, " );
      document.write( " for a total weight of " + totalWeight + " tons. <br>" );
      rice = rice * 2;  // we put twice as many grains on the next square
} 

document.write( "<P>For reference, the total yearly production of rice on earth is 400 million tons" );
document.write( "<br>(reference: <a href='http://www.rice-trade.com/world-wide-rice-production.html'>www.rice-trade.com</a>)<P>" ); 

//Do not write below this line.
//-->
</script>
</body></html>
</code>

Output

Solutions to Lab #4

Name: Carol Christ
Class: A lot of class
Box: 1111
x = 3
y = 9
z = 3 + 9 = 12

All the numbers between 10 and 20 are: 10 11 12 13 14 15 16 17 18 19 20

The sum of all the numbers between 1 and 10 is : 55

The number of grains of rice on Square #1 = 1
The number of grains of rice on Square #2 = 2
The number of grains of rice on Square #3 = 4
The number of grains of rice on Square #4 = 8
The number of grains of rice on Square #5 = 16
The number of grains of rice on Square #6 = 32
The number of grains of rice on Square #7 = 64
The number of grains of rice on Square #8 = 128
The number of grains of rice on Square #9 = 256
The number of grains of rice on Square #10 = 512
The number of grains of rice on Square #11 = 1024
The number of grains of rice on Square #12 = 2048
The number of grains of rice on Square #13 = 4096
The number of grains of rice on Square #14 = 8192
The number of grains of rice on Square #15 = 16384
The number of grains of rice on Square #16 = 32768
The number of grains of rice on Square #17 = 65536
The number of grains of rice on Square #18 = 131072
The number of grains of rice on Square #19 = 262144
The number of grains of rice on Square #20 = 524288
The number of grains of rice on Square #21 = 1048576
The number of grains of rice on Square #22 = 2097152
The number of grains of rice on Square #23 = 4194304
The number of grains of rice on Square #24 = 8388608
The number of grains of rice on Square #25 = 16777216
The number of grains of rice on Square #26 = 33554432
The number of grains of rice on Square #27 = 67108864
The number of grains of rice on Square #28 = 134217728
The number of grains of rice on Square #29 = 268435456
The number of grains of rice on Square #30 = 536870912
The number of grains of rice on Square #31 = 1073741824
The number of grains of rice on Square #32 = 2147483648
The number of grains of rice on Square #33 = 4294967296
The number of grains of rice on Square #34 = 8589934592
The number of grains of rice on Square #35 = 17179869184
The number of grains of rice on Square #36 = 34359738368
The number of grains of rice on Square #37 = 68719476736
The number of grains of rice on Square #38 = 137438953472
The number of grains of rice on Square #39 = 274877906944
The number of grains of rice on Square #40 = 549755813888
The number of grains of rice on Square #41 = 1099511627776
The number of grains of rice on Square #42 = 2199023255552
The number of grains of rice on Square #43 = 4398046511104
The number of grains of rice on Square #44 = 8796093022208
The number of grains of rice on Square #45 = 17592186044416
The number of grains of rice on Square #46 = 35184372088832
The number of grains of rice on Square #47 = 70368744177664
The number of grains of rice on Square #48 = 140737488355328
The number of grains of rice on Square #49 = 281474976710656
The number of grains of rice on Square #50 = 562949953421312
The number of grains of rice on Square #51 = 1125899906842624
The number of grains of rice on Square #52 = 2251799813685248
The number of grains of rice on Square #53 = 4503599627370496
The number of grains of rice on Square #54 = 9007199254740992
The number of grains of rice on Square #55 = 18014398509481984
The number of grains of rice on Square #56 = 36028797018963970
The number of grains of rice on Square #57 = 72057594037927940
The number of grains of rice on Square #58 = 144115188075855870
The number of grains of rice on Square #59 = 288230376151711740
The number of grains of rice on Square #60 = 576460752303423500
The number of grains of rice on Square #61 = 1152921504606847000
The number of grains of rice on Square #62 = 2305843009213694000
The number of grains of rice on Square #63 = 4611686018427388000
The number of grains of rice on Square #64 = 9223372036854776000

The number of grains of rice on Square #1 = 1 grains, for a total weight of 2.5000000000000002e-8 tons. 
The number of grains of rice on Square #2 = 2 grains, for a total weight of 5.0000000000000004e-8 tons. 
The number of grains of rice on Square #3 = 4 grains, for a total weight of 1.0000000000000001e-7 tons. 
The number of grains of rice on Square #4 = 8 grains, for a total weight of 2.0000000000000002e-7 tons. 
The number of grains of rice on Square #5 = 16 grains, for a total weight of 4.0000000000000003e-7 tons. 
The number of grains of rice on Square #6 = 32 grains, for a total weight of 8.000000000000001e-7 tons. 
The number of grains of rice on Square #7 = 64 grains, for a total weight of 0.0000016000000000000001 tons. 
The number of grains of rice on Square #8 = 128 grains, for a total weight of 0.0000032000000000000003 tons. 
The number of grains of rice on Square #9 = 256 grains, for a total weight of 0.0000064000000000000006 tons. 
The number of grains of rice on Square #10 = 512 grains, for a total weight of 0.000012800000000000001 tons. 
The number of grains of rice on Square #11 = 1024 grains, for a total weight of 0.000025600000000000002 tons. 
The number of grains of rice on Square #12 = 2048 grains, for a total weight of 0.000051200000000000004 tons. 
The number of grains of rice on Square #13 = 4096 grains, for a total weight of 0.00010240000000000001 tons. 
The number of grains of rice on Square #14 = 8192 grains, for a total weight of 0.00020480000000000002 tons. 
The number of grains of rice on Square #15 = 16384 grains, for a total weight of 0.00040960000000000004 tons. 
The number of grains of rice on Square #16 = 32768 grains, for a total weight of 0.0008192000000000001 tons. 
The number of grains of rice on Square #17 = 65536 grains, for a total weight of 0.0016384000000000001 tons. 
The number of grains of rice on Square #18 = 131072 grains, for a total weight of 0.0032768000000000003 tons. 
The number of grains of rice on Square #19 = 262144 grains, for a total weight of 0.006553600000000001 tons. 
The number of grains of rice on Square #20 = 524288 grains, for a total weight of 0.013107200000000001 tons. 
The number of grains of rice on Square #21 = 1048576 grains, for a total weight of 0.026214400000000002 tons. 
The number of grains of rice on Square #22 = 2097152 grains, for a total weight of 0.052428800000000005 tons. 
The number of grains of rice on Square #23 = 4194304 grains, for a total weight of 0.10485760000000001 tons. 
The number of grains of rice on Square #24 = 8388608 grains, for a total weight of 0.20971520000000002 tons. 
The number of grains of rice on Square #25 = 16777216 grains, for a total weight of 0.41943040000000004 tons. 
The number of grains of rice on Square #26 = 33554432 grains, for a total weight of 0.8388608000000001 tons. 
The number of grains of rice on Square #27 = 67108864 grains, for a total weight of 1.6777216000000001 tons. 
The number of grains of rice on Square #28 = 134217728 grains, for a total weight of 3.3554432000000003 tons. 
The number of grains of rice on Square #29 = 268435456 grains, for a total weight of 6.710886400000001 tons. 
The number of grains of rice on Square #30 = 536870912 grains, for a total weight of 13.421772800000001 tons. 
The number of grains of rice on Square #31 = 1073741824 grains, for a total weight of 26.843545600000002 tons. 
The number of grains of rice on Square #32 = 2147483648 grains, for a total weight of 53.687091200000005 tons. 
The number of grains of rice on Square #33 = 4294967296 grains, for a total weight of 107.37418240000001 tons. 
The number of grains of rice on Square #34 = 8589934592 grains, for a total weight of 214.74836480000002 tons. 
The number of grains of rice on Square #35 = 17179869184 grains, for a total weight of 429.49672960000004 tons. 
The number of grains of rice on Square #36 = 34359738368 grains, for a total weight of 858.9934592000001 tons. 
The number of grains of rice on Square #37 = 68719476736 grains, for a total weight of 1717.9869184000001 tons. 
The number of grains of rice on Square #38 = 137438953472 grains, for a total weight of 3435.9738368000003 tons. 
The number of grains of rice on Square #39 = 274877906944 grains, for a total weight of 6871.947673600001 tons. 
The number of grains of rice on Square #40 = 549755813888 grains, for a total weight of 13743.895347200001 tons. 
The number of grains of rice on Square #41 = 1099511627776 grains, for a total weight of 27487.790694400002 tons. 
The number of grains of rice on Square #42 = 2199023255552 grains, for a total weight of 54975.581388800005 tons. 
The number of grains of rice on Square #43 = 4398046511104 grains, for a total weight of 109951.16277760001 tons. 
The number of grains of rice on Square #44 = 8796093022208 grains, for a total weight of 219902.32555520002 tons. 
The number of grains of rice on Square #45 = 17592186044416 grains, for a total weight of 439804.65111040004 tons. 
The number of grains of rice on Square #46 = 35184372088832 grains, for a total weight of 879609.3022208001 tons. 
The number of grains of rice on Square #47 = 70368744177664 grains, for a total weight of 1759218.6044416002 tons. 
The number of grains of rice on Square #48 = 140737488355328 grains, for a total weight of 3518437.2088832003 tons. 
The number of grains of rice on Square #49 = 281474976710656 grains, for a total weight of 7036874.417766401 tons. 
The number of grains of rice on Square #50 = 562949953421312 grains, for a total weight of 14073748.835532801 tons. 
The number of grains of rice on Square #51 = 1125899906842624 grains, for a total weight of 28147497.671065602 tons. 
The number of grains of rice on Square #52 = 2251799813685248 grains, for a total weight of 56294995.342131205 tons. 
The number of grains of rice on Square #53 = 4503599627370496 grains, for a total weight of 112589990.68426241 tons. 
The number of grains of rice on Square #54 = 9007199254740992 grains, for a total weight of 225179981.36852482 tons. 
The number of grains of rice on Square #55 = 18014398509481984 grains, for a total weight of 450359962.73704964 tons. 
The number of grains of rice on Square #56 = 36028797018963970 grains, for a total weight of 900719925.4740993 tons. 
The number of grains of rice on Square #57 = 72057594037927940 grains, for a total weight of 1801439850.9481986 tons. 
The number of grains of rice on Square #58 = 144115188075855870 grains, for a total weight of 3602879701.896397 tons. 
The number of grains of rice on Square #59 = 288230376151711740 grains, for a total weight of 7205759403.792794 tons. 
The number of grains of rice on Square #60 = 576460752303423500 grains, for a total weight of 14411518807.585588 tons. 
The number of grains of rice on Square #61 = 1152921504606847000 grains, for a total weight of 28823037615.171177 tons. 
The number of grains of rice on Square #62 = 2305843009213694000 grains, for a total weight of 57646075230.342354 tons. 
The number of grains of rice on Square #63 = 4611686018427388000 grains, for a total weight of 115292150460.68471 tons. 
The number of grains of rice on Square #64 = 9223372036854776000 grains, for a total weight of 230584300921.36942 tons. 

For reference, the total yearly production of rice on earth is 400 million tons
(reference: www.rice-trade.com)