0% found this document useful (0 votes)
6 views2 pages

Array Program

The program takes an integer array as input and calculates the sum of its elements. It also finds the maximum and minimum values in the array and sorts the array in ascending order. Finally, it outputs the sum, maximum, minimum, and the sorted array.

Uploaded by

pghatage66
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Array Program

The program takes an integer array as input and calculates the sum of its elements. It also finds the maximum and minimum values in the array and sorts the array in ascending order. Finally, it outputs the sum, maximum, minimum, and the sorted array.

Uploaded by

pghatage66
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q.

Write an program that takes an array of integers as input and performs following
operations
-Find sum of all elements in array
-Find max. and min. values in the array
-sort the array in ascending order

#include <iostream>

#include <algorithm>

int main()

int n;

std::cout << "Enter the size of the array: ";

std::cin >> n;

int arr[n];

std::cout << "Enter the elements of the array:\n";

for (int i = 0; i < n; i++)

std::cin >> arr[i];

// Calculate the sum of all elements

int sum = 0;

for (int i = 0; i < n; i++)

sum += arr[i];

std::cout << "Sum of all elements: " << sum << std::endl;
// Find the maximum and minimum values

int maxVal = arr[0];

int minVal = arr[0];

for (int i = 1; i < n; i++)

if (arr[i] > maxVal)

maxVal = arr[i];

if (arr[i] < minVal)

minVal = arr[i];

std::cout << "Maximum value: " << maxVal << std::endl;

std::cout << "Minimum value: " << minVal << std::endl;

// Sort the array in ascending order

std::sort(arr, arr + n);

std::cout << "Sorted array in ascending order: ";

for (int i = 0; i < n; i++)

std::cout << arr[i] << " ";

std::cout << std::endl;

return 0;

You might also like