Hi:
I never set up a 2D array in the past and I am trying to do it now. I have a program that reads a 1 line text file and after reading it makes some calculations. After this I made some changes to my code trying to make a 2D array so I a can more than one line at a time. I did my originbal code using pointers since I received help from one my TA's on this matter. I believe that I need to make some changes to the definition of my complexity but I am afraid if I make changes here I will mess up the equation I use to find sumc. For you to understand this you need to read the code. Of course when I run this code I get some errors. Here are my errors and my code.
G
g++ Complex.cpp
Complex.cpp: In function ‘int main()’:
Complex.cpp:36: error: ‘i’ cannot appear in a constant-expression
Complex.cpp:52: error: invalid types ‘unsigned int[int]’ for array subscript
#include <iostream>
#include <fstream>
#include <cmath> // for log
using namespace std;
unsigned int complexity(const unsigned int* S, unsigned int T)
{
unsigned int c = 1;
unsigned int l = 1;
do
{
int kmax =1;
for (int i=0;i<l;i++)
{
int k = 0;
while (S[i+k] == S[l+k])
{
++k;
if (l+k >= T-1) return (++c);
}
if (k >= kmax) kmax = k+1;
}
++c;
l += kmax;
}
while (l < T);
return c;
}
int main(void)
{
unsigned T; // length of string
cout << "Enter length of string: ";
cin >> T;
int i = 0;
unsigned int* S = new unsigned int[T][i];
ifstream K;
unsigned int count = 0;
K.open ("k.txt");
if(K.fail())
{
cout << "What the fuck did you do with the file" << endl;
}
else
{
while(!K.eof())
{
for (unsigned int t=0;t<=T;t++)
{
{
K >> S[t][i];
i++;
}
}
}
}
K.close();
unsigned int sumc = complexity(S,T);
cout << "sumc = " << sumc << endl;
double cnorm;
cnorm = sumc*log((double)T)/(log(2.0)*((double)T));
cout << "cnorm = " << cnorm << endl;
delete[] S;
return 0;
}