DEV Community

Discussion on: Daily Challenge #16 - Number of People on the Bus

Collapse
 
yzhernand profile image
Yozen Hernandez

Didn't see anyone else try it in C, so here's my solution in C:

#include "stdio.h"
#include "stdlib.h"

unsigned int bus( unsigned int ( *data )[2], size_t n ) {
    unsigned int sum = 0;

    for ( size_t i = 0; i < n; ++i ) {
        sum += ( data[i][0] - data[i][1] );
    }

    return sum;
}

int main( int argc, char const *argv[] ) {
    // Read in data, somehow
    printf( "Passengers remaining after last stop: %u\n",
      bus( ( unsigned int[][2] ){{1, 0}, {2, 1}, {5, 3}, {4, 2}}, 4 ) );
    // 6
    return 0;
}