Sort numbers based on the user input Announcing the arrival of Valued Associate #679: Cesar...
Is above average number of years spent on PhD considered a red flag in future academia or industry positions?
Writing Thesis: Copying from published papers
Passing functions in C++
How to pour concrete for curved walkway to prevent cracking?
What LEGO pieces have "real-world" functionality?
What did Darwin mean by 'squib' here?
How do I automatically answer y in bash script?
Interesting examples of non-locally compact topological groups
Using "nakedly" instead of "with nothing on"
What was the last x86 CPU that did not have the x87 floating-point unit built in?
3 doors, three guards, one stone
Can't figure this one out.. What is the missing box?
Did the new image of black hole confirm the general theory of relativity?
If A makes B more likely then B makes A more likely"
How should I respond to a player wanting to catch a sword between their hands?
Array/tabular for long multiplication
Classification of bundles, Postnikov towers, obstruction theory, local coefficients
How to retrograde a note sequence in Finale?
Why use gamma over alpha radiation?
How to say that you spent the night with someone, you were only sleeping and nothing else?
What is the electric potential inside a point charge?
Make it rain characters
Complexity of many constant time steps with occasional logarithmic steps
How did the aliens keep their waters separated?
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)Simple Lotto GameGeneric Pairing Heap Performance
.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
4 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
4 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
4 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
4 mins ago
$begingroup$
You're very welcome!
$endgroup$
– Emma
4 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');
});
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');
});
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');
});
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');
});
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');
});
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
4 mins ago