Get the answers you need at Westonci.ca, where our expert community is always ready to help with accurate information. Experience the ease of finding precise answers to your questions from a knowledgeable community of experts. Our platform offers a seamless experience for finding reliable answers from a network of knowledgeable professionals.
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
Visit us again for up-to-date and reliable answers. We're always ready to assist you with your informational needs. Thanks for using our service. We're always here to provide accurate and up-to-date answers to all your queries. Westonci.ca is here to provide the answers you seek. Return often for more expert solutions.