find numbers in array that add up to a given number c++

Morgan

I am writing this code that takes the input file and sorts all the integers from that file into an array. Then it takes the sum chosen by the user and goes through all the combinations (one by one /professors orders/) to find a pair that equal the desired sum. some of the code is funky because we are required to use a C++03 compiler -sigh-. The program has no errors but when I run it it says

There were 1600 checks made to find the products of your desired sum and

the two numbers that add to your sum are 40 and 40 Segmentation fault (core dumped)

(the desired sum was not one that could have been found in the input file so it should have said " the program did not find any matches. try again. ")

The code is very messy and Im sorry but I have spent hours moving stuff around and adding new things or trying something new to try and make it work.

I have a feeling it is a problem with how I am declaring the boolean variables or maybe how I am passing them.

int x, i, n1, n2, pos1 = 0, pos2 = 0,accesses = 0;
bool fail = false;

int readSortedArray (int array[20], int count, istream& infile)
{   
count = 0;
while(infile >> array[count])
{
    count++;

}

return count;
}

int findproducts (int array[20], int& n1, int& n2, int count, int sum, int& accesses, bool fail, istream& infile)
{   
bool found = true;
while(found == true)
{
for( i = 0; i < count; i++)
{
pos1++;
n1 = array[i];
pos2 = 0;
for( x = 0; x < count; x++)
{
pos2++; accesses ++;
n2 = array[x]; 
if(sum == n1 + n2)
{
    found = false;
    fail = false;
}
if(sum != n1 + n2)
{
    fail = true;
}
}
}
}

return fail;
}

int main ()
{
 int array[20];
 int answer, sum, count = 0;

std::string input_filename;
ifstream infile;

cout << "Please enter name of the input file: ";
cin >> input_filename;
infile.open(input_filename.c_str());
if (!infile)
{
    cout << "Could not open input file one\n";
    return 0;
}

cout << "Please enter the prefered sum ";
cin >> sum;

count = readSortedArray(array, count, infile); 

fail = findproducts(array, n1, n2, count, sum, accesses, fail, infile);

cout << "There were " << accesses << " checks made to find the products of your desired sum and" << endl;

if(fail == true)
{
    cout << " the program did not find any matches. try again. ";
}
if(fail == false)
{
cout << endl << "the two numbers that add to your sum are " << n1 << " and " << n2 << endl << ". These were found in position " << pos1 << " and " << pos2;
}

 return 0;
}
Jeremy Kahan

I incorporated Karan S Warraich's comment about the bool type. I also removed the file parameter to findProducts. But the big issue was the while loop ran forever when it did not find anything. I got rid of it. Also, it was important to get out of the for before the good numbers were lost again and fail was reset. As you note, a number of code cleanups are possible (like found now does nothing, and fail need not be set within findproducts), but here is something that works

bool findproducts (int array[20], int& n1, int& n2, int count, int sum, int& accesses, bool fail)
{   
bool found = true;
for( i = 0; i < count; i++)
{
    pos1++;
    n1 = array[i];
    pos2 = 0;
    for( x = 0; x < count; x++)

        {
        pos2++; accesses++;
        n2 = array[x]; 
        if(sum == n1 + n2)
        {
        found = false;
        fail = false;
        return false;
        }

        }
}

return true;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find subset of numbers that add up to a given number

From Dev

Given an array of integers, find two numbers such that they add up to a specific target number

From Dev

Given an array of integers, find two numbers such that they add up to a specific target number

From Dev

Using an array of numbers to add up to a specific number

From Dev

find if 3 numbers in an array add up to a sum

From Dev

Find triplets in an array such that sum of two numbers is also a number in the given array

From Dev

Find numbers of subarray of an array whose sum is divided by given number

From Dev

checking if 2 numbers of array add up to Input number in ruby

From Dev

find even numbers in given array

From Dev

Given an array, find combinations of n numbers that are less than c

From Dev

Given 2d array of integers, find a path that sum up to a given number recursively

From Dev

Error : Find Largest Number in given numbers

From Dev

Excel: Find a subset of numbers that add to a given total?

From Dev

Rounding to the closest number in a given array of numbers

From Dev

Choose numbers that add up to a specific number (with a twist)

From Dev

Find the largest sequence in a given array of numbers

From Dev

Is there a possible one line equation to find how many positive numbers directly follow another positive number in a given row made up of 50 columns?

From Dev

Javascript function to determine if number any combination of numbers in array add up to max value

From Dev

Javascript function to determine if number any combination of numbers in array add up to max value

From Dev

Random number from given numbers in structure C

From Dev

Use of queues in Java for an array of int, find two numbers that add up to specific sum n

From Dev

How to find a value in an array that is closest to a given number

From Dev

Given a natural number A, I want to find all the pairs of natural numbers (B,C) so that B*C*(C+1) = A

From Dev

Given a natural number A, I want to find all the pairs of natural numbers (B,C) so that B*C*(C+1) = A

From Dev

Find number of missing numbers to complete consecutive array

From Dev

find optimal sum of elements in list of numbers that are > a given number

From Dev

numpy - Given a number, find numbers that sum to it, with fuzzy weights

From Dev

Find the simplest rational number between two given rational numbers

From Dev

Find all possible combinations/partitions of 2 numbers for a given number

Related Related

  1. 1

    Find subset of numbers that add up to a given number

  2. 2

    Given an array of integers, find two numbers such that they add up to a specific target number

  3. 3

    Given an array of integers, find two numbers such that they add up to a specific target number

  4. 4

    Using an array of numbers to add up to a specific number

  5. 5

    find if 3 numbers in an array add up to a sum

  6. 6

    Find triplets in an array such that sum of two numbers is also a number in the given array

  7. 7

    Find numbers of subarray of an array whose sum is divided by given number

  8. 8

    checking if 2 numbers of array add up to Input number in ruby

  9. 9

    find even numbers in given array

  10. 10

    Given an array, find combinations of n numbers that are less than c

  11. 11

    Given 2d array of integers, find a path that sum up to a given number recursively

  12. 12

    Error : Find Largest Number in given numbers

  13. 13

    Excel: Find a subset of numbers that add to a given total?

  14. 14

    Rounding to the closest number in a given array of numbers

  15. 15

    Choose numbers that add up to a specific number (with a twist)

  16. 16

    Find the largest sequence in a given array of numbers

  17. 17

    Is there a possible one line equation to find how many positive numbers directly follow another positive number in a given row made up of 50 columns?

  18. 18

    Javascript function to determine if number any combination of numbers in array add up to max value

  19. 19

    Javascript function to determine if number any combination of numbers in array add up to max value

  20. 20

    Random number from given numbers in structure C

  21. 21

    Use of queues in Java for an array of int, find two numbers that add up to specific sum n

  22. 22

    How to find a value in an array that is closest to a given number

  23. 23

    Given a natural number A, I want to find all the pairs of natural numbers (B,C) so that B*C*(C+1) = A

  24. 24

    Given a natural number A, I want to find all the pairs of natural numbers (B,C) so that B*C*(C+1) = A

  25. 25

    Find number of missing numbers to complete consecutive array

  26. 26

    find optimal sum of elements in list of numbers that are > a given number

  27. 27

    numpy - Given a number, find numbers that sum to it, with fuzzy weights

  28. 28

    Find the simplest rational number between two given rational numbers

  29. 29

    Find all possible combinations/partitions of 2 numbers for a given number

HotTag

Archive