kindly i need this problem in C language Write a code that receives a set of val
ID: 3728843 • Letter: K
Question
kindly i need this problem in C language
Write a code that receives a set of values from the user and sorts them. Ask the user how many numbers they would like to enter (5-20) - Check if the value they entered is within the range of 5-20. If not, ask them to enter a value between 5 and 20 until you receive a response in that range. Store this value in a variable named "N". When the user correctly enters N = the number of values (between 5-20), ask them for the first number. Tell them the number has to be between 1- 100. Check if the number is between 1-100. If not, ask until they enter a number between 1-100. - Repeat asking for new numbers and checking the validity of the numbers until the user enters the correct amount of numbers they specified inExplanation / Answer
Code:
#include <stdio.h>
#include <stdlib.h>
//function to swap the two numbers
void swap(int *a, int *b) {
int t;
//interchanges the numbers stored in the address
t = *a;
*a = *b;
*b = t;
}
//function sort to sort the number in the array in ascending order using bubble sort
void sort(int array[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// calls the function to swap the numbers
swap(&array[j], &array[j + 1]);
}
}
}
}
int main() {
int n;
do {
//prompt the user to enter the value for n
printf(" How many Numbers do you like to sort(5-20)? ");
scanf("%d", &n);
//if the number is not between the 5 - 20 then prompt the user for input
if ((n < 5) || (n > 20))
printf("Sorry!! enter the number between 5 to 20. ");
} while ((n < 5) || (n > 20));//until the user enters the value between 5 to 20 loop performs
int array[n];
int i;
for (i = 0; i < n; i++) {
do {//accepts the array element
printf(" Enter %d element between(1-100) : ", i + 1);
scanf("%d", &array[i]);
//if the user enter the invalid number then prints the message
if ((array[i] < 1) || (array[i] > 100))
printf("Sorry!! enter the number between 1 to 100. ");
} while ((array[i] < 1) || (array[i] > 100));//performs until the valid input for the array
}
//calls the sort method
sort(array, n);
//display the element after sorting
printf(" Sorted numbers are : ");
for (i = 0; i < n; i++) {
printf(" %d", array[i]);
}
}
Sample Run:
How many Numbers do you like to sort(5-20)? 30
Sorry!! enter the number between 5 to 20.
How many Numbers do you like to sort(5-20)? -1
Sorry!! enter the number between 5 to 20.
How many Numbers do you like to sort(5-20)? 5
Enter 1 element between(1-100) : 101
Sorry!! enter the number between 1 to 100.
Enter 1 element between(1-100) : 4
Enter 2 element between(1-100) : 3
Enter 3 element between(1-100) : 7
Enter 4 element between(1-100) : 8
Enter 5 element between(1-100) :-2
Sorry!! enter the number between 1 to 100.
Enter 5 element between(1-100) : 2
Sorted numbers are : 2 3 4 7 8
RUN SUCCESSFUL (total time: 35s)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.