Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4126228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:58:24+00:00 2026-05-20T23:58:24+00:00

I want to write a function that iterates through a list, but takes two

  • 0

I want to write a function that iterates through a list, but takes two elements if they equal a target.

The logic could be to have two loops; first iterate through list and see if any element + the first = target, then second, then third
etc..

int i;
int max = list.size;
for(i=0; i<=max; i++)
{
    if()
}

in c++

    int i;
int max = resistors.size();
int curr;
    for(curr = 0; curr<=resistors.size(); curr++)
    {
   for(i=1; i<=max; i++)
       {
        if((resistors[curr]+resistors[i])==target){
            ComposeSerial(resistors[curr], resistors[i]);

        }
       }

}

i believe one thing is missing to make sure that it goes through again?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:58:24+00:00Added an answer on May 20, 2026 at 11:58 pm

    It might help if you thought about it slightly differently. You said “first iterate through list and see if any element + the first = target”. What if you phrased it as “Iterate through the list and see if the current element + any other element in the list = target”. All I’ve done is consolidated your placement of “the first, then second, then third etc…” into the “current element”.

    Your original phrasing suggests only one loop, with some magic to get the first element, then the second, etc. This reversed phrasing suggests two loops: one to get each element, then an inner loop to test the current element with every other element in the list. Hopefully that way of thinking will clear up any confusion.

    Edit:

    Since this didn’t clear up all the confusion, I’m going to add some pseudocode:

    for all items in list
        for all items in list
            if element from outer loop and element from inner loop meet your condition
                 return some value
    

    One obvious optimization would be for the inner loop to start after the element from the outer loop, like so:

    for all items in list
        for all items in list after the element from the outer loop
            ...
    

    Depending on the problem, you may want to include the outer element at the start of the inner loop:

    for all items in list
        for all items in list starting at the element from the outer loop
    

    To write the actual code, grab your textbook, or the nearest C++ reference, or go to cplusplus.com, look up the documentation for the specific container type you’re using, and determine the best way to iterate through your elements from there.

    Edit 2 – Response to Edited Question

    Thanks for the updated question. The only issues I see here are, as @leetNightshade pointed out in the comment, the inner loop should start at curr + 1 (or curr, if you want to allow the test for an element with itself, which you probably don’t), and your loops should terminate when curr < resistors.size(), and i < max, respectively. Otherwise, you will include one element past the end of your list in your loop, which will have nasty consequences.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that takes an array of letters as an
I want to write a function that accepts two objects as parameters and compare
I want to write a function that returns the nearest next power of 2
I want to write a function that accepts a parameter which can be either
I want to write a function in Python that returns different fixed values based
I want to write a function that reads a file and counts the number
i want to write a function that checks for equality of lists in SML
Let's say I want to write a function that does the following: Given a
I want to write a generic function that has a constraint on the type.
I want to write a function that splits lists into sublists according to what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.