Thread: Finding minimum in Array (2 equal minimums)

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    23

    Finding minimum in Array (2 equal minimums)

    hello!

    I am writing a program to find the minimum value in an array that is within an int function. There are two elements in the array that have the same value and I need to be able to output them both. However, the way my function code is written as far as I understand is that it will only store the location of the minimum it encounters and it seems to ignore the second one. This makes sense to me as the function can only return one value. I define the location as loc = function(variables) in main. I tried using a for loop before I output the university/local tuition corresponding to that location I just calculated but it seems to only still output the first minimum.

    So how can I write this so it returns both? I am only showing the code that is relevant here

    Code:
    int main()
    {
    	string uni[MAX], state[MAX], city[MAX];
    	int enroll[MAX], number, loc;
    	//Null here is actually percent of students accepted but will not be used in anything hence nickname of null
    	double yearlytuit[MAX], null[MAX], fresh[MAX], gradsix[MAX];
    	double avgTuition, lowest;
    	ifstream inFile;
    	ofstream outFile;
    	
    	loc = lowTuit(uni, yearlytuit, number);
    	cout << uni[loc] << yearlytuit[loc];
    
    int lowTuit(string uni[], double yearlytuit[], int number)
    {
    	int loc;
    	string univ;
    	double lowest = yearlytuit[0];
    	for (int i = 0; i < number; i++)
    	{
    		if (lowest > yearlytuit[i])
    		{
    			lowest = yearlytuit[i];
    			loc = i;
    		}
    	}
    	return loc;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    There is no way to do it right.

    But, you could write another function that returns all the values that are less than or equal to the lowest value.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    23
    Quote Originally Posted by stahta01 View Post
    There is no way to do it right.

    But, you could write another function that returns all the values that are less than or equal to the lowest value.

    Tim S.
    Is there no way to use maybe a for condition in the main when I am asking it to cout << uni[loc] << yearlytuit[loc] ?

  4. #4
    Registered User
    Join Date
    May 2019
    Posts
    23
    Never mind. I have figured it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. |C| Question about finding equal values in array
    By vibex in forum C Programming
    Replies: 8
    Last Post: 02-03-2015, 06:40 PM
  2. Need help finding the minimum value of an array
    By Kevin Nguyen in forum C Programming
    Replies: 5
    Last Post: 09-09-2013, 11:55 PM
  3. Finding the minimum value in an array?
    By kabuatama in forum C Programming
    Replies: 8
    Last Post: 02-18-2006, 01:45 PM
  4. finding non-zero minimum in array
    By xstudent in forum C Programming
    Replies: 8
    Last Post: 12-16-2002, 03:58 AM
  5. finding minimum in array
    By xstudent in forum C# Programming
    Replies: 3
    Last Post: 12-15-2002, 12:23 AM

Tags for this Thread