I am trying to write a program that dynamically increase the size of an array. essentially creating an array inside of an array with array a storing 100 int pointers. my code is initially having problems on line

a[count] = new int [n];

any tips or pointers as to how to get my code working correctly would be much appreciated.

#include <iostream>
#include <cstdlib>
using namespace std;

int main(){

    int c[100];

    for( int i = 0; i < 100; i++){
         int n;
         cin >> n;             

         a[count] = new int [n];
         int *p = a[count];
         c[count] = n;
                  for( int j = 0; j < n; j++)
                       p[j] = rand();
                       }
    
    for ( int k = 0; k < count; k++){
        int *b = a[k];
            for ( int l = 0; l <= c[k]; l++){
                cout << b[l] << endl;
            }
        }
                
for ( int m = 0; m <=count ; m++){
    delete [] a[k];
}

delete [] a;
delete [] c;
      
    system("PAUSE");
    return 0;
}

Recommended Answers

All 3 Replies

First you need to explain what you expect the code to do.

Line 13 attempts to assign a value to a[count], but neither a nor count is defined anywhere. Without a hint as to what you expect that statement to do, it is hard to figure out how to get it to do it.

okay after making some revisions i have gotten past this part and the code runs, issue i have now is that its not outputting anything. its suppost to output ever element in the arrays and that where i have

for ( int l = 0; l <= c[k]; l++){
                cout << b[l] << endl;
            }

(updated code thus far)

#include <iostream>
#include <cstdlib>
using namespace std;

int main(){

int *a[100];
int c[100];
int count = 0;


    for( int i = 0; i < 100; i++){
         int n;
         cin >> n; 
                     

         a[count] = new int [n];
         int *p = a[count];
         c[count] = n;
                  count++;
                  for( int j = 0; j < n; j++)
                       p[j] = rand();
                       }
    
    for ( int k = 0; k < count; k++){
        int *b = a[k];
            for ( int l = 0; l <= c[k]; l++){
                cout << b[l] << endl;
            }
            
        }
                
for ( int m = 0; m <=count ; m++){
    delete [] a[m];
}

delete [] a;
delete [] c;
      
    system("PAUSE");
    return 0;
}

You can't tell what your problem is. Don't expect us to reply to your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.