Question

Write a program that finds either the largest or smallest of the ten numbers as command-line...

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.

gcc –Wall –o find_largest_smallest numbers.c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int main() {
int a[30], i, num, smallest,largest;
char f;
printf("\nEnter no of elements :");
scanf("%d", &num);
printf("\nEnter elements :\n");
//Read n elements in an array
for (i = 0; i < num; i++)
scanf("%d", &a[i]);

for (i = 0; i < num ; i++){
if (largest < a[i])
largest = a[i];
}
//Consider first element as smallest
smallest = a[0];
for (i = 0; i < num; i++) {
if (a[i] < smallest) {
smallest = a[i];
}
}
printf("Enter 'l' for largest number or 's' for smallest number \n");
scanf("%s",&f);

if(f == 'l')
printf("\nThe Largest number is %d", largest);
else if(f == 's')
printf("\nThe smallest number is %d", smallest);
else
printf("error\n");


return (0);
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a program that finds either the largest or smallest of the ten numbers as command-line...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that finds either the largest or smallest of the ten numbersas command-line arguments....

    Write a program that finds either the largest or smallest of the ten numbersas command-line arguments. With –l for largest and –s for smallest number, if the userenters 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 41output: The largest number is 424 ./find_largest_smallest –s 5 2 92 424 53 42 8 12 23 41output: The smallest number is 2 1) Name your program...

  • 3. (c-program) Write a program that finds the largest and smallest value in a series of...

    3. (c-program) Write a program that finds the largest and smallest value in a series of numbers entered by a user. The user must enter 25 values. Note if that if the user enters "0" they must correct and add another number. Print out the original entered values, the largest number, the smallest number. Note whether the user tried to enter "O".

  • Write a program in C++ language that finds the largest number in an array of positive...

    Write a program in C++ language that finds the largest number in an array of positive numbers using recursion. Your program should have a function called array_max that should pass in an int array, and an int for the size of the array, respectively, and should return an int value. As an example, given an array of size 10 with contents: {10, 9, 6, 1, 2, 4, 16, 8, 7, 5} The output should be: The largest number in the...

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

  • Your task is to write a C++ program that consumes integer values as command line arguments...

    Your task is to write a C++ program that consumes integer values as command line arguments and returns the arithmetic mean of these values. To increase the flexibility of the program, there should be no set number of arguments. To overcome this, we will require the argument argv[1] to be the number of integers the user enters. For example, if the user wants to calculate the arithmetic mean of 4 numbers, they would pass in 4 as the first argument...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • You will write a C program, q1 sequence.c, that computes the value of the nth term...

    You will write a C program, q1 sequence.c, that computes the value of the nth term in any recursive sequence with the following structure: an = c1 · an−1 + c2 · an−2 a0 > 0 a1 > 0 c1 6= 0 c2 6= 0 Your C program will take 5 integer arguments on the command line: n, a0, a1, c1 and c2. n must be an integer greater than or equal to 0. If more or fewer arguments are...

  • Write a C++ program that takes two numbers from the command line and perform and arithmetic...

    Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces...

  • Write a C program, named sortit, that reads three integers from the command line argument and...

    Write a C program, named sortit, that reads three integers from the command line argument and returns the sorted list of the integers on the standard output screen, For instance, >sortit 3 5 7 >The sorted list is 3, 5, 7. >sortit 4 0 9 >The sorted list is 0, 4, 9 >sortit 1 p 9 >sortit: invalid input p. >sortit >usage: sortit num1 num2 num3! 1. The source code of a c file 2. Screenshots that show successful execution...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT