Write a program that gets two integer numbers from the user. In your main functi
ID: 3652012 • Letter: W
Question
Write a program that gets two integer numbers from the user. In your main function do the following:
Explanation / Answer
>// JB6Q3.c ... Get two numbers and sort them >#include > >int sort_two(int * u, int * v); >int main(void) >{ > char input1; > char input2; > int num1; > int num2; > > printf("In the program, you will enter two numbers and they will be >sorted "); > printf("Enter the first number "); > gets(input1); gets will input a string, that is a nul-terminated sequence of char. input1 is a single char. It does not have enough room to hold more than one. It also has the wrong type since gets expects a pointer. > num1 = atoi(input1); > printf("Enter the second number "); > gets(input2); > num2 = atoi(input2); > > sort_two(&num1, &num2); sort_two will return an int. Don't you think you should save it so you can return it to the OS? > printf("The sorted order is %d, %d ", num1, num2); > > fflush(stdin); fflush is defined only for output files. > printf(" Press enter to continue "); > getchar(); > > return 0; Only if num1} > >int sort_two(int * u, int * v) > >{ > if (num2 { > int temp; > temp = *u; > *u = *v; > *v = temp; > return 1; > } > > else > printf("The numbers were already in sorted order. "); As a matter of good design, sort_two should only sort the numbers and return a status value. Let the caller print out the message if desired. Then sort_two will have more general applicability. > return 0; >}Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.