dimanche 26 mai 2019

Printing last element in an array using if statement

I am currently trying to learn about using pointers and functions together in C, which I don't think is easy. If you take a look at my code where I am trying to print the last element in an array, it actually does the opposite and prints the first element. I know people normally use for loops, but I can't figure out how to do that with exactly this kind of problem and therefore I thought that I would try it out with an if statement instead.

My main.c file:

#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
#define Size 7

int main(void) 
{

    int array1[] = { 11, 88, 5, 9, 447, 8, 68, 4 };

    maxValue(array1, Size);

    return 0;
}

My functions.h file:

#pragma once

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

int maxValue(const int *, int);

#endif


My functions.c file:


#include "functions.h"
#include <stdio.h>
#include <stdlib.h>




int maxValue(const int *array1, int Size)
{

    int max = array1[0];
    if (max < array1[Size]) {
        Size++;
        max = array1[Size];
    }

    printf("Max value: %d \n", max);
}



Aucun commentaire:

Enregistrer un commentaire