Parallel arrays

This is a discussion on Parallel arrays within the C Programming forums, part of the General Programming Boards category; Code: hi everyone, need some help using parallel arrays if the user inputs 1 4 5 4 3 3 1 ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    23

    Parallel arrays

    Code:
    hi everyone,
    need some help using parallel arrays
    if the user inputs
    
    1  4  5 
    4  3  3
    1  3  5  
    
    how can i set up 3 parallel arrays to record these values?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Exactly which part of the question do you want help with??

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's an example of something similiar:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int *getrow(int length) {
    	int i, ii, ch, *row=malloc(length*sizeof(int));
    	char buffer[16];
    	
    	printf("Enter %d numbers seperated by commas\n"
    		"(max 4 digits): ", length);
    	for (i=0; i<length; i++) {
    		ii=0;
    		while ((ch=fgetc(stdin))!=',') {
    			if (ch=='\n') break;
    			buffer[ii]=ch; 	
    			ii++;
    		}
    		buffer[ii]='\0';
    		row[i]=atoi(buffer);
    	}
    	return row;
    }	
    
    
    void showmatrix (int *matrix[], int rows, int cols) {
    	int i, ii;
    	for (i=0; i<rows; i++) {
    		for (ii=0; ii<cols; ii++) printf("%5d", matrix[i][ii]);
    		printf("\n");
    	}
    }
    
    
    		
    
    int main(int argc, char *argv[]) {
    	int rows, cols, i, **matrix;
    	if (argc<3) {
    		printf("Usage \"%s [NO OF ROWS][NO OF COLS]\", eg %s 3 3\n",argv[0],argv[0]);
    		return 0;
    	}
    	
    	rows=atoi(argv[1]); cols=atoi(argv[2]);
    	matrix=malloc(rows*sizeof(int*));
    	for (i=0; i<rows; i++) matrix[i]=getrow(cols);
    	
    	showmatrix(matrix,rows,cols);
    	for (i=0; i<rows; i++) free(matrix[i]);
    	free(matrix);
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Parallel Arrays
    By kippwinger in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2003, 04:18 PM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11:14 PM
  5. parallel arrays
    By KeeNCPP in forum C++ Programming
    Replies: 1
    Last Post: 10-18-2001, 10:56 PM

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