I have the following input and my program is not running: 1 3 2 1 3 7 8

#include<iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int arr[7], min, i=0, j=0;
	
	for(int i=0; i<7; i++)
	{
		cout<<"Enter value at arr["<<i<<"]: ";
		cin>>arr[i];
	}
	
 	min = arr[j];
	for(; i<7; i++)
	{
		if(arr[i] < min)
		{
			min = arr[i];
		}

		if(arr[i] == min)
		{
			min = arr[j+1]; //set min to next value in the array
			i=j+1; //set i to value in the min + 1... program hangs here...
		}
	}

	cout<<endl<<min;
	
	cout<<endl<<endl;
	system("pause");
	return 0;
}

I think my program and the comments reflects my problem clearly... Basically my program tries to do is that when it finds a duplicate min so its set the min value to the next value in the array and set i to the next value pointed out by min so that my loop can start again and find other unique min... But my program hangs... Why?

I belive you need to increment j after line 27 and before line 28. Otherwise you are always starting back at the same place.

I tried but still the program hangs... Any other solution?

First, it is not usually a good idea to shadow loop counter variables like you have. A quick read over your code looks like you never reset i after the first loop.

As far as your logic, add a few print statements and you will see what is happening immediately:

arr[0] == min (1 == 1)
arr[2] < min (2 < 3)
arr[2] == min (2 == 2)
arr[2] < min (2 < 3)
arr[2] == min (2 == 2)
arr[2] < min (2 < 3)
arr[2] == min (2 == 2)
arr[2] < min (2 < 3)
arr[2] == min (2 == 2)
...

For this type of program, the easiest solution is to sort the input then it's an easy matter to find the unique minimum.

Try not to use multiple variables with the same name within the same program. It's asking for trouble. The only exception might be a simple counting variable used within loops, but then make the variable local to the loop so you don't forget to reset the value to a default value.

If the compiler is compliant, the variable i declared on line 10 overrules the variable i declared on line 8 and goes out of scope on line 14. If it didn't, then the value of the variable i after line 14 would be 7 so the loop on line 17 would never run.

Delete the declaration of i and j from line 8.

Assign the value of arr[0] to min on line 16.

Remove (or comment them out) lines 24 through 28.

Declare j within the loop on line 17 and substitue j for i on lines 17. 19 and 21.

Recompile, debug and run the program. The min value should be 1 if the input is in the sequence originally posted. The program will ignore any duplicate version of min (as can be proven by using a variable to keep track of the index of the element representing min).

The min value should be 1 if the input is in the sequence originally posted. The program will ignore any duplicate version of min (as can be proven by using a variable to keep track of the index of the element representing min).

Did you miss the term (and description of) unique min? :icon_wink:

Nope. I didn't miss it, but given your comments, I probably misunderstood what OP was trying to do. My corrections would result in a program that would find the first occurence of the minimum value of the collection. It would not find the minimum value that is also unique within the collection.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.