I think some of the options mentioned in this thread is kind of what you want maybe not the approach you wanted. For a form to optionally display more form elements you'll have to use state API or some sort of ajax form.
Take this form element for instance:
$form['advanced']['s3fs_use_cname'] = array(
'#type' => 'checkbox',
'#title' => t('Use a CNAME'),
'#default_value' => _s3fs_get_setting('use_cname'),
'#description' => t('Serve files from a custom domain by using an appropriately named bucket, e.g. "mybucket.mydomain.com".'),
);
$form['advanced']['s3fs_cname_settings_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('CNAME Settings'),
'#states' => array(
'visible' => array(
':input[id=edit-s3fs-use-cname]' => array('checked' => TRUE),
),
),
's3fs_domain' => array(
'#type' => 'textfield',
'#title' => t('CDN Domain Name'),
'#default_value' => _s3fs_get_setting('domain'),
'#description' => t('If serving files from CloudFront, the bucket name can differ from the domain name.'),
),
);
This example from the s3fs module uses a check box to display a form input field. If we look at the documtation for drupal_process_states() we can see that as a remote conditions we can use value as a determining factor. So we can almost accomplish what you're asking from here. We would first need a input field
$form['field_instance_value'] = array(
'#type' => 'textfield',
'#title' => t('Number of fields'),
'#default_value' => NULL,
'#description' => t('This serves as the number of fields to display below.'),
);
This field is our instance control. On it's own with default values it won't do much. I set it to default to NULL in this case so we can determine if the user has filled in a value or not. Next we'll need to control the group of fields and each additional field. This might be better to do using an ajax form since you can dynamically add as many fields as you like based on the number but i won't cover that.
$form['field_instances'] = array(
'#type' => 'fieldset',
'#title' => t('Field Instances'),
'#states' => array(
'visible' => array(
':input[id=edit-field_instance_value]' => array('filled' => TRUE),
)
),
);
$form['field_instances']['field_instance_one'] = array(
'#type' => 'textfield',
'#title' => t('Field Instance one'),
'#states' => array(
'visible' => array(
':input[id=edit-field_instance_value]' => array('value' => 1),
)
),
);
$form['field_instances']['field_instance_two'] = array(
'#type' => 'textfield',
'#title' => t('Field Instance two'),
'#states' => array(
'visible' => array(
':input[id=edit-field_instance_value]' => array('value' => 2),
)
),
);
So from the above example you can see that we have two control states, the first being on the fieldset containing the field instances and the second being on the instances themselves. The idea here is that we want to make sure that the instances can be seen if there is a value in the field, then we want to only show the field instances that the form 'field_instance_value' element specifies. You could even put in a default value here if the value of the input is empty or something else.
Keep in mind this code isn't tested just what i've come up with based on your question and years of working with Drupal's Form API. Below are some references i used.