array size declaration using const int

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kayjay66
    New Member
    • Mar 2010
    • 5

    array size declaration using const int

    Code:
    #include <iomanip>
    #include<fstream>
    #include <cstdlib>
    #include <ctime>
    #include<iostream>
    using namespace std;
    int main()
    {
    
    
    	ofstream myfile ("sparse 100.txt");
    
    
    	
    	
    	const int i=10,j=10;   //number of rows and columns
    	int AF[i][j],nnz=0;
    	
    	
    	int cte;
    
        srand( time( 0 ) );
    
    
        for ( int a = 0; a < i; a++ )
        {
    	   for ( int b = 0; b <j; b++ )
    	   {
    		   cte=rand() %220;
    		   if(cte>100)
    			   cte=0;
    		   AF[a][b]=cte;
    		   
    		   if(cte!=0)
    		   nnz++;
    	   }
       }
    
     
    
    
    
       if (myfile.is_open())
      {
    	  for ( int a = 0; a <i; a++ )
    	  {
    		for ( int b = 0; b <j; b++ )
    	    {
    			 myfile << AF[a][b]<<endl;
    		}
    	  }
        
        myfile.close();
      }
     
    
       
       [U][I][B]const int NNZ=nnz[/B][/I][/U];
    
     int [B][I][U]A[NNZ],J[NNZ][/U][/I][/B],I[i+1],counter=0;
       I[0]=1;
    	for (int a = 0; a <i; a++ )
    	  {
    		for ( int b = 0; b <j; b++ )
    	    {
    			if(AF[a][b]!=0)
    			{
    				A[counter]=AF[a][b];
    				J[counter]=b;
    				counter++;
    
    
    			}
    		}
    		I[a]=counter+1;
    		cout<<"done";
    	}
    
    
          return 0;  
    
    }
    --------------------------------------------------------------------------------------
    hello,
    i'm writing a code to generate a sparse matrix.
    i'm having this problem:
    error C2466: cannot allocate an array of constant size 0

    i know that the declaration of the size of an array should be an int OR of type const int. in the above program, when i assign the value of NNZ as fallows: NNZ=nnz it's considering it as 0. any help plz? any solution for my problem?

    thank you in advance :)
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    C++ does not have dynamic arrays so the compiler has to set the array size and at compile time the NNZ constant has a value of 0 hence you get that error.

    You probably need to use a vector which should solve all your problems.

    Comment

    • kayjay66
      New Member
      • Mar 2010
      • 5

      #3
      problem solved!
      thank you for your help :)
      i really appreciate it :)

      Comment

      Working...