Question

Q#1 Write a C++ program that reads n integer values and stores them in an array...

Q#1

Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements.

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

SOURCE CODE IN C++:

#include <iostream>
using namespace std;

int main()
{
int n; //declaring variable for size of array
cout << "Enter the size of the array: "; //input prompt
cin >> n; //input
while(n<=0 && n>20) //input validation
{
cout << "Please enter a valid size for the array: "; //re-input prompt
cin >> n; //re-input
}
int arr[n]; //declaring the array with user input size
cout << "Enter the elements in the array: "; //input prompt
for(int i=0;i<n;i++)
cin >> arr[i]; //input
int min=arr[0],max=arr[0]; //setting temporary min and max
for(int i=1;i<n;i++)
{
//comparing every element to min and max and updating them
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
//output
cout << "Maximum element is " << max << " and Minimum element is " << min << endl;
return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Q#1 Write a C++ program that reads n integer values and stores them in an array...
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
  • Q-1: Write a program in Assembly language using MIPS instruction set that reads 15 integer numbers...

    Q-1: Write a program in Assembly language using MIPS instruction set that reads 15 integer numbers from user and stores all the numbers in the array intArray. Now, read another integer number N from the user, find the total number of array elements that are greater or equal to the number N, and the total number of array elements that are lower than the number N You must have two procedures: i. ReadIntegerArray: this procedure should read integer array elements...

  • C++ Problems: 1. Write a program that reads in an array of user given size n and then it reads se...

    C++ Problems: 1. Write a program that reads in an array of user given size n and then it reads several increment values terminated by 0. The program calls each time a function incarray() that accepts an array A, its size S and an increment value inc and updates yje array by incrementing all its values by the passed value of inc. The program prints out the array after each update. 2. write a program that reads in a n...

  • Problem Definition Write a program called process_ages that reads in an array of up to 10...

    Problem Definition Write a program called process_ages that reads in an array of up to 10 integers representing peoples ages in years and calculates and prints the maximum age and the minimum age and the average age. Your program should stop reading when either the 10th number is entered or the user enters -1. If the user enters any other negative number or a value above 150, they should be warned and prompted again for a valid age. You should...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

  • Write a program that reads a positive integer N, where N is greater than 1 and...

    Write a program that reads a positive integer N, where N is greater than 1 and prints the value of the floating-point value SUM where SUM is the results of the following series. SUM = 1/1! + 2/2! + 3/3! + 4/4! + ... N/N! Note that your program should print the message “Sorry, bad N”” if N is <=1, and keep reading user input until the user enters a valid value for N. Also, note that all division operations...

  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • Write a C++ program that reads in 6 elements from the keyboard and stores them in...

    Write a C++ program that reads in 6 elements from the keyboard and stores them in an array x[6]. It should then create a second array y[6] that contains the elements from x in reverse order and with the s Tsecond - Tsmallest shown below. A sample run should look like: rocted ITrom it (e.g., Jlast first Tsmallest Vsecond last , etc). The program should finally print out both arrays to screen, in the format Enter 6 array elements: 4...

  • MIPS CODE required to write an assembly program to find the maximum of an array of integers by...

    required to write an assembly program to find the maximum of anarray of integers by doing the following:1. Prompt user to input array size n (n <= 10)2. Prompt user to input element values of array A one by one3. Display the result on the console.This program must at least include one function. The main program will read the valuesof the array (as user inputs the element values) and stores them in the memory (datasegments section) and at the end...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

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