Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void printArray(const int a[], int n) {
- printf("\n");
- printf("\n");
- printf("[");
- for(int i = 0; i < n; i++) {
- // printf with no dividers
- if(i + 1 == n) {
- printf("%d", a[i]);
- } else {
- printf("%d, ", a[i]);
- }
- }
- printf("]");
- printf("\n");
- printf("\n");
- }
- void moveToTheRightBy1Element(int *a, int count) {
- int copyCount = count;
- a[0] = 0;
- for(int i = count; i > 0; i--) {
- a[i] = a[i - 1];
- }
- }
- void sorrtedByBbleSort(int *src, int length) {
- bool isSorted = false;
- while(!isSorted) {
- isSorted = true;
- for(int i = 0; i < length - 1; i++) {
- int currentElement = src[i];
- if(currentElement > src[i + 1]) {
- isSorted = false;
- src[i] = src[i + 1];
- src[i + 1] = currentElement;
- }
- }
- }
- }
- void task_1() {
- int A, B, n;
- double sum = 0;
- printf("Введите n: ");
- scanf("%d", &n);
- if(n < 0) {
- printf("Input data is not corrected");
- } else {
- int a[n];
- printf("Введите range.");
- printf("\nstart: ");
- scanf("%d", &A);
- printf("end: ");
- scanf("%d", &B);
- if(A > B) {
- int temp = A;
- A = B;
- B = temp;
- }
- for(int i = 0; i < n; i++) {
- printf("current i %d", i);
- a[i] = rand() % (B - A + 1) + A;
- }
- printf("\nArray is ");
- printArray(a, n);
- // calculate an average sum of the elements
- for(int i = 0; i <= n; i++) {
- sum += a[i];
- }
- double average = sum/n;
- if(average < 0) average = -average;
- printf("\nAverage sum is %.3lf", average);
- // make a copy of a
- int modifiedArray[n];
- for(int i = 0; i < n; i++) {
- modifiedArray[i] = a[i];
- }
- // write unOdds to odds positions and vice versa
- for(int i = 0; i < n - 1; i+= 2) {
- modifiedArray[i] = a[i + 1];
- }
- for(int i = 1; i < n; i+= 2) {
- modifiedArray[i] = a[i - 1];
- }
- printf("\n\nArray after replacing odds by unodds is ");
- printArray(modifiedArray, n);
- moveToTheRightBy1Element(modifiedArray, n);
- printf("\n\nArray after moving to the right by 1 ");
- printArray(modifiedArray, n);
- printf("Sorted via the bubble sort array.");
- sorrtedByBbleSort(modifiedArray, n);
- printArray(modifiedArray, n);
- int countOfElementsMoreThanMiddle = 0;
- int middleIndex = n/2;
- if(middleIndex + 1 <= n) {
- int middleElement = modifiedArray[middleIndex];
- printf("middle index %d, middle element %d", middleIndex, middleElement);
- for(int i = middleIndex + 1; i < n; i++) {
- if(modifiedArray[i] > middleElement) countOfElementsMoreThanMiddle++;
- }
- }
- printf("countOfElementsMoreThanMiddle %d ", countOfElementsMoreThanMiddle);
- }
- }
- int main() {
- int numOfTask = 0;
- do
- {
- printf("\nEnter a number of the task: ");
- scanf("%d", &numOfTask);
- switch (numOfTask) {
- case 1:
- task_1();
- break;
- default:
- printf("\nThe task by num %d has not found", numOfTask);
- break;
- }
- } while (numOfTask != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement