How to resolve the algorithm Sort three variables step by step in the C programming language
How to resolve the algorithm Sort three variables step by step in the C programming language
Table of Contents
Problem Statement
Sort (the values of) three variables (X, Y, and Z) that contain any value (numbers and/or literals). If that isn't possible in your language, then just sort numbers (and note if they can be floating point, integer, or other). I.E.: (for the three variables x, y, and z), where: After sorting, the three variables would hold: For numeric value sorting, use: I.E.: (for the three variables x, y, and z), where: After sorting, the three variables would hold: The variables should contain some form of a number, but specify if the algorithm used can be for floating point or integers. Note any limitations. The values may or may not be unique. The method used for sorting can be any algorithm; the goal is to use the most idiomatic in the computer programming language used. More than one algorithm could be shown if one isn't clearly the better choice.
One algorithm could be:
Another algorithm (only for numeric values):
Show the results of the sort here on this page using at least the values of those shown above.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sort three variables step by step in the C programming language
First Code:
The provided C code performs the following tasks:
-
Input Processing: It prompts the user to enter three values (referred to as "st," "nd," and "rd" values) and stores them as strings in the character array
values
. -
Validity Check: It checks each input string character by character to verify if it represents a valid number. If any invalid characters (non-digit, non-period, or non-sign) are encountered, it marks
isString
as1
, indicating that the value(s) are strings, not numbers. -
Conversion to Double Array: If
isString
is0
, it converts the input strings to double-precision floating-point numbers and stores them in theval
array. -
Sorting: It performs a simple sorting algorithm to order the values in either ascending order (if
isString
is0
) or alphabetically (ifisString
is1
). -
Output: It prints the values in the sorted order, with a prefix of "X" followed by a lowercase character (starting from 'a') and the sorted value.
Second Code:
The second code snippet in C demonstrates a simple sorting algorithm for three integers. It uses a do-while
loop to repeatedly check and swap values until they are sorted in ascending order.
-
Initialization: The code initializes three integer variables
x
,y
, andz
with values 77444, -12, and 0, respectively. -
Sorting: It enters a
do-while
loop that exits only when bothx
is less than or equal toy
andy
is less than or equal toz
. Inside the loop, it checks and swaps values as follows:- If
x
is greater thany
, it swapsx
andy
. - If
z
is less thany
, it swapsy
andz
.
- If
-
Output: After sorting, it prints the sorted values of
x
,y
, andz
.
Source code in the c programming language
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define MAX 3
int main()
{
char values[MAX][100],tempStr[100];
int i,j,isString=0;
double val[MAX],temp;
for(i=0;i<MAX;i++){
printf("Enter %d%s value : ",i+1,(i==0)?"st":((i==1)?"nd":"rd"));
fgets(values[i],100,stdin);
for(j=0;values[i][j]!=00;j++){
if(((values[i][j]<'0' || values[i][j]>'9') && (values[i][j]!='.' ||values[i][j]!='-'||values[i][j]!='+'))
||((values[i][j]=='.' ||values[i][j]=='-'||values[i][j]=='+')&&(values[i][j+1]<'0' || values[i][j+1]>'9')))
isString = 1;
}
}
if(isString==0){
for(i=0;i<MAX;i++)
val[i] = atof(values[i]);
}
for(i=0;i<MAX-1;i++){
for(j=i+1;j<MAX;j++){
if(isString==0 && val[i]>val[j]){
temp = val[j];
val[j] = val[i];
val[i] = temp;
}
else if(values[i][0]>values[j][0]){
strcpy(tempStr,values[j]);
strcpy(values[j],values[i]);
strcpy(values[i],tempStr);
}
}
}
for(i=0;i<MAX;i++)
isString==1?printf("%c = %s",'X'+i,values[i]):printf("%c = %lf",'X'+i,val[i]);
return 0;
}
#include<stdio.h>
int main()
{
int x = 77444,y=-12,z=0,temp;
printf("Before sorting :\nx = %d\ny = %d\nz = %d",x,y,z);
do{
temp = x;
if(temp > y){
x = y;
y = temp;
}
if(z < y){
temp = y;
y = z;
z = temp;
}
}while(x>y || y>z);
printf("\nAfter sorting :\nx = %d\ny = %d\nz = %d",x,y,z);
return 0;
}
You may also check:How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the C++ programming language
You may also check:How to resolve the algorithm Yellowstone sequence step by step in the Nim programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the Processing programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the Python programming language