I need to calculate average packet travel speed in ns2. I have to write this formula in awk program. But I have no idea how to do it.
n is the number of received packet, and s is the distance when the packet is transmitted. Any answer will be very helpfull. Thanks.
function topla( array ) {sum=0; for (i in array) {sum=sum+250/array[i];}return sum;}
BEGIN {
total_packets_sent = 0;
total_packets_received = 0;
total_packets_dropped = 0;
first_packet_sent = 0;
last_packet_sent = 0;
last_packet_received = 0;}
{event = $1; time = $2; node = $3; type = $4; reason = $5; packetid = $6;
if ( time < simulation_start || simulation_start == 0 )
simulation_start = time;
if ( time > simulation_end )
simulation_end = time;
if ( type == "AGT" ) {
nodes[node] = node; # to count number of nodes
if ( time < node_start_time[node] || node_start_time[node] == 0 )
node_start_time[node] = time;
if ( time > node_end_time[node] )
node_end_time[node] = time;
if ( event == "s" ) {
flows[node] = node; # to count number of flows
if ( time < first_packet_sent || first_packet_sent == 0 )
first_packet_sent = time;
if ( time > last_packet_sent )
last_packet_sent = time;
# rate
packets_sent[node]++;
total_packets_sent++;
# delay
pkt_start_time[packetid] = time;
}
else if ( event == "r" ) {
if ( time > last_packet_received )
last_packet_received = time;
# throughput
packets_received[node]++;
total_packets_received++;
# delay
pkt_end_time[packetid] = time;
}
}}`
END {
# delay
for ( pkt in pkt_end_time) {
end = pkt_end_time[pkt];
start = pkt_start_time[pkt];
delta = end - start;
if ( delta > 0 ) {
delay[pkt] = delta;
}
}
result=topla(delay)/total_packets_received;
printf(result)
}
I write this awk program. But it gives the division by zero attempted error.And I wrote the transmission range in the topla function as 250, but I did not get the results I wanted.How should I write the transmission range?
Related
I am running the below query to join the two tables and get certain records based on Fuzzy logic (Levenshtein distance)
WITH main_table as (
select *
from
`project.data.Roof_Address`
), reference_table as (
select *
from `project.data.DATA_TREE_Address`
)
select
DR_NBR,
ARRAY_AGG(
STRUCT(n.LotSizeSqFt)
ORDER BY EDIT_DISTANCE(l.ordered_fullname, n.ordered_fullname) LIMIT 1
)[OFFSET(0)].*,
ARRAY_AGG(
EDIT_DISTANCE(l.ordered_fullname, n.ordered_fullname) LIMIT 1
)[OFFSET(0)] distance_score
FROM main_table l
CROSS JOIN reference_table n
GROUP BY 1
having ARRAY_AGG(
EDIT_DISTANCE(l.ordered_fullname, n.ordered_fullname) LIMIT 1
)[OFFSET(0)] < 10
This query will return the
Project_Id(Dr_NBR)
from first table and
Project_area(LotSizeSqFt)
from second table based on the Levenshtein Score filter at the end.
This query is resulting in the below error
Any suggestions how to optimize the above query?
The distance I am using is from the below function
#standardSQL
CREATE TEMPORARY FUNCTION EDIT_DISTANCE(string1 STRING, string2 STRING)
RETURNS INT64
LANGUAGE js AS """
var _extend = function(dst) {
var sources = Array.prototype.slice.call(arguments, 1);
for (var i=0; i<sources.length; ++i) {
var src = sources[i];
for (var p in src) {
if (src.hasOwnProperty(p)) dst[p] = src[p];
}
}
return dst;
};
var Levenshtein = {
/**
* Calculate levenshtein distance of the two strings.
*
* #param str1 String the first string.
* #param str2 String the second string.
* #return Integer the levenshtein distance (0 and above).
*/
get: function(str1, str2) {
// base cases
if (str1 === str2) return 0;
if (str1.length === 0) return str2.length;
if (str2.length === 0) return str1.length;
// two rows
var prevRow = new Array(str2.length + 1),
curCol, nextCol, i, j, tmp;
// initialise previous row
for (i=0; i<prevRow.length; ++i) {
prevRow[i] = i;
}
// calculate current row distance from previous row
for (i=0; i<str1.length; ++i) {
nextCol = i + 1;
for (j=0; j<str2.length; ++j) {
curCol = nextCol;
// substution
nextCol = prevRow[j] + ( (str1.charAt(i) === str2.charAt(j)) ? 0 : 1 );
// insertion
tmp = curCol + 1;
if (nextCol > tmp) {
nextCol = tmp;
}
// deletion
tmp = prevRow[j + 1] + 1;
if (nextCol > tmp) {
nextCol = tmp;
}
// copy current col value into previous (in preparation for next iteration)
prevRow[j] = curCol;
}
// copy last col value into previous (in preparation for next iteration)
prevRow[j] = nextCol;
}
return nextCol;
}
};
var the_string1;
try {
the_string1 = decodeURI(string1).toLowerCase();
} catch (ex) {
the_string1 = string1.toLowerCase();
}
try {
the_string2 = decodeURI(string2).toLowerCase();
} catch (ex) {
the_string2 = string2.toLowerCase();
}
return Levenshtein.get(the_string1, the_string2)
""";
Snapshot for Roof_Address table
Snapshot for DATA_TREE_Address
The main query cost would most likely be the ORDER by in the :
ARRAY_AGG(
STRUCT(n.LotSizeSqFt)
ORDER BY EDIT_DISTANCE(l.ordered_fullname, n.ordered_fullname) LIMIT 1
)[OFFSET(0)].*,
I see you're only returning a single record for each array_agg.
I'd recommend removing the ARRAY_AGG and do a MAX or MIN on the results from the EDIT_DISTANCE. A MAX or MIN is much much cheaper than ORDERING ALL records and taking the first or last one.
What is the best way to copy the value of one variable to another without a direct assignment function?
Variables are 2 byte fixed point numbers with a max value of 2147483.647. The available operators are (reformatted for clarity):
add(<variable>, <constant>) which adds a constant value to the given variable
subtract(<variable>, <constant>) which subtracts
multiply(<variable>, <constant>) which multiplies
divide(<variable>, <constant>) which divides
check(<variable1>, <op>, <variable2/constant>) which compares the value of variable1 with that of <variable2/constant> using the operator op and returns a boolean. op can be =, <, <=, >= or >
set(<variable>, <constant>) which sets the value of the variable.
The scripting language has basic control flow, it has while and if (but not else) as well as basic boolean operators AND, OR, NOT and NOR.
Our first attempt was simply:
while = {
count = var1 //loop var1 times
change_variable = { which = var2 value = 1 } //increment by one
}
My next attempt was:
set_variable = { which = new value = 1 }
while = {
limit = { NOT = { check_variable = { which = old value = new} } }
if = {
limit = {
check_variable = { which = old value > new }
check_variable = { which = stage value = 0 }
}
multiply_variable = { which = new value = 10 }
}
if = {
limit = {
check_variable = { which = stage value = 0 }
check_variable = { which = old value < new }
}
set_variable = { which = stage value = 1 }
}
if = {
limit = {
check_variable = { which = stage value = 1 }
check_variable = { which = old value < new }
}
subtract_variable = { which = new value = 1 }
}
}
E.g., start with 1, multiply by 10 until greater than the target, then subtract 1 until at the target.
But I'm sure there's a much better way to do this.
Context:
A recent patch of the scripting language used in the game "Stellaris" has broken the variable assignment function. set above should be set(<variable>, <variable/constant>). This has broken a lot of existing code, and it's unknown how long it will remain broken.
because 2147483.647 is a (power of 2 - 1) /1000 I can use the successive powers of 2 to approach the solution 1 bit at a time: (written in C using only equivalent operations)
result = 0;
result += 1073741.824;
if( var < result )
result -= 1073741.824;
result += 536870.912;
if( var < result)
result -= 536870.912;
result += 268435.456;
if( var < result)
result -= 268435.456;
result += 134217.728;
if( var < result)
result -= 134217.728;
result += 67108.864;
if( var < result)
result -= 67108.864;
result += 33554.432;
if( var < result)
result -= 33554.432;
result += 16777.216;
if( var < result)
result -= 16777.216;
result += 8388.608;
if( var < result)
result -= 8388.608;
result += 4194.304;
if( var < result)
result -= 4194.304;
result += 2097.152;
if( var < result)
result -= 2097.152;
result += 1048.576;
if( var < result)
result -= 1048.576;
result += 524.288;
if( var < result)
result -= 524.288;
result += 262.144;
if( var < result)
result -= 262.144;
result += 131.072;
if( var < result)
result -= 131.072;
result += 65.536;
if( var < result)
result -= 65.536;
result += 32.768;
if( var < result)
result -= 32.768;
result += 16.384;
if( var < result)
result -= 16.384;
result += 8.192;
if( var < result)
result -= 8.192;
result += 4.096;
if( var < result)
result -= 4.096;
result += 2.048;
if( var < result)
result -= 2.048;
result += 1.024;
if( var < result)
result -= 1.024;
result += 0.512;
if( var < result)
result -= 0.512;
result += 0.256;
if( var < result)
result -= 0.256;
result += 0.128;
if( var < result)
result -= 0.128
result += 0.064;
if( var < result)
result -= 0.064;
result += 0.032;
if( var < result)
result -= 0.032;
result += 0.016;
if( var < result)
result -= 0.016;
result += 0.008;
if( var < result)
result -= 0.008;
result += 0.004;
if( var < result)
result -= 0.004;
result += 0.002;
if( var < result)
result -= 0.002;
result += 0.001;
if( var < result)
result -= 0.001;
only 32 comparisons needed (less if you can live with less precision or know the upper bound).
I have recently written a piece of code that checks temperature over a period of time for some course work. i wrote the program using different bits of code but cannot find the issue to some of the problems i am encountering.
I was just wondering if anyone can see any obvious errors that i am missing and could help out with some information int he right direction.
Link to the JSFiddle
//Counting the average temperatures in a day
//needs debugged!!!
var temperatures = [];
var total = 0;
function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
function getNumDays() {
number = prompt("How many days in the study? Enter 1 - 10");
while ((number < 1) || (number > 10) ||( isNaN(number) === true)) {
alert ("Invalid input!");
number = prompt ("Enter again, 1 - 10 >> ");}
return number;
}
function getTemps(numDays) {
total = 0;
for (i = 0; i < numDays; i++) {
next = prompt ("Enter the temperature for day " + (i+1));
next = parseint(next);
while (isNaN(next)===true) {
next = 0;
next = prompt ("Error in input! Try again >>");
next = parseInt(next);}
temperatures.push(next);
}
total = total + next;
return temperatures;
}
function calcAverage(total, numDays) {
average = total / numDays;
return average;
}
function showStatistics(city, average, numdays) {
alert ("The average daily temperature for "+ city + " is " + average.toFixed(2) + " measured over " + numDays + " days." );
}
//main program
city = getCity();
numDays = getNumDays();
temperatures = getTemps(numDays);
Average = calcAverage(total, numDays);
showStatistics(city, average, numDays);
function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
//main program
city = getCity();
Looks like you're missing a return statement.
Additionally, the line total = total + next; seems to be out of place: I imagine the total to be the total of the temperatures, not 0 + temperatureOfLastDay.
Hello I don't know what is wrong with my BST insert method.
Any suggestions it has too be non recursive it adds to the right always I want to know why it adds to the ends to the BST when I print it shows that the nodes were added at the right only.
void InsertBST(LZWCmp cmp, TreeNode **root, int code)
{
TreeNode tmp = *root;
TreeNode current = NULL;
Code temp;
Code temp1;
int size;
int comp;
int direction = -1;
if(*root == NULL)
root = CreateNode(code)
while(tmp != NULL) {
temp = GetCode(cmp->cst, tmp->cNum);
temp1 = GetCode(cmp->cst, code);
size = temp.size;
if(temp1.size < temp.size)
size = temp1.size;
comp = memcmp(temp1.data, temp.data, size);
if(temp1.size < temp.size && comp == 0)
comp = -1;
else if(temp1.size < temp.size && comp == 0)
comp = 1;
if(comp < 0) {
current = tmp;
direction = FALSE;
tmp = tmp->left;
} else (
current = tmp;
direction = TRUE;
tmp = tmp->right;
}
}
if(direction == FALSE)
current->left = CreateNode(code);
else
current->right = CreateNode(code);
}
There you go:
void InsertBST(LZWCmp cmp, TreeNode **root, int code)
{
Code temp;
Code temp1;
int size;
int comp;
while ( *root ) {
temp = GetCode(cmp->cst, (*root)->cNum);
temp1 = GetCode(cmp->cst, code);
size = (temp1.size < temp.size) ? temp1.size : temp.size;
comp = memcmp(temp1.data, temp.data, size);
if (comp ==0 ) comp = (temp1.size < temp.size ) ? -1 : 1;
root = (comp < 0) ? &(*root)->left
: &(*root)->right;
}
*root = CreateNode(code);
}
NOTE: this code does not check for duplicates. It always inserts a node.
NOTE2: I am not sure about the sign in: if (comp ==0 ) comp = (temp1.size < temp.size ) ? -1 : 1; (in the original the sign was the same in both cases)
I have a C program and I need to test it using a file containing some test data and output the results automatically. How can I do this? Should I modify the program or should I write another program using C or something else like Python? Which is easier?
Here is my C program:
int main()
{
double wh,sh,salary;
int num = 0;
int valid = 1;
printf("Please input two non-negative numbers as a employee's working hours in one week and salary per hour.\n");
printf("Notice that the salary per hour can't be greater than 1000\nAlso you can't input more than 10 sets of data\n");
printf("Input them like this:\n20,34\nthen enter the Enter key\n");
while(scanf("%lf,%lf",&wh,&sh) == 2 && num < 10)
{
if(sh <0 || wh <0)
{
printf("Salary per hour or salary per hour can't be negative!\n");
}
else if(wh > 168)
{
printf("Working hours in one week can't be more than 168!\n");
}
else if(sh > 1000)
{
printf("Salary can't be greater than 1000!\n");
}
else
{
num ++;
if(wh <= 40)
{
if(sh <= 1000)
{
salary = wh * sh;
}
else if(sh > 1000 )
{
salary = wh * 1000;
}
}
else if(wh > 40 && wh <= 50)
{
if(sh*1.5 < 1000)
{
salary = 40*sh + (wh-40)*1.5*sh;
}
else if(sh*1.5 > 1000 && sh < 1000)
{
salary = 40*sh + (wh-40)*1000;
}
else if(sh > 1000)
{
salary = wh * 1000;
}
}
else if(wh > 50)
{
if(sh*3 < 1000)
{
salary = 40*sh + 10*1.5*sh + (wh-50)*3*sh;
}
else if(sh*1.5 < 1000 && sh*3 >=1000)
{
salary = 40*sh + 10*1.5*sh + (wh-50)*1000;
}
else if(sh*1.5 >= 1000 && sh <= 1000)
{
salary = 40*sh + (wh-40)*1000;
}
else if(sh > 1000)
{
salary = wh*1000;
}
}
printf("The total working hours is %lf, and salary per hour is %lf, salary is %lf\n",wh,sh,salary);
}
}
return 0;
}
For this moment please ignore these literals floating around.
I suppose the file containing test data is like this:
1,2
34,34
67,43
...
I really have no idea how this test automation works,please help!
in cmd prompt
enter
c:\>myprog.exe < input.txt > output.txt
put all ur testcases in input.txt
ur output will print in the output.txt file