• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Sorting logic error

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


and my output is attached.. It should be 1,2,3,4,5 ascending order
1111111.png
[Thumbnail for 1111111.png]
this is my output
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I see a few problems. First, the loop starting at line 26 runs until ctr1 <= n-1. In your case n is 5, so the loop will run four iterations with ctr1 having the values 1, 2, 3, and 4. In the last iteration ctrl1 is 4, and you set ctr2 to ctr1 + 1, or 5. The valid indexes for Arr1 are 0 to 4, so you will be indexing outside the array. In Java (for example) you'd get a thrown exception, but in C/C++ you just get the value of that memory location, even though it's outside the array. That's why 0 appears in your sorted list even though it's not one of the input values.

Second, there is a loop starting at line 41 that collects user input again. Fortunately that never runs because the counter ctrl1 is always equal to n when that loop starts. Still, it's useless and distracting. You should remove that.

Finally, which sort algorithm are you trying to implement? It looks like it could be bubble sort, but some of the specifics don't look quite right.
 
Marshal
Posts: 80085
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 25 you appear to start from Arr[1] which is the 2nd element of the array. Why not Arr[0]?
 
mark patindol
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i changed the line 25 to ctr1=0;

and my output become. 0,1,2,3,4. It should be 1,2,3,4,5.

my assignment is to convert my code "java" to c++ lang. i thought its gonna be easy LOL.
 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, you're translating this from Java? The main difference would be the input and output, and you've got that right. The loops and general flow would be exactly the same, and that's what you've got wrong. Either you're not cutting and pasting correctly from the Java code, or the Java code doesn't work either.
 
mark patindol
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i got the correct output. i just change the last part of the code. Now i know in c++ the start of array is on 0.
 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Congratulations! It's satisfying to solve a problem like that, isn't it? You're right that C and C++ arrays start with index 0. That's also true of Java, and most modern languages. I found a nifty language comparison here, and you can see that the ones with a default base index of 1 tend to be older languages: ALGOL, COBOL, Fortran, PL/1, RPG, etc.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see that you have figured out the problem. Just wanted to point out something:

mark patindol wrote:



You are relying on a compiler extension (i.e., it is not likely to work if you use another compiler or even the same compiler with different settings).

The ISO standard for the C++ language (which is clearly what you are using, as opposed to the C language) does not allow you to declare an array whose size is specified only at run-time. If you were converting the code from Java, the Java code would have done a new[]. C++ would allow that as well. Only, in C++ you would have to ensure you do a delete[], whereas in Java, you would not have to (in fact, you would be unable to).

There are also style issues in your code that would go against the grain of most folks who've written C++ for a while, but I will not bring them up now.

- Anand
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic