Created
December 26, 2024 19:36
-
-
Save yusufpapurcu/f99c978a9d943504973fe4398b76d369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| void get_user_input(int base_array[], int array_lenght){ | |
| int i = 0; | |
| do { | |
| scanf("%d", &base_array[i]); | |
| i++; | |
| } while (i < array_lenght); | |
| } | |
| int get_odd_numbers(int base_array[],int odd_numbers[], int array_lenght){ | |
| int counter = 0; | |
| for (size_t i = 0; i < array_lenght; i++) | |
| { | |
| // odd indexes from base array, not odd elements | |
| // for odd elements replace i with base_array[i] bellow | |
| if (i%2!=0){ | |
| odd_numbers[counter] = base_array[i]; | |
| counter++; | |
| } | |
| } | |
| return counter; | |
| } | |
| int main(){ | |
| int array_lenght; | |
| printf("Please type the lenght of the array: "); | |
| scanf("%d", &array_lenght); | |
| int base_array[array_lenght]; | |
| get_user_input(&base_array, array_lenght); | |
| int odd_numbers[array_lenght]; | |
| int odd_count = get_odd_numbers(&base_array, &odd_numbers, array_lenght); | |
| for (int i = 0; i < odd_count; i++) | |
| { | |
| printf("odd index %d: %d\n", i, odd_numbers[i]); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment