Rafaalvfe
Rafaalvfe

Reputation: 73

C program keep crashing

well i have this simple code, it asks the user how many numbers he wants to save, then it asks for the numbers, each time the user write the number the program prints the numbers that he has written already and it sort numbers from lowest to highest.

The problem is when the user tell the program to save more than 9 numbers, the programs just stops, if the user choose to save 8 or less number it works perfect. i don't know whats happening, I'll appreciate your help

The variables and other terms are based in my language, spanish, here are some translations:

function guardaNum: saves the number in the array

function ordena: sort array's numbers

function imprime: prints the array

contador: is a counter

cant: is the amount of number the users wants to save

thanks for your help!

#include <stdio.h>
int guardaNum(int *pnum,int lista[],int *pcontador);
int ordena(int lista[],int *pcontador);
void imprime(int lista[],int *pcant);

int main(void)
{
  int cant, num, *pnum, lista[cant], i,contador,*pcontador ;
  printf("Ingrese la cantidad de numeros que desea agregar a la lista: \n");
  scanf("%d", &cant);

  for(i=0;i<cant;i++)
    {
      lista[i]=-99;
    }

  for(i=0;i<cant;i++)
    {
      printf("Ingrese un Numero: ");
      scanf("%d",&num);

      pnum=&num;
      contador=i;
      pcontador=&contador;

      guardaNum(pnum,lista,pcontador);
      ordena(lista,pcontador);
      imprime(lista,pcontador);
    }

}

int guardaNum(int *pnum,int lista[],int *pcontador)
{
  lista[*pcontador]=*pnum;
  return 0;

}

int ordena(int lista[], int *pcontador)
{
  int i,j, temp;

  for(j=0;j<*pcontador;j++)
    {
      for(i=0;i<*pcontador;i++)
    {

      if(lista[i]>lista[i+1])
        {
          temp=lista[i+1];
          lista[i+1]=lista[i];
          lista[i]=temp;
        }
    }
    }
}

void imprime(int lista[],int *pcontador)
{
  int i;
  for(i=0;i<=*pcontador;i++)
    {
      printf("%d\n",lista[i]);
    }
}

Upvotes: 0

Views: 122

Answers (3)

Scotty Bauer
Scotty Bauer

Reputation: 1277

do this:

int main(void)
{
  int cant, num, *pnum, i,contador,*pcontador ;
  int *lista;
  printf("Ingrese la cantidad de numeros que desea agregar a la lista: \n");
  scanf("%d", &cant);
  lista = malloc(sizeof(int)*cant);
  if(!lista){
    perror("error on malloc");
    exit(1);
  }

  //continue with your code
  free(lista);
}

The problem with your code, is cant is uninitialized so the compiler doesn't know how much space is required for your array.

or you can do it this way:

scanf("%d", &cant);
int lista[cant]
//continue with your code

Upvotes: 1

Leeor
Leeor

Reputation: 19736

Note that you define the array with "cant" as size before you actually take it as an input. You'll either need a max possible array size (and better check your input against it), or if you know how - allocate the array dynamically

Upvotes: 1

Fred Larson
Fred Larson

Reputation: 62113

In this line:

int cant, num, *pnum, lista[cant], i,contador,*pcontador ;

You are using cant while it is uninitialized. You'll have to move your declaration of lista to be after you have input the size.

Upvotes: 1

Related Questions