Sorting array C program
I need to make a program which sorts out a given array in such a way that the numbers with the same digits will be in front. The order must remain the same. It would be a big problem but I'm not allowed to use any addiodional arrays, functions etc. I really don't know how to sort out the numbers so that the order remains the same. And also the array is max 100 elements.
Example:
Input:
1 22 43 444 51 16 7 8888 90 11
Output:
1,22,444,7,8888,11,43,51,16,90.
I've written this so far:
#include <stdio.h>
int main()
{
int a = 0, i = 0, niz[100], temp, N, j, logika, cifra1, cifra2, brojac = 0, brojac2 = 1;
printf("Unesite brojeve: n");
do {
scanf("%d", &niz[i]);
if (niz[i] == -1) {
i--;
break;
}
i++;
} while (i < 100);
N = i;
for (i = 0; i < N; i++) {
a = niz[i];
logika = 1;
cifra1 = a % 10;
cifra2 = niz[i] / 10;
while (cifra2) {
if (cifra2 % 10 != cifra1) {
logika = 0;
break;
}
cifra2 = cifra2 / 10;
}
if (a / 10 == 0) logika = 1;
if (logika == 1) {
niz[brojac++] = niz[i];
}
if (logika == 0) {
niz[i] = temp;
niz[N - 1] = niz[i];
niz[N - i] = temp;
}
}
printf("Nakon preslaganja niz glasi: n");
for (i = 0; i <= N; i++) {
if (i < N)
printf("%d,", niz[i]);
else {
printf("%d.", niz[i]);
}
}
return 0;
}
c arrays sorting
|
show 1 more comment
I need to make a program which sorts out a given array in such a way that the numbers with the same digits will be in front. The order must remain the same. It would be a big problem but I'm not allowed to use any addiodional arrays, functions etc. I really don't know how to sort out the numbers so that the order remains the same. And also the array is max 100 elements.
Example:
Input:
1 22 43 444 51 16 7 8888 90 11
Output:
1,22,444,7,8888,11,43,51,16,90.
I've written this so far:
#include <stdio.h>
int main()
{
int a = 0, i = 0, niz[100], temp, N, j, logika, cifra1, cifra2, brojac = 0, brojac2 = 1;
printf("Unesite brojeve: n");
do {
scanf("%d", &niz[i]);
if (niz[i] == -1) {
i--;
break;
}
i++;
} while (i < 100);
N = i;
for (i = 0; i < N; i++) {
a = niz[i];
logika = 1;
cifra1 = a % 10;
cifra2 = niz[i] / 10;
while (cifra2) {
if (cifra2 % 10 != cifra1) {
logika = 0;
break;
}
cifra2 = cifra2 / 10;
}
if (a / 10 == 0) logika = 1;
if (logika == 1) {
niz[brojac++] = niz[i];
}
if (logika == 0) {
niz[i] = temp;
niz[N - 1] = niz[i];
niz[N - i] = temp;
}
}
printf("Nakon preslaganja niz glasi: n");
for (i = 0; i <= N; i++) {
if (i < N)
printf("%d,", niz[i]);
else {
printf("%d.", niz[i]);
}
}
return 0;
}
c arrays sorting
probably you mean putting the sorted order into a new array while keeping the original array untouched? otherwise it doesn't make sense
– mangusta
Nov 23 at 0:39
I mean to replace the elements of the given array to make the repdigits first.
– syd
Nov 23 at 0:46
Someone asked this question a week ago. Though they didn't get an actual answer — I've not written one up yet because my code uses one of the quadratic sorts. Using a stable sort (bubble, insert, merge) and appropriate predicate functions to test whether the numbers both have all digits the same or not allows the sort to be done. Not using any (user-defined) functions is too painful to even think about — I decline point blank to have anything to do with such nonsense.
– Jonathan Leffler
Nov 23 at 1:19
@JonathanLeffler I decline point blank to have anything to do with such nonsense. – I don't have that much dignity :/
– Swordfish
Nov 23 at 2:34
@Swordfish: One advantage antiquity and decrepitude is that I can make such decisions on the fly without significant risk to my university career (which almost certainly ended longer ago than the OP has been alive).
– Jonathan Leffler
Nov 23 at 2:53
|
show 1 more comment
I need to make a program which sorts out a given array in such a way that the numbers with the same digits will be in front. The order must remain the same. It would be a big problem but I'm not allowed to use any addiodional arrays, functions etc. I really don't know how to sort out the numbers so that the order remains the same. And also the array is max 100 elements.
Example:
Input:
1 22 43 444 51 16 7 8888 90 11
Output:
1,22,444,7,8888,11,43,51,16,90.
I've written this so far:
#include <stdio.h>
int main()
{
int a = 0, i = 0, niz[100], temp, N, j, logika, cifra1, cifra2, brojac = 0, brojac2 = 1;
printf("Unesite brojeve: n");
do {
scanf("%d", &niz[i]);
if (niz[i] == -1) {
i--;
break;
}
i++;
} while (i < 100);
N = i;
for (i = 0; i < N; i++) {
a = niz[i];
logika = 1;
cifra1 = a % 10;
cifra2 = niz[i] / 10;
while (cifra2) {
if (cifra2 % 10 != cifra1) {
logika = 0;
break;
}
cifra2 = cifra2 / 10;
}
if (a / 10 == 0) logika = 1;
if (logika == 1) {
niz[brojac++] = niz[i];
}
if (logika == 0) {
niz[i] = temp;
niz[N - 1] = niz[i];
niz[N - i] = temp;
}
}
printf("Nakon preslaganja niz glasi: n");
for (i = 0; i <= N; i++) {
if (i < N)
printf("%d,", niz[i]);
else {
printf("%d.", niz[i]);
}
}
return 0;
}
c arrays sorting
I need to make a program which sorts out a given array in such a way that the numbers with the same digits will be in front. The order must remain the same. It would be a big problem but I'm not allowed to use any addiodional arrays, functions etc. I really don't know how to sort out the numbers so that the order remains the same. And also the array is max 100 elements.
Example:
Input:
1 22 43 444 51 16 7 8888 90 11
Output:
1,22,444,7,8888,11,43,51,16,90.
I've written this so far:
#include <stdio.h>
int main()
{
int a = 0, i = 0, niz[100], temp, N, j, logika, cifra1, cifra2, brojac = 0, brojac2 = 1;
printf("Unesite brojeve: n");
do {
scanf("%d", &niz[i]);
if (niz[i] == -1) {
i--;
break;
}
i++;
} while (i < 100);
N = i;
for (i = 0; i < N; i++) {
a = niz[i];
logika = 1;
cifra1 = a % 10;
cifra2 = niz[i] / 10;
while (cifra2) {
if (cifra2 % 10 != cifra1) {
logika = 0;
break;
}
cifra2 = cifra2 / 10;
}
if (a / 10 == 0) logika = 1;
if (logika == 1) {
niz[brojac++] = niz[i];
}
if (logika == 0) {
niz[i] = temp;
niz[N - 1] = niz[i];
niz[N - i] = temp;
}
}
printf("Nakon preslaganja niz glasi: n");
for (i = 0; i <= N; i++) {
if (i < N)
printf("%d,", niz[i]);
else {
printf("%d.", niz[i]);
}
}
return 0;
}
c arrays sorting
c arrays sorting
edited Nov 23 at 2:00
Swordfish
1
1
asked Nov 23 at 0:30
syd
11
11
probably you mean putting the sorted order into a new array while keeping the original array untouched? otherwise it doesn't make sense
– mangusta
Nov 23 at 0:39
I mean to replace the elements of the given array to make the repdigits first.
– syd
Nov 23 at 0:46
Someone asked this question a week ago. Though they didn't get an actual answer — I've not written one up yet because my code uses one of the quadratic sorts. Using a stable sort (bubble, insert, merge) and appropriate predicate functions to test whether the numbers both have all digits the same or not allows the sort to be done. Not using any (user-defined) functions is too painful to even think about — I decline point blank to have anything to do with such nonsense.
– Jonathan Leffler
Nov 23 at 1:19
@JonathanLeffler I decline point blank to have anything to do with such nonsense. – I don't have that much dignity :/
– Swordfish
Nov 23 at 2:34
@Swordfish: One advantage antiquity and decrepitude is that I can make such decisions on the fly without significant risk to my university career (which almost certainly ended longer ago than the OP has been alive).
– Jonathan Leffler
Nov 23 at 2:53
|
show 1 more comment
probably you mean putting the sorted order into a new array while keeping the original array untouched? otherwise it doesn't make sense
– mangusta
Nov 23 at 0:39
I mean to replace the elements of the given array to make the repdigits first.
– syd
Nov 23 at 0:46
Someone asked this question a week ago. Though they didn't get an actual answer — I've not written one up yet because my code uses one of the quadratic sorts. Using a stable sort (bubble, insert, merge) and appropriate predicate functions to test whether the numbers both have all digits the same or not allows the sort to be done. Not using any (user-defined) functions is too painful to even think about — I decline point blank to have anything to do with such nonsense.
– Jonathan Leffler
Nov 23 at 1:19
@JonathanLeffler I decline point blank to have anything to do with such nonsense. – I don't have that much dignity :/
– Swordfish
Nov 23 at 2:34
@Swordfish: One advantage antiquity and decrepitude is that I can make such decisions on the fly without significant risk to my university career (which almost certainly ended longer ago than the OP has been alive).
– Jonathan Leffler
Nov 23 at 2:53
probably you mean putting the sorted order into a new array while keeping the original array untouched? otherwise it doesn't make sense
– mangusta
Nov 23 at 0:39
probably you mean putting the sorted order into a new array while keeping the original array untouched? otherwise it doesn't make sense
– mangusta
Nov 23 at 0:39
I mean to replace the elements of the given array to make the repdigits first.
– syd
Nov 23 at 0:46
I mean to replace the elements of the given array to make the repdigits first.
– syd
Nov 23 at 0:46
Someone asked this question a week ago. Though they didn't get an actual answer — I've not written one up yet because my code uses one of the quadratic sorts. Using a stable sort (bubble, insert, merge) and appropriate predicate functions to test whether the numbers both have all digits the same or not allows the sort to be done. Not using any (user-defined) functions is too painful to even think about — I decline point blank to have anything to do with such nonsense.
– Jonathan Leffler
Nov 23 at 1:19
Someone asked this question a week ago. Though they didn't get an actual answer — I've not written one up yet because my code uses one of the quadratic sorts. Using a stable sort (bubble, insert, merge) and appropriate predicate functions to test whether the numbers both have all digits the same or not allows the sort to be done. Not using any (user-defined) functions is too painful to even think about — I decline point blank to have anything to do with such nonsense.
– Jonathan Leffler
Nov 23 at 1:19
@JonathanLeffler I decline point blank to have anything to do with such nonsense. – I don't have that much dignity :/
– Swordfish
Nov 23 at 2:34
@JonathanLeffler I decline point blank to have anything to do with such nonsense. – I don't have that much dignity :/
– Swordfish
Nov 23 at 2:34
@Swordfish: One advantage antiquity and decrepitude is that I can make such decisions on the fly without significant risk to my university career (which almost certainly ended longer ago than the OP has been alive).
– Jonathan Leffler
Nov 23 at 2:53
@Swordfish: One advantage antiquity and decrepitude is that I can make such decisions on the fly without significant risk to my university career (which almost certainly ended longer ago than the OP has been alive).
– Jonathan Leffler
Nov 23 at 2:53
|
show 1 more comment
2 Answers
2
active
oldest
votes
#include <stdio.h>
enum { MAX_NUMBERS = 100 };
int main(void)
{
int numbers[MAX_NUMBERS];
int numbers_count = 0;
int input;
puts("Please enter your numbers, end input with -1:");
while (numbers_count < MAX_NUMBERS && scanf(" %d", &input) == 1 && input != -1)
numbers[numbers_count++] = input;
int current_insertion_point = 0; // where to insert
for (int i = 0; i < numbers_count; ++i) {
int all_digits_the_same = 1;
// check if number only contains one probably repeated digit:
int current_number = numbers[i];
int last_digit = -1;
while (current_number && all_digits_the_same) {
int current_digit = current_number % 10;
current_number /= 10;
if (last_digit != -1 && current_digit != last_digit)
all_digits_the_same = 0;
last_digit = current_digit;
}
// insert the number:
if (all_digits_the_same) {
int temp = numbers[i];
// nove the range current_insertion_point ... i to the right
for (int k = i; k > current_insertion_point; --k)
numbers[k] = numbers[k - 1];
// so there is space to insert the number previously numbers[i]
numbers[current_insertion_point++] = temp;
}
}
for (int i = 0; i < numbers_count; ++i)
printf("%d ", numbers[i]);
putchar('n');
}
Output:
Please enter your numbers, end input with -1:
1 22 43 444 51 16 7 8888 90 11 -1
1 22 444 7 8888 11 43 51 16 90
add a comment |
I have done selection sort.It's not giving output as you expecting exactly.
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Enter Size Of Array";
cin>>x;
const int SIZE=x;
int a[SIZE];
int copy[SIZE];
cout<<"Enter "<<SIZE<<" Value";
for(int i=0;i<SIZE;i++){
cin>>a[i];
}
for(int i=0;i<SIZE;i++){
copy[i]=a[i];
}
for(int i=0;i<SIZE;i++){
int n=a[i];
int temp;
int rem=n%10;
bool isAllDigitSame=false;
while(n!=0){
if(rem==n%10){
isAllDigitSame=true;
}
else{
isAllDigitSame=false;
break;
}
n=n/10;
}
if(isAllDigitSame){
temp=a[i];
a[i] = a[i]%10;
}
}
cout<<"A="<<endl;
for(int i=0;i<SIZE;i++){
cout<<a[i]<<endl;
}
cout<<endl;
for(int i=0;i<SIZE;i++){
int min=i;
for(int j=i;j<SIZE;j++){
if(a[j]<a[min]){
min=j;
}
}
int temp;
temp=copy[i];
copy[i]=copy[min];
copy[min]=temp;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
cout<<"Copy="<<endl;
for(int i=0;i<SIZE;i++){
cout<<copy[i]<<endl;
}
return 0;
}
It is sorting other element also.
Input :1 22 43 444 51 16 7 8888 90 11
Output: 1 11 22 444 7 8888 16 43 51 90
Welcome to Stack Overflow. Please read the About and How to Answer pages soon. You have written C++ code as an answer to a question tagged for C only. This is not helpful as your code certainly won't compile with a pure C compiler. Additionally, as you yourself admit, the output from your code isn't what the OP wants. Within the 'same digits' numbers, the output sequence must be the same as in the input sequence, and within the 'different digits' numbers, the output sequence must also be the same as the input sequence. Your output moves11before22,444,7and8888— oops!
– Jonathan Leffler
Nov 23 at 17:02
I recommend deleting this answer until you can fix the issues — use C library functions and headers rather than C++, and make the algorithm work correctly. (It can be done!)
– Jonathan Leffler
Nov 23 at 17:03
add a comment |
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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53439395%2fsorting-array-c-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
#include <stdio.h>
enum { MAX_NUMBERS = 100 };
int main(void)
{
int numbers[MAX_NUMBERS];
int numbers_count = 0;
int input;
puts("Please enter your numbers, end input with -1:");
while (numbers_count < MAX_NUMBERS && scanf(" %d", &input) == 1 && input != -1)
numbers[numbers_count++] = input;
int current_insertion_point = 0; // where to insert
for (int i = 0; i < numbers_count; ++i) {
int all_digits_the_same = 1;
// check if number only contains one probably repeated digit:
int current_number = numbers[i];
int last_digit = -1;
while (current_number && all_digits_the_same) {
int current_digit = current_number % 10;
current_number /= 10;
if (last_digit != -1 && current_digit != last_digit)
all_digits_the_same = 0;
last_digit = current_digit;
}
// insert the number:
if (all_digits_the_same) {
int temp = numbers[i];
// nove the range current_insertion_point ... i to the right
for (int k = i; k > current_insertion_point; --k)
numbers[k] = numbers[k - 1];
// so there is space to insert the number previously numbers[i]
numbers[current_insertion_point++] = temp;
}
}
for (int i = 0; i < numbers_count; ++i)
printf("%d ", numbers[i]);
putchar('n');
}
Output:
Please enter your numbers, end input with -1:
1 22 43 444 51 16 7 8888 90 11 -1
1 22 444 7 8888 11 43 51 16 90
add a comment |
#include <stdio.h>
enum { MAX_NUMBERS = 100 };
int main(void)
{
int numbers[MAX_NUMBERS];
int numbers_count = 0;
int input;
puts("Please enter your numbers, end input with -1:");
while (numbers_count < MAX_NUMBERS && scanf(" %d", &input) == 1 && input != -1)
numbers[numbers_count++] = input;
int current_insertion_point = 0; // where to insert
for (int i = 0; i < numbers_count; ++i) {
int all_digits_the_same = 1;
// check if number only contains one probably repeated digit:
int current_number = numbers[i];
int last_digit = -1;
while (current_number && all_digits_the_same) {
int current_digit = current_number % 10;
current_number /= 10;
if (last_digit != -1 && current_digit != last_digit)
all_digits_the_same = 0;
last_digit = current_digit;
}
// insert the number:
if (all_digits_the_same) {
int temp = numbers[i];
// nove the range current_insertion_point ... i to the right
for (int k = i; k > current_insertion_point; --k)
numbers[k] = numbers[k - 1];
// so there is space to insert the number previously numbers[i]
numbers[current_insertion_point++] = temp;
}
}
for (int i = 0; i < numbers_count; ++i)
printf("%d ", numbers[i]);
putchar('n');
}
Output:
Please enter your numbers, end input with -1:
1 22 43 444 51 16 7 8888 90 11 -1
1 22 444 7 8888 11 43 51 16 90
add a comment |
#include <stdio.h>
enum { MAX_NUMBERS = 100 };
int main(void)
{
int numbers[MAX_NUMBERS];
int numbers_count = 0;
int input;
puts("Please enter your numbers, end input with -1:");
while (numbers_count < MAX_NUMBERS && scanf(" %d", &input) == 1 && input != -1)
numbers[numbers_count++] = input;
int current_insertion_point = 0; // where to insert
for (int i = 0; i < numbers_count; ++i) {
int all_digits_the_same = 1;
// check if number only contains one probably repeated digit:
int current_number = numbers[i];
int last_digit = -1;
while (current_number && all_digits_the_same) {
int current_digit = current_number % 10;
current_number /= 10;
if (last_digit != -1 && current_digit != last_digit)
all_digits_the_same = 0;
last_digit = current_digit;
}
// insert the number:
if (all_digits_the_same) {
int temp = numbers[i];
// nove the range current_insertion_point ... i to the right
for (int k = i; k > current_insertion_point; --k)
numbers[k] = numbers[k - 1];
// so there is space to insert the number previously numbers[i]
numbers[current_insertion_point++] = temp;
}
}
for (int i = 0; i < numbers_count; ++i)
printf("%d ", numbers[i]);
putchar('n');
}
Output:
Please enter your numbers, end input with -1:
1 22 43 444 51 16 7 8888 90 11 -1
1 22 444 7 8888 11 43 51 16 90
#include <stdio.h>
enum { MAX_NUMBERS = 100 };
int main(void)
{
int numbers[MAX_NUMBERS];
int numbers_count = 0;
int input;
puts("Please enter your numbers, end input with -1:");
while (numbers_count < MAX_NUMBERS && scanf(" %d", &input) == 1 && input != -1)
numbers[numbers_count++] = input;
int current_insertion_point = 0; // where to insert
for (int i = 0; i < numbers_count; ++i) {
int all_digits_the_same = 1;
// check if number only contains one probably repeated digit:
int current_number = numbers[i];
int last_digit = -1;
while (current_number && all_digits_the_same) {
int current_digit = current_number % 10;
current_number /= 10;
if (last_digit != -1 && current_digit != last_digit)
all_digits_the_same = 0;
last_digit = current_digit;
}
// insert the number:
if (all_digits_the_same) {
int temp = numbers[i];
// nove the range current_insertion_point ... i to the right
for (int k = i; k > current_insertion_point; --k)
numbers[k] = numbers[k - 1];
// so there is space to insert the number previously numbers[i]
numbers[current_insertion_point++] = temp;
}
}
for (int i = 0; i < numbers_count; ++i)
printf("%d ", numbers[i]);
putchar('n');
}
Output:
Please enter your numbers, end input with -1:
1 22 43 444 51 16 7 8888 90 11 -1
1 22 444 7 8888 11 43 51 16 90
edited Nov 23 at 2:37
answered Nov 23 at 2:29
Swordfish
1
1
add a comment |
add a comment |
I have done selection sort.It's not giving output as you expecting exactly.
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Enter Size Of Array";
cin>>x;
const int SIZE=x;
int a[SIZE];
int copy[SIZE];
cout<<"Enter "<<SIZE<<" Value";
for(int i=0;i<SIZE;i++){
cin>>a[i];
}
for(int i=0;i<SIZE;i++){
copy[i]=a[i];
}
for(int i=0;i<SIZE;i++){
int n=a[i];
int temp;
int rem=n%10;
bool isAllDigitSame=false;
while(n!=0){
if(rem==n%10){
isAllDigitSame=true;
}
else{
isAllDigitSame=false;
break;
}
n=n/10;
}
if(isAllDigitSame){
temp=a[i];
a[i] = a[i]%10;
}
}
cout<<"A="<<endl;
for(int i=0;i<SIZE;i++){
cout<<a[i]<<endl;
}
cout<<endl;
for(int i=0;i<SIZE;i++){
int min=i;
for(int j=i;j<SIZE;j++){
if(a[j]<a[min]){
min=j;
}
}
int temp;
temp=copy[i];
copy[i]=copy[min];
copy[min]=temp;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
cout<<"Copy="<<endl;
for(int i=0;i<SIZE;i++){
cout<<copy[i]<<endl;
}
return 0;
}
It is sorting other element also.
Input :1 22 43 444 51 16 7 8888 90 11
Output: 1 11 22 444 7 8888 16 43 51 90
Welcome to Stack Overflow. Please read the About and How to Answer pages soon. You have written C++ code as an answer to a question tagged for C only. This is not helpful as your code certainly won't compile with a pure C compiler. Additionally, as you yourself admit, the output from your code isn't what the OP wants. Within the 'same digits' numbers, the output sequence must be the same as in the input sequence, and within the 'different digits' numbers, the output sequence must also be the same as the input sequence. Your output moves11before22,444,7and8888— oops!
– Jonathan Leffler
Nov 23 at 17:02
I recommend deleting this answer until you can fix the issues — use C library functions and headers rather than C++, and make the algorithm work correctly. (It can be done!)
– Jonathan Leffler
Nov 23 at 17:03
add a comment |
I have done selection sort.It's not giving output as you expecting exactly.
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Enter Size Of Array";
cin>>x;
const int SIZE=x;
int a[SIZE];
int copy[SIZE];
cout<<"Enter "<<SIZE<<" Value";
for(int i=0;i<SIZE;i++){
cin>>a[i];
}
for(int i=0;i<SIZE;i++){
copy[i]=a[i];
}
for(int i=0;i<SIZE;i++){
int n=a[i];
int temp;
int rem=n%10;
bool isAllDigitSame=false;
while(n!=0){
if(rem==n%10){
isAllDigitSame=true;
}
else{
isAllDigitSame=false;
break;
}
n=n/10;
}
if(isAllDigitSame){
temp=a[i];
a[i] = a[i]%10;
}
}
cout<<"A="<<endl;
for(int i=0;i<SIZE;i++){
cout<<a[i]<<endl;
}
cout<<endl;
for(int i=0;i<SIZE;i++){
int min=i;
for(int j=i;j<SIZE;j++){
if(a[j]<a[min]){
min=j;
}
}
int temp;
temp=copy[i];
copy[i]=copy[min];
copy[min]=temp;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
cout<<"Copy="<<endl;
for(int i=0;i<SIZE;i++){
cout<<copy[i]<<endl;
}
return 0;
}
It is sorting other element also.
Input :1 22 43 444 51 16 7 8888 90 11
Output: 1 11 22 444 7 8888 16 43 51 90
Welcome to Stack Overflow. Please read the About and How to Answer pages soon. You have written C++ code as an answer to a question tagged for C only. This is not helpful as your code certainly won't compile with a pure C compiler. Additionally, as you yourself admit, the output from your code isn't what the OP wants. Within the 'same digits' numbers, the output sequence must be the same as in the input sequence, and within the 'different digits' numbers, the output sequence must also be the same as the input sequence. Your output moves11before22,444,7and8888— oops!
– Jonathan Leffler
Nov 23 at 17:02
I recommend deleting this answer until you can fix the issues — use C library functions and headers rather than C++, and make the algorithm work correctly. (It can be done!)
– Jonathan Leffler
Nov 23 at 17:03
add a comment |
I have done selection sort.It's not giving output as you expecting exactly.
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Enter Size Of Array";
cin>>x;
const int SIZE=x;
int a[SIZE];
int copy[SIZE];
cout<<"Enter "<<SIZE<<" Value";
for(int i=0;i<SIZE;i++){
cin>>a[i];
}
for(int i=0;i<SIZE;i++){
copy[i]=a[i];
}
for(int i=0;i<SIZE;i++){
int n=a[i];
int temp;
int rem=n%10;
bool isAllDigitSame=false;
while(n!=0){
if(rem==n%10){
isAllDigitSame=true;
}
else{
isAllDigitSame=false;
break;
}
n=n/10;
}
if(isAllDigitSame){
temp=a[i];
a[i] = a[i]%10;
}
}
cout<<"A="<<endl;
for(int i=0;i<SIZE;i++){
cout<<a[i]<<endl;
}
cout<<endl;
for(int i=0;i<SIZE;i++){
int min=i;
for(int j=i;j<SIZE;j++){
if(a[j]<a[min]){
min=j;
}
}
int temp;
temp=copy[i];
copy[i]=copy[min];
copy[min]=temp;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
cout<<"Copy="<<endl;
for(int i=0;i<SIZE;i++){
cout<<copy[i]<<endl;
}
return 0;
}
It is sorting other element also.
Input :1 22 43 444 51 16 7 8888 90 11
Output: 1 11 22 444 7 8888 16 43 51 90
I have done selection sort.It's not giving output as you expecting exactly.
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Enter Size Of Array";
cin>>x;
const int SIZE=x;
int a[SIZE];
int copy[SIZE];
cout<<"Enter "<<SIZE<<" Value";
for(int i=0;i<SIZE;i++){
cin>>a[i];
}
for(int i=0;i<SIZE;i++){
copy[i]=a[i];
}
for(int i=0;i<SIZE;i++){
int n=a[i];
int temp;
int rem=n%10;
bool isAllDigitSame=false;
while(n!=0){
if(rem==n%10){
isAllDigitSame=true;
}
else{
isAllDigitSame=false;
break;
}
n=n/10;
}
if(isAllDigitSame){
temp=a[i];
a[i] = a[i]%10;
}
}
cout<<"A="<<endl;
for(int i=0;i<SIZE;i++){
cout<<a[i]<<endl;
}
cout<<endl;
for(int i=0;i<SIZE;i++){
int min=i;
for(int j=i;j<SIZE;j++){
if(a[j]<a[min]){
min=j;
}
}
int temp;
temp=copy[i];
copy[i]=copy[min];
copy[min]=temp;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
cout<<"Copy="<<endl;
for(int i=0;i<SIZE;i++){
cout<<copy[i]<<endl;
}
return 0;
}
It is sorting other element also.
Input :1 22 43 444 51 16 7 8888 90 11
Output: 1 11 22 444 7 8888 16 43 51 90
answered Nov 23 at 13:03
the summer
13
13
Welcome to Stack Overflow. Please read the About and How to Answer pages soon. You have written C++ code as an answer to a question tagged for C only. This is not helpful as your code certainly won't compile with a pure C compiler. Additionally, as you yourself admit, the output from your code isn't what the OP wants. Within the 'same digits' numbers, the output sequence must be the same as in the input sequence, and within the 'different digits' numbers, the output sequence must also be the same as the input sequence. Your output moves11before22,444,7and8888— oops!
– Jonathan Leffler
Nov 23 at 17:02
I recommend deleting this answer until you can fix the issues — use C library functions and headers rather than C++, and make the algorithm work correctly. (It can be done!)
– Jonathan Leffler
Nov 23 at 17:03
add a comment |
Welcome to Stack Overflow. Please read the About and How to Answer pages soon. You have written C++ code as an answer to a question tagged for C only. This is not helpful as your code certainly won't compile with a pure C compiler. Additionally, as you yourself admit, the output from your code isn't what the OP wants. Within the 'same digits' numbers, the output sequence must be the same as in the input sequence, and within the 'different digits' numbers, the output sequence must also be the same as the input sequence. Your output moves11before22,444,7and8888— oops!
– Jonathan Leffler
Nov 23 at 17:02
I recommend deleting this answer until you can fix the issues — use C library functions and headers rather than C++, and make the algorithm work correctly. (It can be done!)
– Jonathan Leffler
Nov 23 at 17:03
Welcome to Stack Overflow. Please read the About and How to Answer pages soon. You have written C++ code as an answer to a question tagged for C only. This is not helpful as your code certainly won't compile with a pure C compiler. Additionally, as you yourself admit, the output from your code isn't what the OP wants. Within the 'same digits' numbers, the output sequence must be the same as in the input sequence, and within the 'different digits' numbers, the output sequence must also be the same as the input sequence. Your output moves
11 before 22, 444, 7 and 8888 — oops!– Jonathan Leffler
Nov 23 at 17:02
Welcome to Stack Overflow. Please read the About and How to Answer pages soon. You have written C++ code as an answer to a question tagged for C only. This is not helpful as your code certainly won't compile with a pure C compiler. Additionally, as you yourself admit, the output from your code isn't what the OP wants. Within the 'same digits' numbers, the output sequence must be the same as in the input sequence, and within the 'different digits' numbers, the output sequence must also be the same as the input sequence. Your output moves
11 before 22, 444, 7 and 8888 — oops!– Jonathan Leffler
Nov 23 at 17:02
I recommend deleting this answer until you can fix the issues — use C library functions and headers rather than C++, and make the algorithm work correctly. (It can be done!)
– Jonathan Leffler
Nov 23 at 17:03
I recommend deleting this answer until you can fix the issues — use C library functions and headers rather than C++, and make the algorithm work correctly. (It can be done!)
– Jonathan Leffler
Nov 23 at 17:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2fstackoverflow.com%2fquestions%2f53439395%2fsorting-array-c-program%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
probably you mean putting the sorted order into a new array while keeping the original array untouched? otherwise it doesn't make sense
– mangusta
Nov 23 at 0:39
I mean to replace the elements of the given array to make the repdigits first.
– syd
Nov 23 at 0:46
Someone asked this question a week ago. Though they didn't get an actual answer — I've not written one up yet because my code uses one of the quadratic sorts. Using a stable sort (bubble, insert, merge) and appropriate predicate functions to test whether the numbers both have all digits the same or not allows the sort to be done. Not using any (user-defined) functions is too painful to even think about — I decline point blank to have anything to do with such nonsense.
– Jonathan Leffler
Nov 23 at 1:19
@JonathanLeffler I decline point blank to have anything to do with such nonsense. – I don't have that much dignity :/
– Swordfish
Nov 23 at 2:34
@Swordfish: One advantage antiquity and decrepitude is that I can make such decisions on the fly without significant risk to my university career (which almost certainly ended longer ago than the OP has been alive).
– Jonathan Leffler
Nov 23 at 2:53