need help quickly, going insane with this

This is a discussion on need help quickly, going insane with this within the C++ Programming forums, part of the General Programming Boards category; hey guys, heres my problem, ive been workin on a problem known as knapsack for my c++ course for 3 ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    need help quickly, going insane with this

    hey guys, heres my problem, ive been workin on a problem known as knapsack for my c++ course for 3 weeks now, heres the premise of it, you enter numbers individually, set a goal number, and the program works out which ones will give you that number, ive been doin this and i keep on getting errors or blue screens or w/e, please anyone who can help, provide me with the code if you have it or anythin thanks

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    post the code you have or the code you believe is causing the blue screen error - blue screens and errors like that could be caused by access violations. Check you initialize and use arrays correctly and dont overflow them. Come back with the code..
    Monday - what a way to spend a seventh of your life

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    3
    heres the code : please help as soon as you can i have to hand it in tommorow


    #include <iostream.h>
    #include <lvp\string.h>
    #include <lvp\vector.h>

    //typedef vector<int> KnapsackArr;
    //------------------------------------------------------------------------------
    void LoadData(int &goal, int n, vector <int> &OldArr)
    {
    //Pre: None
    //Post:Enters data into array, enters number for search

    // vector <int> KnapsackArr (n);
    n=0;
    cout<<"Enter value (0=done) : ";
    cin>>OldArr[n];

    while (OldArr[n]!=0)
    {
    n++;
    cout<<"Enter value (0=done) : ";
    cin>>OldArr[n];
    }

    cout<<"Enter goal (0=done) : ";
    cin>>goal;

    }
    //------------------------------------------------------------------------------
    void FindCombo(int index, int sum, int goal, vector <int> &NewArr, const vector<int> &OldArr)
    {
    //Pre:Array with values in array
    //Post:Returns combinations for number

    /* for (x=0;KnapsackArr.length()-1;x++)
    for (y=0;KnapsackArr.length()-1;y++){
    if (KnapsackArr[x]+KnapsackArr[y]==goal)
    cout<<"Possible Solutions : "<<KnapsackArr[x]<<" "<<KnapsackArr[y];
    else
    break;
    } */
    int temp=0;
    //sum=0;
    while (index>0)
    sum+=NewArr[index];

    }
    //---------------------------------------------------------------------------
    void Reverse (vector <int> &NewArr, int &sum, int index, int goal)
    {

    while (index>0 && sum!=goal)
    {
    for (index=NewArr.length()-1; index>0; index--)
    sum+=NewArr[index];
    Reverse (NewArr, sum, index-1, goal);
    }
    cout<<"Sum : "<<sum<<" "<<"Index : "<<index<<endl;
    }
    //------------------------------------------------------------------------------
    void DisplayResults(int &goal)
    {
    if (goal!=0)
    cout<<"Possible Solutions : "<<goal;
    else
    cout<<"No Solution";
    }
    //------------------------------------------------------------------------------
    void TryAgain(int sum, int goal, int index, vector <int> &NewArr)
    {
    for (index=NewArr.length()-1; ;index--)
    { sum=0;
    sum+=NewArr[index];
    if(sum==goal)
    cout<<"Got It";
    else
    TryAgain(sum, goal, index, NewArr);
    }
    }
    //---------------------------------------------
    int main ()
    {
    int index,sum, n, goal;
    vector <int> NewArr(15);
    vector <int> OldArr(15);
    LoadData(goal, n, OldArr);
    NewArr=OldArr;
    TryAgain(sum,goal, index, NewArr);


    // FindCombo(index, sum, goal, NewArr, OldArr);
    // Reverse (NewArr, sum, index, goal);

    // DisplayResults(goal);

    // cout<<goal<<" ";
    // for (n=0; n<=KnapsackArr.length()-1; n++)
    // cout<<KnapsackArr[n]<<" ";

    return (0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insane MIdget Assassins and Abachler
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-03-2009, 12:10 PM
  2. Ways to evaluate loops quickly
    By Mister C in forum C++ Programming
    Replies: 5
    Last Post: 09-17-2004, 11:48 AM
  3. Quickly learn C# for a C++ expert
    By BigDaddyDrew in forum C# Programming
    Replies: 9
    Last Post: 06-06-2004, 05:38 PM
  4. How can I initiate my growth of beard more quickly?
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 07-21-2003, 06:15 PM
  5. Reading from a file quickly
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-11-2002, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21