for Robot Artificial Inteligence

7. Array representation

|

  1. what is an Array
  2. Declaring and initializing
  3. Accessing Array

Static Array

int x = 10 // Scalar
int A[5] // Vector
A[2] = 15
cout<<A[2]; // output 15;

Dynamic Array

int* p = new int[5]
delet []p;

//or

std::unique_ptr<int> p(new int[5]);

int* p = new int[5]
delet []p;

p = 5;
  • pointer는 기본적으로 array first element를 addressing 한다.

Example

#include <stdio.h>
#include <stdlib.h>
int main()
{
int A[5]={2,4,6,8,10};
int * p;
int i;
p=(int * )malloc(5*sizeof(int));
p[0]=3;
p[1]=5;
p[2]=7;
p[3]=9;
p[4]=11;
for(i=0;i<5;i++)
printf("%d ",A[i]);
printf("\n");
for(i=0;i<5;i++)
printf("%d ",p[i]);
return 0;
}

2D array

Example

#include <stdio.h>
#include <stdlib.h>
int main()
{
int * p,* q;
int i;
p=(int * )malloc(5*sizeof(int));
p[0]=3;p[1]=5;p[2]=7;p[3]=9;p[4]=11;
q=(int * )malloc(10*sizeof(int));
for(i=0;i<5;i++)
q[i]=p[i];
free(p);
p=q;
q=NULL;
for(i=0;i<5;i++)
printf("%d \n",p[i]);
return 0;
}

Array in compiler(Mapping address in Array)

Example

#include <stdio.h>
#include <stdlib.h>
int main()
{
int A[3][4]={1,2,3,4},{2,4,6,8},{1,3,5,7};
// {}여기 더 붙여야한다.
int * B[3];
int ** C;
int i,j;
B[0]=(int * )malloc(4*sizeof(int));
B[1]=(int * )malloc(4*sizeof(int));
B[2]=(int * )malloc(4*sizeof(int));
C=(int ** )malloc(3*sizeof(int *));
C[0]=(int * )malloc(4*sizeof(int));
C[1]=(int * )malloc(4*sizeof(int));
C[2]=(int * )malloc(4*sizeof(int));
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("%d ",C[i][j]);
printf("\n");
}
return 0;
}

Comments