All pastes #1978433 Raw Edit

mycode

public text v1 · immutable
#1978433 ·published 2010-11-01 04:28 UTC
rendered paste body
#include <stdlib.h>
#include <stdio.h>

void print_array(int a[],int num_elements);


int main ( int argc, char *argv[] )
{
	int array[1000];
	int length=0,in;

	printf("\nArray:\n");
	if ( argc != 2 )
	{
		printf( "usage: %s filename", argv[0] );
	}
	else
	{
		FILE *infile = fopen( argv[1], "r" );

		if ( !infile )
		{
			printf("unable to open %s\n", argv[1]);
			exit(1);
		}
		else
		{
			printf("the first test\n");
			while (fscanf(infile, "%d", &in) == 1)
			{
				printf("the test in side while, just read %d\n", in);
				array[length]=in;
				++length; // Get in the habit of prefix increment for C++ efficiency
			}
			fclose(infile);
		}
		fclose(infile );
	}
	print_array(array,length);

	printf("the test number two \n");

	return (0);
}

void print_array(int a[],int num_elements)
{
	int i;
	printf("%i elements\n", num_elements);
	for(i=0; i<=num_elements; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}
 

this are the input numbers from the file.

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