I have some C code that is double printing some values in my code, effectively I have stored some strings in a buffer, these strings are items such as:
GPU
ROUTER
My code has already stored and saved these items into a buffer.
I am writing a simple exchange program that prints BUY and SELL orders for the items in the buffer. I have saved all the orders from the user into my exchange.all_orders
buffer.
Here is a brief example:
When the user types
1. BUY 30 GPU 500 (0th element in all_orders array)
The system will print out
BUY 30 GPU 500 (1 order)
When the user types same order again i.e:
BUY 30 GPU 500
The system should print out, note the order quantity is incremented.
BUY 60 GPU 500
Subsequently when a unique order is placed BUY 30 GPU 501
, then the system will print out
BUY 60 GPU 500 (2 orders)
BUY 30 GPU 501 (1 order)
Right now my code does not do this, here is what I have so far
Struct Definitions
struct exchange_order_book {
int number_of_orders;
int exchange_fees;
struct exchange_order all_orders[100];
};
struct exchange_order {
int quantity;
int price;
char action[5];
char item[20];
int traderId;
int orderId;
};
void print_buy_levels(){
for(int i = 0; i<buffer_size; ++i){
int buys = 0, sells = 0;
double buy_prices[100] = {0}; // assuming at most 100 unique prices
double sell_prices[100] = {0}; // assuming at most 100 unique prices
int buy_count = 0, sell_count = 0;
// iterate through all orders in the exchange
for(int j = 0; j<exchange.number_of_orders; ++j){
if(strcmp(buffer[i],exchange.all_orders[j].item) == 0){
if(strcmp(exchange.all_orders[j].action,"BUY") == 0){
// check if this is a unique buy price
int found = 0;
for(int k = 0; k < buy_count; ++k){
if(exchange.all_orders[j].price == buy_prices[k]){
found = 1;
break;
}
}
if(!found){
buy_prices[buy_count++] = exchange.all_orders[j].price;
++buys;
}
}else if(strcmp(exchange.all_orders[j].action,"SELL") == 0)
{
// check if this is a unique sell price
int found = 0;
for(int k = 0; k < sell_count; ++k){
if(exchange.all_orders[j].price == sell_prices[k]){
found = 1;
break;
}
}
if(!found){
sell_prices[sell_count++] = exchange.all_orders[j].price;
++sells;
}
}
}
}
printf("%s\tProduct: %s; Buy levels: %d; Sell levels: %d\n",LOG_PREFIX,buffer[i],buys,sells);
}
}
Here are the cases where my code fails:
When the user enters BUY 30 GPU 500
, it SUCCESSFULLY prints
BUY 30 GPU 500 (1 order)
When the user enters BUY 30 GPU 501
, it SUCCESSFULLY prints
BUY 30 GPU 500 (1 order)
BUY 30 GPU 501 (1 order)
When the user enters BUY 30 GPU 501
, AGAIN my code fails
BUY 30 GPU 500 (1 order)
BUY 60 GPU 501 (2 order)
BUY 30 GPU 501 (1 order)
Although it increments the quantity and count correctly, it prints the new added order into std.
Now I think I need to find a way to keep track of seen orders
I have been working on this for a long while and any help will be greatly appreciated.
As you can see this is the step my code is failing
This is how it is suppose to look like
Ignore the other input, only input that matters is COMMAND and the subsequent print statements for the orders
parse command, parses the command and uses sscanf to store it into the struct variables.
EDITED CODE AFTER SUGGESTION
void print_orders() {
for (int i = 0; i < buffer_size; ++i) {
for (int j = 0; j < exchange.number_of_orders; ++j) {
if ( !exchange.all_orders[j].duplicate && strcmp(buffer[i], exchange.all_orders[j].item) == 0) {
struct exchange_order current_order = exchange.all_orders[j];
int count = 1;
int quantity = current_order.quantity;
int is_duplicate = 0;
for (int k = j + 1; k < exchange.number_of_orders; ++k) {
struct exchange_order next_order = exchange.all_orders[k];
if (current_order.price == next_order.price &&
strcmp(current_order.item, next_order.item) == 0) {
++count;
quantity += next_order.quantity;
is_duplicate = 1;
next_order.duplicate = true;
}
}
if (!is_duplicate) {
printf("%s\t\t%s %d @ $%d (%d order)\n", LOG_PREFIX, current_order.action, quantity,
current_order.price, count);
} else {
printf("%s\t\t%s %d @ $%d (%d orders)\n", LOG_PREFIX, current_order.action, quantity,
current_order.price, count);
}
}
}
}
}
struct exchange_order {
int quantity;
int price;
char action[5];
char item[20];
int traderId;
int orderId;
bool duplicate;
};
And exchange_order is unchanged
@H.S
Suggest an alternate method
"To check duplicate, one way is to compare the current processing order with the already processed (previous) orders in the list and if found duplicate then skip it. If the list is long then this is going to add a lot of unnecessary iteration and may impact the performance."
My list will be short, maybe this method might work