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
What are the different parts of a word processing program?<br><br> I need this ASAP please help!!
uranmaximum [27]

Answer:

The basics of the Word window

Title bar. This displays the document name, followed by a program name.

Menu bar. This contains a list of options to manage and customize documents.

Standard toolbar. ...

Formatting toolbar. ...

Ruler. ...

Insertion point. ...

End-of-document marker. ...

Help.

3 0
3 years ago
Which one of the following statements is true regarding the fetch-execute cycle? Each step of the fetch-execute cycle is perform
AfilCa [17]

Answer:

The device responsible for performing the fetch-execute cycle is the CPU.

Explanation:

The fetch execution cycle is the way or method the CPU executes instructions given to it. In this process or cycle, the CPU fetches the instruction, then decodes it, executes it, and stores the information in its database.

The CPU executes one instruction at a time.

8 0
4 years ago
Specify either "Write Through" or "Write Back" as the policy which best corresponds to each of the conditions below.
Mariulka [41]

Answer:

1. Write through

2. Write back

3. Write back

4. Write through.

Explanation:

The answers are given accordingly as above.

4 0
4 years ago
Where would you go to add fractions to a document in adobe indesign
prisoha [69]

Just select the fraction text and choose Open Type > Fractions from the Character panel menu.

5 0
3 years ago
Write a program that keeps taking integers until the user enters in python
soldi70 [24.7K]

int main {

//variables

unsigned long num = 0;

std::string phrase = " Please enter your name for confirmation: " ;

std::string name;

//codes

std::cout << phrase;

std::cin>> name;

while ( serial.available() == 0 ) {

num++;

};

if ( serial.avaliable() > 0 ) {

std::cout << " Thank you for your confirmation ";

};

};

3 0
3 years ago
Other questions:
  • to create an app for a mobile device, yourself, you would use BLANK software like Swift, Corona or Scratch.
    13·1 answer
  • Can't find the right year for this. (Attachment)
    6·1 answer
  • Write a function called "power" that takes in integers b&gt;0 and e≥0 and recursively computes b^3 and returns the result.
    5·2 answers
  • What is the purpose of interrupts? How does an interrupt differ from a trap? Can traps be generated intentionally by a user prog
    11·1 answer
  • How many types of sharing of Google Forms are possible?
    15·1 answer
  • Choose the answer that best completes the
    8·2 answers
  • A student is having trouble finding enough time in his busy schedule to work on his school science project. He has a demanding s
    5·2 answers
  • A central issue of public sharing is:
    13·2 answers
  • Is there a way to delete a question on brainly?
    8·2 answers
  • Add this in binary numbers . (1100011+11111+111) ​
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!