Help with user typing

Asked

Viewed 54 times

1

Hi, I’m doing a Sort Selection program in c, but I’m having a hard time. How or where, and what I put in order for the user to type the array size as well as its elements.

I tried so:

#include <stdio.h>

int main(){ 
 int num[];
 int tam, l;
 int i, j, min, aux;
 printf("Digite o tamanho do array: ");
 scanf("%d", tam);
 printf("Digite o array: ");
 scanf("%d", num[]);
 for (i = 0; i < (tam-1); i++){
   min = i;
   for (j = (i+1); j < tam; j++) {
      if(num[j] < num[min]) {
         min = j;
      }
   }
   if (i != min) {
      aux = num[i];
      num[i] = num[min];
      num[min] = aux;
   }
   printf("\n");

 }
}

But when you type something already closes the program, someone knows how to fix it?

  • Why you have to inform the array?

2 answers

2


You declared the array but did not determine its size.

If you will decide the size of it at runtime, you need to allocate memory to it. One solution is to use the malloc function. It is part of the header file

<stdlib.h>

So make sure to include it in your program.

First declare a pointer

int *array;

After you read the size of it, allocate the memory positions:

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

So your pointer will contain tam elements and you can index like this:

array[0] = 1;

You tried to access a position that does not exist in memory, so it gave error and the program was aborted.

Another way is to create a large array and use whatever the user wants. But it is a less efficient solution.

Another thing, your scanf failed to & read the variable. The function needs to know the address of the variable tam and you need to use that operator for such &:

scanf("%d", &tam);

That way he’ll know where tam is in memory and will save the value read inside the variable.

The reading of the array elements needs to be done one by one, i.e., use a repetition loop to read each position of your array:

for(i = 0; i < tam; i++) {
    scanf("%d", &array[i]);
}
  • The cast in the malloc is, at best, redundant, and may hide an error that the compiler would pick up in the absence of the cast.

0

I changed your name code take a look maybe it’ll help you.

int main()
{
    int *array;
    int tam = 0, num = 0;
    int i, j, min, aux;

    printf("Digite o tamanho do array: ");
    scanf_s("%d", &tam);

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

    printf("\nDigite o array: ");
    scanf_s("%d", &num);
    setbuf(stdin, NULL);

    for (i = 0; i < (tam - 1); i++)
    {
        min = i;
        for (j = (i + 1); j < tam; j++)
        {
            if (array[j] < array[min])
            {
                min = j;
            }
        }
        if (i != min) 
        {
            aux = array[i];
            array[i] = array[min];
            array[min] = aux;
        }
        printf("\n");
    }

    getchar();
    free(array);
    return 0;
}
  • I don’t understand why you have to inform the array. Anyway, I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.