### To mark both smallest and largest element

I just got largest, didn't get smallest number. thanks, I appreciate your help
here is code:

#include <iostream>

using namespace std;

int main()
{
const int CAPACITY = 1000;
double values[CAPACITY];
int current_size = 0;

cout << "Please enter values, Q to quit:" << endl;
double input;
while (cin >> input)
{
if (current_size < CAPACITY)
{
values[current_size] = input;
current_size++;
}
}
double largest = values[0];
for (int i = 1; i < current_size; i++)
{
if (values[i] > largest)
{
largest = values[i];
}
}
for (int i = 0; i <current_size; i++)
{
cout << values[i];
if (values[i] == largest)
{
cout << " <== largest value";
}
}
double smallest = values[1];
for (int j = 0; j < current_size; j++)
{
if (values[j] > smallest)
{
smallest = values[j];
}
}
for (int j = 1; j < current_size; j++)
{
cout << values[j];
if (values[j] == smallest)
{
cout << " <== smallest value";
}
}
cout << endl;
return 0;
}

http://www.cplusplus.com/reference/algorithm/sort/

index 0 and "CAPACITY" for min and max
Can you give me more details? Helping me out. Thank you darkmaster

it sorts your array, so min and max are the beginning and end of your array

if you're not allowed to make changes to the array, you need a loop and 2 if statements
Last edited on
I think I did put double largest and smallest at the begining of each function. Darkmaster if you don't mind can you fix mine and post in here. It will help me understand more. Bzecause I'm just a beginner. Thanks
tried to keep the idea of your code.
however i'd write it different :)

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include int main() { const int CAPACITY = 1000; double values[CAPACITY]; int current_size = 0; std::cout << "Please enter values, Q to quit:" << std::endl; double input; while(std::cin >> input) { if(current_size < CAPACITY) { values[current_size] = input; current_size++; } } double largest, smallest; largest = smallest = values[0]; for(int i=0; i < current_size; ++i) { if (values[i] > largest) largest = values[i]; else if (values[i] < smallest) smallest = values[i]; } std::cout << "Largest number = " << largest << std::endl; std::cout << "Smallest number = " << smallest << std::endl; }``````
thank you sooo much darkmaster
There is standard algorithm std::minmax_element in C++ that does what you want.
This could also be done in real time as the input is coming in, to optimize the program.

Alos, keep in mind that if you only had one value it would be both the largest and smallest value in the array. Probably want to set your initial largest and smallest to the first value from input.
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include int main() { const int CAPACITY = 1000; double values[CAPACITY]; int current_size = 0; std::cout << "Please enter values, Q to quit:" << std::endl; double input; while(std::cin >> input) { if(current_size < CAPACITY) { values[current_size] = input; current_size++; } } double largest, smallest; largest = smallest = values[0]; for(int i=0; i < current_size; ++i) { if (values[i] > largest) largest = values[i]; else if (values[i] < smallest) smallest = values[i]; } std::cout << "Largest number = " << largest << std::endl; std::cout << "Smallest number = " << smallest << std::endl; }``````

can anyone tell me why this doesn't work with int input in line 12?
Last edited on
Topic archived. No new replies allowed.