Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Do not change the parameters please or add a function declaration. (40 points) W

ID: 3798773 • Letter: D

Question

Do not change the parameters please or add a function declaration. (40 points) 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 <stdlib.h> to convert a string to integer form. 3) Generate the executable as find_largest_smallest.

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#define N 10
int main(int argc, char *argv[])
{
   int l[10];
   int i;
   int s[10];
   if(strcmp(argv[1],"-l")==0)
   {
       for(i=1;i<argc;i++)
       {
           if(atoi(argv +N])>l)
           {
               l=atoi(argv[N]);
           }
       }
       printf("The Largest Number is %d",*l);
   }
   else if(strcmp(argv[1],"-s")==0)
   {
       for(i=1;i<argc;i++)
       {
           if(atoi(argv[N])<s)
           {
               s=atoi(argv[N]);
           }
       }
       printf("The Smallest Number is %d",*s);
   }
   else
   {
       printf("Invalid options");
   }
   return 0;
}

apparently i am initializing the i and s wrong and thats why i am getting wrong values when compiling. can anybody help?

Explanation / Answer

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#define N 10
int main(int argc, char *argv[])
{
int l = atoi(argv[2]);
int i;
int s = atoi(argv[2]);
if(strcmp(argv[1],"-l")==0)
{
for(i=2;i<argc;i++)
{
if(atoi(argv[i])>l)
{
l=atoi(argv[i]);
}
}
printf("The Largest Number is %d", l);
}
else if(strcmp(argv[1],"-s")==0)
{
for(i=2;i<argc;i++)
{
if(atoi(argv[i])<s)
{
s=atoi(argv[i]);
}
}
printf("The Smallest Number is %d",s);
}
else
{
printf("Invalid options");
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote