How to add 2d array to array

Hello i am a student learning C++ coding and only know some of the basics.What i want to find out is how do you add an array to a 2d array column by column.
In this case what i want to do is to add busstopwaitingtime to bustransittime[Maxbus][Timeslots] which is 5+10 ,8+10 ,18+10 for the first column and so on and so forth.I would be grateful if anyone could help me :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <stdio.h>
 
#define Timeslots 5
#define Maxbus 3


void main(void)
{
	int bustransittime[Maxbus][Timeslots]={
                                                                      {5, 5, 6, 6, 7} ,  
                                                                      {8, 8, 7, 7, 6} , 
                                                                      {18, 19, 17, 20, 21}   
                                                                       };
	int busstopwaitingtime [5]={10,10,15,15,8};

}
Last edited on
Two nested for-loops. Do you already know the concept of a loop (aka iteration)?


PS. There is no matching opening brace (on line 9) for the closing brace on line 12.
Thanks i fixed the brace.I know the basic concept of a loop but i do not know how to apply it in a 2d array.


Last edited on
For example:
FOR each column in bustransittime DO something (with corresponding element of busstopwaitingtime).

The something could be like
FOR each element in a column DO add X to element

See http://www.cplusplus.com/doc/tutorial/arrays/
I am sorry but i forgot to include in the main topic that i need to find the longest time to reach home after adding up the column.But i did my best to try and do it but the longest time traveled value is zero.
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
#include <stdio.h>
 
#define Timeslots 5
#define Maxbus 3


void main(void)
{
	int j,k,Totaltimetoreachome[Maxbus][Timeslots],Longesttimetoreachhome;
	int bustransittime[Maxbus][Timeslots] = {  
 {10, 10, 10, 10, 10} ,  
 {30, 30, 30, 30, 30} , 
 {35, 35, 35, 35, 35}   
               };
	int busstopwaitingtime[5]={5,5,5,5,5};
	Longesttimetoreachhome=0;
	for (j=0; j<5; j++) 
	{
      for (k=0; k<3; k++) 
      Totaltimetoreachome[k][j]=bustransittime[k][j]+busstopwaitingtime[j];
	  if (Totaltimetoreachome[k][j]>Totaltimetoreachome[k-1][j-1])Totaltimetoreachome[k][j]=Longesttimetoreachhome;

	}
        printf("%d\n",Longesttimetoreachhome);
            

}
 
Please explain line 21.
I was trying to get the longest time to reach home by replacing Longesttimetoreachhome with the larger number if there is one when bustransittime[k][j]+busstopwaitingtime[j].Is there a better way to do this?
to iterate through a 2D array, use a nested for loop:

1
2
3
4
5
6
for (int row; row<Maxbus; row++) {
for (int column; column<Timeslots; column++) {
cout << NameOfArray[row][column]
}
cout << endl;
}


also, main() should return an int. If your compiler allows void, its probably outdated.
Last edited on
Sorry but I am currently limited to using code from C for this assignment so i am unable to use cout.
I am currently using Microsoft visual studio 2010 so yes it is outdated.
You have:
1
2
3
4
Longesttimetoreachhome = 0;
for (k=0; k<3; k++) 
Totaltimetoreachome[k][j]=bustransittime[k][j]+busstopwaitingtime[j];
  if (Totaltimetoreachome[k][j]>Totaltimetoreachome[k-1][j-1])Totaltimetoreachome[k][j]=Longesttimetoreachhome;

This is the same code, with some reformat:
1
2
3
4
5
6
7
8
Longesttimetoreachhome = 0;
for ( k=0; k<3; k++ ) {
  Totaltimetoreachome[k][j] = bustransittime[k][j]+busstopwaitingtime[j];
}

if ( Totaltimetoreachome[k][j] > Totaltimetoreachome[k-1][j-1] ) {
  Totaltimetoreachome[k][j] = Longesttimetoreachhome;
}

The first point to note is that the if-clause is not in the loop. The 'k' will be 3 in the if-clause and that is not within the arrays.

The second point is that you compare two elements in Totaltimetoreachome rather than against current Longesttimetoreachhome. As side-effect, if the 'k' could be 0, you would compare to element in index -1, i.e. again not within the array.

Finally, you do update the Totaltimetoreachome rather than the Longesttimetoreachhome.


You will learn to pay attention to such details.
Well it took me awhile but i think i figured it out.However i could not get if ( Longesttimetoreachhome < currenttotaltime ) to check the first two row after the addition
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
#include <stdio.h>
 
#define Timeslots 5
#define Maxbus 3


void main(void)
{
	int j,k,Longesttimetoreachhome,currenttotaltime;
	int bustransittime[Maxbus][Timeslots] = {  
 {10, 10, 10, 10, 10} ,  
 {40, 20, 30, 30, 30} , 
 {35, 25, 32, 34, 36}   
               };
	int busstopwaitingtime[5]={5,5,5,5,5};
	currenttotaltime=0;
	Longesttimetoreachhome=0;
	for (j=0; j<5; j++) 
	{
      for (k=0; k<3; k++) 
      currenttotaltime=bustransittime[k][j]+busstopwaitingtime[j];
	if ( Longesttimetoreachhome < currenttotaltime )
	      Longesttimetoreachhome=currenttotaltime;
	  
	
	} 
	      printf("Longest time to reach home is %d\n",Longesttimetoreachhome);

}
 
Last edited on
The "body" of the loop of line 20 is only the line 21. The If-clause of lines 22-23 happens only once and after the loop.
Hmm i ran out of idea what i can do to make the If-clause run inside the "body" of the loop of line 20.Could you give me any hints or what topics should i read up on.
Last edited on
sorry but I still do not understand how should i use the if else loop in my case. If i use it in this way
1
2
3
4
5
6
7
	for (k=0; k<3; k++)
if (Longesttimetoreachhome < currenttotaltime ) 

Longesttimetoreachhome=currenttotaltime;      

else currenttotaltime=bustransittime[k][j]+busstopwaitingtime[j];  
		

it will only work if this does not happenif (Longesttimetoreachhome < currenttotaltime )
That is not what you want.

In the beginning of that linked tutorial word block was described with if-clause.
If you want to include more than a single statement to be executed when the condition is fulfilled, these statements shall be enclosed in braces ({}), forming a block:


You already had braces on lines 19 and 26, so that the block of statements on lines 20--25 is executed 5 times for the outer loop (for (j=0; j<5; j++)).
Ok thank you for elaborating.I have corrected my mistake by adding another for loop.Its seems to work correctly now.Is there any way i could improve on my code?



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
31
#include <stdio.h>
 
#define Timeslots 5
#define Maxbus 3


void main(void)
{
	int j,k,i,Longesttimetoreachhome,currenttotaltime;
	int bustransittime[Maxbus][Timeslots] = {  
 {10, 10, 10, 10, 10} ,  
 {40, 20, 30, 30, 50} , 
 {35, 25, 32, 34, 36}   
               };
	int busstopwaitingtime[5]={5,5,5,5,5};
	currenttotaltime=0;
	Longesttimetoreachhome=0;
   {
	   for (j=0; j<5; j++) 
	{
      for (k=0; k<3; k++) 
	  {
		  for(i=0;i<1;i++)
         currenttotaltime=bustransittime[k][j]+busstopwaitingtime[j];
	     if ( Longesttimetoreachhome < currenttotaltime )
	      Longesttimetoreachhome=currenttotaltime;
	  } 
	}    printf("Longest time to reach home is %d\n",Longesttimetoreachhome);

   }

What is the purpose of line 23?
hmm for the loop to run 3times once.But apparently that was what the program was doing and that line is useless.
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
31
32
#include <stdio.h>
 
#define Timeslots 5
#define Maxbus 3


void main(void)
{
	int j,k,Longesttimetoreachhome,currenttotaltime;
	int bustransittime[Maxbus][Timeslots] = {  
 {10, 10, 10, 10, 10} ,  
 {40, 20, 50, 30, 10} , 
 {35, 25, 32, 34, 36}   
               };
	int busstopwaitingtime[5]={5,5,5,5,5};
	currenttotaltime=0;
	Longesttimetoreachhome=0;
   {
	   for (j=0; j<Timeslots; j++) 
	{
      for (k=0; k<Maxbus; k++) 
	  {
         currenttotaltime=bustransittime[k][j]+busstopwaitingtime[j];
	     if ( Longesttimetoreachhome < currenttotaltime )
	      Longesttimetoreachhome=currenttotaltime;
	  } 
	}    
        printf("Longest time to reach home is %d\n",Longesttimetoreachhome);

   }

}
Topic archived. No new replies allowed.