-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?

surajs1n
  • 1,493
  • 6
  • 23
  • 34
Keaire
  • 879
  • 1
  • 11
  • 30
  • 3
    Theres no way you actually searched. This is an incredibly common beginners exercise – user1231232141214124 Dec 18 '15 at 17:49
  • I spent 10 seconds to search around and found this: http://www.programiz.com/c-programming/examples/array-largest-element – vk239 Dec 18 '15 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. – haccks Dec 18 '15 at 17:56
  • 1
    Guys lets remember that noobs have not been here forever to know every duplicate question and its link. – ForeverStudent Dec 18 '15 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. – mah Dec 18 '15 at 18:53
  • Are you want to find the smallest or the largest ? Your title says __Largest__ while the comment in the code says __Smallest__ ? – ThisaruG Dec 18 '15 at 19:40
  • Possible duplicate of [Finding smallest value in an array most efficiently](http://stackoverflow.com/questions/1042507/finding-smallest-value-in-an-array-most-efficiently) – surajs1n Dec 20 '15 at 07:19

3 Answers3

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.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    @redFIVE; Because he just needed a small push instead of copying the code from the internet as I guess. – haccks Dec 18 '15 at 17:54
  • 2
    ...until he comes back and asks the next ridiculously basic question without bothering to do any research himself. Cool. – user1231232141214124 Dec 18 '15 at 17:55
  • 2
    @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. – Taelsin Dec 18 '15 at 17:59
  • I agree with @redFIVE here. Of course, Stack Overflow is very democratic and so it really doesn't matter what any one (or two) people think, and that's a good thing. Personally, I would rather spend much more time debating why questions like this should not be answered though, than I would spend answering them. It's fine that others feel differently, but I am not willing to help someone that's unwilling to take basic steps to try to help themselves before asking others to go out of their way. – mah Dec 18 '15 at 18:05
  • 1
    @mah; He is asking for algorithm not for code. There are plenty of questions here with good upvotes. – haccks Dec 18 '15 at 18:06
  • 2
    @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. – Taelsin Dec 18 '15 at 18:09
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;

ForeverStudent
  • 2,487
  • 1
  • 14
  • 33
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.

Simon
  • 19
  • 4