1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Yuki888 [10]
3 years ago
13

Write a program, named sortlist, that reads three to four integers from the command line arguments and returns the sorted list o

f the integers on the standard output screen. You need to use strtok() function to convert a string to a long integer. For instance,
Computers and Technology
1 answer:
yawa3891 [41]3 years ago
7 0

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  

<em> while (tok != 0) {  </em>

<em>  int y = atoi(tok); </em><em>-> Convert token to integer</em><em> </em>

<em>  Myarr[i] = y; </em><em>-> Pass token to array</em><em> </em>

<em>  tok = strtok(0, s); </em><em>-> Read token</em><em> </em>

<em>  i++; </em>

<em> }  </em>

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

<em>   a =  Myarr[i]; </em>

<em>   Myarr[i] = Myarr[j]; </em>

<em>   Myarr[j] = a; </em>

       }   }  }

The following iteration prints the sorted array

<em>for(int j = 0;j<charlent;j++){ printf(" %d",Myarr[j]); }  </em>

}

<em>See attachment for illustration of how to call the function from main</em>

Download cpp
You might be interested in
If E=mc2 then what does F equal?
Vladimir79 [104]

Answer:

First of all,there is not theory F=ma^2!!

Its F=ma and they are definitely not the same.Here F means force and E means Energy.

a means acceleration, but c means the velocity of light. Clearly there's difference between acceleration and velocity and force and energy.

F=ma can be applied on things we normally see but E=mc^2 is applied on things that can travel nearly in light speed!which means atoms,molecules, protons,electrons etc. energy can be determined by the Einstein theory but F=ma is for determining human energy, or energy of cars,vehicles or any regular moving things

Explanation:

4 0
2 years ago
What tv show inspired the term spam for junk email?
Pani-rosa [81]

Answer:

Monty Python's Flying Circus.

Explanation:

3 0
2 years ago
Read 2 more answers
Be able to list a technology-based company and discuss whether it enjoys sustainable competitive advantage based on the resource
aev [14]

Answer:

idk good luck finding one

5 0
2 years ago
What environmental concern will hybrid cars address?
Over [174]
The answer to the above question is:
d. all of the above
5 0
3 years ago
Read 2 more answers
HELPPPP KOKICHI IS OUT TO KILL ME
Ne4ueva [31]
Oh my- good luck with that-
3 0
2 years ago
Other questions:
  • 1. Potential incidents represent threats that have yet to happen. Why is the identification of the threat important to maintaini
    6·1 answer
  • A ________ restricts the number of a certain type of product that can be imported into a country.
    5·1 answer
  • What is an activity that can help you enhance the appearance of your computer’s desktop?
    13·1 answer
  • What does %d, , %c, %s mean and what's their difference?
    12·1 answer
  • What steps might a company or organization need take in order to keep its digital data secure online?
    8·1 answer
  • Write the definition of a function named fscopy. This function can be safely passed two fstream objects, one opened for reading,
    11·1 answer
  • Check My Work Sherri is considering replacing a processor on her laptop. The laptop is running slower than she would like. What
    9·1 answer
  • Lasses give programmers the ability to define their own types. <br><br> a. True<br> b. False
    10·1 answer
  • What is essential for a good study routine? Select four options.
    7·1 answer
  • How to square a number in java.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!