Thread: pointers exercise

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    6

    Post pointers exercise

    Have to make a program that given an array of 7 integer from input, applies a function like this one:
    void firstlasteven(int arr[], int dim, int* firstocc, int* lastocc);
    that returns the position of the first and last even number or "-1" if there's none.
    example input/output:

    3 1 5
    6
    4
    8
    8
    -8
    1

    This is my code so far:
    Code:
    #include <stdio.h>
    
    
    
    
    int *primoultimopari(int arr[], int dim, int* primaocc, int* ultimaocc){
        int i;
        int s=0;
        for(i=0;i<dim;i++){
            if((arr[i]%2)==0){
                if(s==0){
                    *primaocc=i;
                    s++;
                }
                else{
                    *ultimaocc=i;
                }
            }
        }
    }
    
    
    int main(void){
        int dimensione=7;
        int i;
        int A[7];
        for(i=0;i<7;i++){
            scanf("%d", &A[i]);
        }
        int prima,ultima;
        primoultimopari(A[7], dimensione, &prima, &ultima);
        printf("%d %d", prima,ultima);
        return 0;
    }
    (prima is first, ultima is last.)

    Any help would be much appreciated.
    These are the errors i get:

    Code:
    Lez6Es4c72.c:28:21: warning: passing argument 1 of ‘primoultimopari’ makes pointer from integer without a cast [-Wint-conversion]
         primoultimopari(A[7], dimensione, &prima, &ultima);
    
    Lez6Es4c72.c:4:6: note: expected ‘int *’ but argument is of type ‘int’
     int *primoultimopari(int arr[], int dim, int* primaocc, int* ultimaocc){
          ^~~~~~~~~~~~~~~

  2. #2
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    303

    Post

    this might help...

    Code:
    #include <stdio.h>
    
    void primoultimopari(char list[], int dimensione, int *prima, int *ultima) {
         *prima = -1; 
        *ultima = -1;
        for (int i=0; i<dimensione; i++) {
            if ( list[i] % 2 ) {
                if (*prima > 0) *ultima = i;
                if (*prima < 0) *prima = i;            
            }
        }
    }
    
    int main(void){
        int dimensione = 7;
        int prima, ultima;
        char list[dimensione];
    
        primoultimopari( list, dimensione, &prima, &ultima );
    
        printf( "%i %i", prima, ultima );
        return 0;
    }
    Last edited by Structure; 09-11-2020 at 11:55 AM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exercise with pointers in C
    By Luca43 in forum C Programming
    Replies: 1
    Last Post: 11-10-2016, 10:23 AM
  2. K&R Exercise 1-16 Help
    By JordanWatson in forum C Programming
    Replies: 11
    Last Post: 02-25-2015, 06:31 AM
  3. exercise help
    By ali.franco95 in forum C Programming
    Replies: 2
    Last Post: 09-24-2011, 11:02 AM
  4. exercise in c
    By hugoguan in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 04:32 AM
  5. Exercise 2-6 K&R help
    By allix in forum C Programming
    Replies: 19
    Last Post: 08-18-2006, 09:25 AM

Tags for this Thread