bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / assignment-primary / assignment-p-12_v1.c

assignment-p-12_v1.c (1282B)


      1 /* Write a C program that takes multiple integers as command-line arguments and finds the
      2 maximum and minimum value among them. */
      3 
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 
      7 // use atoi()
      8 
      9 int main(int argc, char *argv[])
     10 {
     11     int current_val, max_val, min_val, i;
     12     char *endptr;
     13     long first_arg_val;
     14     if (argc < 2)
     15     {
     16         printf("Usage: %s <integer1> <integer2> ...\n", argv[0]);
     17         return 1;
     18     }
     19     first_arg_val = strtol(argv[1], &endptr, 10);
     20     if (*endptr != '\0' || argv[1] == endptr)
     21     {
     22         printf("Error: Argument '%s' is not a valid integer.\n", argv[1]);
     23         return 1;
     24     }
     25     max_val = (int)first_arg_val;
     26     min_val = (int)first_arg_val;
     27     for (i = 2; i < argc; i++)
     28     {
     29         long val = strtol(argv[i], &endptr, 10);
     30         if (*endptr != '\0' || argv[i] == endptr)
     31         {
     32             printf("Error: Argument '%s' is not a valid integer.\n", argv[i]);
     33             return 1;
     34         }
     35         current_val = (int)val;
     36         if (current_val > max_val)
     37         {
     38             max_val = current_val;
     39         }
     40         if (current_val < min_val)
     41         {
     42             min_val = current_val;
     43         }
     44     }
     45     printf("The maximum value is: %d\n", max_val);
     46     printf("The minimum value is: %d\n", min_val);
     47     return 0;
     48 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror