The index for the maximum

Category: C/C++ -> Beginner Author: guoer0916 Date: 2014-12-14 10:24:00
 
guoer0916
2014-12-14 10:24:00
Write a maximum code: But how to display the maximum value of the index it?
#include<stdio.h>
int max(int *,int n);
int main(void)
{
    int ar[10];
    int i;
    printf("please enter some numbers:\n");

    for(i=0;i<10;i++)
        scanf("%d",&ar[i]);
    printf("The max is:%d\n",max(ar,10));

    return 0;
}

int max(int ar[],int n)
{
    int i;
    int m;
    for(i=0;i<n-1;i++)
    {
        if(ar[i]>ar[i+1])
            m=ar[i];
        else
            m=ar[i+1];
    }
    return m;
}
caiweit
2014-12-14 10:31:59
you always get the previous element in the array and post a comparison, but the problem is they are not necessarily the biggest two ah! Want to find the biggest one, you should find out the maximum value currently not compare with the next comparison, the largest of which one will be saved for the next round of contrast.
tongmaosong
2014-12-14 10:51:30
#include<stdio.h>
int max(int *,int n);
int main(void)
{
    int ar[10];
    int i, indx;
    int p;
    printf("please enter some numbers:\n");

    for(i=0;i<10;i++)
        scanf("%d",&ar[i]);
    p = max( ar, 10, &indx );
    printf("The max is:%d, the index is %d \n",p, indx );

    return 0;
}

int max(int ar[],int n, int* indx)
{
    int i;
    int m;
    for(i=0;i<n-1;i++)
    {
        if(ar[i]>ar[i+1])
        {
            m=ar[i];
            *indx = i;
        }
        else
            m=ar[i+1];
    }
    return m;
}
book_goldenrain
2014-12-14 10:58:52
There is a place:
int max(int ar[],int n, int* indx)
{
    int i;
    int m;
    for(i=0;i<n-1;i++)
    {
        if(ar[i]>ar[i+1])
        {
            m=ar[i];
            *indx = i;
        }
        else
        {
             m=ar[i+1];
             *indx = i + 1;
         }
    }
    return m;
}
jjtoma
2014-12-14 11:07:16
Input: 5 4 3 2 1 4 3 2 1 0
output: 1
......

your program is wrong
kai2004521
2014-12-14 11:12:43
landlord but not to the output index, the first check for the maximum of that for loop algorithm, there is a problem.
snowdolly
2014-12-14 11:19:53
Example:
#include<stdio.h>

int max(int ar[], int n, int *p);

int main(void)
{
    int ar[10];
    int i,p,maxv;
    printf("please enter some numbers:\n");

    for(i=0;i<10;i++)
        scanf("%d",&ar[i]);
    maxv=max(ar,10,&p);
    printf("The max is:%d, :%d\n",maxv,p);

    return 0;
}

int max(int ar[], int n, int *p)
{
    int i;
    int m=-2000000000;
    for(i=0;i<n;i++)
    {
        if(ar[i]>m)
        {
            m=ar[i];
            *p=i;
        }
    }
    return m;
}
uvwmv6727
2014-12-14 11:22:54
just checked all right Yeah. . . Collapsed. . . What's wrong ah. .
zhangyinke
2014-12-14 11:33:44

#include<stdio.h>

int max(int *, int n, int *);

int main(void)
{
int ar[10] = {0};
int i, m = 0, index = 0;

printf("please enter some numbers:\n");

for(i = 0; i < 10; i++)
scanf("%d", &ar[i]);

m = max(ar, 10, &index);

printf("The max is: %d. index = %d.\n", m, index);

return 0;
}

int max(int ar[], int n, int *p)
{
int i;
int m = ar[0];

for(i = 1; i < n-1; i++)
{
if(m < ar[i])
{
m = ar[i];
*p = i;
}
}

return m;
}
kk475125652
2014-12-14 11:38:57
int max(int ar[], int n, int *p)
{
    int i;
    int m=-2000000000;
    for(i=0;i<n;i++)
    {
        if(ar[i]>m)
        {
            m=ar[i];
            *p=i;
        }
    }
    return m;
}

Brother, in accordance with my thoughts ar [i]> ar [i +1]; such ideas can not I? Why not ah?
* p, p is the address I always thought ah. . . Why display the index ah. .
weixibaba
2014-12-14 11:57:48
Scratching an arrested heart, the questions are not so simple to do, blame uncomfortable
xtwbt812008
2014-12-14 12:06:28

head. . Thinking wrong. . Studied. . Thank