Thread: programming with ansi c - BJ Holmes

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    Question 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
    i re-iterate please
    ive been on this problem a few days now

    thanks in advance
    luigi
    Last edited by luigi40; 06-01-2004 at 06:36 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to know if my code is ANSI strict ?
    By jabka in forum C Programming
    Replies: 1
    Last Post: 10-19-2007, 07:32 AM
  2. ANSI C Reference
    By Stack Overflow in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-09-2005, 10:08 PM
  3. how closely does vc++.net 2003 follow ANSI?
    By krygen in forum C++ Programming
    Replies: 7
    Last Post: 09-22-2004, 06:48 PM
  4. sigaction() and ANSI C
    By awoodland in forum Linux Programming
    Replies: 4
    Last Post: 04-25-2004, 01:48 AM
  5. Replies: 5
    Last Post: 10-09-2002, 12:37 PM