Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Find the largest and smallest elements of an array.cpp #66

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions Array/Find the largest and smallest elements of an array.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
#include<iostream>
using namespace std;
#include <iostream>

int main()
{
int a[] = {23,6,328,34,12,234,9,23,4,54};
int largest = a[0];
int smallest = a[0];
int main() {
int arr[] = {4, 2, 9, 5, 1, 7, 8};
int n = sizeof(arr) / sizeof(arr[0]);

for(int i=0;i<10;i++)
{
if(a[i]>largest)
largest = a[i];
if(a[i]<smallest)
smallest = a[i];
if (n == 0) {
std::cout << "The array is empty." << std::endl;
return 1;
}
cout << "Largest "<<largest<<"\nSmallest "<<smallest<<"\n";

int minElement = arr[0];
int maxElement = arr[0];

for (int i = 1; i < n; ++i) {
if (arr[i] < minElement) {
minElement = arr[i];
}
if (arr[i] > maxElement) {
maxElement = arr[i];
}
}

std::cout << "Smallest element: " << minElement << std::endl;
std::cout << "Largest element: " << maxElement << std::endl;

return 0;
}
}

72 changes: 25 additions & 47 deletions Array/Max and min of an array (Simple lLinear Search).cpp
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
#include<bits/stdc++.h>
using namespace std;
#include <iostream>

struct Pair{
int min;
int max;
};

struct Pair getMinMax(int a[], int n) {
struct Pair minmax;
int i;

if(n==1) {
minmax.min = a[0];
minmax.max = a[0];
}

if(a[0] > a[1]) {
minmax.min = a[1];
minmax.max = a[0];
}
else {
minmax.min = a[0];
minmax.max = a[1];
}

for(int i = 2; i < n; i++) {
if(a[i] > minmax.max)
minmax.max = a[i];

else if(a[i] < minmax.min)
minmax.min = a[i];
}
return minmax;
}
int main() {
int n;
cin>>n;
int a[n];
for(int i = 0; i < n; i++)
cin>>a[i];

struct Pair minmax = getMinMax(a, n);

cout<<"Minimum element is "<<minmax.min<<endl;

cout<<"Maximum element is "<<minmax.max<<endl;

return 0;
int arr[] = {4, 2, 9, 5, 1, 7, 8};
int n = sizeof(arr) / sizeof(arr[0]);

if (n == 0) {
std::cout << "The array is empty." << std::endl;
return 1;
}

int minElement = arr[0];
int maxElement = arr[0];

for (int i = 1; i < n; ++i) {
if (arr[i] < minElement) {
minElement = arr[i];
}
if (arr[i] > maxElement) {
maxElement = arr[i];
}
}

std::cout << "Smallest element: " << minElement << std::endl;
std::cout << "Largest element: " << maxElement << std::endl;

return 0;
}