Click here to Skip to main content
15,943,085 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int findMin(int x[100],int n){
	int min = x[0];
	for(int i=1;i<n;i++){
	return min;
int findMin2(int x[100],int n){
			int min_2=x[1];

	int min= findMin(x,n);
	for(int i=0;i<n;i++){
		if (min_2>min && min_2>x[i]){
	return min_2; 
int main(){
	int a[100],n;
	printf("Please enter the number n is");
	for(int i=0;i<n;i++){
 printf("\nThrough which we have this array:");
 for (int i=0;i<n;i++){
 printf("\n%d",findMin(a,n) ) ;
 printf("\n%d",findMin2(a,n) );

What I have tried:

i don't understand this problem
Updated 6-Dec-22 9:08am

You have a single array of values, and two methods to find the smallest number. So obviously both answers will be the same. However, if findMin2 is supposed to find the second smallest, then you need to check and ignore any number less than or equal to min. But you have not provided a detailed explanation of the question.
Share this answer
To be honest, the code you have doesn't make a lot of sense. What makes you think that x[1] isn't the smallest value? If it is, your code can't find any other value because it can never get past the if condition in your loop!

If you are trying to find the two lowest values, then the easiest way is to sort all the values: the two lowest are at the bottom then.
Or, you could use a single loop, and keep two minimums:
#include <stdio.h>
#define MaxInt 0x7FFFFFFF;
int main()
    int arr[] = {9,8,7,3,1,5,2,6,4};
    int min1 = MaxInt;
    int min2 = MaxInt;
    for (int i = 0; i < 9; i++)
       if (arr[i] < min1)
          min2 = min1;
          min1 = arr[i];
       else if (arr[i] < min2)
          min2 = arr[i];
    printf("%u,%u\n", min1, min2);
    return 0;
Share this answer
i want to code better 5-Dec-22 3:34am    
Does that code need to add limit.h library?
OriginalGriff 5-Dec-22 3:59am    
Does it use anything from limit.h?
i want to code better 5-Dec-22 5:18am    
I have 1 more question:
i can find min and in my opinion then the value of min will be fixed(no change). then i compare min2 >min, so why does it give the same result? Can you explain it to me?
OriginalGriff 5-Dec-22 7:09am    
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Run your code through the debugger: it'll be pretty obvious!

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

It's well worth spending a few minutes learning how to do this on a simple project like this: when they get bigger the same things work, but it's way easier to learn how to debug on "small stuff"
i want to code better 5-Dec-22 9:26am    
May I ask a stupid question why 0x8000000 ? is that the limit of number?
Your two functions both find the same minimum. The minimum could also occur several times with different indices. Here it is not clear whether the value or the index should be searched or compared. To avoid searching the same minimum twice it would be necessary to tell the function findMin2() which min it should not find. I would return the index then you can implement both. The main program would look like this:
int main() {
   int a[] = { 9,8,7,3,-3,1,5,2,-8,6,4 };
   size_t n = sizeof(a) / sizeof(a[0]);

   unsigned min = findMin(a, n);
   unsigned min2 = findMin2(a, n, min);

   printf("\n%d, %d\n", a[min], a[min2]);
   return 0;

In Function findMin2() you can do this:
int min2 = 0;
if (n < 2) return 0;  // not to solve!
if (min1 == 0) min2++;

and in loop:
if (x[min2] > x[i] && (i != min1)) { ..
Share this answer
To find both minima at the same time, you could write a function that searches for both values. min1 and min2 could have the same value if the field contains the lowest number more than once, or if the field contains only one element.
void find2Mins(int *x, int n, int *min1, int *min2)
	int midx1 = 0;
	int midx2 = 0;

	if (n > 1) midx2++;
	for (int i = 1; i < n; i++) {
		if (x[i] <= x[midx1]) {
			midx2 = midx1;
			midx1 = i;
	*min1 = x[midx1];
	*min2 = x[midx2];
Share this answer
i want to code better 7-Dec-22 5:25am    
I tried your code and it's wrong
merano99 7-Dec-22 11:36am    
I had tested it, of course. What exactly is wrong?
i want to code better 8-Dec-22 2:07am    
it can run but the result is not correct
merano99 8-Dec-22 15:42pm    
For which input does it not work and what is the result? Are you perhaps calling the subroutine incorrectly?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900