Help with Arrays :-)

This is a discussion on Help with Arrays :-) within the C Programming forums, part of the General Programming Boards category; Hi I would like to know how I can use a 2d array for instance to input some variable for ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    18

    Talking Help with Arrays :-)

    Hi I would like to know how I can use a 2d array for instance to input some variable for instance ...........


    I need 4 columns and a definable amount of rows

    Person ID........... Wage...........Sex............Age
    ?
    ?
    ?
    ?
    ?
    ?

    thanks

    Greg !!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    #define NUM_COLS 4 /* Person ID, Wage, Sex, Age */
    #define NUM_ROWS 10 /* definable amount of rows */
    int twod_array[NUM_ROWS][NUM_COLS];

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    18

    thanks

    but I need the user to be able to define the number of rows not .... to actually state it in the c source file .... if u understand what I mean :-)

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Well if the number of columns is constant, then do this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int row_t[4];
    
    int main ( ) {
        int    (*aptr)[4];
        row_t  *twod;
        int     nrows = 100;    // or user input
        int     r, c;
    
        aptr = malloc( nrows * sizeof(int[4]) );
        twod = malloc( nrows * sizeof(row_t) );
        for ( r = 0 ; r < nrows ; r++ ) {
            for ( c = 0 ; c < 4 ; c++ ) {
                aptr[r][c] = 0;
                twod[r][c] = 0;
            }
        }
    
        free( aptr );
        free( twod );
        return 0;
    }
    The only difference between aptr and twod is readability - the choice is yours.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    18
    that code brings up 2 error warnings in vis studio ....... is it meant to be adapted............ ? thanks for your help :-)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > is it meant to be adapted
    NO!

    It's meant to be compiled as C and not C++

    Either write in C or write in C++, and name your files appropriately.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    18

    .......

    lol uppss forgot bout that ! thanks works fine ....... just need to know how to use the scanf command to enter data into the array i.e. row1 column1 row1 column2 etc...... oh is there a way I can make it so that after filling a row eg all 4 colums contain data I can make it ask if the user wishes to add more data or finish the input ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21