Question

In: Computer Science

modify code to write the output as an HTML table to a file in the output...

modify code to write the output as an HTML table to a file in the output directory.

The file that is saying to work at :

SOURCE CODE IN PERL:

print "Enter principal amount: ";
$P=;
while($P<=0)
{
print "Principal must be positive. Try again: ";
$P=;
}
print "Enter number of times interest is applied in a year: ";
$n=;
while($n!=12 && $n!=4 && $n!=2 && $n!=1)
{
print "It must be 12, 4, 2 or 1. Try again: ";
$n=;
}
print "Enter the annual rate of interest: ";
$r=;
while($r<=0 || $r>=25)
{
print "Rate of interest should be between 1 and 24. Try again: ";
$r=;
}
print "Enter the number of years: ";
$t=;
while($t<=0 || $t>=100)
{
print "Rate of interest should be between 1 and 99. Try again: ";
$t=;
}
print "\nYear Period Starting Balance Interest Ending Balance\n";
for($i=1; $i<=$t; $i=$i+1)
{
for($j=1; $j<=$n; $j=$j+1)
{
$interest=$P*($r/($n*100));
print sprintf("%-4d %-6d \$%-15.2f \$%-7.2f \$%-13.2f\n",$i, $j, $P, $interest, $P+$interest);
$P=$P+$interest;
}
}
print sprintf("Final Balance is \$%.2f\n", $P);

Solutions

Expert Solution

print "Enter principal amount: ";
$P= <STDIN>;
while($P<=0)
{
print "Principal must be positive. Try again: ";
$P= <STDIN>;
}
print "Enter number of times interest is applied in a year: ";
$n= <STDIN>;
while($n!=12 && $n!=4 && $n!=2 && $n!=1)
{
print "It must be 12, 4, 2 or 1. Try again: ";
$n= <STDIN>;
}
print "Enter the annual rate of interest: ";
$r= <STDIN>;
while($r<=0 || $r>=25)
{
print "Rate of interest should be between 1 and 24. Try again: ";
$r= <STDIN>;
}
print "Enter the number of years: ";
$t= <STDIN>;
while($t<=0 || $t>=100)
{
print "Rate of interest should be between 1 and 99. Try again: ";
$t= <STDIN>;
}
print "\nYear Period Starting Balance Interest Ending Balance\n";

#open the file for output named output table.html
open(outFile, '>', "output table.html") or die $!;

#this is the table headers
$toWrite="<table border=\"1\">
  <tr>
    <th>Year</th>
    <th>Period</th>
    <th>Starting Balance</th>
    <th>Interest</th>
    <th>Ending Balance</th>
  </tr>\n";

#this command writes the content from $toWrite to the output file
print outFile $toWrite;

for($i=1; $i<=$t; $i=$i+1)
{
for($j=1; $j<=$n; $j=$j+1)
{
$interest=$P*($r/($n*100));
print sprintf("%-4d %-6d \$%-15.2f \$%-7.2f \$%-13.2f\n",$i, $j, $P, $interest, $P+$interest);

#generate the table row data and save it to variable toWrite
$toWrite = sprintf("<tr><td>%-4d</td><td> %-6d </td> <td>\$%-15.2f</td> <td>\$%-7.2f</td> <td>\$%-13.2f</td></tr>\n",$i, $j, $P, $interest, $P+$interest);
$P=$P+$interest;
print outFile $toWrite; #write table row to the output file
}
}
print outFile "</table>"; #ending html table tag
close(outFile);

print sprintf("Final Balance is \$%.2f\n", $P);

I have modified the code so it will write a html table as output table.html to the same directory.

I have given the explanation in the code as comment.

Heres a result/output of a test run

After running the code the written html table file (output table.html) contains

<table border="1">
  <tr>
    <th>Year</th>
    <th>Period</th>
    <th>Starting Balance</th>
    <th>Interest</th>
    <th>Ending Balance</th>
  </tr>
