I trying to find the minimum and maximum numbers using pointers in C++ but in this code that I get from the video-sharing site gives me the wrong value. when I type 1 2 3 4 5 6 its answer is the minimum is 2 and the maximum is 2.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int value[6], * maxi, * mini, a;
cout << "Enter six numbers with space: ";
for (int a = 0; a < 6; a++)
cin >> *(value + a);
maximum = value;
minimum = value;
for (i = 1; i < 6; i++)
{
if (*(value + i) > * maximum)
*maximum = *(value + i);
}
for (i = 0; i < 6; i++)
{
if (*(value + i) < *minimum)
*minimum = *(value+ i);
}
/* Print variable value with their memory address */
cout << "Integer with the Maximum value = " << *minimum << ", Address of integer = " << &maximum
<< endl;
cout << "Integer with the minimum value = " << *minimum << ", Address of integer = " << &minimum
<< endl;
return 0;
}
this is the result of the code
Enter five (6) numbers separated by a space: 1 2 3 4 5 6
Integer with the largest value = 2, Address of integer = 00AFF9F4
Integer with the smallest value = 2, Address of integer = 00AFF9E8
Here you are the complete code:
#include <iostream>
#define N_VALUES 6
using namespace std;
int main()
{
int value[N_VALUES], *maximum, *minimum, i;
cout << "Enter six numbers with space: ";
for (i = 0; i < N_VALUES; i++) cin >> *(value + i);
maximum = value;
minimum = value;
for (i = 1; i < N_VALUES; i++) {
if (*(value + i) > (*maximum)) {
maximum = (value + i);
}
}
for (i = 1; i < N_VALUES; i++) {
if (*(value + i) < (*minimum)) {
minimum = (value+ i);
}
}
/* Print variable value with their memory address */
cout << "Integer with the Maximum value = " << *maximum << ", Address of integer = " << maximum << endl;
cout << "Integer with the minimum value = " << *minimum << ", Address of integer = " << minimum << endl;
return 0;
}