Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
it dose not show any output and I do not know the reason.

The program I am trying to do is to have an ordered array and ask the user to add another element, and this element should be add in the right place.

for example; if I have this array {1,3,4,5,0} and the user wants to add {2}, the array should be updated to be {1,2,3,4,5}.

but what really happen is that it gives a wrong output which is {1,3,4,2,0}

What I have tried:

Java
public class TTTTTTEEESST {

	public static void main(String[] args) {
		
		int[] a = new int[5];
		int cap = -1 ;
		a[0]=1;
		cap++;
		a[1]=3;
		cap++;
		a[2]=4;
		cap++;
		a[3]=5;
		cap++;
		TTTTTTEEESST t = new TTTTTTEEESST();
		for(int e=0; e<=4; e++)
			System.out.println(a[e]);
		System.out.println("");
		t.add(2, a, cap);
		}
	
	public void add(int el, int[] arr, int c) {
		int i=c;
		while( i <= c ) {
			if( arr[i] >= el && arr[i] != 0 ) {		
				arr[i++] = arr[i];
				i--;
			}		
			else {	
				arr[i++]=el;
			}
		}
		for(int e=0; e<=4; e++)
			System.out.println(arr[e]);
	}
}
Posted
Updated 4-Feb-20 14:23pm
v3
Comments
ZurdoDev 30-Jan-20 16:35pm    
All you have to do is debug it to find out. Do you know how to debug?

It doesn't look like you are ever calling add, unless t is an instance of the class that has add in it.
AbdelrahmanNassar 31-Jan-20 8:18am    
unfortunately I do not how to debug

The method " add " i sin the same class where the main function is. That is why I mad an object of the class. it is called in line 11.
ZurdoDev 31-Jan-20 8:59am    
Sorry, I missed the declaration of it.

Debugging is the most important skill you can have as a developer so you should learn that first. It depends on what IDE you are using so read the docs in the IDE.

Basically, you put a breakpoint in your code and run it then you can step line by line through it and see exactly what is happening.
AbdelrahmanNassar 31-Jan-20 9:11am    
Thanks a lot.

Java
while( i >= 0 ) {
    if( arr[i] >= el ) {
        arr[i+1]=arr[i];
        i--;
    }		
    else {
        arr[++i]=el;
    }
}

The if clause will start by being false, since i is 10 and you only have 4 elements in your array. So it will just walk off the end of the array and continue until it hits some memory access error.
 
Share this answer
 
Comments
AbdelrahmanNassar 31-Jan-20 8:40am    
ok, I changed the variable i to be 3, but it still gives me a wrong output.
Richard MacCutchan 31-Jan-20 8:42am    
You cannot expect us to guess what "it still gives me a wrong output." means. Please update your question with the full details of the modified code, the results you expect and the actual results you see.
AbdelrahmanNassar 31-Jan-20 8:41am    
i=4; **
AbdelrahmanNassar 31-Jan-20 9:12am    
Question and code are updated.
Richard MacCutchan 31-Jan-20 9:26am    
Look at the following two lines in the add method:
arr[i++] = arr[i]; // copies arr[i] to arr[i] - that is does nothing. Then increments i by 1.
i--;               // decrements i by 1 so it goes back to its previous value.

You need to rethink the algorithm for moving elements within your array. Try writing it out on paper and following the code so you can actually see the values changing.
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Quote:
it dose not show any output and I do not know the reason.

As programmer, your job is to translate algorithms to code, to create code by imagining how it will work.
But sometimes, your code don't work as expected, but as your code run, it is a black box, you can only imagine what it is doing.
It is where the debugger comes into play, it turn your running code as a white box, you can see what is going on inside your app, you can slow it down and inspect about anything inside the app.
Give a try to debugger, secondary effect, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
AbdelrahmanNassar 31-Jan-20 8:21am    
How do I debug it ?
Patrice T 31-Jan-20 15:13pm    
Details differ depending on your IDE, a little Google should give correct answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900