0

I sorta need help getting the minimum I keep getting thirteen can some one help me out? The issue I believe is I'm not showing the formula for low n line I'm confused I have tried to switch out the values for the array and I can't figure it out just if someone could explain to m please.

#include <iostream>
using namespace std;

int getHighest(int numArray[], int numElements);
int getLowest(int numArray[], int numelements);

int main()
{
    int numbers[4] = { 13, 2, 40, 25 };

    cout << "The highest number in the array is " << getHighest(numbers, 4) << "." << endl;
    cout << "The lowest number in the array is "<< getLowest(numbers,0) << "." << endl;
    return 0;
}
int getHighest(int numArray[], int numElements)
{
    int high = numArray[0];

    for (int sub = 1; sub < numElements; sub += 1)
        if (numArray[sub] > high)
            high = numArray[sub];
       
    return high;
}
int getLowest(int numArray[], int numElements)
{
    int low = numArray[0];
    for (int sub = 0; sub >= numElements; sub--)
        if (numArray[sub]< low)
            low = numArray[sub];
    return low;
}
6
  • for(int sub=0; sub >= numElements; sub--) What would sub be in each iteration? with numElements being 0 your for loop only runs once. Hence, the output 13. Commented Oct 16, 2021 at 7:04
  • 1
    Yet another bug: getLowest(numbers,0). The 2nd parameter provides the number of elements (of the array in the 1st parameter). That's in your case 4 as you did in getHighest(numbers, 4). Commented Oct 16, 2021 at 7:06
  • hey Chester that's the part I'm confused about i get what your saying just don't fully understand? Commented Oct 16, 2021 at 7:06
  • If you use sub-- you DEcrement your loop index. (sub-- could be written as sub -= 1 or sub = sub - 1 as well.) Hence, you have to start with the highest possible index, and you have to check whether index is still >= 0 in the loop condition. Sloppy spoken, you go backwards through your array. So, you have to start at the end of it. Commented Oct 16, 2021 at 7:09
  • Btw. if you have a decrementing loop then be careful with the start index. If an array has n elements the last element has index n - 1. Commented Oct 16, 2021 at 7:11

2 Answers 2

1

Concerning getLowest():

There is actually no need to iterate backwards. It could be done like in getHighest(). However, say this is a requirement for teaching…

The test array is

int numbers[4] = { 13, 2, 40, 25 };
// indices:        0   1  2   3
// number of elements: 4

A loop to iterate backwards has to start with index numElements - 1 (3 in this case) and to stop at index 0.

for (int sub = numElements - 1; sub >= 0; sub--)

Nevertheless, this will check the last element which is already assigned before the loop. (getHighest() starts the loop with the 2nd element for this reason: for (int sub = 1;…) Thus, this can be corrected to:

for (int sub = numElements - 2; sub >= 0; sub--)
Sign up to request clarification or add additional context in comments.

Comments

0

This is the corrected example:

int getLowest(int numArray[], int numElements)
{
    int low = numArray[0];
    for (int sub = 1; sub < numElements; ++sub)
        {
        //std::cout<<"checking: "<<numArray[sub]<<"with"<<low<<std::endl;
        if (numArray[sub]< low){
            low = numArray[sub];
        }
            
        }
    return low;
}

The complete working example is here

Also note in your given example you have made a mistake at:

cout << "The lowest number in the array is "<< getLowest(numbers,0) << "." << endl;

Instead of passing 0 as the second argument you should pass 4 as i did here.

Another mistake was the initial value of varaible sub in the for loop. You started with sub = 0 instead of sub = numelements - 1. That is the for loop should have looked like:

//note in the next line we have sub >=1 instead of sub>=0 becuase you have already stored numArray[0] in variable low
for (int sub = numElements -1; sub >=1; --sub)
{
    ...other code here
}

2 Comments

@anoop Rana thank you that makes sense why did you put the ++ before the sub in that ling you corrected? thank you again for your help.
@ColtonGil The ++ in ++i is called pre-increment operator. A pre-increment operator is used to increment the value of a variable by 1. So for example if we have int x = 10; and then we write ++x; this means value of variable x will be incremented by 1. You can read more about it here . You can also use sub +=1 in the for loop instead of ++sub. The result will be same.

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.