In C please Write a program that finds either the largest or
In C, please.
Write a program that finds either the largest or smallest of the ten numbers as command-line arguments. With –l for largest and –s for smallest number, if the user enters an invalid option, the program should display an error message.
Example runs of the program:
./find_largest_smallest –l 5 2 92 424 53 42 8 12 23 41
output: The largest number is 424
./find_largest_smallest –s 5 2 92 424 53 42 8 12 23 41
output: The smallest number is 2
1) Name your program numbers.c.
2) Use atoi function in to convert a string to integer form.
3) Generate the executable as find_largest_smallest.
gcc –Wall –o find_largest_smallest numbers.c
Solution
C Programming CODE
#include <stdio.h> // printf function
#include <stdlib.h> // atio function
#include <string.h> // strcmp function
int main(int argc, char *argv[] ) // receiving the command line arguments in argv,
// argc will have the total number of commandline arguments
{
int i,S = 1,Num; // declering variables for the program
if(!strcmp(argv[1],\"-s\"))// checking largest or smallest
{
S = -1; // if small, make S = -1 and multiply it to all variables and find
// largest number
printf(\"The smallest number is \");
}
else
printf(\"The largest number is \");
Num = atoi(argv[2])*S; // Get the first element in the array to Num
for(i = 3;i<argc;i++) // loop to compare the remaining elements
{
if(Num < atoi(argv[i])*S ) // if Num is small, then update the value of Num
Num = atoi(argv[i])*S; // updating the value of num
}
printf(\"%d\ \",Num*S); // Output to the display
return 0;
}
SAMPLE OUTPUTS
gcc –Wall –o find_largest_smallest numbers.c
./find_largest_smallest –l 5 2 92 424 53 42 8 12 23 41
The largest number is 424
./find_largest_smallest –s 5 2 92 424 53 42 8 12 23 41
The smallest number is 2

