Question
(C programming language) So far my program prompts user to input integers into an array. If a number of 0 or less is entered, input
(C programming language) So far my program prompts user to input integers into an array. If a number of 0 or less is entered, input stops, or if input reaches 30 (the max size of the array), input stops.
I can correctly print contents of the array, but now I need the array to print in ascending order as well using a bubble sort if possible, and then I also need to print those contents but without any numbers input duplicating. Below I copy and pasted my code so far (the bubble sort needs work, and I don't have any idea yet how to print in order without duplicating).
#include
#include
#define SIZE 30
int main() {
// initialize the array "numbers" with 30 or less elements
int numbers[SIZE];
// get user input
printf("Enter up to 30 integers, and enter an integer less than or equal to zero to stop input. ");
int counter = 1;
while(counter < SIZE + 1) {
printf("Enter integer: ");
scanf("%d", &numbers[counter]);
if (numbers[counter] <= 0)
break;
else;
counter++;
}
// print array
printf(" Array order: ");
for(int j = 1; j < counter; ++j) {
printf("%d ", numbers[j]);
}
// sort array - bubble sort
printf(" Sorting array: ");
int temp = 1;
for(int i = 1; i < counter; ++i) {
for(int j = 1; j < counter - 1; ++j) {
if(numbers[j] > numbers[j + 1]) {
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started