Удалить первый элемент массива с заданным значением - C (СИ)
Формулировка задачи:
Нужна помощь. Если можно с комментариями, буду очень благодарен.
Задание:
1) Сформировать одномерный массив целых чисел, используя датчик случайных чисел.
2) Распечатать полученный массив.
3) Удалить первый элемент с заданным значением.
4) Сдвинуть массив циклически на К элементов вправо.
5) Распечатать полученный массив.
Решение задачи: «Удалить первый элемент массива с заданным значением»
textual
Листинг программы
- #include <stdio.h>
- #include <time.h>
- #include <Windows.h>
- // Defined values for changing array size, low and high border of the array
- #define ARRAY_SIZE 8
- #define NEW_ARRAY_SIZE (ARRAY_SIZE-1)
- #define LOW_BORDER -30
- #define HIGH_BORDER 30
- int main(void)
- {
- srand(time(NULL));
- // Variables initialization
- int i, j;
- int arr[ARRAY_SIZE];
- int new_arr[NEW_ARRAY_SIZE];
- int del_el = 0, pos = 0, found = 0, shift = 0;
- for (i = 0; i < ARRAY_SIZE; i++)
- {
- // TASK1. Generating and printing in the console window generated array with rand() function
- arr[i] = rand() % (HIGH_BORDER - LOW_BORDER + 1) + LOW_BORDER;
- // TASK2. Outputting the generated array to the console
- printf("%d ",arr[i]);
- }
- // Processing user's input of the element to be deleted
- printf("\nEnter the element to be deleted: ");
- scanf("%d", &del_el);
- // Search for the entered del_el element in array arr[ARRAY_SIZE]
- for (i = 0; i < ARRAY_SIZE; i++)
- {
- if (arr[i] == del_el)
- {
- found = 1;
- pos = i;
- break;
- }
- }
- if (found == 1) // If element was found(entered correctly)
- {
- for (i = 0, j = 0; i < ARRAY_SIZE, j < NEW_ARRAY_SIZE; i++,j++)
- {
- if (i == pos)
- {
- j--;
- continue;
- }
- new_arr[j] = arr[i];
- }
- }
- else // Jump here if user has entered the element which is not in the array
- printf("Element %d is not found in the vector\n", del_el);
- // TASK4. Shifting the array on the entered number of positions
- printf("Enter the value of the right shift: ");
- scanf("%d", &shift);
- shift %= NEW_ARRAY_SIZE;
- while (shift)
- {
- int tmp = new_arr[NEW_ARRAY_SIZE - 1];
- for (int i = NEW_ARRAY_SIZE - 1; i>0; i--)
- new_arr[i] = new_arr[i - 1];
- new_arr[0] = tmp;
- shift--;
- }
- // TASK5. Outputting the resultant array after all the operations
- printf("The resultant array after the removing of the element with the entered value is \n");
- for (i = 0; i < NEW_ARRAY_SIZE; i++)
- {
- printf("%d ", new_arr[i]);
- }
- printf("\n");
- system("pause");
- return 0;
- }
Объяснение кода листинга программы
- Установить размер массива, границу нижнего и верхнего индексов
- Сгенерировать и вывести в консоль массив с помощью функции rand()
- Попросить пользователя ввести элемент для удаления
- Найти введенный элемент в массиве
- Создать новый массив с уменьшенным размером
- Сдвинуть элементы массива на введенное количество позиций вправо
- Вывести полученный массив в консоль
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д