In this challenge, you are required to calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.
Function Description
Complete the aVeryBigSum function in the editor below. It must return the sum of all array elements.
aVeryBigSum has the following parameter(s):
int ar[n] : an array of integers .Return
long : the sum of all array elementsInput Format
The first line of the input consists of an integer . The next line contains space-separated integers contained in the array.
Output Format
Return the integer sum of the elements in the array.
Constraints
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Output
5000000015
Note:
The range of the 32-bit integer is .
When we add several integer values, the resulting sum might exceed the above range. You might need to use long int C/C++/Java to store such sums.
SOLUTION :
#include < assert.h >
#include < ctype.h >
#include < limits.h >
#include < math.h >
#include < stdbool.h >
#include < stddef.h >
#include < stdint.h >
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
char * readline();
char * ltrim(char *);
char * rtrim(char *);
char ** split_string(char *);
int parse_int(char *);
long parse_long(char *);
long aVeryBigSum(int ar_count, long * ar) {
long sum=0.000000000 ;
for (int i=0 ;i<ar_count;i++)
{
sum=sum+*(ar+i);
}
return sum;
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH" ), "w" );
int ar_count = parse_int(ltrim(rtrim(readline())));
char ** ar_temp = split_string(rtrim(readline()));
long * ar = malloc(ar_count * sizeof (long ));
for (int i = 0 ; i < ar_count; i++) {
long ar_item = parse_long(*(ar_temp + i));
*(ar + i) = ar_item;
}
long result = aVeryBigSum(ar_count, ar);
fprintf(fptr, "%ld\n" , result);
fclose(fptr);
return 0 ;
}
char * readline() {
size_t alloc_length = 1024 ;
size_t data_length = 0 ;
char * data = malloc(alloc_length);
while (true ) {
char * cursor = data + data_length;
char * line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break ;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1 ] == '\n' ) {
break ;
}
alloc_length <<= 1 ;
data = realloc(data, alloc_length);
if (!data) {
data = ' \0 ' ;
break ;
}
}
if (data[data_length - 1 ] == '\n' ) {
data[data_length - 1 ] = ' \0 ' ;
data = realloc(data, data_length);
if (!data) {
data = ' \0 ' ;
}
} else {
data = realloc(data, data_length + 1 );
if (!data) {
data = ' \0 ' ;
} else {
data[data_length] = ' \0 ' ;
}
}
return data;
}
char * ltrim(char * str) {
if (!str) {
return ' \0 ' ;
}
if (!*str) {
return str;
}
while (*str != ' \0 ' && isspace(*str)) {
str++;
}
return str;
}
char * rtrim(char * str) {
if (!str) {
return ' \0 ' ;
}
if (!*str) {
return str;
}
char * end = str + strlen(str) - 1 ;
while (end >= str && isspace(*end)) {
end--;
}
*(end + 1 ) = ' \0 ' ;
return str;
}
char ** split_string(char * str) {
char ** splits = NULL;
char * token = strtok(str, " " );
int spaces = 0 ;
while (token) {
splits = realloc(splits, sizeof (char *) * ++spaces);
if (!splits) {
return splits;
}
splits[spaces - 1 ] = token;
token = strtok(NULL, " " );
}
return splits;
}
int parse_int(char * str) {
char * endptr;
int value = strtol(str, &endptr, 10 );
if (endptr == str || *endptr != ' \0 ' ) {
exit(EXIT_FAILURE);
}
return value;
}
long parse_long(char * str) {
char * endptr;
long value = strtol(str, &endptr, 10 );
if (endptr == str || *endptr != ' \0 ' ) {
exit(EXIT_FAILURE);
}
return value;
}
No comments:
Post a Comment