Sort numbers based on the user input Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Servo commands based on serial port inputScalability of running commands from user inputSort and display input numbersSumming user inputGet initials from name input by userCustom user input functionSimple Lotto GameGeneric Pairing Heap PerformanceHandling user input - follow-upScan the user input and then work with these letters
How do I automatically answer y in bash script?
What is the electric potential inside a point charge?
How can I protect witches in combat who wear limited clothing?
Keep going mode for require-package
What are the performance impacts of 'functional' Rust?
How is simplicity better than precision and clarity in prose?
Strange behaviour of Check
What was Bilhah and Zilpah's ancestry?
What did Darwin mean by 'squib' here?
Do working physicists consider Newtonian mechanics to be "falsified"?
Two different pronunciation of "понял"
Problem when applying foreach loop
What's the difference between (size_t)-1 and ~0?
I'm having difficulty getting my players to do stuff in a sandbox campaign
Windows 10: How to Lock (not sleep) laptop on lid close?
Classification of bundles, Postnikov towers, obstruction theory, local coefficients
How does modal jazz use chord progressions?
Why is "Captain Marvel" translated as male in Portugal?
How did passengers keep warm on sail ships?
Was credit for the black hole image misattributed?
I'm thinking of a number
Can smartphones with the same camera sensor have different image quality?
Limit for e and 1/e
Working around an AWS network ACL rule limit
Sort numbers based on the user input
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Servo commands based on serial port inputScalability of running commands from user inputSort and display input numbersSumming user inputGet initials from name input by userCustom user input functionSimple Lotto GameGeneric Pairing Heap PerformanceHandling user input - follow-upScan the user input and then work with these letters
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
The purpose of the my program is to sort numbers based on the user input. Everything works perfectly, until user wants to input more than 6 data (inp = 5) and I am not sure why is it happening. Can you review it for best coding practices?
Code
#include <stdio.h>
#include <stdlib.h>
int size =0;
void ascSort(int a[size], int size, int temp)
for(int i=1; i< size; i++)
for(int j=i; j>0; j--)
if(a[j-1]>a[j])
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
void descSort(int a[size], int size, int temp)
for(int i=1; i< size; i++)
for(int j=i; j>0; j--)
if(a[j-1]<a[j])
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
int main()
char inpChar, isContinue='y';
int inp, temp=0;
int a[size];
do
printf("How many numbers would you like to sort?n");
fflush(stdin);
scanf("%d", &inp);
size = inp;
printf("%dn", size);
for(int i=0; i<size; i++)
//Below, I am printing 'size' for debugging purposes
printf("Type %d more numbers to sort ---- '%d'n", inp, size);
fflush(stdin);
scanf("%d", &a[i]);
inp--;
printf("Please input a for ascending order and d for descending ordern");
fflush(stdin);
scanf(" %c", &inpChar);
switch (inpChar)
case 'a':
ascSort(a, size, temp);
break;
case 'd':
descSort(a, size, temp);
break;
default: printf("Invalid charactern");
printf("Sorted Array: n" );
for(int i=0; i<size; i++)
printf("%dn", a[i]);
printf("Do you want to repeat? Type y/nn");
fflush(stdin);
scanf(" %c", &isContinue);
while(isContinue != 'n');
c
New contributor
Gaous Muhammad Saklaen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
The purpose of the my program is to sort numbers based on the user input. Everything works perfectly, until user wants to input more than 6 data (inp = 5) and I am not sure why is it happening. Can you review it for best coding practices?
Code
#include <stdio.h>
#include <stdlib.h>
int size =0;
void ascSort(int a[size], int size, int temp)
for(int i=1; i< size; i++)
for(int j=i; j>0; j--)
if(a[j-1]>a[j])
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
void descSort(int a[size], int size, int temp)
for(int i=1; i< size; i++)
for(int j=i; j>0; j--)
if(a[j-1]<a[j])
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
int main()
char inpChar, isContinue='y';
int inp, temp=0;
int a[size];
do
printf("How many numbers would you like to sort?n");
fflush(stdin);
scanf("%d", &inp);
size = inp;
printf("%dn", size);
for(int i=0; i<size; i++)
//Below, I am printing 'size' for debugging purposes
printf("Type %d more numbers to sort ---- '%d'n", inp, size);
fflush(stdin);
scanf("%d", &a[i]);
inp--;
printf("Please input a for ascending order and d for descending ordern");
fflush(stdin);
scanf(" %c", &inpChar);
switch (inpChar)
case 'a':
ascSort(a, size, temp);
break;
case 'd':
descSort(a, size, temp);
break;
default: printf("Invalid charactern");
printf("Sorted Array: n" );
for(int i=0; i<size; i++)
printf("%dn", a[i]);
printf("Do you want to repeat? Type y/nn");
fflush(stdin);
scanf(" %c", &isContinue);
while(isContinue != 'n');
c
New contributor
Gaous Muhammad Saklaen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
1
$begingroup$
Thanks a lot. Gonna do it now. Didn't know that before, by the way.
$endgroup$
– Gaous Muhammad Saklaen
6 mins ago
$begingroup$
You're very welcome!
$endgroup$
– Emma
3 mins ago
add a comment |
$begingroup$
The purpose of the my program is to sort numbers based on the user input. Everything works perfectly, until user wants to input more than 6 data (inp = 5) and I am not sure why is it happening. Can you review it for best coding practices?
Code
#include <stdio.h>
#include <stdlib.h>
int size =0;
void ascSort(int a[size], int size, int temp)
for(int i=1; i< size; i++)
for(int j=i; j>0; j--)
if(a[j-1]>a[j])
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
void descSort(int a[size], int size, int temp)
for(int i=1; i< size; i++)
for(int j=i; j>0; j--)
if(a[j-1]<a[j])
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
int main()
char inpChar, isContinue='y';
int inp, temp=0;
int a[size];
do
printf("How many numbers would you like to sort?n");
fflush(stdin);
scanf("%d", &inp);
size = inp;
printf("%dn", size);
for(int i=0; i<size; i++)
//Below, I am printing 'size' for debugging purposes
printf("Type %d more numbers to sort ---- '%d'n", inp, size);
fflush(stdin);
scanf("%d", &a[i]);
inp--;
printf("Please input a for ascending order and d for descending ordern");
fflush(stdin);
scanf(" %c", &inpChar);
switch (inpChar)
case 'a':
ascSort(a, size, temp);
break;
case 'd':
descSort(a, size, temp);
break;
default: printf("Invalid charactern");
printf("Sorted Array: n" );
for(int i=0; i<size; i++)
printf("%dn", a[i]);
printf("Do you want to repeat? Type y/nn");
fflush(stdin);
scanf(" %c", &isContinue);
while(isContinue != 'n');
c
New contributor
Gaous Muhammad Saklaen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
The purpose of the my program is to sort numbers based on the user input. Everything works perfectly, until user wants to input more than 6 data (inp = 5) and I am not sure why is it happening. Can you review it for best coding practices?
Code
#include <stdio.h>
#include <stdlib.h>
int size =0;
void ascSort(int a[size], int size, int temp)
for(int i=1; i< size; i++)
for(int j=i; j>0; j--)
if(a[j-1]>a[j])
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
void descSort(int a[size], int size, int temp)
for(int i=1; i< size; i++)
for(int j=i; j>0; j--)
if(a[j-1]<a[j])
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
int main()
char inpChar, isContinue='y';
int inp, temp=0;
int a[size];
do
printf("How many numbers would you like to sort?n");
fflush(stdin);
scanf("%d", &inp);
size = inp;
printf("%dn", size);
for(int i=0; i<size; i++)
//Below, I am printing 'size' for debugging purposes
printf("Type %d more numbers to sort ---- '%d'n", inp, size);
fflush(stdin);
scanf("%d", &a[i]);
inp--;
printf("Please input a for ascending order and d for descending ordern");
fflush(stdin);
scanf(" %c", &inpChar);
switch (inpChar)
case 'a':
ascSort(a, size, temp);
break;
case 'd':
descSort(a, size, temp);
break;
default: printf("Invalid charactern");
printf("Sorted Array: n" );
for(int i=0; i<size; i++)
printf("%dn", a[i]);
printf("Do you want to repeat? Type y/nn");
fflush(stdin);
scanf(" %c", &isContinue);
while(isContinue != 'n');
c
c
New contributor
Gaous Muhammad Saklaen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Gaous Muhammad Saklaen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 10 mins ago
Emma
2021215
2021215
New contributor
Gaous Muhammad Saklaen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 43 mins ago
Gaous Muhammad SaklaenGaous Muhammad Saklaen
12
12
New contributor
Gaous Muhammad Saklaen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Gaous Muhammad Saklaen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Gaous Muhammad Saklaen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
$begingroup$
Thanks a lot. Gonna do it now. Didn't know that before, by the way.
$endgroup$
– Gaous Muhammad Saklaen
6 mins ago
$begingroup$
You're very welcome!
$endgroup$
– Emma
3 mins ago
add a comment |
1
$begingroup$
Thanks a lot. Gonna do it now. Didn't know that before, by the way.
$endgroup$
– Gaous Muhammad Saklaen
6 mins ago
$begingroup$
You're very welcome!
$endgroup$
– Emma
3 mins ago
1
1
$begingroup$
Thanks a lot. Gonna do it now. Didn't know that before, by the way.
$endgroup$
– Gaous Muhammad Saklaen
6 mins ago
$begingroup$
Thanks a lot. Gonna do it now. Didn't know that before, by the way.
$endgroup$
– Gaous Muhammad Saklaen
6 mins ago
$begingroup$
You're very welcome!
$endgroup$
– Emma
3 mins ago
$begingroup$
You're very welcome!
$endgroup$
– Emma
3 mins ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Gaous Muhammad Saklaen is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217460%2fsort-numbers-based-on-the-user-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Gaous Muhammad Saklaen is a new contributor. Be nice, and check out our Code of Conduct.
Gaous Muhammad Saklaen is a new contributor. Be nice, and check out our Code of Conduct.
Gaous Muhammad Saklaen is a new contributor. Be nice, and check out our Code of Conduct.
Gaous Muhammad Saklaen is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217460%2fsort-numbers-based-on-the-user-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Thanks a lot. Gonna do it now. Didn't know that before, by the way.
$endgroup$
– Gaous Muhammad Saklaen
6 mins ago
$begingroup$
You're very welcome!
$endgroup$
– Emma
3 mins ago