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

  • Home
  • SEARCH
  • 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 8755919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:57:00+00:00 2026-06-13T13:57:00+00:00

Here’s the code that I’m currently using template <class T> float Knapsack<T>::knapTable() { const

  • 0

Here’s the code that I’m currently using

template <class T>
float Knapsack<T>::knapTable()
{
  const int MAXSIZE = 40000;
  int temps = nObjects - 1;
  float** memoize = new float*[MAXSIZE];

  for(int y = 0; y < weight[nObjects]; y++)
    memoize[nObjects][y] = 0;

  for(int y = weight[nObjects]; y < knapCap; y++)
    memoize[nObjects][y] = price[y];

  for(int i = temps; i >= 1; i--)
  {
    for(int y = weight[i]; y < knapCap; y++)
      memoize[i][y]= max(memoize[i+1][y], (memoize[i+1][y-weight[i]]+price[i]));
  }

  return memoize[1][nObjects];

}

For some reason I keep getting the error: knapsack.hpp:68:64: error: invalid types ‘float*[float]’ for array subscript.

That’s this line: float** memoize = new float*[MAXSIZE];

For some reason the compiler seems to be recognizing MAXSIZE as a float, it’s a const int.

Is there a way I can fix this?

Edited for more code
header file
#ifndef KNAPSACK_H
#define KNAPSACK_H

#include <stdexcept>
#include <assert.h>
#include <iostream>
#include <limits.h>
using namespace std;

template <class T>
class Knapsack
{
  private:
    float knapPrice;
    int knapCap, nObjects;
    float weight[40000];
    float price[40000];
  public:

    Knapsack(): knapPrice(0), knapCap(0), nObjects(0) {}
    ~Knapsack() {knapPrice = 0; knapCap = 0;}

    float knapFull (int position, int currentCap);

    float knapTable ();

    float greedyKnap (int currentCap);

    float max(float noAdd,float addOb);

    void printPrice();
    //valueized and valued are modified versions of mergeSort and merge
    //designed to sort two arrays by a fraction of the two.
    void valueize(int ini, int last);

    void valued(int ini, int middle, int last);

    void fillWandP();

    void setNObjects(int n);

    void setKnapCap(int boom);
};
#include "knapsack.hpp"
#endif

Main function //Though I don’t think this would affect it
#include “sortClass.h”
#include “knapsack.h”
#include
#include
#include
#include
using namespace std;

//mergeSort main;
int main()
{
    Knapsack<float> a;
    float sacked = 0;

    int nO = 18;
    int cap = 700;
    a.setNObjects(nO);

a.setKnapCap(cap);

    a.fillWandP();

    for(int b = 0; b <3800000; b++)//for getting good times
  sacked = b;

    int startAll = clock()*1000000;
    sacked = a.knapFull(1, cap);
    int knapped = clock()*1000000;
    int boom = a.knapTable();
    int tabled = clock()*1000000;
    a.valueize(1, cap);
    int andDone = a.greedyKnap(cap);
    int greedified = clock()*1000000;
    cout<<startAll<<endl;

    greedified = greedified - tabled;
    tabled = tabled - knapped;
    knapped = knapped - startAll;
    cout<<"Recursion profit:"<<sacked<<" Time: "<<knapped<<endl;
    cout<<"Memoization profit:"<<boom<<" Time: "<<tabled<<endl;
    cout<<"Greedy profit: "<<andDone<<" Time: "<<greedified<<endl;



    return 0;
}
  • 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-06-13T13:57:01+00:00Added an answer on June 13, 2026 at 1:57 pm

    weight is declared as float weight[40000] in class Knapsack.

    You then use an element of weight as an index into memoize in the knaptable() function:

    memoize[i][y]= max(memoize[i+1][y], (memoize[i+1][y-weight[i]]+price[i]));
    //                                                  ^^^^^^^^^
    

    And for the record, that’s the line that the error is produced for by g++ 4.6.1; it doesn’t point to the line where memoize is declared.

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

Sidebar

Related Questions

Here is the code I'm using inside my AsyncTask DefaultHttpClient httpClient = new DefaultHttpClient();
Here is a code snippet I was working with: int *a; int p =
Here is my class: public class A{ private void doIt(int[] X, int[] Y){ //change
Here's the code I have. It works. The only problem is that the first
here is my code, SiteMember class @OneToMany(mappedBy = member,cascade=CascadeType.ALL) private List<MemberThread> memberThread = new
Here is the code from my project where I am using a datepicker. The
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here is my code: int main() { int tiles[9]; int counter=0; int i=1; while
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>

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.