Morgan
Morgan

Reputation: 153

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

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;
}

Upvotes: 0

Views: 476

Answers (2)

Jeremy Kahan
Jeremy Kahan

Reputation: 3826

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;
}

Upvotes: 1

Karan S Warraich
Karan S Warraich

Reputation: 23

In your findproduct function you are returning bool value while your function is of int type that is either make your function a bool return type or return 1 for true and 0 for false

Upvotes: 0

Related Questions