Welcome to Westonci.ca, the place where your questions find answers from a community of knowledgeable experts. Get immediate and reliable solutions to your questions from a knowledgeable community of professionals on our platform. Get quick and reliable solutions to your questions from a community of experienced experts on our platform.
Sagot :
Answer:
In C++:
void sortlist(char nums[],int charlent){
int Myarr[charlent];
const char s[4] = " ";
char* tok;
tok = strtok(nums, s);
int i = 0;
while (tok != 0) {
int y = atoi(tok);
Myarr[i] = y;
tok = strtok(0, s);
i++;
}
int a;
for (int i = 0; i < charlent; ++i) {
for (int j = i + 1; j < charlent; ++j) {
if (Myarr[i] > Myarr[j]) {
a = Myarr[i];
Myarr[i] = Myarr[j];
Myarr[j] = a;
} } }
for(int j = 0;j<charlent;j++){ printf(" %d",Myarr[j]); }
}
Explanation:
This line defines the sortlist function. It receives chararray and its length as arguments
void sortlist(char nums[],int charlent){
This declares an array
int Myarr[len];
This declares a constant char s and also initializes it to space
const char s[4] = " ";
This declares a token as a char pointer
char* tok;
This line gets the first token from the char
tok = strtok(nums, s);
This initializes variable i to 0
int i = 0;
The following while loop passes converts each token to integer and then passes the tokens to the array
while (tok != 0) {
int y = atoi(tok); -> Convert token to integer
Myarr[i] = y; -> Pass token to array
tok = strtok(0, s); -> Read token
i++;
}
Next, is to sort the list.
int a;
This iterates through the list
for (int i = 0; i < charlent; ++i) {
This iterates through every other elements of the list
for (int j = i + 1; j < charlent; ++j) {
This condition checks if the current element is greater than next element
if (Myarr[i] > Myarr[j]) {
If true, swap both elements
a = Myarr[i];
Myarr[i] = Myarr[j];
Myarr[j] = a;
} } }
The following iteration prints the sorted array
for(int j = 0;j<charlent;j++){ printf(" %d",Myarr[j]); }
}
See attachment for illustration of how to call the function from main
Thank you for choosing our platform. We're dedicated to providing the best answers for all your questions. Visit us again. We appreciate your time. Please come back anytime for the latest information and answers to your questions. Thank you for using Westonci.ca. Come back for more in-depth answers to all your queries.