<tr><td>1   </td><td> 1      </td> <td>$5000.00        </td> <td>$125.00 </td> <td>$5125.00      </td></tr>
<tr><td>1   </td><td> 2      </td> <td>$5125.00        </td> <td>$128.12 </td> <td>$5253.12      </td></tr>
<tr><td>2   </td><td> 1      </td> <td>$5253.12        </td> <td>$131.33 </td> <td>$5384.45      </td></tr>
<tr><td>2   </td><td> 2      </td> <td>$5384.45        </td> <td>$134.61 </td> <td>$5519.06      </td></tr>
<tr><td>3   </td><td> 1      </td> <td>$5519.06        </td> <td>$137.98 </td> <td>$5657.04      </td></tr>
<tr><td>3   </td><td> 2      </td> <td>$5657.04        </td> <td>$141.43 </td> <td>$5798.47      </td></tr>
<tr><td>4   </td><td> 1      </td> <td>$5798.47        </td> <td>$144.96 </td> <td>$5943.43      </td></tr>
<tr><td>4   </td><td> 2      </td> <td>$5943.43        </td> <td>$148.59 </td> <td>$6092.01      </td></tr>
<tr><td>5   </td><td> 1      </td> <td>$6092.01        </td> <td>$152.30 </td> <td>$6244.31      </td></tr>
<tr><td>5   </td><td> 2      </td> <td>$6244.31        </td> <td>$156.11 </td> <td>$6400.42      </td></tr>
</table>

which if you open it in browser looks like


Related Solutions

take the following html code and make it work for html validator. heres the ,html file...
take the following html code and make it work for html validator. heres the ,html file <!DOCTYPE html> <html lang="en">    <head>        <title> GettingStarted</title>        <meta charset="utf-8">        <link href="Style.css" rel="stylesheet">    </head>       <body>        <header><h1>GettingStarted</h1></header>        <nav>               <b>        <a href="Home.html">Home</a>&nbsp;        <a href="GettingStarted.html">Getting Started</a>&nbsp;        <a href="MaterialsNeeded.html">Materials Needed</a>&nbsp;                      <a href="TroubleShooting.html">TroubleShooting</a>&nbsp;        <a href="InfoMaterials.html">Infomation on materials</a>&nbsp;   ...
use VISUAL STUDIO CODE to write this javascript program Exercise 1 (a) Create a HTML file...
use VISUAL STUDIO CODE to write this javascript program Exercise 1 (a) Create a HTML file that uses createElement and appendChild to dynamically insert three paragraphs when a button is clicked. (b) Create a HTML file that includes JavaScript that is similar to: let recs = [“my item …1”,”my item…2”, …] i.e. an array that contains several CSV item records. When the user clicks a button, each array element will be rendered as a list element. Use a HTML list....
Tables:  Write the HTML code for a table data cell that contain the text “Holiday” and spans...
Tables:  Write the HTML code for a table data cell that contain the text “Holiday” and spans across 3 rows and 4 columns.
•Modify p4.c so that the output file p4.output is created but also displayed to standard output...
•Modify p4.c so that the output file p4.output is created but also displayed to standard output ( the screen ). This should be done by another instance of exec(). •Implement the pipe() command to do the following: $> grep –o else p4.c | wc –l p4.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <sys/wait.h> int main(int argc, char *argv[]) { int rc = fork(); if (rc < 0) {     // fork failed     fprintf(stderr, "fork...
Write, test, and debug (if necessary) HTML file with the Javascript codes in an external file...
Write, test, and debug (if necessary) HTML file with the Javascript codes in an external file for the following problem: Input: a number, n, using prompt, which is the number of the factorial numbers to be displayed. Output: a bordered table of numbers with proper caption and headings in which the first column displays the numbers from 1 to n and the second column displays the first n factorial numbers. For example, if a user enters 10 to be the...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
please use linux or unix to complete, and include pictures of the output. Modify the code...
please use linux or unix to complete, and include pictures of the output. Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the...
Change the program to modify the output file by making each sentence a new paragraph (inserting...
Change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must have worked through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE. How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be...
Change the program to modify the output file by making each sentence a new paragraph (inserting...
Change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must have worked through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE. How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT