Kryssel Tillada
Kryssel Tillada

Reputation: 180

finding the maximum value of an array its location

#include <iostream>

using namespace std;

int main()
{
    const int MAXNUM = 10; // this code creates 10 variables in fmax
    int fmax[MAXNUM], maximum, i, l; // initialize fmax that contains 10 variables , maximum, i for the loop, and l for storing the location

    cout << "enter 10 numbers: ";

    maximum = fmax[0]; // this sets the maximum to 0

    for(i = 0; i < MAXNUM; i++) // this is the code for finding the maximum numbers
    {
        cin >> fmax[i];
        if(fmax[i] > maximum){
            maximum = fmax[i];
            l = i;
        }
        else{
            maximum = maximum;
            l = l;
        }
    }

    cout << "the maximum number: " << maximum << endl; // outputs the results
    cout << "the location of the number: " << l << endl;

    return 0;
}

I have a problem on this exercise the problem is the output of the program. it doesn't displays the maximum number and where it is located it always shows like this

the maximum number is: 1987579782

the location of the number is: 26355764

I need to display the maximum number entered and its subscript I don't know how there's something wrong with my code

here is the exercise problem

a.write, compile and run a c++ program to input 10 integer number into an array named fmax and determine the maximum value entered your program should contain only one loop and the maximum should determined as array element values are being input (hint. set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.) and keeping track both the maximum element in the array and the index number for the maximum.

Upvotes: 0

Views: 2768

Answers (4)

Minar Mahmud
Minar Mahmud

Reputation: 2665

maxinum = fmax[0]; // this sets the maxinum to 0

This does not set maxinum to 0 as you havent set fmax[0] to 0.

You can do this:

#include <iostream>

using namespace std;

int main()
{
    const int MAXNUM = 10; // this code creates 10 variables in fmax
    int fmax[MAXNUM], maxinum, i, l; // initialize fmax that contains 10 variables , maxinum, i for the loop, and l for storing the location
    cout << "enter 10 numbers: ";
    for(i = 0; i < MAXNUM; i++) // this is the code for finding the maxinum numbers
    {
        cin >> fmax[i];
        if(i==0)
            maxinum = fmax[i];  //....... this will do what you are trying to achieve
        if(fmax[i] > maxinum){
            maxinum = fmax[i];
            l = i;
        }                      // the else block you wrote is not necessary :)
    }
    cout << "the maxinum number: " << maxinum << endl; // outputs the results
    cout << "the location of the number: " << l << endl;
    return 0;
}

Upvotes: 0

brettwhiteman
brettwhiteman

Reputation: 4452

You need to initialize the fmax[MAXNUM] array before you use it. Simply declaring a variable does not guarantee that it will be 0. At the moment the value of fmax[0] could be anything within the range of int as you have not initialized it to a value.

You also need to initialize the l variable to 0 (this is why you are getting the wrong location)

Try this:

#include <iostream>

using namespace std;

int main()
{
 const int MAXNUM = 10; // this code creates 10 variables in fmax
int fmax[MAXNUM], maxinum, i, l = 0; // initialize fmax that contains 10 variables , maxinum, i for the loop, and l for storing the location

for(i = 0; i < MAXNUM; ++i)
{
    fmax[i] = 0;
}

cout << "enter 10 numbers: ";

maxinum = fmax[0]; // this sets the maxinum to 0

for(i = 0; i < MAXNUM; i++) // this is the code for finding the maxinum numbers
{
    cin >> fmax[i];
    if(fmax[i] > maxinum){
        maxinum = fmax[i];
        l = i;
    }
    else{
        maxinum = maxinum;
        l = l;
    }
}

cout << "the maxinum number: " << maxinum << endl; // outputs the results
cout << "the location of the number: " << l << endl;

return 0;

}

EDIT: Since it is a requirement that the code should only contain one loop, you might want to change the way you are doing this. The example I provided above is not a very elegant way to do it either. This is how I would do it (without using any potentially confusing C++ Standard Library functions)

#include <iostream>

int main()
{
    std::cout << "Enter 10 numbers: ";
    const int MAXNUM = 10;
    int fmax[MAXNUM] = {0}; // this is an easy way to initialize the array elements to zero
    int maxNum = 0, location;

    for(int i = 0; i < MAXNUM; i++) {
        std::cin >> fmax[i];
        if(fmax[i] > maxNum) {
            maxNum = fmax[i];
            location = i;
        }
    }

    std::cout << "The maximum number is " << maxNum << std::endl;
    std::cout << "The location of the number is " << location << std::endl;

    return 0;
}

Make sure you understand why this works - let me know if you have any questions.

Upvotes: 4

jepio
jepio

Reputation: 2281

Local variables (non-class) are not initialized to zero by default in C++. Your problem is that you initialize maxinum with the value of fmax[0] which in the beginning is garbage. If you then never enter any bigger number, the value of I is never changed and is also garbage. You need to explicitly initialize those variables to zero:

int fmax[MAXNUM] = { 0 };
into maxinum = 0, I = 0

Upvotes: 1

awesoon
awesoon

Reputation: 33701

You are initializing maximum with garbage value:

maxinum = fmax[0]; // this sets the maxinum to 0

Since you have not entered any elements yet.

I would suggest you use built-in function std::max_element from algorithm library: it will return pointer to the max element, so, you could output position of max element along with it's value:

#include <algorithm>

// Your code
// You should enter the whole array

auto max_element = std::max_element(std::begin(fmax), std::end(fmax));
std::cout << "Position: " << (max_element - std::begin(fmax)) << std::end;
std::cout << "Value: " << *max_element << std::endl;

Upvotes: 1

Related Questions