I am trying to complete a final project for an intro to c++ course. I need to find that maximum and minimum values of an array from user supplied numbers of the type float. How the heck do you do this?
Printable View
I am trying to complete a final project for an intro to c++ course. I need to find that maximum and minimum values of an array from user supplied numbers of the type float. How the heck do you do this?
Just do something like this:
maxsize = array[0];
minsize = array[0];
for(USHORT i=0; i<strlen(array); i++){
if(array[i] > maxsize){
maxsize = array[i];
}
else if(array[i] < minsize){
minsize = array[i];
}
}
Good luck!
something like this the code probably isn't the best but it will work I hope
#include <iostream.h>
void main()
{
int x;
float y;
float array[10]; //don't know how many numbers you need sorry
cout << "Please enter nine numbers with spaces in between them";
cin >> array[0] >> array[1] >> array[2] >> array[3] >> array[4] >> array[5] >> array[6] >> array[7] >> array[8] >> array[9];
//for highest number
y = array[0]
x=1;
do
{
if(array[x] > y)
{
y = array[x];
}
x++;
}
while(x < 9);
cout << y;
y=array[0];
x=1;
do
{
if(array[x] < y)
{
y=array[x];
}
x++;
}
while(x < 9);
cout << array[x];
}
hopefully that works
crossbows example is more along the lines of where I'm trying to go. It seems to have worked great with getting me the max value but not the minimum.
here's what I'm looking at:
// Function to display data
void data(float temp[], int &i)
{
float max = temp[0],
min = temp[0],
total = 0,
avg;
int tempNum = 0;
for (i = 0; i < 5; i++)
if (temp[i] > max)
{
max = temp[i];
}
if (temp[i] < min)
{
min = temp[i];
}
total += temp[i];
avg = total / i;
cout << "\nmax temperature was..." << max << endl;
cout << "\nmin temperature was..." << min << endl;
cout << "\nThe total number of temperatures was..." << i << endl;
cout << "\nThe average temperature was..." << avg << endl;
You need to put parenthisis around the things in your for statement:
void data(float temp[], int &i)
{
float max = temp[0],
min = temp[0],
total = 0,
avg;
int tempNum = 0;
for (i = 0; i < 5; i++)
{
if (temp[i] > max)
{
max = temp[i];
}
if (temp[i] < min)
{
min = temp[i];
}
total += temp[i];
avg = total / i;
}
cout << "\nmax temperature was..." << max << endl;
cout << "\nmin temperature was..." << min << endl;
cout << "\nThe total number of temperatures was..." << i << endl;
cout << "\nThe average temperature was..." << avg << endl;
}
I think it should work then.
that did it! Thanks a lot. Maybe I can get some sleep tonight after all! Thanks again