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