Question

In: Computer Science

Design a modular program which asks the user to enter a list of numbers. The numbers...

Design a modular program which asks the user to enter a list of numbers. The numbers must be stored in an array. The program then finds the index of the first occurrence of the smallest element in the array and the last occurrence of the largest element in the array. The program displays the position and value of each of these items.

Solutions

Expert Solution

Answer:

#include<iostream>
using namespace std;
int smallest(int[],int);
int largest(int[],int);
int main()
{
   int a[20],i,n,si,li; /* a is an array, i is counter variable, n is the number of elements, si is smallest number index, li is largest number index*/
   cout<<"Enter the number of elements to be entered into the array:";
   cin>>n; /* Taken the number of elements of the array*/
   cout<<"Enter the array elements one by one\n";
   for(i=0;i<n;i++)
   cin>>a[i]; /* Takes the n elements into array a one by one from the keyboard*/
   cout<<"The elements of the array are\n";
   for(i=0;i<n;i++)
   cout<<a[i]<<" "; /* Displays the array elements one by one */
   si=smallest(a,n); /* Function call to smallest(), the arguments are array's base address and number of elements n*/
   cout<<"\nThe index of the first occurrence of the smallest element of the array is:"<<si;
   li=largest(a,n); /* Function call to largest(), the arguments are array's base address and number of elements n*/
cout<<"\nThe index of the last occurrence of the largest element of the array is:"<<li;
   return 0;
}
int smallest(int a[],int n)
{
   int i,min;
   min=a[0]; /* Treat the first element as minmum*/
   for(i=1;i<n;i++) /* Comparision for minimum element of the array starts at index 1 i.e from the second element*/
   {
       if(a[i]<min) /* if the ith index element is less than minimum then make a[i] as minimum*/
       {
           min=a[i];
       }
   }
   cout<<"\nThe Minimum element of the array is:"<<min; /* Displays the minimum element of the array*/
   for(i=0;i<n;i++)
   {
       if(a[i]==min) /* Once we find the minimum element compare it to every element to get the first occcurrence of that element*/
       break; /* Once min matches with ith index element it stops comparision and exits from the loop*/
   }
return i; /* returns ith index value (i value) to the main() function*/
}
int largest(int a[],int n)
{
   int i,max,index;
   max=a[0]; /* Treat the first element as maximum*/
   for(i=1;i<n;i++) /* Comparision for maximum element of the array starts at index 1 i.e from the second element*/
   {
       if(a[i]>max) /* if the ith index element is greater than maximum then make a[i] as maximum*/
       {
           max=a[i];
       }
   }
   cout<<"\nThe Maximum element of the array is:"<<max; /* Displays the maximum element of the array*/
   for(i=0;i<n;i++)
   {
       if(a[i]==max) /* Once we find the maximum element compare it to every element to get the last occcurrence of that element*/
       {
           index=i; /* Finally last occurrence of maximum element's index will be stored in index*/
       }
   }
return index; /* returns ith index value (index value) to the main() function*/
}​

Output:


Related Solutions

Q2. Design a Pearson modular program that asks the user to enter the monthly costs for...
Q2. Design a Pearson modular program that asks the user to enter the monthly costs for each of the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, and maintenance (create a module for each expense). The program should then display the total monthly cost of these expenses, and the total annual cost of these expenses in the main Module. Use Pass arguments By Reference method to design your modules. Submit your pseudocode with comments at...
Please write in python Use modular design to write a program that asks the user to...
Please write in python Use modular design to write a program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet. PLANET CONVERSION FACTOR Mercury 0.4155 Venus 0.8975 Earth 1.0000 Moon 0.1660 Mars 0.3507 Jupiter 2.5374 Saturn 1.0677 Uranus 0.8947 Neptune 1.1794 Pluto 0.0899 The...
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
Design the logic for a program that allows a user to enter 20 numbers, then displays...
Design the logic for a program that allows a user to enter 20 numbers, then displays them in the reverse order of entry. Design the logic for a program that allows a user to enter 20 numbers, then displays each number and its difference from the numeric average of the numbers entered. The program is C++. I need a Pseudocode
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT