Advertisement
kolychestiy

читаем массив и выводим четные элементы

Dec 20th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int read_int(FILE *input){
  5.  
  6.     int num = 0;
  7.     char c;
  8.  
  9.     while((c = fgetc(input)) >= '0' && c <= '9'){
  10.         num *= 10;
  11.         num += c - '0';
  12.     }
  13.  
  14.     return num;
  15.  
  16. }
  17.  
  18. int *get_even_num(int n, int *array){
  19.  
  20.     int *ans = (int *)malloc(n / 2);
  21.  
  22.     for (int i = 1, *cur = ans; i < n; i += 2, cur++){
  23.         *cur = *(array + i);
  24.     }
  25.  
  26.     return ans;
  27.  
  28. }
  29.  
  30. signed main(){
  31.  
  32.     FILE *input;
  33.     input = fopen("input.txt", "r");
  34.  
  35.     int n = read_int(input);
  36.     int *array = (int *)malloc(sizeof(int) * n);
  37.  
  38.     for (int i = 0; i < n; i++){
  39.         *(array + i) = read_int(input);
  40.     }
  41.  
  42.     int *new_array = get_even_num(n, array);
  43.  
  44.     for (int i = 0; i < n / 2; i++){
  45.         cout << *(new_array + i) << ' ';
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement