Dividing array elements on digits - problem with crashing
I'm a beginner who has unfortunately encountered a problem I cannot solve :) I apologize in advance since English is not my first language.
My task is to sort an array so that the elements with the same digits come first, followed by the rest of an array.
Example:
Input array: 1 22 43 444 51 16 7 8888 90 11 -1
After sorting: 1,22,444,7,8888,11,43,51,16,90
Input array: 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13
The catch is, we are not allowed to use functions or more than one array to solve the problem.
Here's what I've already tried:
for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
digit=a[j]%10;
while (a[j]>0)
{
if (a[j]%10!=digit)
same=0;
a[j]/=10;
}
digit1=a[j+1]%10;
while (a[j+1]>0)
{
if (a[j+1]%10!=digit1)
same1=0;
a[j+1]/=10;
}
if (same==0 && same1==1)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
I'm not sure what's wrong with the code and why the program crashes. Thank you.
arrays sorting crash
add a comment |
I'm a beginner who has unfortunately encountered a problem I cannot solve :) I apologize in advance since English is not my first language.
My task is to sort an array so that the elements with the same digits come first, followed by the rest of an array.
Example:
Input array: 1 22 43 444 51 16 7 8888 90 11 -1
After sorting: 1,22,444,7,8888,11,43,51,16,90
Input array: 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13
The catch is, we are not allowed to use functions or more than one array to solve the problem.
Here's what I've already tried:
for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
digit=a[j]%10;
while (a[j]>0)
{
if (a[j]%10!=digit)
same=0;
a[j]/=10;
}
digit1=a[j+1]%10;
while (a[j+1]>0)
{
if (a[j+1]%10!=digit1)
same1=0;
a[j+1]/=10;
}
if (same==0 && same1==1)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
I'm not sure what's wrong with the code and why the program crashes. Thank you.
arrays sorting crash
First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the indexi
and what does that outer loop accomplish? What is the indexj
and what does that inner loop accomplish?
– Jorge Adriano
Nov 23 '18 at 17:00
add a comment |
I'm a beginner who has unfortunately encountered a problem I cannot solve :) I apologize in advance since English is not my first language.
My task is to sort an array so that the elements with the same digits come first, followed by the rest of an array.
Example:
Input array: 1 22 43 444 51 16 7 8888 90 11 -1
After sorting: 1,22,444,7,8888,11,43,51,16,90
Input array: 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13
The catch is, we are not allowed to use functions or more than one array to solve the problem.
Here's what I've already tried:
for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
digit=a[j]%10;
while (a[j]>0)
{
if (a[j]%10!=digit)
same=0;
a[j]/=10;
}
digit1=a[j+1]%10;
while (a[j+1]>0)
{
if (a[j+1]%10!=digit1)
same1=0;
a[j+1]/=10;
}
if (same==0 && same1==1)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
I'm not sure what's wrong with the code and why the program crashes. Thank you.
arrays sorting crash
I'm a beginner who has unfortunately encountered a problem I cannot solve :) I apologize in advance since English is not my first language.
My task is to sort an array so that the elements with the same digits come first, followed by the rest of an array.
Example:
Input array: 1 22 43 444 51 16 7 8888 90 11 -1
After sorting: 1,22,444,7,8888,11,43,51,16,90
Input array: 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13
The catch is, we are not allowed to use functions or more than one array to solve the problem.
Here's what I've already tried:
for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
digit=a[j]%10;
while (a[j]>0)
{
if (a[j]%10!=digit)
same=0;
a[j]/=10;
}
digit1=a[j+1]%10;
while (a[j+1]>0)
{
if (a[j+1]%10!=digit1)
same1=0;
a[j+1]/=10;
}
if (same==0 && same1==1)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
I'm not sure what's wrong with the code and why the program crashes. Thank you.
arrays sorting crash
arrays sorting crash
asked Nov 23 '18 at 16:27
Jinhyeong
1
1
First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the indexi
and what does that outer loop accomplish? What is the indexj
and what does that inner loop accomplish?
– Jorge Adriano
Nov 23 '18 at 17:00
add a comment |
First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the indexi
and what does that outer loop accomplish? What is the indexj
and what does that inner loop accomplish?
– Jorge Adriano
Nov 23 '18 at 17:00
First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the index
i
and what does that outer loop accomplish? What is the index j
and what does that inner loop accomplish?– Jorge Adriano
Nov 23 '18 at 17:00
First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the index
i
and what does that outer loop accomplish? What is the index j
and what does that inner loop accomplish?– Jorge Adriano
Nov 23 '18 at 17:00
add a comment |
2 Answers
2
active
oldest
votes
This might help,
for (int i = 0; i < in.length; i++) {
for (int j = in.length - 1; j > i; j--) {
if (in[j] < in[j - 1]) {
int temp = in[j];
in[j] = in[j - 1];
in[j - 1] = temp;
}
}
}
add a comment |
Its not really coherent your definition of the problem to the samples you are providing, for instance you say :
Input array : 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13
But according to your statements, should not 11 be the very first element?
Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :
#include <iostream>
#include <vector>
using namespace std;
bool samedigits(int x)
{
//if ( x < 10 ) return false;
int digit = x%10;
while(x>0)
{
if(x%10 != digit) return false;
x = x/10;
}
return true;
}
int main() {
int same = 0;
int same1 = 1;
int temp = 0;
int n = 0;
int digit = 0;
int digit1 = 0;
vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};
for(int n : a) {
cout << n << " ";
}
for (int i=0; i <= a.size() -1; i++)
{
for (int j=0; j <= a.size() - 1 ; j++)
{
if ( j != i) {
if (samedigits(a[j]) && samedigits(a[i])) {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}else if (! samedigits(a[j]) && samedigits(a[i]) ) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
} else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
continue;
}else {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}
}
}
}
cout << endl;
for(int n : a) {
cout << n << " ";
}
}
And it returns :
I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.
You can test this here :
https://www.jdoodle.com/online-compiler-c++
Regards!
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%2f53450120%2fdividing-array-elements-on-digits-problem-with-crashing%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
This might help,
for (int i = 0; i < in.length; i++) {
for (int j = in.length - 1; j > i; j--) {
if (in[j] < in[j - 1]) {
int temp = in[j];
in[j] = in[j - 1];
in[j - 1] = temp;
}
}
}
add a comment |
This might help,
for (int i = 0; i < in.length; i++) {
for (int j = in.length - 1; j > i; j--) {
if (in[j] < in[j - 1]) {
int temp = in[j];
in[j] = in[j - 1];
in[j - 1] = temp;
}
}
}
add a comment |
This might help,
for (int i = 0; i < in.length; i++) {
for (int j = in.length - 1; j > i; j--) {
if (in[j] < in[j - 1]) {
int temp = in[j];
in[j] = in[j - 1];
in[j - 1] = temp;
}
}
}
This might help,
for (int i = 0; i < in.length; i++) {
for (int j = in.length - 1; j > i; j--) {
if (in[j] < in[j - 1]) {
int temp = in[j];
in[j] = in[j - 1];
in[j - 1] = temp;
}
}
}
answered Nov 23 '18 at 16:59
Chameera
628
628
add a comment |
add a comment |
Its not really coherent your definition of the problem to the samples you are providing, for instance you say :
Input array : 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13
But according to your statements, should not 11 be the very first element?
Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :
#include <iostream>
#include <vector>
using namespace std;
bool samedigits(int x)
{
//if ( x < 10 ) return false;
int digit = x%10;
while(x>0)
{
if(x%10 != digit) return false;
x = x/10;
}
return true;
}
int main() {
int same = 0;
int same1 = 1;
int temp = 0;
int n = 0;
int digit = 0;
int digit1 = 0;
vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};
for(int n : a) {
cout << n << " ";
}
for (int i=0; i <= a.size() -1; i++)
{
for (int j=0; j <= a.size() - 1 ; j++)
{
if ( j != i) {
if (samedigits(a[j]) && samedigits(a[i])) {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}else if (! samedigits(a[j]) && samedigits(a[i]) ) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
} else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
continue;
}else {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}
}
}
}
cout << endl;
for(int n : a) {
cout << n << " ";
}
}
And it returns :
I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.
You can test this here :
https://www.jdoodle.com/online-compiler-c++
Regards!
add a comment |
Its not really coherent your definition of the problem to the samples you are providing, for instance you say :
Input array : 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13
But according to your statements, should not 11 be the very first element?
Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :
#include <iostream>
#include <vector>
using namespace std;
bool samedigits(int x)
{
//if ( x < 10 ) return false;
int digit = x%10;
while(x>0)
{
if(x%10 != digit) return false;
x = x/10;
}
return true;
}
int main() {
int same = 0;
int same1 = 1;
int temp = 0;
int n = 0;
int digit = 0;
int digit1 = 0;
vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};
for(int n : a) {
cout << n << " ";
}
for (int i=0; i <= a.size() -1; i++)
{
for (int j=0; j <= a.size() - 1 ; j++)
{
if ( j != i) {
if (samedigits(a[j]) && samedigits(a[i])) {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}else if (! samedigits(a[j]) && samedigits(a[i]) ) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
} else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
continue;
}else {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}
}
}
}
cout << endl;
for(int n : a) {
cout << n << " ";
}
}
And it returns :
I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.
You can test this here :
https://www.jdoodle.com/online-compiler-c++
Regards!
add a comment |
Its not really coherent your definition of the problem to the samples you are providing, for instance you say :
Input array : 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13
But according to your statements, should not 11 be the very first element?
Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :
#include <iostream>
#include <vector>
using namespace std;
bool samedigits(int x)
{
//if ( x < 10 ) return false;
int digit = x%10;
while(x>0)
{
if(x%10 != digit) return false;
x = x/10;
}
return true;
}
int main() {
int same = 0;
int same1 = 1;
int temp = 0;
int n = 0;
int digit = 0;
int digit1 = 0;
vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};
for(int n : a) {
cout << n << " ";
}
for (int i=0; i <= a.size() -1; i++)
{
for (int j=0; j <= a.size() - 1 ; j++)
{
if ( j != i) {
if (samedigits(a[j]) && samedigits(a[i])) {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}else if (! samedigits(a[j]) && samedigits(a[i]) ) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
} else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
continue;
}else {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}
}
}
}
cout << endl;
for(int n : a) {
cout << n << " ";
}
}
And it returns :
I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.
You can test this here :
https://www.jdoodle.com/online-compiler-c++
Regards!
Its not really coherent your definition of the problem to the samples you are providing, for instance you say :
Input array : 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13
But according to your statements, should not 11 be the very first element?
Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :
#include <iostream>
#include <vector>
using namespace std;
bool samedigits(int x)
{
//if ( x < 10 ) return false;
int digit = x%10;
while(x>0)
{
if(x%10 != digit) return false;
x = x/10;
}
return true;
}
int main() {
int same = 0;
int same1 = 1;
int temp = 0;
int n = 0;
int digit = 0;
int digit1 = 0;
vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};
for(int n : a) {
cout << n << " ";
}
for (int i=0; i <= a.size() -1; i++)
{
for (int j=0; j <= a.size() - 1 ; j++)
{
if ( j != i) {
if (samedigits(a[j]) && samedigits(a[i])) {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}else if (! samedigits(a[j]) && samedigits(a[i]) ) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
} else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
continue;
}else {
if (a[j] > a[i]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
continue;
}
}
}
}
}
cout << endl;
for(int n : a) {
cout << n << " ";
}
}
And it returns :
I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.
You can test this here :
https://www.jdoodle.com/online-compiler-c++
Regards!
answered Nov 23 '18 at 17:52
Matias Barrios
1,530316
1,530316
add a comment |
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%2f53450120%2fdividing-array-elements-on-digits-problem-with-crashing%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
First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the index
i
and what does that outer loop accomplish? What is the indexj
and what does that inner loop accomplish?– Jorge Adriano
Nov 23 '18 at 17:00