0

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;
}

3 Answers3

2

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!

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Initializing `min` to some value like `INT_MAX` has the advantage of `min` having a defined value at `printf()` time should `n <= 0`. – chux - Reinstate Monica Apr 24 '14 at 17:33
  • @chux The problem with that is that when `n` is zero or less, `min` is really undefined. There needs to be a check upfront that exits early when a negative or zero `n` is entered. – Sergey Kalinichenko Apr 24 '14 at 17:36
  • Thanks for your answer !!! But how can I get the size of the table from the user without using malloc ? – Eugen Bartz Apr 24 '14 at 18:32
  • @user3403697 The same way that you do now - `scanf("%d",&n);` The change that you need to make is to combine the two loops that you have in one, and replace `array[i]` with a temporary `int` variable that you read, use, and discard in the same iteration of the loop. – Sergey Kalinichenko Apr 24 '14 at 18:41
0

Line 22, need

if (i=1) 

set to

if (i==1)

Right now you're setting i equal to 1

JoeManiaci
  • 435
  • 3
  • 15
  • You have right,I didn't see that,I correct it but the problem remains. – Eugen Bartz Apr 24 '14 at 17:32
  • I was able with certain numbers, to get the correct sum, but not the lowest number given. So hopefully it's not a code problem, just a logical issue. – JoeManiaci Apr 24 '14 at 18:14
0
#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);
   }
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
  • Thank you ! You are very analytical ! I learn many things from your post but I think that the problem isn't solved. – Eugen Bartz Apr 24 '14 at 18:29