Related
I receive data from an external device in decimal values, that I need to convert in ascii and then push it in a list
data example:
#onData: 1,3,200,
78,69,84,71,69,65,82,45,71,117,101,115,116,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
70,114,105,103,111,109,97,116,45,49,50,51,52,53,54,55,56,45,87,73,70,73,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,45,71,85,69,83,84,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,123
The first 3 elements of #onData need to be ignored and the last 2 elements as well.
In ascii it would be:
NETGEAR-Guest
Frigomat-12345678-WIFI
MICRO-SYSTEMS
MICRO-SYSTEMS-GUEST
MICRO-SYSTEMS
I have a method to convert:
arrayBuffer2str(buf) {
var str = "";
var ui8 = new Uint8Array(buf);
for (var i = 0; i < ui8.length; i++) {
str = str + String.fromCharCode(ui8[i]);
}
return str;
}
I would need to get rid of the zeros and then push it in an array of strings:
this.wifiNetworks.push("Network-1");
Thank you
If you want to extract the buffer between two zeros and isolate the significant response, try this
let prevVal;
let phrase= [];
for (let i=0; i<array.length; i++) {
let val = array[i];
if (val == 0 && prevVal != 0) {
// end of response
process(phrase);
}
if (prevVal == 0 && val != 0) {
// start new response
phrase= [];
}
phrase.push(val);
prevVal = val;
}
function process(phrase) {
// TODO Process your phrase here and remove the inner zeros
}
Also, consider removing your zeros using the .filter function
Try this,
Assuming that we are getting the input as an array.
const inputArray = [1, 3, 200,
78, 69, 84, 71, 69, 65, 82, 45, 71, 117, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70, 114, 105, 103, 111, 109, 97, 116, 45, 49, 50, 51, 52, 53, 54, 55, 56, 45, 87, 73, 70, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 45, 71, 85, 69, 83, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 123];
const resultArray = [];
let stringIndex = 0;
for (i = 3; i <= inputArray.length - 3; i++) {
if (inputArray[i] !== 0) {
resultArray[stringIndex] = resultArray[stringIndex]
? resultArray[stringIndex].concat(String.fromCharCode(inputArray[i]))
: ''.concat(String.fromCharCode(inputArray[i]));
} else if (inputArray[i - 1] !== 0) {
stringIndex = stringIndex + 1
}
}
console.log(resultArray);
// Add code to push the result to server,
The output would be
[ 'NETGEAR-Guest', 'Frigomat-12345678-WIFI', 'MICRO-SYSTEMS',
'MICRO-SYSTEMS-GUEST', 'MICRO-SYSTEMS' ]
I would like to know how to reshape a 2-D linearized Array in Scala without loops, i.e. by using functional programming.
Assume a 2-D Array of 6x12 = 72 elements. What I need is to resize this Array so that it has 8x16 = 128 elements, assuming that the new elements are initialized to zero.
This is how I do it with loops:
val a = (1 to 72).toArray
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72)
scala> val h = 6
h: Int = 6
scala> val w = 12
w: Int = 12
scala> val H = 8
H: Int = 8
scala> val W = 16
W: Int = 16
scala> val b = Array.ofDim[Int](H * W)
b: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
for (i <- 0 until h)
for (j <- 0 until w)
b(i * W + j) = a(i * w + j)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
The question is: how to do this by using functional programming?
Thank you very much!
a.grouped(w).map(_.padTo(W, 0)).padTo(H, Array.fill(W)(0)).flatten.toArray
Result (and string copied from your code, for comparison):
result : Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
I am currently developing a display application for F1 2017 on my iPhone using swift. I am able to read the UDP packets from my PS4 to my phone by using SwiftSockets.
I received the following data:
[25, 79, 3, 64, 176, 153, 15, 61, 244, 133, 165, 69, 0, 20, 145, 192, 223, 145, 218, 194, 165, 106, 64, 64, 15, 88, 234, 67, 220, 222, 109, 59, 168, 219, 67, 187, 42, 245, 4, 59, 200, 163, 186, 57, 65, 126, 46, 63, 22, 207, 77, 59, 13, 81, 59, 191, 127, 79, 59, 191, 0, 73, 203, 187, 141, 126, 46, 191, 180, 81, 37, 190, 5, 254, 41, 189, 251, 167, 151, 62, 145, 185, 39, 62, 7, 210, 92, 63, 203, 186, 128, 192, 218, 25, 99, 64, 112, 21, 0, 191, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 24, 106, 72, 61, 239, 89, 49, 187, 0, 0, 192, 64, 191, 199, 133, 69, 0, 0, 0, 0, 0, 0, 144, 65, 0, 80, 195, 72, 0, 80, 195, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 75, 253, 209, 66, 0, 0, 210, 66, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 80, 182, 232, 65, 80, 182, 232, 65, 80, 182, 232, 65, 80, 182, 232, 65, 0, 0, 172, 65, 0, 0, 172, 65, 0, 0, 184, 65, 0, 0, 184, 65, 0, 0, 0, 64, 0, 0, 198, 66, 57, 170, 165, 69, 128, 93, 205, 66, 0, 128, 84, 70, 0, 96, 134, 69, 0, 0, 16, 65, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 252, 68, 240, 253, 176, 66, 131, 119, 113, 188, 93, 249, 95, 187, 28, 146, 43, 59, 116, 163, 23, 188, 87, 87, 88, 88, 0, 0, 0, 0, 5, 54, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 200, 88, 12, 67, 0, 0, 255, 20, 0, 146, 115, 234, 194, 88, 249, 59, 64, 95, 94, 231, 67, 128, 93, 205, 66, 176, 153, 15, 61, 24, 134, 204, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 122, 64, 100, 2, 18, 7, 5, 0, 0, 0, 0, 245, 73, 129, 193, 65, 222, 85, 64, 134, 169, 0, 68, 32, 234, 205, 66, 224, 188, 92, 67, 94, 87, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 62, 112, 162, 69, 34, 2, 15, 3, 5, 1, 2, 0, 0, 134, 253, 181, 65, 69, 178, 83, 64, 5, 6, 10, 68, 136, 6, 200, 66, 70, 194, 141, 67, 180, 236, 199, 66, 0, 0, 0, 0, 0, 0, 0, 0, 17, 208, 160, 69, 16, 0, 5, 3, 5, 1, 2, 0, 0, 16, 137, 185, 195, 194, 6, 44, 64, 211, 20, 153, 66, 64, 15, 198, 66, 238, 232, 27, 61, 64, 15, 198, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 208, 253, 67, 0, 1, 1, 4, 5, 0, 0, 0, 0, 83, 13, 149, 65, 96, 78, 84, 64, 101, 6, 9, 68, 170, 16, 200, 66, 82, 86, 44, 68, 212, 184, 199, 66, 0, 0, 0, 0, 0, 0, 0, 0, 75, 252, 160, 69, 6, 1, 3, 3, 5, 1, 2, 0, 0, 247, 239, 27, 68, 227, 16, 124, 64, 33, 229, 220, 67, 171, 9, 202, 66, 0, 0, 0, 0, 171, 9, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 86, 90, 110, 69, 7, 11, 11, 2, 5, 0, 2, 0, 0, 91, 113, 167, 67, 92, 188, 60, 64, 223, 10, 42, 68, 58, 73, 206, 66, 0, 0, 0, 0, 58, 73, 206, 66, 0, 0, 0, 0, 0, 0, 0, 0, 214, 218, 146, 69, 18, 5, 19, 3, 5, 0, 2, 0, 0, 235, 33, 44, 68, 26, 50, 118, 64, 22, 232, 74, 68, 64, 189, 200, 66, 0, 0, 0, 0, 64, 189, 200, 66, 0, 0, 0, 0, 0, 0, 0, 0, 190, 18, 132, 69, 5, 6, 7, 3, 5, 0, 2, 0, 0, 63, 156, 38, 68, 27, 127, 136, 64, 55, 132, 5, 68, 48, 100, 203, 66, 0, 0, 0, 0, 48, 100, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 12, 190, 116, 69, 35, 7, 16, 3, 5, 0, 2, 0, 0, 214, 118, 54, 196, 239, 49, 70, 64, 111, 224, 185, 195, 176, 46, 202, 66, 200, 232, 13, 61, 176, 46, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 128, 229, 135, 68, 3, 7, 13, 4, 5, 0, 0, 0, 0, 122, 220, 252, 67, 157, 82, 60, 64, 88, 205, 89, 68, 208, 106, 202, 66, 0, 0, 0, 0, 208, 106, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 213, 35, 138, 69, 14, 11, 14, 2, 5, 0, 2, 0, 0, 43, 225, 15, 68, 97, 160, 103, 64, 244, 178, 187, 67, 216, 10, 202, 66, 0, 0, 0, 0, 216, 10, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 64, 42, 105, 69, 1, 8, 12, 3, 5, 0, 2, 0, 0, 12, 73, 197, 67, 40, 119, 53, 64, 115, 141, 56, 68, 199, 157, 201, 66, 0, 0, 0, 0, 199, 157, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 5, 231, 143, 69, 10, 3, 8, 2, 5, 0, 2, 0, 0, 66, 178, 177, 67, 10, 45, 66, 64, 88, 176, 42, 68, 168, 124, 203, 66, 0, 0, 0, 0, 168, 124, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 230, 9, 146, 69, 20, 3, 17, 3, 5, 0, 2, 0, 0, 140, 235, 54, 68, 99, 244, 101, 64, 198, 155, 62, 68, 16, 171, 201, 66, 0, 0, 0, 0, 16, 171, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 109, 212, 129, 69, 33, 6, 9, 2, 5, 0, 2, 0, 0, 21, 102, 86, 66, 112, 68, 96, 64, 60, 2, 135, 65, 128, 194, 199, 66, 164, 206, 103, 66, 128, 194, 199, 66, 96, 174, 4, 66, 0, 0, 0, 0, 236, 0, 63, 69, 22, 0, 4, 3, 5, 0, 1, 0, 0, 47, 20, 201, 67, 201, 159, 49, 64, 18, 130, 60, 68, 8, 44, 199, 66, 86, 200, 170, 66, 8, 44, 199, 66, 64, 16, 4, 66, 64, 253, 217, 65, 94, 90, 143, 69, 9, 4, 2, 3, 5, 0, 2, 0, 0, 32, 154, 227, 194, 27, 193, 93, 64, 243, 43, 77, 196, 251, 231, 201, 66, 0, 0, 0, 0, 251, 231, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 112, 189, 255, 68, 23, 8, 10, 3, 5, 0, 1, 0, 0, 157, 12, 27, 66, 80, 210, 84, 64, 234, 220, 13, 68, 240, 20, 200, 66, 204, 189, 15, 67, 240, 20, 200, 66, 0, 0, 0, 0, 0, 0, 0, 0, 113, 36, 160, 69, 15, 4, 6, 2, 5, 1, 2, 0, 0, 70, 217, 139, 67, 81, 9, 51, 64, 70, 253, 57, 68, 81, 176, 206, 66, 204, 63, 149, 60, 81, 176, 206, 66, 0, 0, 0, 0, 0, 0, 0, 0, 71, 132, 149, 69, 31, 5, 20, 2, 5, 0, 2, 0, 0, 182, 135, 20, 192, 86, 227, 218, 187, 41, 207, 77, 187, 23, 39, 22, 59, 188, 55, 6, 59, 86, 40, 253, 58, 203, 2, 123, 66, 62, 192, 141, 194, 179, 242, 115, 66, 2, 233, 141, 194, 207, 30, 49, 191, 192, 105, 55, 62, 231, 125, 246, 190]
How would I be able to decode the data into a UDPPacket? In order to do this, I think I need to convert the data into four bytes and then somehow convert it to a float.
Here is my swift code:
import UIKit
import SwiftSocket
class ViewController: UIViewController {
var server: UDPServer!
var timer: Timer!
override func viewDidLoad() {
super.viewDidLoad()
server = UDPServer(address: "192.168.1.158", port: 20777)
timer = Timer.scheduledTimer(withTimeInterval: 0, repeats: true) { _ in
print("a")
let data = self.server.recv(1289).0
if let data = data {
print(data)
self.timer.invalidate()
}
}
timer.fire()
}
}
This is what the UDPPacket should look like:
struct UDPPacket
{
float m_time;
float m_lapTime;
float m_lapDistance;
float m_totalDistance;
float m_x; // World space position
float m_y; // World space position
float m_z; // World space position
float m_speed; // Speed of car in MPH
float m_xv; // Velocity in world space
float m_yv; // Velocity in world space
float m_zv; // Velocity in world space
float m_xr; // World space right direction
float m_yr; // World space right direction
float m_zr; // World space right direction
float m_xd; // World space forward direction
float m_yd; // World space forward direction
float m_zd; // World space forward direction
float m_susp_pos[4]; // Note: All wheel arrays have the order:
float m_susp_vel[4]; // RL, RR, FL, FR
float m_wheel_speed[4];
float m_throttle;
float m_steer;
float m_brake;
float m_clutch;
float m_gear;
float m_gforce_lat;
float m_gforce_lon;
float m_lap;
float m_engineRate;
float m_sli_pro_native_support; // SLI Pro support
float m_car_position; // car race position
float m_kers_level; // kers energy left
float m_kers_max_level; // kers maximum energy
float m_drs; // 0 = off, 1 = on
float m_traction_control; // 0 (off) - 2 (high)
float m_anti_lock_brakes; // 0 (off) - 1 (on)
float m_fuel_in_tank; // current fuel mass
float m_fuel_capacity; // fuel capacity
float m_in_pits; // 0 = none, 1 = pitting, 2 = in pit area
float m_sector; // 0 = sector1, 1 = sector2, 2 = sector3
float m_sector1_time; // time of sector1 (or 0)
float m_sector2_time; // time of sector2 (or 0)
float m_brakes_temp[4]; // brakes temperature (centigrade)
float m_tyres_pressure[4]; // tyres pressure PSI
float m_team_info; // team ID
float m_total_laps; // total number of laps in this race
float m_track_size; // track size meters
float m_last_lap_time; // last lap time
float m_max_rpm; // cars max RPM, at which point the rev limiter will kick in
float m_idle_rpm; // cars idle RPM
float m_max_gears; // maximum number of gears
float m_sessionType; // 0 = unknown, 1 = practice, 2 = qualifying, 3 = race
float m_drsAllowed; // 0 = not allowed, 1 = allowed, -1 = invalid / unknown
float m_track_number; // -1 for unknown, 0-21 for tracks
float m_vehicleFIAFlags; // -1 = invalid/unknown, 0 = none, 1 = green, 2 = blue, 3 = yellow, 4 = red
float m_era; // era, 2017 (modern) or 1980 (classic)
float m_engine_temperature; // engine temperature (centigrade)
float m_gforce_vert; // vertical g-force component
float m_ang_vel_x; // angular velocity x-component
float m_ang_vel_y; // angular velocity y-component
float m_ang_vel_z; // angular velocity z-component
byte m_tyres_temperature[4]; // tyres temperature (centigrade)
byte m_tyres_wear[4]; // tyre wear percentage
byte m_tyre_compound; // compound of tyre – 0 = ultra soft, 1 = super soft, 2 = soft, 3 = medium, 4 = hard, 5 = inter, 6 = wet
byte m_front_brake_bias; // front brake bias (percentage)
byte m_fuel_mix; // fuel mix - 0 = lean, 1 = standard, 2 = rich, 3 = max
byte m_currentLapInvalid; // current lap invalid - 0 = valid, 1 = invalid
byte m_tyres_damage[4]; // tyre damage (percentage)
byte m_front_left_wing_damage; // front left wing damage (percentage)
byte m_front_right_wing_damage; // front right wing damage (percentage)
byte m_rear_wing_damage; // rear wing damage (percentage)
byte m_engine_damage; // engine damage (percentage)
byte m_gear_box_damage; // gear box damage (percentage)
byte m_exhaust_damage; // exhaust damage (percentage)
byte m_pit_limiter_status; // pit limiter status – 0 = off, 1 = on
byte m_pit_speed_limit; // pit speed limit in mph
float m_session_time_left; // NEW: time left in session in seconds
byte m_rev_lights_percent; // NEW: rev lights indicator (percentage)
byte m_is_spectating; // NEW: whether the player is spectating
byte m_spectator_car_index; // NEW: index of the car being spectated
// Car data
byte m_num_cars; // number of cars in data
byte m_player_car_index; // index of player's car in the array
CarUDPData m_car_data[20]; // data for all cars on track
float m_yaw; // NEW (v1.8)
float m_pitch; // NEW (v1.8)
float m_roll; // NEW (v1.8)
float m_x_local_velocity; // NEW (v1.8) Velocity in local space
float m_y_local_velocity; // NEW (v1.8) Velocity in local space
float m_z_local_velocity; // NEW (v1.8) Velocity in local space
float m_susp_acceleration[4]; // NEW (v1.8) RL, RR, FL, FR
float m_ang_acc_x; // NEW (v1.8) angular acceleration x-component
float m_ang_acc_y; // NEW (v1.8) angular acceleration y-component
float m_ang_acc_z; // NEW (v1.8) angular acceleration z-component
};
CarUDPData:
struct CarUDPData
{
float m_worldPosition[3]; // world co-ordinates of vehicle
float m_lastLapTime;
float m_currentLapTime;
float m_bestLapTime;
float m_sector1Time;
float m_sector2Time;
float m_lapDistance;
byte m_driverId;
byte m_teamId;
byte m_carPosition; // UPDATED: track positions of vehicle
byte m_currentLapNum;
byte m_tyreCompound; // compound of tyre – 0 = ultra soft, 1 = super soft, 2 = soft, 3 = medium, 4 = hard, 5 = inter, 6 = wet
byte m_inPits; // 0 = none, 1 = pitting, 2 = in pit area
byte m_sector; // 0 = sector1, 1 = sector2, 2 = sector3
byte m_currentLapInvalid; // current lap invalid - 0 = valid, 1 = invalid
byte m_penalties; // NEW: accumulated time penalties in seconds to be added
};
If it helps, I am at the start of a Grand Prix (not career), racing as Lewis Hamilton at Australia and the starting lights have not gone off yet.
Here is a link to the UDP data specifications for F1 2017.
I'm writing a function that finds two identical elements in an array and then prints the value of their index. I can't seem to get the right second index. What's my problem?
code:
the function I'm troubled with called 'couplesSearch' and it returns its
value to 'printCouples'.line 139
#include <stdio.h>
#include <stdlib.h>
#define NUM_GUESTS 200
#define FALSE 0
#define TRUE 1
#define KIDS_AGE 12
float averageAge(int ages[], int size);
int meals(int guestAges[], int size);
int search(int guestAges[], int size);
void print(int condition);
int coupleSearch(int guestAges[], int size);
void printCouples(int size, int condition2, int ageFriend);
int main(void)
{
int guestAges[NUM_GUESTS] = {42, 108, 95, 101, 90, 5, 79, 79, 83, 105, 66, 66, 2, 28, 2, 12, 116, 63, 28, 37,
112, 85, 63, 34, 53, 23, 22, 117, 39, 96, 48, 7, 12, 19, 70, 113, 108, 20, 116,
55, 24, 52, 3, 94, 34, 105, 22, 32, 54, 29, 108, 45, 23, 118, 118, 20, 84, 22,
50, 59, 77, 36, 111, 43, 49, 107, 41, 63, 65, 89, 87, 46, 51, 10, 11, 111, 7, 22,
34, 69, 70, 24, 85, 35, 37, 81, 47, 57, 12, 29, 25, 40, 27, 44, 18, 59, 39, 43,
10, 102, 34, 36, 80, 19, 25, 91, 100, 27, 114, 67, 102, 66, 45, 113, 31, 70, 18,
94, 58, 73, 107, 91, 42, 37, 36, 48, 16, 95, 72, 53, 111, 71, 22, 5, 47, 71, 28,
72, 8, 58, 98, 48, 34, 64, 66, 30, 50, 39, 102, 109, 63, 107, 27, 71, 94, 9,
61, 72, 43, 96, 11, 120, 25, 18, 69, 4, 116, 82, 3, 111, 92, 117, 15, 101, 37, 22,
109, 40, 109, 5, 2, 55, 54, 80, 19, 99, 61, 69, 8, 108, 9, 14, 49, 44, 48, 22,
31, 18, 14, 35};
int size = 0;
printf("average is: %.2f\n", averageAge(guestAges,size));
printf("Number of kids 12 and under: %d\n" , meals(guestAges, size));
print(search(guestAges,size));
coupleSearch(guestAges,size);
/*printf("Two guests with age 16 at index %d and index %d");
printf("Youngest guest age: ");
printf("Oldest guest age: ");*/
return 0;
}
/*
Function will return average age of party guests
input: age array, number of guests
output: average age
*/
float averageAge(int guestAges[], int size)
{
float sum = 0;
float avg = 0;
for(size = 0; size < NUM_GUESTS; ++size)
{
sum += guestAges[size];
}
avg = sum / NUM_GUESTS;
return avg;
}
int meals(int guestAges[], int size)
{
int kids = 0;
for (size = 0; size < NUM_GUESTS; size++)
{
if (guestAges[size] == KIDS_AGE || guestAges[size] < KIDS_AGE)
{
kids++;
}
}
return kids;
}
int search(int guestAges[], int size)
{
int condition = 0;
int age = 0;
printf("Enter age to search: ");
scanf("%d", &age);
while (size < NUM_GUESTS && age != guestAges[size])
{
size++;
}
if (size < NUM_GUESTS)
{
condition = TRUE;
} else
{
condition = FALSE;
}
return condition;
}
void print(int condition)
{
if (condition == TRUE)
{
printf("Guest found!\n");
}
else if (condition == FALSE)
{
printf("No guest this age.\n");
}
}
int coupleSearch(int guestAges[], int size)
{
int condition2 = 0;
int ageFriend = 0;
printf("Enter age of guest looking for friend: ");
scanf("%d", &ageFriend);
while (size < NUM_GUESTS && ageFriend != guestAges[size])
{
size++;
}
if (size < NUM_GUESTS)
{
condition2 = TRUE;
} else {
condition2 = FALSE;
}
printCouples(size,condition2,ageFriend);
return condition2;
}
void printCouples(int size, int condition2, int ageFriend)
{
if (condition2 == TRUE)
{
printf("Two guest with age %d at index %d and index %d\n", ageFriend, size+1, size+2);
}
else if (condition2 == FALSE)
{
printf("No guest this age.\n");
}
}
Thanks.
Your test assumes that there are two entries in the age array. That is, the age of the guest looking for a friend is not unique and that the array has been sorted. The array that you pass in is not sorted by age. Thus, size+2 will not contain the same age as size+1.
First sort your age array and then search for the first and last entry of that age in the array.
If you do not want to sort the array, then you would have to test every entry in the array and keep track of matching indexes.
You also exit coupleSearch() as soon as you have found the first match. You need to search for the second match separately if you do not want to sort and search for first and last match.
Since you print size+1, then your printed answer is one based.
You only search for the first match. In other words - you never search for a couple.
Maybe you should try something like:
void printCouples(int size, int condition2, int ageFriend)
{
int j = size+1; // Start after the first match
int found = 0;
while (j < NUM_GUESTS )
{
if (ageFriend == guestAges[j])
{
found = 1;
printf("Two guest with age %d at index %d and index %d\n", ageFriend, size, j);
}
}
if (!found)
{
printf("No guest this age.\n");
}
}
Notice that condition2 isn't used anymore.
I'm using the array shuffle function from here: http://iosdevelopertips.com/swift-code/swift-shuffle-array-type.html.
On this line:
for var index = array.count - 1; index > 0; index -= 1
in the code below
func shuffleArray<T>( arrayparam: Array<T>) -> Array<T>
{
var array = arrayparam
for var index = array.count - 1; index > 0; index -= 1
{
// Random int from 0 to index-1
let j = Int(arc4random_uniform(UInt32(index-1)))
// Swap two array elements
// Notice '&' required as swap uses 'inout' parameters
swap(&array[index], &array[j])
}
return array
}
Swift throws this warning:
C-style for statement is deprecated and will be removed in a future
version of Swift
There isn't any recommendation of what should be used here. Any ideas what should replace it?
take a look at http://bjmiller.me/post/137624096422/on-c-style-for-loops-removed-from-swift-3
only decrease by 1:
for i in (0...n).reverse() {
}
or
decrease by more steps:
for i in someNum.stride(through: 0, by: -2) {
}
More info: there are two versions for stride: through and to. Difference is <= and >= for through, while < and > for to, depending on what you need.
func shuffle<T>(array: Array<T>) -> Array<T> {
var result = array
for index in array.indices.reverse() {
// generate random swapIndex and add a where clause
// to make sure it is not equal to index before swaping
guard
case let swapIndex = Int(arc4random_uniform(UInt32(array.count - index))) + index
where index != swapIndex
else { continue }
swap(&result[index], &result[swapIndex])
}
return result
}
var arrInt = Array(1...100)
shuffle(arrInt) // [28, 19, 25, 53, 35, 60, 14, 62, 34, 15, 81, 50, 59, 40, 89, 30, 2, 54, 27, 9, 82, 21, 11, 67, 84, 75, 44, 97, 66, 83, 36, 20, 26, 1, 76, 77, 8, 13, 72, 65, 64, 80, 88, 29, 98, 37, 33, 70, 52, 93, 100, 31, 4, 95, 45, 49, 61, 71, 24, 16, 12, 99, 94, 86, 46, 69, 63, 22, 48, 58, 51, 18, 43, 87, 41, 6, 92, 10, 38, 23, 68, 85, 42, 32, 55, 78, 56, 79, 3, 47, 39, 57, 90, 17, 5, 73, 7, 91, 74, 96]
for var index = array.count - 1; index > 0; index -= 1
Use a reverse range. Form the range and reverse it:
for index in (1..<array.count).reverse
However, as discussed in my answer here, there's a nicer way; I provide a >>> operator so you can say
for index in array.count>>>1
why don't you try:
for var index = array.count - 1; index > 0; index =index-1 ;