-8

I've searched around, but I haven't found anything about my question.

I want create an algorithm in C that finds the smallest number in an array that can hold 10 values.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(void) {
    int array[10];
    int i, smaller;

    srand(time(NULL));

    for (i = 0; i < 10; i++) {
        array[i] = rand() % 100 + 1;
    }

    // Find smaller number in the array
    for (i = 0; i < 10; i++) {
        ...
    }

    printf("Smaller: %d\n", smaller);
    return 0;
}

Any tips on how can I do?

7
  • 3
    Theres no way you actually searched. This is an incredibly common beginners exercise Commented Dec 18, 2015 at 17:49
  • I spent 10 seconds to search around and found this: programiz.com/c-programming/examples/array-largest-element Commented Dec 18, 2015 at 17:50
  • 3
    @vk239; It is easy to search on Google instead of solving by himself. I don't think it should be downvoted so badly. At least he tried something. And he is asking for algorithm not for code. Commented Dec 18, 2015 at 17:56
  • 1
    Guys lets remember that noobs have not been here forever to know every duplicate question and its link. Commented Dec 18, 2015 at 18:21
  • 1
    @IIIIIIIIIIIIIIIIIIIIIIII That doesn't preclude them from either using the search function, or simply reading the links that pop up while you're composing your question. I started a new question, put in only the title from this question, and was presented with a number of possible duplicates. I pasted the content of this question into my new question and was given additional similar questions. Not all of these are relevant but many of them are germane to the topic. Commented Dec 18, 2015 at 18:53

3 Answers 3

7

Assign first element of array to smaller

smaller = array[0];

Start a loop with i = 1 and compare elements with smaller. If smaller is greater than any of array element then replace it with that elment otherwise left it as it.

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

6 Comments

@redFIVE; Because he just needed a small push instead of copying the code from the internet as I guess.
...until he comes back and asks the next ridiculously basic question without bothering to do any research himself. Cool.
@redFIVE You're spending more effort complaining about this than just answering the question would take. I realize this is technically against the rules, but this is a site to help people. If you don't like questions like this just move on. Ignore them.
@mah; He is asking for algorithm not for code. There are plenty of questions here with good upvotes.
@mah That's a perfectly valid opinion. I'm just making the point that redFive should have just ignored the question. No one here is forced to answer questions they don't want to.
|
2

Since your array is such a small size and unsorted, you can do a simple O(n) linear search like this:

int main(void) 
{
    int array[10];

    srand(time(NULL));

    int i;
    for (i = 0; i < 10; i++)array[i] = rand() % 100 + 1;

    int smallestSoFar=array[0];

    for (i = 1; i < 10; i++)  if(smallestSoFar>array[i]) smallestSoFar=array[i];


    printf("Smallest value: %d\n", smallestSoFar);
    return 0;
}

What is happening is you assume that the first element in the array is indeed the smallest. Then you iterate through the entire array one by one, and change your mind if you see a smaller value;

Comments

1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
    follow good practice and never hardcode an array
    use symbolic names instead that way if you have to
    increase or decrease the size of the array you only
    have to change the value here
*/
#define NO_OF_ELEMENTS 10

int main(void)
{
    // declare and initialize all elements to 0
    int array[NO_OF_ELEMENTS] = {0};
    int smallest = 0, largest = 0, i = 0;

    srand(time(NULL));

    for(i = 0; i < NO_OF_ELEMENTS; i++)
    {
        array[i] = (rand() % 100) + 1;

        // Compare against each element as you go to find the largest
        if(largest < array[i])
        {
            largest = array[i];
        }

        printf("\nElement %d: %d", i, array[i]);
    }

    // assume smallest element is in the first position
    smallest = array[0];

    for(i = 0; i < NO_OF_ELEMENTS; i++)
    {
        if(smallest > array[i])
        {
            smallest = array[i];
        }
    }

    printf("\n\nSmallest element in array is: %d", smallest);
    printf("\nLargest element in array is: %d", largest);

    getchar();

    return 0;
}

That program should help you out it will return the largest and smallest value in the array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.