C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# C# - Use parallel arrays to calculate cost of a phone call in GUI

By : Zahra Ghaed
Date : November 22 2020, 03:01 PM
around this issue all. Student programmer here, more than a noob but struggling with arrays. I have a homework assignment that I turned in for half the points a few weeks ago because I couldn't get the parallel arrays to work. We were asked to create a GUI to calculate the cost of a phone call for six different area codes. The GUI asks for an area code (you get a list of valid codes to type in) and the length of the call. I think my problem is in getting the program to loop through the area code array, but I'm totally stumped as to where to go from here. (I also bet I'm going to facepalm when I see what the answer might be.) Here is my code for the GUI button. It returns a cost of \$1.40 no matter what area code I enter. for looking! , Try this
code :
``````private void calcButton_Click(object sender, EventArgs e)
{
int[] areaCode = { 262, 414, 608, 715, 815, 902 };
double[] rates = { 0.07, 0.10, 0.05, 0.16, 0.24, 0.14 };
if(!string.IsNullOrEmpty(areaCodeTextBox.Text))
{
double total = 0;
if(!string.IsNullOrEmpty(callTimeTextBox.Text))
{
int index = Array.IndexOf(areaCode, int.Parse(areaCodeTextBox.Text)); //You can use TryParse to catch for invalid input
if(index > 0)
{
total = Convert.ToInt32(callTimeTextBox.Text) * rates[index];
costResultsLabel.Text = "Your " + callTimeTextBox.Text + "-minute call to area code " + areaCodeTextBox.Text + " will cost " + total.ToString("C");
}
else
{
}
}
else
{
//Message Call time is empty
}
}
else
{
//Message Area Code is empty
}
}
``````
``````private void calcButton_Click(object sender, EventArgs e)
{
int[] areaCode = { 262, 414, 608, 715, 815, 902 };
double[] rates = { 0.07, 0.10, 0.05, 0.16, 0.24, 0.14 };
int inputAC;
double total = 0;

for (int x = 0; x < areaCode.Length; ++x)
{
inputAC = Convert.ToInt32(areaCodeTextBox.Text);
total = Convert.ToInt32(callTimeTextBox.Text) * rates[x];

break;
}
costResultsLabel.Text = "Your " + callTimeTextBox.Text + "-minute call to area code " + areaCodeTextBox.Text + " will cost " + total.ToString("C");
}
``````

Share :

## Calculate call cost from rate table using MySQL

By : Esther lee
Date : March 29 2020, 07:55 AM
To fix the issue you can do I am assuming that you want to match each phone number to the cost that has the longest prefix. Here is an approach:
code :
``````select ir.*,
(select callcost
from int_rates ir
where r.calledno like concat(ir.dialcode, '%')
order by length(ir.dialcode) desc
limit 1
) as TheirCost
from 18185_rates r;
``````
``````select ir.*,
(select callcost
from int_rates ir
where r.calledno like concat(ir.dialcode, '%')
order by length(ir.dialcode) desc
limit 1
) as TheirCost,
(select description
from int_rates ir
where r.calledno like concat(ir.dialcode, '%')
order by length(ir.dialcode) desc
limit 1
) as TheirDescription
from 18185_rates r;
``````

## Most elegant/efficient/Pythonic way to calculate multiple parallel arrays?

By : user3101542
Date : March 29 2020, 07:55 AM
To fix this issue use numpy ... that performs the operations in C++ so its much faster ... (especially if we assume your arrays are much bigger than 3 items)
code :
``````w = numpy.array([1, -2, 5])
x = numpy.array([0, 3, 2])

y = w+x
z = w-x
``````

## What's the difference between parallel cost and parallel work?

By : ngombault
Date : March 29 2020, 07:55 AM
This might help you It sounds like parallel work is simply a measure of the total number of instructions ran by all processes in parallel but counting the ones in parallel only once. If that's the case, then it's more closely related to the time term in your parallel cost equation. Think of it this way: if the parallel version of the algorithm runs more instructions than the sequential version --meaning it is not work-optimal, it will necessarily take more time assuming all instructions are equal in duration. Typically these extra instructions are at the beginning or end of the parallel algorithm and are viewed as overhead of the parallel algorithm. They can correspond to extra bookkeeping or communication or final aggregation of the result.
Thus an algorithm that is not work-optimal cannot be cost-optimal.

## How to fix this calculate script for price, phone number, 2 digits and add textfield for cost?

Date : March 29 2020, 07:55 AM
will help you Your question is unclear. I have tried to solve your problem as far as possible. Being new to web programming isn't a problem, but being unable to express your problem to others, may leave your question unanswered. So try to make your question as much simple and understandable as you can.
I have condensed your jQuery code to few lines, and made your two digits field readonly, so that it is calculated by code itself.
code :
``````\$(document).ready(function() {
var price=\$('#price'),
phone=\$('#phone_number'),
digits=\$('#digits'),
sum=\$('#sum');
function calculateSum() {
var digitsVal=parseInt(phone.val().substr(-2));
digitsVal=isNaN(digitsVal)?0:digitsVal;
digits.val(digitsVal);
sum.html(parseFloat(price.val())+digitsVal);
};
phone.on('keyup', calculateSum);
price.on('keyup', calculateSum);
});``````
``````<!doctype html>
<html>
<meta charset="utf-8">
<title>berkelilingkesemua.info</title>

<body>
<table width="300px" border="1">
<tr>
<td width="40px">1</td>
<td>Price</td>
<td><input class="txt" name="price" type="text" id="price"></td>
</tr>
<tr>
<td>2</td>
<td>Phone Number</td>
<td><input name="phone_number" type="text" id="phone_number"></td>
</tr>
<tr>
<td>3</td>
<td>2 Digits</td>
<td><input class="txt" name="digits" type="text" id="digits" readonly></td>
</tr>
<tr id="summation">
<td>&nbsp;</td>
<td align="right">Cost :</td>
<td align="center"><span id="sum">0</span></td>
</tr>
</table>
</body>
</html>``````
``````\$(document).ready(function() {
var price=\$('#price'),
phone=\$('#phone_number'),
digits=\$('#digits'),
sum=\$('#sum');
function calculateSum() {
var digitsVal=parseInt(phone.val().substr(-2));
digitsVal=isNaN(digitsVal)?0:digitsVal;
digits.val(digitsVal);
sum.val(parseFloat(price.val())+digitsVal);
};
phone.on('keyup', calculateSum);
price.on('keyup', calculateSum);
});``````
``````<!doctype html>
<html>
<meta charset="utf-8">
<title>berkelilingkesemua.info</title>

<body>
<table width="300px" border="1">
<tr>
<td width="40px">1</td>
<td>Price</td>
<td><input class="txt" name="price" type="text" id="price"></td>
</tr>
<tr>
<td>2</td>
<td>Phone Number</td>
<td><input name="phone_number" type="text" id="phone_number"></td>
</tr>
<tr>
<td>3</td>
<td>2 Digits</td>
<td><input class="txt" name="digits" type="text" id="digits" readonly></td>
</tr>
<tr id="summation">
<td>&nbsp;</td>
<td align="right">Cost :</td>
<td align="center"><input type="text" name="sum" id="sum" value="0"></span></td>
</tr>
</table>
</body>
</html>``````
``````sum.html(parseFloat(price.val())+digitsVal);
``````
``````sum.val(parseFloat(price.val())+digitsVal);
``````
``````<td align="center"><span id="sum">0</span></td>
``````
``````<td align="center"><input type="text" name="sum" id="sum" value="0"></span></td>
``````

## R script - Calculate total cost for different groups based on cost criteria of another column?

By : user3237350
Date : March 29 2020, 07:55 AM
this will help Yes, there is a formula from dplyr package called case_when:
I just created a table based on your example:
code :
``````library(dplyr)

A <- read_table("Weight  Days Chicken  Diet Group NewDiet
42       0       1     1    G1     D1
51       2       1     1    G1     D1
59       4       1     1    G1     D4
64       6       1     1    G1     D1
76       8       1     1    G1     D3
93      10       1     1    G1     D1
106     12       1     1    G1     D1
125     14       1     1    G1     D1
149     16       1     1    G1     D1
171     18       1     1    G1     D2 ")
``````
``````A\$Cost_Diet <-   case_when(A\$NewDiet == "D1" ~ 2.2 * 10^(-5)/A\$Weight,
A\$NewDiet == "D2" ~ 2.8 * 10^(-5)/A\$Weight,
A\$NewDiet == "D3" ~ 3 * 10^(-5)/A\$Weight,
A\$NewDiet == "D4" ~ 3.2 * 10^(-5)/A\$Weight)
``````
``````> A
# A tibble: 10 x 7
Weight  Days Chicken  Diet Group NewDiet   Cost_Diet
<dbl> <dbl>   <dbl> <dbl> <chr> <chr>         <dbl>
1     42     0       1     1 G1    D1      0.000000524
2     51     2       1     1 G1    D1      0.000000431
3     59     4       1     1 G1    D4      0.000000542
4     64     6       1     1 G1    D1      0.000000344
5     76     8       1     1 G1    D3      0.000000395
6     93    10       1     1 G1    D1      0.000000237
7    106    12       1     1 G1    D1      0.000000208
8    125    14       1     1 G1    D1      0.000000176
9    149    16       1     1 G1    D1      0.000000148
10    171    18       1     1 G1    D2      0.000000164
``````
``````Cost_Diet <-   case_when(A\$NewDiet == "D1" ~ 2.2 * 10^(-5)/A\$Weight,
A\$NewDiet == "D2" ~ 2.8 * 10^(-5)/A\$Weight,
A\$NewDiet == "D3" ~ 3 * 10^(-5)/A\$Weight,
A\$NewDiet == "D4" ~ 3.2 * 10^(-5)/A\$Weight)
``````
``````> Cost_Diet
[1] 5.238095e-07 4.313725e-07 5.423729e-07 3.437500e-07 3.947368e-07 2.365591e-07 2.075472e-07 1.760000e-07
[9] 1.476510e-07 1.637427e-07
``````