Write and test a function that returns the position of the largest and smallest
ID: 3691143 • Letter: W
Question
Write and test a function that returns the position of the largest and smallest values in an array of double-precision numbers.
ALL MY CODE HAS TO DO IS DISPLAY THE LARGES AND THE SMALLEST VALUE. THIS IS WHAT I HAVE SO FAR, I DON'T KNOW WHAT MY MISTAKE IS. PLEASE HELP! (C++ FUNCTIONS AND ARRAYS)
#include <iostream>
#include <cmath>
using namespace std;
void displaymaxandmin(int arry[], int size);
int main()
{
int arry[10] = { 110, 20, 30, 40, 50, 160, 70, 80, 90, 100 };
displaymaxandmin(arry, 10);
system("PAUSE");
return 0;
}
void displaymaxandmin(int arry[], int size)
{
double max, min;
max = arry[0];
for (int i = 1; i < size; i++)
if (arry[i] > max)
{
max = arry[i];
}
cout << " Max value is : " << max << endl;
min = arry[0];
for (int i = 1; i < size; i++)
{
if (arry[i] < min)
{
min = arry[i];
}
}
cout << " Min value is :" << min << endl;
}
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
void displaymaxandmin(int arry[], int size);
int main()
{
int arry[10] = { 110, 20, 30, 40, 50, 160, 70, 80, 90, 100 };
displaymaxandmin(arry, 10);
system("PAUSE");
return 0;
}
void displaymaxandmin(int arry[], int size)
{
double max, min;
max = arry[0];
for (int i = 1; i < size; i++)
{
if (arry[i] > max)
{
max = arry[i];
}
}
cout << " Max value is : " << max << endl;
min = arry[0];
for (int i = 1; i < size; i++)
{
if (arry[i] < min)
{
min= arry[i];
}
}
cout << " Min value is :" << min << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.