# programming with ansi c - BJ Holmes

This is a discussion on programming with ansi c - BJ Holmes within the C Programming forums, part of the General Programming Boards category; chapter 10.6.4 structures & arrays this is a question at the end the chapter: [4] a selection of towns/cities in ...

1. ## programming with ansi c - BJ Holmes

chapter 10.6.4 structures & arrays

this is a question at the end the chapter:

[4] a selection of towns/cities in three conties in england have populations to the nearest 100 as shown

county.................town/city.............population
-----------------------------------------------------------

wiltshire.................salisbury.............37 000
............................swindon............170 000
............................marlborough..........7 000

oxfordshire..............oxford..............11500 0
............................banbury..............3 8700
............................witney................ 17500

dorset....................poole................137 000
............................dorchester...........1 4500
............................weymouth...........494 00

(ps..forget the dots, just there to align the columns, spaces wouldnt work)

initialise the following the arrays.

(a) A one dimensional array containing the names of the counties.
(b) A two dimensional array containing records of the names of the towns/ cities and their respective populations, where each row represents a different county, in the order given in the first array.

Using the arrays from (a) and (b) write procedures to input the name of a county and the name of a town/ city, and perform a serial search on the one dimensional array to match the county and obtain a row subscript, then perform a serial search on the two dimensional array, to match the town/ city, and obtain a column subscript. Using the row and column subscripts, access the two dimensional array and display the value for the population of the chosen town/ city.

-----------------------------------------------------------------------------------

i have been trying various ways to attack this problem with my limited knowledge of 'c', however i cant get a mental picture of the array structure as listed above.

1) how does one link array (a) with array(b) ?

2) how does the row subscript on array (a) effect the column (or row) subscript on array (b) ?

i know what a two dimensional arra looks like and how to initialise one, this is my code so far

#include <stdio.h>

/* one dimensional array containing the names of the counties
0 9 20 */
char counties[28] = "WiltshireOxfordshireDorset";

/* two dimensional array containing records of the names of the
towns/cities and their respective populations */
char towns_cities[3][48] = {{"salisbury 37000 swindon 170000 marlborough 7000"},
{"oxford 115000 banbury 38600 witney 17500"},
{"poole 137000 dorchester 14500 weymouth 49400"}};

void main(void)
{

}

i dont want the answer, just a step in the right direction please
ive been on this problem a few days now

luigi

2. Well you've started off wrong. main returns an int [b]ALWAYS[/color]. Second, I wouldn't smash all the names together like that in your first single dimension array. I'd at least put a space between the names. Otherwise it's going to be a pain trying to print a single name or what not from the array. (Though I have no idea why the lesson creator decided a single dimension array was best suited for this task...)

Walk through the objective on paper or something to see exacty what you need to do.
Code:
```print a header bar
loop through all three counties
print a county name
loop through three cities in a line
print a city line```
That would simply display everything for you. It's just a matter of breaking the task up into pieces. If you do want it run all together, you can access each element of the array by index, and compare that value with what you're looking for.

I'd work on a program to just print them out first. Once you've got that down, try and figure out how to search for a given name.

Quzah.