Thread: HELP with 2D arrays

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    HELP with 2D arrays

    hi i need help scanning values from a file into a 2d dynamic array
    i attached the file i need to scan

    and heres my code so far

    the array should be 7X6


    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "linear_algebra.h"
    
    int main(){
    	int n,i,j;
    
    	FILE * ifile;
      ifile = fopen ("points.csv","r");
    	fscanf(ifile, "%d\n", &n);/*read in the number of points n.*/
    	printf("%d\n", n);
    
    
    
      double **a;     /* this is the array of points name */
      int num_rows; /* this variable will be used for the first  dimension */
      int num_col; /* this variable will be used for the second dimension */
    
    
      num_rows = n;
      num_col = 6;
    
      /*  allocate storage for an array of pointers */
      a = malloc(num_rows * sizeof(double *));
    
      /* for each pointer, allocate storage for an array of doubles */
      for (i = 0; i < num_rows; i++) {
        a[i] = malloc(num_col * sizeof(double));
      }
    
      /* assign the value to each element        */
      for (i = 0; i < num_rows; i++) {
        for (j = 0; j < num_col; j++) {
          fscanf(ifile,"%lf\n", &a[i][j]);
    
        }
    
      }
    
      /* now show the contents that were assigned */
      for (i = 0; i < num_rows; i++) {
        for (j = 0; j < num_col; j++) {
          printf("%lf",a[i][j]);
        }
    
      }
    
    
    	/* now for each pointer, free its array of dbles */
      for (i = 0; i < num_rows; i++) {
        free(a[i]);
      }
      /* now free the array of pointers */
      free(a);
    
    	fclose(ifile);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    so what is the problem? what is it doing/not doing...show some output

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what's the question?

    > fscanf(ifile,"%lf\n", &a[i][j]);
    The \n is unnecessary ?

    > printf("%lf",a[i][j]);
    There is no space between output numbers, so it's all mushed together into one long string?

    > double **a;
    C doesn't support mixed declarations and statements. Move these above your initial statements to where the rest of your variables are declared.

    Everything else seems OK.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  2. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 07:33 AM