Question #3320

Write a program to find the biggest and smallest numbers in the array?

Expert's answer

#include <iostream>

using namespace std;

int main()

{

const int N = 5;

int arr[N] = { 10, 46, -37, 8, 19 };

int smallest = 0, biggest = 0;

for (int i = 1; i < N; ++i) {

if (arr[smallest] > arr[i])

smallest = i;

if (arr[biggest] < arr[i])

biggest = i;

}

cout << "The smallest number in the array is " << arr[smallest] << endl;

cout << "The biggest number in the array is " << arr[biggest] << endl;

return 0;

}

using namespace std;

int main()

{

const int N = 5;

int arr[N] = { 10, 46, -37, 8, 19 };

int smallest = 0, biggest = 0;

for (int i = 1; i < N; ++i) {

if (arr[smallest] > arr[i])

smallest = i;

if (arr[biggest] < arr[i])

biggest = i;

}

cout << "The smallest number in the array is " << arr[smallest] << endl;

cout << "The biggest number in the array is " << arr[biggest] << endl;

return 0;

}

## Comments

## Leave a comment