To mark both smallest and largest element

Mar 13, 2013 at 8:39am
Hi please help me for this code, asking to mark both the smallest and the 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;
}

Mar 13, 2013 at 8:48am
http://www.cplusplus.com/reference/algorithm/sort/

index 0 and "CAPACITY" for min and max
Mar 13, 2013 at 8:52am
Can you give me more details? Helping me out. Thank you darkmaster
Mar 13, 2013 at 8:54am
just read the website i linked :P

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 Mar 13, 2013 at 8:56am
Mar 13, 2013 at 9:05am
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
Mar 13, 2013 at 9:18am
tried to keep the idea of your code.
however i'd write it different :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>


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;
}
Mar 13, 2013 at 2:14pm
thank you sooo much darkmaster
Mar 13, 2013 at 3:23pm
There is standard algorithm std::minmax_element in C++ that does what you want.
Mar 13, 2013 at 6:40pm
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.
Mar 14, 2013 at 7:31am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>


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 Mar 14, 2013 at 7:31am
Topic archived. No new replies allowed.