• Post Reply Bookmark Topic Watch Topic
  • New Topic

While loop issue  RSS feed

 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so for class I'm supposed to Write a class DeleteElements that prints out an array and asks the user which number should be
deleted and then deletes that number, if it exists from the array.

the first part tells me to


Step 1
o Start by simply searching the element.

o Pseudocode:
Create an array and populate it with random positive integers.
Print the array to show the user which elements the array contains.
Use a WHILE loop to search the element—a WHILE loop is better than a FOR
loop because you can stop when the element is found by using a Boolean
value in the loop condition—and also keep track of the location where the
element might be found.
If the element is not found, output the error message, “Number not found.”
Otherwise output the message, “Number found at index #.” where # is the index
where the element is.
Print out the array.

Example of output:
34 65 12 76 45 39 86 71 67
Number to delete: 76
Number found at index 3



this is what I've got so far but I can't seem to get the "number not found part to print out only once".




Current output result if value does not exist:

61 89 52 16 20 71 37 91 4 36
Number to delete:
23
Number not found.
Number not found.
Number not found.
Number not found.
Number not found.
Number not found.
Number not found.
Number not found.
Number not found.
Number not found.
Current output result if value exists:

97 48 51 22 89 5 42 97 43 96
Number to delete:
89
Number found at index 4


Maybe I'm not understanding the part about the boolean value.
I would appreciate all the help i could get.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jairo Navarrete wrote:this is what I've got so far but I can't seem to get the "number not found part to print out only once".


Hello there. Looking at your code, I don't think it's necessary to have "else if" part for checking numDel!=arr[n]. Because if it's not found, the n will increment and continue with the rest of the array elements.

You may want to add a flag to indicate the number is found or not and use this variable to determine whether the "Number not found" message should be printed.

 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:
You may want to add a flag to indicate the number is found or not and use this variable to determine whether the "Number not found" message should be printed.



While this certainly works, if I look at the description of the problem:

Use a WHILE loop to search the element—a WHILE loop is better than a FOR
loop because you can stop when the element is found by using a Boolean
value in the loop condition—and also keep track of the location where the
element might be found.


I think they want to see something like:


Greetz,
Piet

Edit: but I must admit: this code looks quite ugly...
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a WHILE loop to search the element—a WHILE loop is better than a FOR
loop because you can stop when the element is found by using a Boolean
value in the loop condition—and also keep track of the location where the
element might be found.


I'm not sure I agree with the tutor here. A for-loop seems a much better fit for this problem to me than a while loop. A for-loop can be structured so it stops when the element is found and also to keep track of where the element is found, and since the maximum number of iterations is known (the size of the array) I would say the for-loop syntax is better suited to the problem than the while loop syntax.

Of course for-loops and while loops are interchangeable so they can each be used to solve the same problems and a while-loop will certainly work in this situation. And if the tutor says to use a while-loop then you should do that since (s)he's the boss.
 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah she wants it done how the assignment says, this helped me out a lot guys thank you so much
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!