-1

What is wrong in the following program? Why isn't it returning smallest element as i have tried to implement. Kindly spot the errors.Please tell me the errors regarding the logic and the syntax.

#include<stdio.h>
int ArrayMinimum(int a[], size_t size);
#define SIZE 9
int main()
{
    int a[SIZE];
    for (int i = 0; i < SIZE; i++)
    {
        a[i] = 1 + rand() % 99;
        printf("%d  ", a[i]);
    }
    printf("\n\nThe smallest number of the array is %d  \n", ArrayMinimum(a, SIZE));
}
int ArrayMinimum(int a[], size_t size)
{
    if (size == 1)
    {
        return a[0];
    }
    for (int i = 0; i <= size ; i++)
    {
        if (a[i] > a[i + 1])
        {
            int temp = a[i + 1];
            a[i + 1] = a[i];
            a[i] = temp;
        }
    }
    int b[] = { 0 };
    for (int y = 0; y < size; y++)
    {
        b[y] = a[y];
    }
    ArrayMinimum(b, size -1 );

}
6
  • 1
    int i = 0; i <= size... - you are off by 1. Array last element is on size-1 index. Adding 1 to it in the loop worsens the problem. Commented Dec 3, 2018 at 18:38
  • It is still not working Commented Dec 3, 2018 at 18:46
  • Your code is weird modification of bubble sort (recursion? Why?) and not for finding minimum. Please explain to yourself what it is supposed to do and how. Commented Dec 3, 2018 at 18:51
  • 2
    Possible duplicate of Finding the smallest number in an array of integers Commented Dec 3, 2018 at 18:53
  • well , after iterating through the whole for loo, the last element (largest) will be sorted, and then I just remove it and recall the function to find the largest of the remaining array until the array size is 1 (only contains smallest element). I don't know if it's right (just new to programming). Commented Dec 3, 2018 at 18:57

3 Answers 3

1

Your function is defined to return an int; and it will return an integer value, if and only if size == 1.

If size has another value, it will not return anything at all!
Where is a second return statement?

There are other substantial problems, such as the size of Array b is not well defined, and you overwrite memory there.

Sign up to request clarification or add additional context in comments.

Comments

0

You have't put #include <stdlib.h> at the top of your file and as a result, the functions you called were assumed to accept an unknown number of arguments and return a value of type int. This causes undefined behavior.

Implicit declaration of functions srand, rand and system

Also your ArrayMinimum() is wrong.The SIZE constant is always equal to 9 and it gets passed in this method and used by the size variable.Hence it will never satisfy the 'if' condition of this method.With that the BubbleSort mechanism you have implemented is also wrong.Your code just swaps the values only once.

Use this approach to find the minimum of the array:-

minimum = array[0];

    for (c = 1; c < size; c++)
    {
        if (array[c] < minimum)
        {
           minimum = array[c];
           location = c+1;
        }
    }

Comments

0

Your ArrayMinimum logic is attempting to sort the array and has logical issues with no return defined if size > 1.

If the purpose is to return the minimum value, a simpler logic can be as follows.

int ArrayMinimum(int a[], size_t size)
{
    int min = a[0];
    if (size == 1)
    {
        return a[0];
    }


    for (int i = 1; i < size ; i++)
    {
        if (a[i] < min)
        {
            min = a[i];
        }
    }
    return min;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.