-2

enter image description here

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.

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
  • 2
    Welcome to StackOverflow. "Enter image description", i.e. describe what the linked math formula does. Then write pseudo code to outline the algorithm. Then apply your knowledge of awk to write your own attempt of doing that. Then add your awk code here to ask a specific question. Working through an awk tutorial might help, in case you are completely new to awk. You might want to start by taking the [tour]. Making a [mcve] is a very helpful thing, for yourself and for people trying to help you. – Yunnosch Jun 01 '17 at 06:52
  • Show us what you have tried, then we can start to give the support. – CWLiu Jun 01 '17 at 09:13
  • your formula is wrong! summation index (`i`) goes from `n` to `n`. neither `t` nor `s` depend on `i`. – karakfa Jun 01 '17 at 14:05

1 Answers1

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