Question

(you can use any language) Write an algorithm that works with an array of size N...

(you can use any language)

Write an algorithm that works with an array of size N and shows the user the next menu:

1. Fill array.

2. Get sum of array elements

3. Get the maximum value of the array elements

4. Get the minimum value of the array elements.

5. Exit

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

Algorithm:

Step 1: START
Step 2:Initiliaze or declare required variables like i,j,N,max,min,sum.

Step 3: Read N size of the array from the user.

Step 4: Declare array with size N and using while loop show the menu to the user.

1. Fill array.

2. Get sum of array elements

3. Get the maximum value of the array elements

4. Get the minimum value of the array elements.

5. Exit

Step 5: Read any one option from the user.

Step 6: Pass option to the switch statement:

  Case 1:

using for loop from i=0 to i<N read the elements into the array and break statement at the end.

  Case 2:

Using sum variable and for loop from i=0 to i<N calculate sum as sum=sum+array[i] then display the sum and break.

Case 3:

  Using for loop find maximum value by using comparison if max<array[i] if true then max=array[i]. print max.

Case 4:

  Using for loop find minimum value by using comparison if min>array[i] if true then min=array[i]. print min.

Case 5:

Write exit(0) statement to exit from the program.

Step 7:  STOP

Source code in C++:

#include<iostream> using namespace std; /*main function*/ int main() { /*variables*/ int ch, N, i, sum=0,max=0, min; /*read s{ /*option 1*/ case 1: cout<<Enter the <<N<< elements:<<endl; /*read elements from user*/ for(i=0; i<N;i++) { cin>>array[case 3: for(i=0; i<N;i++) { /*find max*/ if(max<array[i]) max=array[i]; } /*display maximum value*/ cout<<The maximum value/*option 5*/ case 5: /*exit*/ exit(0); /*default option*/ default: cout<<Choose correct option!<<endl; } } }

Output:

C:\Users\Kumar Reddi\Desktop\arraymenu.exe Enter the size of the array: 5 1.Fill array 2. Get sum of array elements 3. Get th1.Fill array 2. Get sum of array elements 3. Get the maximum value of the array elements 4. Get the minimum value of the arra

Code in text format (See above images for indentation):

#include<iostream>
using namespace std;
/*main function*/
int main()
{
   /*variables*/
   int ch,N,i,sum=0,max=0,min;
   /*read size of the array*/
   cout<<"Enter the size of the array: ";
   cin>>N;
   /*declare array*/
   int array[N]={0};
   while(1)
   {
       /*show menu to user*/
       cout<<"1.Fill array\n2.Get sum of array elements"<<endl;
       cout<<"3.Get the maximum value of the array elements"<<endl;
       cout<<"4.Get the minimum value of the array elements"<<endl;
       cout<<"5.Exit\nChoose your option: ";
       /*read option from user*/
       cin>>ch;
       /*switch case*/
       switch(ch)
       {
           /*option 1*/
           case 1:
               cout<<"Enter the "<<N<<" elements:"<<endl;
               /*read elements from user*/
               for(i=0;i<N;i++)
               {
                   cin>>array[i];
               }
               cout<<"Filled"<<endl;
               break;
           /*option 2*/
           case 2:
               sum=0;
               /*calculate sum*/
               for(i=0;i<N;i++)
               {
                   sum=sum+array[i];
               }
               /*display sum*/
               cout<<"The sum of array elements is: "<<sum<<endl;
               break;
           /*option 3*/
           case 3:
               for(i=0;i<N;i++)
               {
                   /*find max*/
                   if(max<array[i])
                       max=array[i];
               }
               /*display maximum value*/
               cout<<"The maximum value of the array elements is: "<<max<<endl;
               break;
           /*option 4*/
           case 4:
               min=array[0];
               /*find min*/
               for(i=1;i<N;i++)
               {
                   if(min>array[i])
                       min=array[i];
               }
               /*display minimum*/
               cout<<"The minimum value of the array elements is: "<<min<<endl;  
               break;
           /*option 5*/
           case 5:
               /*exit*/
               exit(0);
           /*default option*/
           default:
               cout<<"Choose correct option!"<<endl;
       }  
   }
}

Add a comment
Know the answer?
Add Answer to:
(you can use any language) Write an algorithm that works with an array of size N...
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
  • Using Matlab Write a program which will: a. Accept two numbers from the user m and...

    Using Matlab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Provide the user the following menu from which he can select: 1. Sum of all elements in the array. Print the result. 2. Sum of the element in a row that he’ll specify. Print the result. 3. Sum of the...

  • mathlab Q2. Write a script file that accepts the user input of array of any size...

    mathlab Q2. Write a script file that accepts the user input of array of any size then uses nested for loops to find the minimum and maximum elements in the array. See slides 43,44 & 45 for hints Use the following 2 arrays to test your script: A- A 10 10 -4 17 2 5 0 13 -7 9 AND لیا M -1 5 -7 4 7 17 6 9 10 Sample Outputs: For the given array, the min. value...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • C programming Write a function that goes through all elements of an array of size n,...

    C programming Write a function that goes through all elements of an array of size n, and output the minimum and maximum values using pointers. Since C functions can only return one value, you must use pointers to output the minimum and maximum values from the function. You are not expected to print the values on the screen. Note: you will be assessed on the functionality and the coding style.

  • Write a C Language Program that will ask the user how many elements the "maxminarray" will...

    Write a C Language Program that will ask the user how many elements the "maxminarray" will have. Read the elements from user and fill the "maxminarray” with it. Find maximum value and minimum value in maxminarray See the output below: Input number of elements of the array :6 Input 6 elements for the maxminarray : element [0]: 23 element [1] : 56 element [2] : 11 element [3]: 78 element [4]: 21 element [5]: 400 The Maximum element is :...

  • Programming Problem: SUMMING ARRAY ELEMENTS -       Use Assembly Language x86 (MASM) Write an assembly code...

    Programming Problem: SUMMING ARRAY ELEMENTS -       Use Assembly Language x86 (MASM) Write an assembly code calculates the sum of all array elements. Save the sum in the EAX register. ------------------------------------------------------------------------------- This is my code so far: INCLUDE Irvine32.inc N=10 .data array SDWORD N DUP(0,1,2,3,4,5,6,7,8,9) j DWORD ? k DWORD ? .code     main PROC         ; not complete          exit    main ENDP END main

  • in C language Write a program to find the element of an array, which occurs maximum...

    in C language Write a program to find the element of an array, which occurs maximum number of times and its count. The program should accept the array elements as input from the user and print out the most occurring element along with its count. Hint: array size is 5. For example: Enter array elements (array size 5): 22321 Output: Max occurring element: 2. count: 3

  • the programming language is in java Problem 2 You are given an array A with n...

    the programming language is in java Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...

  • Suppose that you are given an array of N elements. Develop an optimum algorithm that finds...

    Suppose that you are given an array of N elements. Develop an optimum algorithm that finds the minimum k elements of this array in at most nlogn time. Try your algorithm on an example N-sized array and some value of k.

  • Need help with my Java Hw: Consider an algorithm that sorts an array of n elements...

    Need help with my Java Hw: Consider an algorithm that sorts an array of n elements by finding the smallest and largest elements and then exchanges those elements with the elements in the first and last positions in the array. Then the size of the array is reduced by two elements after excluding the two elements that are already in the proper positions, and the process is repeated on the remaining part of the array until the entire array is...

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