Thread: Failure to determine array's maximum

  1. #1
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31

    Failure to determine array's maximum

    Code:
    #include <stdio.h>
    #define N 6
    int main(void){
        int i,k,v[N];
        printf("Input the vector:\n");
        for(i=0;i<N;i++){
                         printf("Element %d:",i+1);
                         scanf("%d",&v[i]);}
                         printf("\n");
        maxpos(v[N]);
        return 0;
    }
    
    int maxpos(int vec[]){
        int k,max,pos;
        max=0;
        for(k=0;k<N;k++){
                         if(vec[k]>vec[k+1]){
                                             max=vec[k];
                                             pos=k;}
                         }
        printf("\n");
        printf("Maximum:%d;\nPos:%d;",max,pos);
        system("pause");
    }

    When I run this code, regardless of the vector I input, the outcome is invariably the same: Maximum:1 Pos:4

    I've check the code up and down but I can't find anything wrong with it.
    Can anyone tell me what I'm doing wrong here?

    Appreciate.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
                         if(vec[k]>vec[k+1]){
    loop condition is k < N, so if k is N-1, k + 1 is N which is out of bound for array vec.since valid indices [0 to N-1] inclusive

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by KAUFMANN View Post
    Code:
    #include <stdio.h>
    #define N 6
    int main(void){
        int i,k,v[N];
        printf("Input the vector:\n");
        for(i=0;i<N;i++){
                         printf("Element %d:",i+1);
                         scanf("%d",&v[i]);}
                         printf("\n");
        maxpos(v[N]);
        return 0;
    }
    
    int maxpos(int vec[]){
        int k,max,pos;
        max=0;
        for(k=0;k<N;k++){
                         if(vec[k]>vec[k+1]){
                                             max=vec[k];
                                             pos=k;}
                         }
        printf("\n");
        printf("Maximum:%d;\nPos:%d;",max,pos);
        system("pause");
    }
    When I run this code, regardless of the vector I input, the outcome is invariably the same: Maximum:1 Pos:4

    I've check the code up and down but I can't find anything wrong with it.
    Can anyone tell me what I'm doing wrong here?

    Appreciate.
    1) Set 'max' to the first element in the array.
    2) Compare 'vec[k]' with 'max', not 'vec[k+1]'.
    3) FYI, accessing 'vec[k+1]' on the last iteration will necessarily put you past the bounds of the array...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31
    Quote Originally Posted by Sebastiani View Post
    1) Set 'max' to the first element in the array.
    2) Compare 'vec[k]' with 'max', not 'vec[k+1]'.
    3) FYI, accessing 'vec[k+1]' on the last iteration will necessarily put you past the bounds of the array...
    Very well. I read what you suggested and I agree. However, with the code now corrected in that way, the very same error pops up: Max:1 Pos:4
    There's something more to it :|

    Code:
    #include <stdio.h>
    #define N 6
    int main(void){
        int i,k,v[N];
        printf("Input the vector:\n");
        for(i=0;i<N;i++){
                         printf("Element %d:",i+1);
                         scanf("%d",&v[i]);}
                         printf("\n");
        maxpos(v[N]);
        return 0;
    }
    
    int maxpos(int vec[]){
        int k,max,pos;
        max=vec[0];
        for(k=0;k<N;k++){
                         if(vec[k]>max && vec[k]<N){
                                             max=vec[k];
                                             pos=k;}
                         }
        printf("\n");
        printf("Maximum:%d;\nPos:%d;",max,pos);
        system("pause");
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
        for(k=0;k<N;k++){
           if(vec[k]>max && vec[k]<N){
               max=vec[k];
               pos=k;} }
    See the part I boldfaced for you?
    Why do you care about that?

  6. #6
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31
    Quote Originally Posted by CommonTater View Post
    Code:
        for(k=0;k<N;k++){
           if(vec[k]>max && vec[k]<N){
               max=vec[k];
               pos=k;} }
    See the part I boldfaced for you?
    Why do you care about that?
    I see. It's not necessary since that restriction is already defined within the 'for' loop. I've removed it now, and it still doesn't output what it should. Maybe there's some screw up in the manner in which I wrote the function? Could that be?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by KAUFMANN View Post
    I see. It's not necessary since that restriction is already defined within the 'for' loop. I've removed it now, and it still doesn't output what it should. Maybe there's some screw up in the manner in which I wrote the function? Could that be?
    Actually, that's not guaranteed by the for loop. You don't care if an element (vec[k]) is less than N, only that the index (k) is less than N.

    Your maxpos function should have a return type void since you don't actually return anything.

    Also, don't pass in v[N] to maxpos. First, your maxpos function expects an array of ints, so pass in just plain old v. Also, Bayint Naung already mentioned, 0 to N-1 are the only valid indexes. v[N] is trying to access the 7th element of a 6-element array, which is out of bounds and produces undefined behavior.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, 'pos' is uninitialized...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grading Program using multidimensional arrays
    By HBlakeH in forum C Programming
    Replies: 15
    Last Post: 11-18-2010, 12:37 AM
  2. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  3. maximum number
    By drdodirty2002 in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 05:33 PM
  4. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM