Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

array calculation in ABAP

Former Member
0 Likes
2,051

hi gurus,

I have to use array for some calculations. I begin new project and have to calculate distance with using array. I searched forums but I found nothing. please help me?

thanks,

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,374

thanks all.

I want to explain that I want.

I want go from A to B.

intervals: 1) A-B = 600

2) A-C=50 C-F=150 F-G= 200 G-B=50

3) A-D=100 D-H=200 H-G=50 G-B=50.

we have to choose 3. but how I find this direction and min. interval? please help me.

note: sorry for my english.

13 REPLIES 13
Read only

former_member69765
Contributor
0 Likes
1,374

In ABAP we do not have concepts such as Arrays in C++ or JAVA.

We have a similar thing called Internal tables.

Internal tables are noting but a 2 dimensional arrays. You can imagine them like a table filled with values.

Try to get you job done using internal tables.

Read only

Former Member
0 Likes
1,374

In ABAP the closest concept to an "array" is an "internal table".

http://help.sap.com/saphelp_nw04/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

and there are field-symbols which are same as pointers in C.

Edited by: Craig Cmehil on Jul 3, 2008 3:30 PM

Read only

Former Member
0 Likes
1,375

thanks all.

I want to explain that I want.

I want go from A to B.

intervals: 1) A-B = 600

2) A-C=50 C-F=150 F-G= 200 G-B=50

3) A-D=100 D-H=200 H-G=50 G-B=50.

we have to choose 3. but how I find this direction and min. interval? please help me.

note: sorry for my english.

Read only

0 Likes
1,374

Hello



data: begin of itab occurs 0,
    index rype i,
    field1 type i,
    field2 type i,
    field3 type i,
    field4 type i,
end of itab.  
data: sum1 type i,
      sum2 type i,
      ind type i.

clear itab.
itab-index = itab-index + 1.
itab-field1 = 600. itab-field2 = 0. itab-field3 = 0. itab-field4 = 0.
append itab.
itab-index = itab-index + 1.
itab-field1 = 50. itab-field2 = 150. itab-field3 = 200. itab-field4 = 50.
append itab.
itab-index = itab-index + 1.
itab-field1 = 100. itab-field2 = 200. itab-field3 = 50. itab-field4 = 50.
append itab.
read table itab index 1.
if sy-subrc = 0.
  sum1 = itab-field1 + itab-field2 + itab-field3 + itab-field4.
  ind = itab-index.
endif.
loop at itab
  clear: sum2.
  sum2 = itab-field1 + itab-field2 + itab-field3 + itab-field4.
  if sum2 < sum1.
    sum1 = sum2.
    ind = itab-index.
  endif.
endloop.
read table itab with key index = ind.
if sy-subrc = 0.
  write: 'Your direction is', itab-index, 'min. interval = ', sum1.
endif.

Read only

0 Likes
1,374

hi Maroz,

I do not mean like that. Firstly I have to find direction firstly. I dont know which I use that directions. my table fields like:

begin, end, interval. (for example: begin = 'A'. end = 'B'. interval = '600'.)

from my example, how can find 2. direction or 3. direction. (means from A to D, from D to H, H to G and from G to B. may be I go wrong direction like from H to F.) that is I have to find direction from A to B with min interval.

I think, I have to calculate all records in my table and find min intervals. but I dont know how I do it.

note: please forgive me for my english.

Read only

0 Likes
1,374

Hello

Are you have example of the code on other language (C/C++/C# etc) ?

Read only

0 Likes
1,374

#include <stdio.h>

int main()
{

int m,n,mat[100][100],i,j,l;
int a,b,top,max=50000;

char k1='c'; 
	
printf("total country = ");
scanf(" %d", &n);

printf(" total direction = ");  // total road: A-C is a road B-G is another road
scanf(" %d", &m);


	for(i=1;i<=n;i++){

		for(j=1;j<=n;j++){

			mat<i>[j]=max;
		}	
	}

for(i=1;i<=n;i++)
{
    mat<i><i>=0;
}

for(i=1;i<=m;i++)
{
printf("enter first country ");
scanf("%d", &a);

printf("enter second country",i);
scanf(" %d", &b);

printf("intervals of countries");
scanf("%d", &mat[a]<b>);

mat<b>[a]=mat[a]<b>;
}

for(l=1;l<=n;l++)
{

	for(i=1;i<=n;i++)
{
	       

	                	for(j=1;j<=n;j++)
{

							
		                          top=mat<i>[l] + mat[l][j];
			  
			                           if(top < mat<i>[j]){
			             
			                              mat<i>[j]=top;
			}
												
		}	
	
	}


}
while(k1 != 'q')
{
printf(" enter to find interval of 2 countries = ");

scanf(" %d", &a);
scanf(" %d", &b);

printf(" min interval = %d \n",mat[a]<b>);

printf(" q for quit any key for continue = ");
scanf(" %c", &k1);
}




}

intervals: 1) A-B = 600

2) A-C=50 C-F=150 F-G= 200 G-B=50 (that is A-B = 450)

3) A-D=100 D-H=200 H-G=50 G-B=50. (that is A-B = 400)

example records in my table

A | B | C | D | F | G | H

A | 0 | 600 | 50 | 100 | - | - | -

B |600 | 0 | - | - | - | 50| -

C | 50 | - | 0 | - | 150| - | -

D |100 | - | - | 0 | - | - |200

F |- | - |150| - | 0 |200|-

G |- | 50 |- | - | 200| 0 |50

H |- |- | - | 200 | - | 50| 0

after program run

A | B | C | D | F | G | H

A | 0 | 400 | 50 | 100 | 200 |350 | 300

B |400 | 0 | 400| 300 | 250 | 50 | 100

C | 50 | 400 | 0 | 150 | 150 | 350 | 400

D |100 |300 | 150 | 0 | 300 | 250 |200

F |200 |250 |150 |300 | 0 | 200 |250

G |350 | 50 |350 | 250 | 200 | 0 |50

H |300 |100 | 400 | 200 | 250 | 50 | 0

I dont update my table. I just want find direction and min interval to go from A to B.

I think not hard for gurus.

thank all for help.

Read only

matt
Active Contributor
0 Likes
1,374

Define your "array" like this:

TYPES: BEGIN OF mat_ty,
         dim1 TYPE i,
         dim2 TYPE i,
         valu TYPE i,
       END OF mat_ty.

DATA: t_mat  TYPE HASHED TABLE OF mat_ty WITH UNIQUE KEY dim1 dim2,
      ls_mat TYPE mat_ty.

FIELD-SYMBOLS: <ls_mat> TYPE mat_ty.

Now, the array has to be initialised before you can use it. For each "point" in the "array" x,y, where x and y are integers, you need to do something like:

ls_mat-dim1 = x.
ls_mat-dim2 = y.
ls_mat-valu = 50000. " for example
INSERT ls_mat INTO TABLE lt_mat.

Once you've initialised all the points, you can access a "point" x,y in the "array" using READ.

READ TABLE ls_mat WITH TABLE KEY dim1 = x dim2 = y ASSIGNING <ls_mat>.
IF <ls_mat> IS ASSIGNED... " If it isn't assigned then the point wasn't initialised
  <ls_mat>-valu = new_value. " This line changes the value of the "point" you've just read from the "array". 
,,,

With the code you've already got, that should be sufficient to get your solution. Here's a beginning.

DATA: i TYPE i,
      j TYPE i,
      l TYPE i,
      a TYPE i,
      b TYPE i,
      top TYPE i.

CONSTANTS: max TYPE i VALUE 50000.

FIELD-SYMBOLS: <ls_mat> TYPE mat_ty.

TYPES: BEGIN OF mat_ty,
         dim1 TYPE i,
         dim2 TYPE i,
         valu TYPE i,
       END OF mat_ty.

DATA: t_mat  TYPE HASHED TABLE OF mat_ty WITH UNIQUE KEY dim1 dim2,
      ls_mat TYPE mat_ty.

PARAMETERS: n TYPE i, " Total country
            m TYPE i. " Total direction.

i = 1.
DO.
  IF i GT n.
    EXIT.
  ENDIF.
  DO.
    j = 1.
    IF j GT n.
      EXIT.
    ENDIF.
    ls_mat-dim1 = i.
    ls_mat-dim2 = j.
    ls_mat-valu = max.
    INSERT ls_mat INTO TABLE lt_mat.
    ADD 1 TO j.
  ENDDO.
  ADD 1 TO i.
ENDDO.

i=1.
DO.
  IF i GT n.
    EXIT.
  ENDIF.
  READ TABLE lt_mat WITH TABLE KEY dim1 = i dim2 = i ASSIGNING <ls_mat>.
  CLEAR <ls_mat>-valu.
ENDDO.

Regards

matt

Read only

0 Likes
1,374

thanks matt,

I did like you. of course changed something but generally problem solved with your method. again thanks. another thanks to jan.

Read only

Former Member
0 Likes
1,374

hi,

Internal Table is the concept which is similar to Arrays in other programming languages.

begin of fs_data,
   int type i,
end of fs_data.

data t_data like
standard table of fs_data.

This will create a single dimensional Array like structure. Fill data in it and perform Operations.

Hope this will help.

Sumit Agarwal

Edited by: Craig Cmehil on Jul 3, 2008 3:30 PM

Read only

Former Member
0 Likes
1,374

thanks Matt,

your code for helpful for me. I wrote like that, but still I dont know direction that I use. and there is no calculation for min interval. if I am wrong you just find interval of just 2 countries. I want to say may be no any road from A to B to directly ( mean, there is no record in my table). you have to go via neighbor of countries. ( ex: Via D,H and G) I have to calculate interval of neigbor's interval of these two countries.

note: I dont know, but may be there is a FM for this problem. if it is, may result my problem.

Read only

matt
Active Contributor
0 Likes
1,374

I've answered your question (I think) about how to emulate arrays, so that you should be able to just translate your C program into ABAP quite easily. Does the C program work? I've only made a start on translating from your program into ABAP - you need to do the rest yourself.

I don't really understand what the program is trying to achieve, so I can't help you on that bit, I'm afraid.

Read only

0 Likes
1,374

Hi.

I think Matthew has given a lot of input about how to solve your task in ABAP. I would suggest that you try to understand the algorithm used in the C-code beforehand. The interesting part is in the nested loops... But I would say that the used algorithm to compute the shortest distance might not be the best available. Do you really have a business requirement for such a program? If yes: how large might the number of citys/countrys get, what is the typical number? Do you really need distances for all pairs or do you just need a few? Depending on your scenario some other algorithms might be better.

Best regards,

Jan Stallkamp