-2

I wrote this code that works with array and it is divided into two files one that contains functions and another that contains main. But when I compile it, it gave me an output error as following. Do you have any advice on how can I fix this?

functions.c:

#include <string.h> 
#include <stdio.h>

void return_array(int a[], int n) {
    int c, d, min, max, location = 1;
    int b[n];
    
    for (c = n - 1, d = 0; c >= 0; c--, d++)
        b[d] = a[c];
    
    for (c = 0; c < n; c++) {
        a[c] = b[c];
        
    }
    printf("Array after reverse of elements:\n");
    
    for (c = 0; c < n; c++) {
        printf("%d\n", a[c]);
    }
    return;
}

void return_min_max(int a[], int n, int * min, int * max) {
    for (int i = 1; i < n; i++) {
        if (a[i] > * max) {
            * max = a[i];
        }
        
        if (a[i] < * min) {
            * min = a[i];
        }
    }
    printf("Smallest element in array is: %d\n", * min);
    printf("Largest element in array is: %d\n", * max);
    return;
}

Errors:

       6: error: multiple definition of `return_array'
       29: error: multiple definition of `return_min_max'
      :-1: error: collect2.exe: error: ld returned 1 exit status
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Leviathan
  • 5
  • 1
  • 7

1 Answers1

2

Couple of things wrong with what you posted, to begin with, you shouldn't include .c files. Instead, create a module, place the definitions in a header file, and the implementation in a .c file.

Now, in your code, you have int a[n] in the first line of your main() function. What value do you expect n to have? n is uninitialized , thus the value of n is indeterminate, which means you can't know what the value will be. In your case, I had a segmentation fault while running this, but the result may be different in different computers.

This should be what you're looking for:

functions.h:

#ifndef FUNCTIONS_H /* include guard */
#define FUNCTIONS_H

void return_array(int a[], int n);

void return_min_max(int a[], int n, int *min, int *max);

#endif

functions.c:

#include "functions.h" /* include your header with the declarations */
#include <stdio.h>

void return_array(int a[], int n)
{
    int c, d;
    int b[n];

    for (c = n - 1, d = 0; c >= 0; c--, d++)
    {
        b[d] = a[c];
    }

    for (c = 0; c < n; c++)
    {
        a[c] = b[c];
    }
    printf("Array after reverse of elements:\n");

    for (c = 0; c < n; c++)
    {
        printf("%d\n", a[c]);
    }
    return;
}

void return_min_max(int a[], int n, int *min, int *max)
{
    for (int i = 1; i < n; i++)
    {
        if (a[i] > *max)
        {
            *max = a[i];
        }

        if (a[i] < *min)
        {
            *min = a[i];
        }
    }
    printf("Smallest element in array is: %d\n", *min);
    printf("Largest element in array is: %d\n", *max);

    return;
}

main.c:

#include "functions.h" /* note the double "" */
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, c, min, max;

    printf("Enter number of elements to array\n");
    if (scanf("%d", &n) != 1 || n <= 0) /* check return value of scanf() for input errors */
    {                                   /* also make sure n is greater than 0 */
        exit(EXIT_FAILURE);
    }

    int a[n]; /* declare your VLA */

    printf("Enter elements to array\n");

    for (c = 0; c < n; c++)
    {
        if(scanf("%d", &a[c]) != 1)
        {
            exit(EXIT_FAILURE);
        }
    }

    min = a[0];
    max = a[0];

    return_min_max(a, n, &min, &max);
    printf("\n");

    return_array(a, n);

    return 0;
}

Compiled with:

gcc -Wall -pedantic main.c functions.c
alex01011
  • 1,670
  • 2
  • 5
  • 17