For-loop for finding numbers in an array

Hello,

I´m writing a program where I have an array with numbers and I need to find the smallest an the biggest number in the array. The teacher made the example here below and I need to finish the functions below with a for- loop but I dont know how, can someone help me?

the code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int find_smallest_number(int number[], int n);
int find_biggest_number(int number[], int n);

int main()

{

int smallest_number = find_smallest_number(number, n);

int biggest_number = find_biggest_number(number, n);

    for (int a = smallest_number; a <= biggest_number; a++) {

        cout << a << ": " << number_of_people(department, number);

    }

   return 0;

}



int find_smallest_number(int number[], int n){
//for loop
// what to do?
}

int find_biggest_number(int number[], int n){
//for loop
// what to do?
}


Last edited on
I'll give you pseudocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int find_smallestNumber( int number[] , int n )
{
    int smallestNumber;

    // Assume that the first element is the smallest number
    Assign the value of the first element, number[0] to smallestNumber

    // Using for loop, iterate through the entire array and find any number that is smaller than
    // current smallestNumber
    for( From int index = 1 to and not including n )
        if number[index] is smaller than smallestNumber
            Assign number[index] to smallestNumber

    // After the for-loop exits, smallestNumber variable will have the smallest element
    // from the array
    return smallestNumber;
}


Use the same logic for finding the biggest number.

Try it and let me know if you need further help.
Last edited on
Topic archived. No new replies allowed.