how to dynamic array?

main()
{
int *x;
int n, i;
cin>>n;
for(i = 0; i< n; i++)
cin>>a[i];
int k = func(a, n);

/* add k to the a[n] */

}

to add k value from function to the a[n] ?
Use [code][/code] tags when posting code

where is 'a' declared?

is this what you mean: a[n]+=k; ?
added value but need to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
main()
{
int *x;
int n, i;
cin>>n;
for(i = 0; i< n; i++)
  cin>>x[i];

/* reapeat following until n times with new array */
int k = func(x, n);

for(i = 0;i<n; i++)
  x[i] = x[i+1];
x[n-1] = k;



then to repeat until n times with added new value k?
i tried goto command but still not good work
Try this:
[code=CPLUSPLUS]
#include<iostream>
using namespace std;

int main(){
int n, i, *x;

cout << "Enter array size" << enl;
cin >> n;

x = new int[n];

..........

deletc [] x;
}
[/code]
my problem is can't goto the func(x, n) array with new element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
main()
{
int *x;
int n, i;
int loop=0;
cin>>n;
for(i = 0; i< n; i++)
  cin>>x[i];

goto again:
int k = func(x, n);    /* k would be x massiv's last element then goto func() again */

for(i = 0;i<n; i++)
  x[i] = x[i+1];
x[n-1] = k;             // k will be last elm of massiv

loop++;
if(loop<n)
goto again;

delete[] x;
delete[] y;
}


double *func(int *x, int n)
{
  /*        */
 return k;
}
Last edited on
Topic archived. No new replies allowed.