"Control Break" - Multi-Dimensional array

Reading Array OK .. Cannot think of the logic for the print function

Page 1 of 1

3 Replies - 828 Views - Last Post: 10 August 2009 - 06:21 AM Rate Topic: -----

#1 morgog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-April 09

"Control Break" - Multi-Dimensional array

Posted 09 August 2009 - 09:03 AM

The problem is to do with reading a rainfall.txt file into an Array (The coding is written & works)

File structure: City Code, Month Day of the month, rainfall for that day.

rainfall.txt - a total of 78 recodrs
[/quote]
D,6,23,39
D,5,12,29
D,1,2,67
D,3,2,50
D,2,26,66
D,4,12,96
D,1,28,58
D,5,14,26
D,1,10,17
D,6,12,40
D,1,29,16
D,3,16,78
D,3,14,16
D,6,18,52
D,3,20,62
D,4,26,28
D,4,2,83
S,5,29,95
S,1,10,11
S,5,15,69
S,5,12,10
S,6,2,59
S,1,30,71
S,3,13,13
S,1,14,31
S,2,20,43
S,3,6,34
S,6,17,77
S,1,2,82
S,3,14,17
B,5,23,66
B,1,5,72
B,4,13,36
B,5,9,55
B,4,25,71
B,5,15,96
B,2,23,62
P,1,5,23
P,1,19,103
P,6,29,103
P,2,21,77
P,1,1,97
[quote]



and then printing into the format of (This is the part where I cannot envisage the logic to proceed)
[

[/b]Sydney

January
1		 2		 3	   4	 5	6	7	8	 9	10...
		  16													  11

March
1		 2

Etc



I have tried 3 or 4 ways to code, but I cannot think of the logic required to proceed.
I would appreciate any suggestions (NOT CODE) as to the logical process of accomplishing the print portion of the exercise.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: "Control Break" - Multi-Dimensional array

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: "Control Break" - Multi-Dimensional array

Posted 09 August 2009 - 09:31 AM

View Postmorgog, on 9 Aug, 2009 - 03:03 PM, said:

The problem is to do with reading a rainfall.txt file into an Array (The coding is written & works)

File structure: City Code, Month Day of the month, rainfall for that day.




and then printing into the format of (This is the part where I cannot envisage the logic to proceed)
[

[/b]Sydney

January
1		 2		 3	   4	 5	6	7	8	 9	10...
		  16													  11

March
1		 2

Etc



I have tried 3 or 4 ways to code, but I cannot think of the logic required to proceed.
I would appreciate any suggestions (NOT CODE) as to the logical process of accomplishing the print portion of the exercise.

Thanks!

are you using C or C++ - you can set the field width so data in output in columns, e.g. in C++
http://www.cplusplus...ios_base/width/
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: "Control Break" - Multi-Dimensional array

Posted 09 August 2009 - 09:41 AM

Well to adequately setup a control break type of scenario, you are going to need to sort the data. That way when you hit sidney, you go into an inner loop which will loop through all records for january, going into another loop for days. In other words, all records for sydney are in a group, then it is grouped by months in order, then days in order.

S,1,2,82
S,1,10,11
S,1,14,31
S,1,30,71
S,2,20,43
S,3,6,34
S,3,13,13
S,3,14,17
S,5,12,10
S,5,15,69
S,5,29,95
S,6,2,59
S,6,17,77



So now you have the months in order where months are 1, 2, 3, 5, 6 and for each of those months the days are in order. Now you can create three nested loops, one which loops for each day of a given month, once the month changes it restarts. That will be inside another loop which will go through each month until the city code changes. That will be inside another loop which will keep going until there are no more city codes.

Loop for each city code
	 Loop for each month (once city code changes, break)
		  Loop through the days of given month (once month changes, break)
		  End loop
	 End loop
End loop



Hopefully you get the idea. The breaks are conditional on the element before it changing. Once you detect the change, you need to reset the inner loop. You reset going through days when you detect a change in the month of a given city code. You detect a reset of the months when the city code changes.

:)
Was This Post Helpful? 1
  • +
  • -

#4 morgog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-April 09

Re: "Control Break" - Multi-Dimensional array

Posted 10 August 2009 - 06:21 AM

Sorry for not being clear.
It is C not C++


Martyr2 :^:

Damm!! :rolleyes:

Even after a quick glance at your post I can see the logic you are setting out ... and feeling a fool for not going down that track myself.
I'll printout your post and start down the right way of coding this.

Thanks very much for your clear and concise explanation of the logic to program for this.
:bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1