1

i have a problem in the result of the average in the function calcular, not return a correct number, and the rand return values equals, i dont know the reason for the bad average

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

int calcular(int * _arreglo, int * _tam);

int main(int argc, char * argv[]) {
    int tam = 0;
    if (argc == 2) {
        tam = atoi(argv[1]);
    } else {
        printf("No se ha ingresado correctamente el tamaño \n");
        exit(1);
    }
    int * arreglo;
    arreglo = (int * ) malloc(sizeof(int) * tam);
    int i;
    for (i = 0; i < tam; i++) {
        arreglo = (int * )(rand() % 101);
        printf("soy %d \n", ((int)(arreglo)));
        arreglo++;
    }
    int promedio = calcular(arreglo, & tam);
    printf("Promedio: %d \n", promedio);
    free(arreglo);
    return 0;
}

int calcular(int * _arreglo, int * _tam) {
    int pro = 0;
    int i;
    _arreglo = _arreglo - ( * _tam);
    for (i = 0; i < * _tam; i++) {
        pro = pro + ((int)(_arreglo));
        _arreglo++;
    }
    return (pro / ( * _tam));
}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83

2 Answers2

3

Main issues:

  1. This:

    for(i=0;i<tam;i++){
    arreglo=(int *)(rand()%101);
    printf("soy %d \n",((int)(arreglo)));
    arreglo++;
    }
    

    doesn't make any sense. You probably wanted

    for(i = 0; i < tam; i++) {
        arreglo[i] = (rand() % 101);
        printf("soy %d \n", arreglo[i]);
    }
    
  2. Here:

    int calcular(int *_arreglo, int *_tam){
    int pro=0;
    int i;
    _arreglo=_arreglo-(*_tam);
    for(i=0;i<*_tam;i++){
    pro=pro+((int)(_arreglo));
    _arreglo++;
    }
    return (pro/(*_tam));
    }
    

    you probably wanted

    int calcular(int *_arreglo, int *_tam){
        int pro = 0;
        int i;
    
        for(i = 0; i < *_tam; i++){
            pro = pro + _arreglo[i];
        }
    
        return (pro / (*_tam));
    }
    

Other issues:

  1. You need to call seed rand for getting a different set of random values on each run of the program so that you don't get the same set of random numbers on each run of the program. Add srand(time(NULL)); at the start of main after including time.h. This will return a different set of random numbers, provided that the program isn't run more than one time in the same second.
  2. It doesn't make sense why you are passing tam by reference into calcular. Pass it by value instead.
  3. There is no need cast the result of malloc (and family) in C.
  4. Consider checking the return value of malloc to see if it was successful. Change:

    arreglo = (int * ) malloc(sizeof(int) * tam);
    

    to

    if((arreglo = malloc(sizeof(int) * tam)) == NULL) /* If malloc failed */
    {
        fputs("malloc failed; Exiting...", stderr); /* Print the error message in the `stderr` */
        exit(-1); /* Exit the program */
    }
    
Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

You have a serious mistake

arreglo = (int *) (rand() % 101);

is assigning a random address to the pointer arreglo, dereferencing it is undefined behavior.

Reassigning the pointer after malloc() will cause a memory leak, because now you can't free() the pointer returned by malloc(). Moreover, you free() the pointer with the invalid address assigned with rand(), and that is undefined behavior.


Some other considerations

  1. Make your code readable to humans

    • Surround operatrs with white spaces, it adds a lot of clarity to the code because otherwise it becomes difficult to differentiate between tokens.

    • Indent your code correctly.

  2. You don't need to cast malloc(), this also improves readability.

  3. Check the pointer returned by malloc() for NULL, on error malloc() returns NULL, if you dereference a NULL pointer undefined behavior will occur.

  4. Use strtol() instead of atoi() to check for invalid input, like this

    char *endptr;
    tam = strtol(argv[1], &endptr, 10);
    if (*endptr != '\0')
        return -1; /* invalid input */
    
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97