Advertisement
Guest User

Untitled

a guest
Dec 25th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define MAX(A, B) (A > B ? A : B)
  5.  
  6.  
  7. int max_sum(int, int*);
  8.  
  9. int main(){
  10.     FILE *filePointer;
  11.     FILE *filePointer_out;
  12.     int *array;
  13.     int n, i = 0;
  14.  
  15.     filePointer = fopen("input.txt", "r");
  16.     if (filePointer){
  17.         fscanf(filePointer, "%d", &n);
  18.         array = (int*)malloc(sizeof(int) * n);
  19.         while (!feof(filePointer)){
  20.             fscanf(filePointer, "%d", &array[i]);
  21.             i++;
  22.         }
  23.     }
  24.     else{
  25.         printf("File txt not found...\n");
  26.     }
  27.     filePointer_out = fopen("output.txt", "w");
  28.     if (filePointer_out){
  29.         fprintf(filePointer_out, "%d", max_sum(n, array));
  30.     }
  31.     else{
  32.         printf("File txt out not found...\n");
  33.     }
  34. }
  35.  
  36. int max_sum(int n, int* D_array){
  37.     bool odd;
  38.     int i, sum1 = 0, sum2 = 0;
  39.     odd = false;
  40.  
  41.     if (n%2 != 0)
  42.         odd = true;
  43.  
  44.     if (odd){
  45.         for (i = 1; i < n; i += 2){
  46.             sum1 += D_array[i];
  47.         }
  48.         return sum1;
  49.     }
  50.     else{
  51.         for(i = 0; i < n; i+= 2){
  52.             sum1 += D_array[i];
  53.         }
  54.         for(i = 1; i < n; i += 2){
  55.             sum2 += D_array[i];
  56.         }
  57.  
  58.         return MAX(sum1, sum2);
  59.  
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement