2-Dimensional Dynamic Array

This is a discussion on 2-Dimensional Dynamic Array within the C Programming forums, part of the General Programming Boards category; Hi, I tried to get my program to count the number of rows in the input data file and then ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    London
    Posts
    6

    2-Dimensional Dynamic Array

    Hi,
    I tried to get my program to count the number of rows in the input data file and then output back the same data with 7 columns and however many rows. It's giving me all zeros in the output file though, which isn't supposed to happens since the input file is just random numbers, e.g.:

    123.45 111.23 33.45 66.345 43545.54 543.54 5435.43

    Could someone take a look at the code?

    Code:
    	int j , i; 
    	int rows = 1;
    	float num;
    	int c;
    
    	while ((c = fgetc(fp)) != EOF)
    	{
    		if (c == '\n')
    		{
    		rows++;
    		printf("%d\n", rows);
    		}
    	}
    	
    	float **data;
    	data = (float**) malloc(rows * sizeof(int));
    	for (int i = 0; i < rows; i++)
    	{data[i] = (float*)malloc(7*sizeof(int));
    	}
    	
    	//declares array with 7 columns and number of rows in file
    	
    	for (j=0; j< rows; j++) //repeats for max number of columns
    	{
    		for (i=0; i< 7; i++)
    		{
    			fscanf(fp, "%f", &num);
    			
    			data[j][i] = num;
    		}
    	}	
    	
    	
    	for(j = 0; j < rows; j++) //repeats for max number of columns
    	{
    		for (i=0; i< 7; i++)
    		{ 
    			num = data [j][i];
    			
    			fprintf(fp2, "%f ", num);
    			
    		}
    		fprintf(fp2, "\n");
    	}
    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    data = (float**) malloc(rows * sizeof(int));
    What on earth is this supposed to do?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    London
    Posts
    6
    Oh sorry, those floats are meant to be integers, I was just trying some stuff and I forgot to change it back. Revised code:

    Code:
    int j , i; 
    	int rows = 1;
    	float num;
    	int c;
    
    	while ((c = fgetc(fp)) != EOF)
    	{
    		if (c == '\n')
    		{
    		rows++;
    		printf("%d\n", rows);
    		}
    	}
    	
    	int **data;
    	data = (int**) malloc(rows * sizeof(int));
    	for (int i = 0; i < rows; i++)
    	{data[i] = (int*)malloc(7*sizeof(int));
    	}
    	
    	//declares array with 7 columns and number of rows in file
    	
    	for (j=0; j< rows; j++) //repeats for max number of columns
    	{
    		for (i=0; i< 7; i++)
    		{
    			fscanf(fp, "%f", &num);
    			
    			data[j][i] = num;
    		}
    	}	
    	
    	
    	for(j = 0; j < rows; j++) //repeats for max number of columns
    	{
    		for (i=0; i< 7; i++)
    		{ 
    			num = data [j][i];
    			
    			fprintf(fp2, "%f ", num);
    			
    		}
    		fprintf(fp2, "\n");
    	}

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    The data you want to read in looks like floating point to me. So you want to allocate floats, not ints.

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by juice_box View Post
    Code:
    ...
    	while ((c = fgetc(fp)) != EOF)
    	{
    ...	
    	for (j=0; j< rows; j++) //repeats for max number of columns
    	{
    		for (i=0; i< 7; i++)
    		{
    			fscanf(fp, "%f", &num);
    			
    			data[j][i] = num;
    		}
    	}
    Ignoring your problems with malloc(), you're reading to the end of the file, and then reading some more.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic array allocation
    By Maulrus in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2010, 05:08 PM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Dynamic 2 Dimensional Array
    By mrb260478 in forum C Programming
    Replies: 23
    Last Post: 06-21-2008, 07:19 AM
  5. Dynamic 2 dimensional array with new?
    By Quantum1024 in forum C++ Programming
    Replies: 10
    Last Post: 11-19-2005, 01:46 PM

Tags for this Thread


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