Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: What is the invarient of this code

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is the invarient of this code

    i made this code but i'm stuck in :
    the fundamental operation in the described algorithm? How many operations does it take?

    What is the invariant of this sorting algorithm?
    the code :
    public class Ass {
     
    public static void swap(int [] x) {
    		int i = 0 ;
    	while (i<x.length){
     
    	for (int j = 0 ; j<x.length ; j++){
    		int b = 0 ;
    		if ( x[j] > x[i] ) {
    			b++ ;
    		}
    		if (b!=0) {
    			 int tmp = x[i] ;
    			 x[i]=x[j] ;
    			 x[j]= tmp ;
     
    		}
     
     
     
     
     
    	} 
    		i++ ;
    	}
    }
    public static void main (String [] args) {
    	int [] array = {5,7,3,6,9,4,2} ;
    	swap(array) ;
    	int m = array.length ;
    	for  (int k = 0 ; k<array.length ; k++) 
    System.out.print(array[k] + " ");
    }
    }
    Last edited by helloworld922; October 15th, 2012 at 12:19 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is the invarient of this code

    That's not how this works. You're going to have to tell us. We won't do your homework for you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is the invarient of this code

    Provide your answer and we'll comment on it as needed.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What is the invarient of this code

    @alloka, please be respectful of other members. I have removed your post which can be construed as offensive (and has been reported as such by other members). Continued postings such as the one deleted above will force further action. If you wish to receive help, I recommend providing as much information as possible regarding your problem - the link in my signature entitled "Getting Help" may help you along these lines.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is the invarient of this code

    so is this forum supposed for helping people or telling others what to do ?? , i asked for something and someone come and tell me we will not answer your homework , and btw this isn't a homework .

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is the invarient of this code

    and thankx for helping btw , i will not ask here again , i don't ask so others can keep info from me .

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What is the invarient of this code

    Quote Originally Posted by alloka View Post
    so is this forum supposed for helping people or telling others what to do ?? , i asked for something and someone come and tell me we will not answer your homework , and btw this isn't a homework .
    Getting help is as much (if not more) an effort on your part as it is the effort of contributors (contributors who are unpaid volunteers). Your post asked 3 questions about code. What did you try to answer said questions? What do you think the answers are? Do you understand the definitions being used in the questions? Including this information brews much better discussion, and give you a much higher chance of receiving help. Not including any of this information is suggestive no attempt was made (whether one was made or not), and is suggestive one is trying to get answers to homework (whether or not this is the case). There are hundreds of posts here, each one competing for attention of contributors. Thus providing clear and concise context rather than "here, answer this" rise to the top of posts that get answered faster.

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is the invarient of this code

    i asked specificly for the invarint , and just to let you know i searched for it all over the internet and didn't get quite a way for getting the invariant and it wasn't explained to me before so i tried to get help but sadly i didn't expect you to be like this , if i know something and one asked for it i will answer with no doubt and teach him not to do what you did .

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What is the invarient of this code

    Quote Originally Posted by alloka View Post
    i asked specificly for the invarint , and just to let you know i searched for it all over the internet and didn't get quite a way for getting the invariant and it wasn't explained to me before so i tried to get help but sadly i didn't expect you to be like this , if i know something and one asked for it i will answer with no doubt and teach him not to do what you did .
    I have done nothing other remove an inflammatory post of yours and suggest you provide more information, and yet I get accused of something...so much for trying, which I am done doing.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is the invarient of this code

    As was stated in post#2, we don't do OPs work for them. We try to help Ops solve their java programming problems. If you have a java programming problem please ask it.
    Your question seems to be about aspects of an algorithm.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is the invarient of this code

    yeah at least teach me how to get the invariant ??

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is the invarient of this code

    how to get the invariant
    What does that have to do with java programming?

    Have you done a search for invariant? What did you find?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: What is the invarient of this code

    Please respect what is being said or you will be banned.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  14. #14
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What is the invarient of this code

    Gosh... I don't know what to think...

    This is what I found about the invariant, and I don't know if it is applicable to your problem. The invariant is either an input that is unvaried by the algorithm, or a statement that IF it holds at the beginning, will also hold during and after the algorithm, at all points in time.

    What are some inputs that are not changed by the algorithm? What statements are always true during the algorithm? Here's a doozy: What are some statements that ASSUMING they are true at the beginning, will always be true within the context of the algorithm?

  15. #15
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What is the invarient of this code

    The invariant is part of the contract between the user and the code. It states how you are allowed to use that code, what assumptions are made, etc. In short it spells out what requirements must be made in order to ensure that the output is always "valid". For example, a Square object requires all sides to be the same length, there are exactly 4 sides, and each side is connected to at most 2 other sides orthogonally (with right angles). Violating any of these rules breaks the invariant and you no longer have a valid Square object. Further reading: Wikipedia - Class invariant

    In the context of a method invariants usually refer to the requirements of the parameters passed in. One example is a sqrt method. This method is not allowed to take in negative numbers, so the invariant is that the parameter passed in must be non-negative.

    Now it's your turn: Figure out what it is you want as a contract between your method and a user. Usually this is done before you have a complete method, but that shouldn't stop you from creating a contract. Presumably you want to sort an array. What restrictions are there on that array such as size constraints, element type constraints, element value, etc.? In the contract you can also specify what happens when a violation to this invariant is made (throw an exception, try to mediate with some other value, undefined behavior, etc.), but I don't think this is part of the invariant. You can also specify in a black-box or grey-box manner what outputs and/or side-effects the method has, though again I don't think this is part of the invariant.

  16. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is the invarient of this code

    I have given this user a week-long ban for his abusive language and poor attitude.

    Alloka, you are asking other people to do your homework for you (which is academic dishonesty and can get all of us, especially you, in trouble), and ignoring all the help you're getting. You're going to need to provide more input no matter where you go- nobody, on any website, is going to do your homework for you. And even if they did, it's not really helping you. You either want to learn this stuff, or you don't. That's up to you, but don't repay our attempts at helping you with foul language and a terrible attitude. You can try harder when your ban is up, and if this behavior continues, I'll ban you indefinitely.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  2. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  3. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  4. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM