asked 181k views
1 vote
---IN C ONLY PLEASE---

Part 1: Finding the middle item

Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd.

Ex: If the input is: 2 3 4 8 11 -1
the output is: Middle item: 4

The maximum number of list values for any test case should not exceed 9. If exceeded, output "Too many numbers".

Hint: First read the data into an array. Then, based on the array's size, find the middle item.

-------------------------------------------

Part 2: Reverse

Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a comma, including the last one. Assume that the list will always contain less than 20 integers.

Ex: If the input is: 5 2 4 6 8 10
the output is: 10,8,6,4,2

To achieve the above, first read the integers into an array. Then output the array in reverse.

---IN C ONLY PLEASE---

asked
User Kingdaro
by
8.2k points

1 Answer

2 votes

Answer:

Part 1:

#include <stdio.h>

int main() {

int arr[9];

int num, i, middle;

// Read integers into the array

for (i = 0; i < 9; i++) {

scanf("%d", &num);

if (num < 0) {

break;

}

arr[i] = num;

}

// Check if too many numbers were entered

if (i == 9 && num >= 0) {

printf("Too many numbers\\");

return 0;

}

// Find the middle integer

middle = i / 2;

printf("Middle item: %d\\", arr[middle]);

return 0;

}

Part 2:

#include <stdio.h>

int main() {

int arr[20];

int num, i;

// Read integers into the array

scanf("%d", &num);

for (i = 0; i < num; i++) {

scanf("%d", &arr[i]);

}

// Output the integers in reverse

for (i = num - 1; i >= 0; i--) {

printf("%d,", arr[i]);

}

printf("\\");

return 0;

}

answered
User Phtrivier
by
8.7k points

No related questions found