Question #64700

How can i get the 3 least minimum data from an array set and display the answers with the corresponding months of those data.

Expert's answer

#include <stdio.h>
void getMinimums(int set[12], size_t *i1, size_t *i2, size_t *i3);
void swap(size_t *a, size_t *b);
char *getMonth(char *months[13], size_t i);
int main(void) {
    int set[12] = {1, -12, -2, 3, -4, 5, 0, -67, 78, -89, 90, 10};
    char *months[13] = {
        "",
        "January", "February", "March", "April",
        "May", "June", "July", "August",
        "September", "October", "November", "December"
    };
    size_t i1, i2, i3;
    getMinimums(set, &i1, &i2, &i3);
    printf("%s: %d, %s: %d, %s: %d\n",
        getMonth(months, i1 + 1), set[i1],
        getMonth(months, i2 + 1), set[i2],
        getMonth(months, i3 + 1), set[i3]);
    return 0;
}
void getMinimums(int set[12], size_t *i1, size_t *i2, size_t *i3) {
    size_t m1, m2, m3;
    size_t i;
    m1 = 0; m2 = 1; m3 = 2;
    /* printf("%d, %d, %d\n", set[m1], set[m2], set[m3]); */
    if (set[m2] < set[m1])
        swap(&m2, &m1);
    /* printf("%d, %d, %d\n", set[m1], set[m2], set[m3]); */
    if (set[m3] < set[m2])
        swap(&m3, &m2);
    /* printf("%d, %d, %d\n", set[m1], set[m2], set[m3]); */
    if (set[m2] < set[m1])
        swap(&m2, &m1);
    /* printf("%d, %d, %d\n", set[m1], set[m2], set[m3]); */
    for (i = 3; i < 12; i++) {
        if (set[i] < set[m1]) {
            m3 = m2; m2 = m1; m1 = i;
        } else if (set[i] < set[m2]) {
            m3 = m2; m2 = i;
        } else if (set[i] < set[m3]) {
            m3 = i;
        } else if (set[i] == set[m1]) {
            continue;
        } else if (set[i] == set[m2]) {
            continue;
        } else if (set[i] == set[m3]) {
            continue;
        }
        /* printf("%d, %d, %d\n", set[m1], set[m2], set[m3]); */
    }
    *i1 = m1; *i2 = m2; *i3 = m3;
}
void swap(size_t *a, size_t *b) {
    size_t t;
    t = *a; *a = *b; *b = t;
}
char *getMonth(char *months[13], size_t i) {
    return (i > 0 && i < 13) ? months[i] : months[0];
}


Answer provided by AssignmentExpert.com

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS