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...
Design a complete program that asks the user to enter a series of 20 numbers. The...
Design a complete program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array and then display each of the following data: I. The lowest number in the array II. The highest number in the array III. The total of the numbers in the array IV. The average of the numbers in the array *PYTHON NOT PSUEDOCODE AND FLOW CHART!!!!*
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)
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
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...
Write a python program that asks the user to enter a string containing numbers separated by...
Write a python program that asks the user to enter a string containing numbers separated by commas, e.g., s = '1.23,2.4,3.123', Your program should then calculate and print the sum of the numbers entered. Hint: you need to iterate over the string searching for the commas, i.e. their index. The first number is obtained by slicing between the start of the string and the index of the first comma. The second number is between the last comma and the next...
Design, plan, test, and write a computer program in Java that asks the user to enter...
Design, plan, test, and write a computer program in Java that asks the user to enter 1 number and a String. You will display the first n characters of the string where n is the number entered. For example, if the user enters 3 and java then you will print jav. If they enter 5 and Halloween then you print Hallo. If the user enters a number less than 0 then set the number to 0. Assume the user will...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT