Skip to content

Instantly share code, notes, and snippets.

@robert-saramet
Created March 21, 2024 06:51
Show Gist options
  • Select an option

  • Save robert-saramet/ec103eb87fcc0786bc62c29fbc2f66f8 to your computer and use it in GitHub Desktop.

Select an option

Save robert-saramet/ec103eb87fcc0786bc62c29fbc2f66f8 to your computer and use it in GitHub Desktop.
First C program: reads properties of a shipment and prints chargeable weight taking volume into account.
#include <stdio.h>
#include <string.h>
#define INTERNATIONAL_TRESHOLD 166
#define LOCAL_TRESHOLD 194
int calc_volume(int length, int width, int height) {
return length * width * height;
}
int calc_dimensional_weight(int volume, int treshold) {
return volume / treshold;
}
int calc_chargeable_weight(int weight, int dimensional_weight) {
return weight > dimensional_weight ? weight : dimensional_weight;
}
int main(void) {
int length, width, height, weight;
char destination[14];
printf("Enter shipment data in the following format: `$length $width $height $weight local/international`\n");
if (scanf("%d %d %d %d %s", &length, &width, &height, &weight, destination) != 5) {
fprintf(stderr, "Error reading input data");
return -1;
}
int volume = calc_volume(length, width, height);
int treshold = strcmp(destination, "local") == 0 ? LOCAL_TRESHOLD : INTERNATIONAL_TRESHOLD;
int dimensional_weight = calc_dimensional_weight(volume, treshold);
int chargeable_weight = calc_chargeable_weight(weight, dimensional_weight);
printf("Shipment volume: %d\n", volume);
printf("Dimensional weight: %d\n", dimensional_weight);
printf("Chargeable weight: %d\n", chargeable_weight);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment