• 0

C++ Problem


Question

I am taking a C++ refresher course and I decided to do this assignment for extra credit regarding arrays:

Implement a method:

boolean findPair(int[] array, int sum)

That should return true if there are any two values that add up to "sum". So for instance if I passed in (0, 3, -1, 6) as the array, then it would return true if sum was 9 or 2, but false for 100 or 8.

If there are 4 members in this array, then I believe I have to do a loop 24 times or 4! to add up to the sum?

Link to comment
https://www.neowin.net/forum/topic/474672-c-problem/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

I wouldn't go through the loop all the times required, just exit the loop if your sum is found.

So, compare the first element to all the elements. Match? Yes, exit (return true). No, compare the next element to all elements. Match? Yes, exit (return true). No....and so on.

for(int i=0;i<length;i++) {
	for(int j=0;j<lenth;j++) {
		if(j != i) {
			 if((sum == array[i] + array[j]) {
					  return true;
			 }
		 }
	 }
}
return false;

--That code is probably no where near perfect, but the gist of it should work. I think...half the time i code stuff that never works, so don't listen to me.

In this case, each outter loop 4 times and for each outter loop, each inner loop 4 times. That would be a total of 20.

Outter

Inner

Inner

Inner

Inner

Outter

Inner

Inner

Inner

Inner

Outter

Inner

Inner

Inner

Inner

Outter

Inner

Inner

Inner

Inner

Link to comment
https://www.neowin.net/forum/topic/474672-c-problem/#findComment-587656158
Share on other sites

  • 0
  jon419 said:

for(int i=0;i<length;i++) {
	for(int j=0;j<lenth;j++) {
		if(j != i) {
			 if((sum == array[i] + array[j]) {
					  return true;
			 }
		 }
	 }
}
return false;

I belive you're doing some extra work there. With the numbers the OP used, the first iteration of the outter loop you'd add:

0+3, 0+(-1), 0+6

then on the next iteration of the outter loop you'd add

3+0, 3+(-1), 3+6

you just calculated 0+3 twice.

instead of starting the inner loop at 0, start it at j=i+1.

  jon419 said:

half the time i code stuff that never works, so don't listen to me.

same goes with me, i didnt test this so it might no be right :D

Link to comment
https://www.neowin.net/forum/topic/474672-c-problem/#findComment-587656610
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • This is what I thought of earlier today because it seems a bit stupid to have an iPhone 17 running iOS 26 (or iOS 2026 / or even iOS 25/2025). Just make it simple so that the year of the hardware release and the software release are in sync. I personally think they should go with 25 or 2025 (not 26 or 2026), but syncing the hardware and software version numbers could be easier to keep track of. At first, it will maybe be jarring due to all of the changes across the ecosystem, but from that point on it will be easier to keep track of.
    • my dad is experiencing the same thing except it's with Excel. the font became thin compared to windows 10, all the settings the same. i've chalked it up to it being that its connected via DVI instead of HDMI. is your setup the same? i have no technical reasons to believe it's DVI, just a plain guess since the other screen he's connected to seems better to me although may just be my mind playing tricks.  also, why don't you change the text size in accessibility? maybe this will help?   
    • I think you missed the point. I don't know why I think 2026 makes more sense than 26. Microsoft didn't call it Office 16, they referred to the (next) year of the product and so the naming convention made sense. Going from 19 to 26 could cause confusion for someone that takes a glance; I guess that is my thought process. But you're right, it doesn't make a difference. Although by that token, why change the naming convention to begin with?
    • I have checked the scaling, and it is not different at all I reapplied my current theme and no thing changed.   Fonts still too damn small and NO OPTION IN WINDOWS TO ADDRESS IT ANYMORE!   Removing options for customization is NOT UPGRADING IN ANYWAY!
  • Recent Achievements

    • Week One Done
      abortretryfail earned a badge
      Week One Done
    • First Post
      Mr bot earned a badge
      First Post
    • First Post
      Bkl211 earned a badge
      First Post
    • One Year In
      Mido gaber earned a badge
      One Year In
    • One Year In
      Vladimir Migunov earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      492
    2. 2
      +FloatingFatMan
      256
    3. 3
      snowy owl
      248
    4. 4
      ATLien_0
      224
    5. 5
      +Edouard
      189
  • Tell a friend

    Love Neowin? Tell a friend!