logo
down
shadow

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


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
            {
                //Message Area code not found  
            }
        }
        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];

        if(inputAC == areaCode[x]) //ADDED condition
            break;
    }
    costResultsLabel.Text = "Your " + callTimeTextBox.Text + "-minute call to area code " + areaCodeTextBox.Text + " will cost " + total.ToString("C");
}


Share : facebook icon twitter icon
Calculate call cost from rate table using MySQL

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?

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?

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?

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


By : Sanduni Anuradha
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>
<head>
<meta charset="utf-8">
<title>berkelilingkesemua.info</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<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>
<head>
<meta charset="utf-8">
<title>berkelilingkesemua.info</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<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?

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)
library(readr)

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
Related Posts Related Posts :
  • How can I get NHibernate to map to a Tuple or Class?
  • MVC 5 C# Window Authentication pop up when I navigate to a certain area
  • Mongo C# driver toJson() DateTime
  • Coprime integers
  • What is the purpose with files ending with *CS.cs in Xamarin Forms projects?
  • MVC5 / Entity | Update/Create Complex Model
  • How to return auto increment ID from SQL using Petapoco ORM in .NET MVC
  • Create new web app offline c# using Visual Studio
  • How many times is ordered the IEnumerable in this case?
  • C# error when update database 2008 to sql 2016
  • How to insert string in multiple continuous pattern in windows c#
  • How to send temp data from Partial View to its parent View
  • C# encapsulation when getting updates from asynchronous method
  • Asp net core Content Security Policy implementation
  • @Html.DropDownList() Placeholder as Disable and Selected
  • Set AM/PM in Material Time Picker WPF
  • Ninject passing reference in constructor
  • C# Entity Framework text SQL query wrapper
  • Core 2.0 MVC - AD Group Authorization for logging
  • Decentralized NLog Target per Http request
  • How to use x:Object and when?
  • Displays file icons in asp.net on GridView
  • How do I avoid two (or more) threads that work on a table at the same time to not work on same row?
  • Add xml attribute for property in DB First
  • How do i execute 2 sql queries by clicking a button [C#]
  • convert jsonstring to objects c#
  • How do request a correct access token in ASP.NET Core for Azure AD to access Microsoft Graph
  • How to run a .NET Core console application on Linux
  • UI not updating for bound element
  • C# Odata v4 open type raw values
  • DocumentFormat.OpenXml Modify Creator Propery of Document
  • How to SetBasePath in ConfigurationBuilder in Core 2.0
  • ASP MVC5 public readonly variable changes value when it should not
  • How to conditionally select item from hashset with queryable linq?
  • Progress Bar C# not showing the real progress
  • Correct logic to reconnect in SignalR with HubConnection
  • ComboBox with two or more sections
  • .NET listbox to SQLite DB
  • C# Ninject binding from Dictionary<Type, Type>
  • asp.net mvc -view displaying real-time progress status
  • Getting reference to dependent in service with generic type
  • Implementing EqualityCompare vs overriding GetHashCode and Equals
  • C# Linq Find all indexes of item in List<int> within another List<int>
  • XML image how to display at data grid columns in c#.net
  • How to override Equals and GetHash of HashSet
  • Creating Windows authentication and SQL Server authentication in login form
  • How to interop with Azure Service Bus topics created by MassTransit using Rebus?
  • MVC 5 Google Calendar Integration
  • Use a int in entire code
  • How to display newly added element at the top of StackPanel in WPF?
  • T4MVC is generating T4MVC.cs and T4MVC1.cs
  • Remove blur effect on certain controls inside UserControl
  • Sql Transcation Query to Linq Query in Wcf Rest Service
  • Setting AWS S3 credentials in C# app
  • does not contain in an ObservableCollection
  • App doesn't run it crashed directly after I started it
  • Is it possible in Entity Framework MVC application to run .sql scripts on a different database
  • Type Inference with Deconstruct tuple assignment extension methods
  • Setting up mock objects for EF dbcontext to test repository methods
  • Using C# in XSLT with MSXSL 6.0
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org