Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void show_menu();
- int *read_array(int *);
- void print_array(int *, int);
- int biggest_odd(int *, int);
- int smallest_even(int *, int);
- int main()
- {
- int size;
- int *arr;
- while (1)
- {
- show_menu();
- int choice;
- scanf("%d", &choice);
- if (choice == 1)
- {
- arr = read_array(&size);
- }
- else if (choice == 2)
- {
- print_array(arr, size);
- }
- else if (choice == 3)
- {
- printf("The biggest odd number is: %d\n", biggest_odd(arr, size));
- }
- else if (choice == 4)
- {
- printf("The smallest even number is: %d\n", smallest_even(arr, size));
- }
- else if (choice == 5)
- {
- return 0;
- }
- else
- {
- printf("Invalid input!\n\n");
- }
- }
- return 0;
- }
- void show_menu()
- {
- printf("1. Read an array\n");
- printf("2. Print array\n");
- printf("3. Get largest odd number\n");
- printf("4. Get smallest even number\n");
- printf("5. Exit\n");
- printf("Choose your option: ");
- }
- int *read_array(int *size)
- {
- *size = 1;
- int *arr;
- int arr_size = 10;
- arr = (int *)malloc(arr_size * sizeof(int));
- printf("Enter value of element at index %d (EOF to stop input): ", *size - 1);
- while (scanf("%d", &arr[*size - 1]) == 1)
- {
- getchar();
- *size = *size + 1;
- if (*size > arr_size)
- {
- arr_size += 10;
- arr = realloc(arr, arr_size * sizeof(int));
- }
- printf("Enter value of element at index %d (EOF to stop input): ", *size - 1);
- }
- printf("\n");
- *size = *size - 1;
- return arr;
- }
- void print_array(int *array, int size)
- {
- printf("[");
- for (int i = 0; i < size; i++)
- {
- printf("%d", array[i]);
- if (i != size - 1)
- printf(", ");
- }
- printf("]\n");
- }
- int biggest_odd(int *arr, int size)
- {
- int max = arr[0];
- for (int i = 0; i < size; i++)
- {
- if(arr[i] > max) {
- if(arr[i] % 2 == 1) max = arr[i];
- }
- }
- return max;
- }
- int smallest_even(int *arr, int size)
- {
- int min = arr[0];
- for (int i = 0; i < size; i++)
- {
- if(arr[i] < min) {
- if(arr[i] % 2 == 0) min = arr[i];
- }
- }
- return min;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement