-7

I am trying to get the greatest value from the array and its index number also by using a function maxin but my logic somehow isn't working?

#include <iostream>
#include <conio.h>
#include <proceass.h>

void maxin(double[], int);

void main()
{
    const int k = 10;
    int l = 0;
    double num[k];
    for (int j = 0; j < k; j++)
    {
        cout << "Enter the number " << j + 1 << " = ";
        cin >> num[j];
        if (cin.fail())
        {
            cout << "Wrong data entered " << "\nTry       again";
            getch();
            exit(0);
        }
    }
    maxin(num, l);
    cout << "The Greatest number is = " << num;
    cout << "\nIt is " << l << "th   number";
    getch();
}

void maxin(double k[], int p)
{
    int l, s;
    l = 10;
    s = 0;
    double m;
    for (int n = 0; n < l; n++)
    {
        if (k[s] > k[n++])
        {

            m = k[n];
        }
        else
        {
            m = k[n++];
            s = ++;
        }
    }
    p = s;
    k[s] = m;
}
David G
  • 94,763
  • 41
  • 167
  • 253
  • 1
    You are doing several things wrong. Try something simpler. – Beta Jan 01 '14 at 04:11
  • can u explain a bit more? – shujah kiani Jan 01 '14 at 04:13
  • 2
    You're trying to iterate over an array, get something back from a function and print out a result, and you're doing all of these things wrong. You must try new things *one at a time*, and master them separately, before you try to combine them. – Beta Jan 01 '14 at 04:20
  • Why are you using a non-standard C header (`conio.h`) in a C++ program? – JBentley Jan 01 '14 at 06:06

2 Answers2

2

Your maxin function is invoking Undefined Behavior on your program for causing access to areas beyond the bounds of the array k. This happens because not only is n incremented in the for loop statement, but again in the if statement which is evaluated on each iteration as well. This also happens in the else statement, which is another case of the problem.

When n is 1 less than l, n++ will be >= l, and subsequently dereferencing that address, k[n++], will cause Undefined Behavior. After that, anything can happen to your program, including valid or invalid side effects.

When finding the maximum/minimum value in an array, a variable is usually set to an arbitrary value in the array (typically the first index), and then iteration is performed to check if any other value in the array is smaller/larger than that variable. When that condition passes, the variable is set to the new value in the array.

Furthermore, since you said you needed to set the variable to the index at which the largest value was found, it is necessary that you pass p by reference.

Community
  • 1
  • 1
David G
  • 94,763
  • 41
  • 167
  • 253
  • i understand what ur saying but can u give me a solution for my function? – shujah kiani Jan 01 '14 at 04:36
  • The best solution is to consider how you would do it with paper and pencil, then start over, this time writing C++ rather than a made up language that works as you imagine C++ might. – DavidO Jan 01 '14 at 04:38
  • @DavidO Are you speaking to me or to the OP? :) – David G Jan 01 '14 at 04:41
  • The OP... there's too much wrong with his original code for there to be an expectation that someone use it as the basis for providing a correct solution. – DavidO Jan 01 '14 at 04:42
  • What's really needed is a chapter or two from a good book, and/or a one on one consultation with the professor. – DavidO Jan 01 '14 at 04:44
  • @shujahkiani I already gave you the solution in my answer... – David G Jan 01 '14 at 04:47
2

The STL approach:

vector< double > v = {1,2,3,4,5};

auto maxElemIter = std::max_element(begin(v), end(v));

cout << "Max is: " << *maxElemIter;

cout << ", at index: " << distance(begin(v), maxElemIter) << endl;

(I know, this is a cruel suggestion, given code as stated in question above...)

David G
  • 94,763
  • 41
  • 167
  • 253
David Neiss
  • 8,161
  • 2
  • 20
  • 21