# Incorrect values returned from functions, have 2 warnings I don't understand

• 01-30-2002
cazil
Incorrect values returned from functions, have 2 warnings I don't understand
Environment: Desktop, Compiler: MS VC++ 6.0

My program is not putting out the correct result and I don't understand what those warnings means. All I am trying to find is the minimum and maximum value from the array.
Is there MS library that explain those mysterious messages?
Thank you anyone for your help
My codes and results below:

#include <iostream>
using namespace std;

#define num_elems 8

// function prototype
int max_price(int priceList[num_elems], int);
int min_price(int priceList[num_elems], int);

// last upated: Jan. 29, 2002 08:45 p.m - Jan. 30 09:15 p.m

void main()
{
int lowestPrice;
int highestPrice;
// The members of the array
int priceList[num_elems] = {8, 25, 36, 44, 52, 60, 75, 89};
//int minPrice = priceList[0];
//int maxPrice = priceList[0];
//int a = 8;

// Announce the result
lowestPrice = min_price(priceList, num_elems);

cout << "The lowest price in the array is: ";
cout << lowestPrice;
cout << endl;

// Announce the result
//Display the highest price << Maximum << "." << endl;

highestPrice = max_price(priceList, num_elems);
cout << "The highest price in the array is: ";
cout << highestPrice << endl; // num_elems being the # of elements in the array

}// end of main

// =============== ========================= =========== =======
// FUNCTION DEFINITIONS

int max_price(int priceList[], int)
{
//cout << "Finding the Maximum" << endl;
int maxIndex = 0;
int max = priceList[maxIndex]; // assume first element ismax
// now go through the rest of the array
// Compare the members
for ( int i = 1; i < num_elems; ++i)
{
if (priceList[i] > max)
{
max = priceList[i];
maxIndex = i;
}
return maxIndex;
}
}

// find the minimum pice
int min_price(int priceList[], int)
{
int minIndex = 0;
int min = priceList[minIndex]; // assume first element is min
// now go through the rest of the array
// Compare the members
for (int i = 1; i < num_elems; ++i)
{
if (priceList[i] < min)
{
min = priceList[i];
minIndex = i;
}
return minIndex;
}
}

/*
error list & warnings:
-----------Configuration: find_min_max - Win32 Debug------------
Compiling...
find_min_max.cpp
H:\ject\find_min_max.cpp(60) : warning C4715: 'max_price' : not all control paths return a value
H:\ject\find_min_max.cpp(78) : warning C4715: 'min_price' : not all control paths return a value

find_min_max.obj - 0 error(s), 2 warning(s)

// Result of test

The lowest price in the array is: 0 <---WRONG RESULT
The highest price in the array is: 1 <---WRONG RESULT
Press any key to continue

*/
• 01-30-2002
Prelude
Try this
Code:

```int max_price(int priceList[], int) {     int maxIndex = 0;     int max = priceList[maxIndex];     for ( int i = 0; i < num_elems; ++i)     {         // What if priceList[i] is == to max?         if (priceList[i] > max)         {             max = priceList[i];             maxIndex = i;         }      }     return maxIndex; }```
Since you're going through the entire array searching for max then why return inside the loop? Wait until the loop has completed and return the result then, that will clear the error and you'll find that the results are more as you expected.

-Prelude
• 01-30-2002
golfinguy4
int max_price(int priceList[], int)
int min_price(int priceList[], int)

What is the point of having an int inputted if it cannot be used(no title in which u can use it)?

int max_price(int priceList[num_elems], int);
int min_price(int priceList[num_elems], int);

Try omitting the size of the matrix in the first declaration and then you can have the size be inputted as the second parameter.