Question

I want to find the minimum number and summary from a dynamic integer table. I do not know why results not shown. Have I done something wrong on the malloc ? May I use realloc too ?

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

int main()
{
    int n,i,min,sum,xronos;
    int* array;
    printf("Give me how many numbers does the table will have: \n");
    scanf("%d",&n);
    array=(int*)malloc(n*sizeof(int));

    for(i=1;i<=n;i++)
    {
        printf("Give the number %d",i);
        printf("\n");
        scanf("%d",&array[i]);
    }

    for(i=1;i<=n;i++)
    {
        sum=sum+array[i];
        if (i=1)
        {
            min=array[i];
        }
        else
        {
            if (array[i]<min)
            {
                min=array[i];
            }
        }
    }

    printf("%d",sum);
    printf("\n The answer is :",(n-2)*min+(sum-min));
    getch();
    return 0;
}
Was it helpful?

Solution 2

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>  /* Defines: ENOMEM */

int main()
   {

Added rCode to assist in error handling.

   int rCode=0;

Removed xronos as it is not used.

   int n,i,min;

Initialized sum to 0;

   int sum=0;

Initialize array pointer to NULL so cleanup is easier. int *array = NULL;

   printf("Give me how many numbers does the table will have: \n");
   scanf("%d",&n);

There is nothing inherently wrong with casting the output of malloc to (int *). In some situations, it is a good coding practice. (good job).

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

You should always test that you actually got memory from malloc.

   if(NULL == array)
      {
      rCode=ENOMEM;
      fprintf(stderr, "malloc() failed.");
      goto CLEANUP;
      }

The array element index is in the range of 0 through (n-1). Start indexing at zero, and be sure to stop at (n-1).

   for(i=0;i<n;i++)
      {
      printf("Give the number %d",i);
      printf("\n");
      scanf("%d",&array[i]);
      }

Again, the array element index is in the range of 0 through (n-1). Start indexing at zero, and be sure to stop at (n-1).

   for(i=0;i<n;i++)
      {
      sum=sum+array[i];

      if(1 == i)
         min=array[i];
      else
         {
         if(array[i] < min)
            min=array[i];
         }
      }

   printf("%d",sum);
   printf("\n The answer is :",(n-2)*min+(sum-min));
   getch();

CLEANUP:

From my early years, my father would tell me: 'When you bring your toys to the sandbox, always remember to put them away when you are finished.'

   if(array)
       free(array);

   return(rCode);
   }

OTHER TIPS

Yes, that is almost exactly how you are supposed to use malloc, except for three small things and one big thing:

  • Do not cast malloc result in C,
  • Use indexes from zero to n-1, inclusive (your code goes from one to n, inclusive)
  • Add a call free(array) to avoid a memory leak.

The big thing is that you do not need malloc in order to solve this problem: you can calculate the sum and the min as you go, without saving the individual items to an array.

You can replace the chain of ifs in the loop with this check:

if (i == 0 || array[i] < min) {
    min=array[i];
}

This covers both the assignment of the first element, and comparison of elements other than the first one.

Finally, you can rewrite this

sum=sum+array[i];

as

sum += array[i];

using a composite assignment operator. Don't forget to initialize the sum to zero!

Line 22, need

if (i=1) 

set to

if (i==1)

Right now you're setting i equal to 1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top