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 !!!
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 !!!
#define NUM_COLS 4 /* Person ID, Wage, Sex, Age */
#define NUM_ROWS 10 /* definable amount of rows */
int twod_array[NUM_ROWS][NUM_COLS];
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 :-)
Well if the number of columns is constant, then do this
The only difference between aptr and twod is readability - the choice is yours.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; }
that code brings up 2 error warnings in vis studio ....... is it meant to be adapted............ ? thanks for your help :-)
> 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.
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 ?