#include<iostream.h>
#include<stdio.h>
#include<io.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>

void minmax(int);

float max[13],min[13],array[13][270];
void main()
{
    FILE *fp;
    char line[100];
	char symptom[10];
	float f;
	fp=fopen("input.txt","r");
	int i=0,j=0,k,l=0;
	int rw_cnt = 0;
	int cl_cnt = 0;
	int len;

while(!feof(fp))
	
	{
		//i=0;
		//l=0;
		fgets(line,99,fp);
         printf("%s \n",line);
	//	 getch();
		 rw_cnt++;
	//	 len=strlen(line)+;
	//	line[len]='\0';
		//for(i=0;line[i]!='\n';i++)
		 for (i=0;i<50;i++)
		{
			while(line[i]!=' ')//&& line[i]=='\n')
				{
					symptom[j]=line[i];
					j++;
					i++;
					cl_cnt++;
				}
			symptom[j]='\0';
			printf("Symptom= %s \n",symptom);
			printf("Symptom = %f \n",atof(symptom));

		/*for(int l = 0; l<10;l++)
		{
			symptom[l]=0;
		}*/

		j = 0;
		
		}
		printf("");

}
cout << rw_cnt;

		/*
		f=atof(symptom);
			array[l][k]=f;
			l++;
			i++;
			j=0;
			k++;
		printf("array=%d",array[l][k]);
	}
	*/

I have text file of 270 rows and 13 columns consisting of floating point values.
I want to separate each column separately in the array and find min and max of each array.
Thanks in advance

Recommended Answers

All 4 Replies

Is this a C or C++ program? You have a mixture of the two header files. It looks like mostly C so I'm moving it into the C board.

line 1: delete it because stream.h is not a valid header file except in some very old compilers such as Turbo C++. Since you are writing a C program you can't use it here. Also remove most of the other header files because they aren't needed. Looks like all you need is stdio.h and stdlib.h.

line 12: int main() not void main(). main always returns an integer.

line 11. If the file contains 270 rows and 13 columns of data then the arrays you declared are backwards. Also not how to initialize them to 0 values.

float array[270][13] = {0.0F};
float max[270] = {0.0F};
float min[270] = {0.0F};

After using fgets() to retrieve a line you can use strtok() to split it up into individual parts

int column = 0;
char *token = strtok(line," ");
while( token != NULL)
{
    array[row][column] = atof(token);
    token = strtok(line);
}

Thanks for the help. I tried the suggestion but it didnt help.Then I tried code which is given below. I have used iostream because i am going to use c++ also.

#include "stdafx.h"
#include<stdio.h>
#include<math.h>

float example[14][270];
int  main()
{
	//printf("Hello World!\n");

FILE *fp;
const int nLineSize = 4096;
char line[nLineSize + 2];
char symptom[10];
int i=0,j=0,k=0,m=0;
int row_cnt=0;
float val;
fp=fopen("inputk.txt","r");

if (fp == NULL)
{
	perror ("Error opening file");
	exit(1);
}
 for(k=0;k<270;k++)// loop for no. of the rows
	   {
            i=0;
			m=0;
			line[0] = 0;
            fgets(line,nLineSize,fp);
            row_cnt++;
            puts (line);
         while(line[i]!='\n'&&m<13)
		 {
           while(line[i]!=' ')
			  {
		        symptom[j]=line[i];
                j++;
		        i++;
			  }
	       val=atof(symptom);
		   example[m][k]=val;
			m++;
			i++;
            j=0;
     		 }
   }
  
     
     symptom[0]=line[i];
		val=atof(symptom);
		//cout<<val;
		example[m][k]=val;
cout<<"total rows:="<<row_cnt<<endl;

for( i=0;i<270;i++)
	{
		for(j=0;j<13;j++)
			cout<<example[j][i]<<"  ";//display the array
				
	  cout<<"\n";
	 }

}

But now my problem is, code works only for the fixed number of rows and columns.
But I want to make it work for any no of row and column .
please help me

use const int values to define the array dimensions then use those variables in the code. Or you could do something like this:

int rows = sizeof( example ) / sizeof( example[0] );
int cols = sizeof( example[0] ) / sizeof(float);
for(k=0;k< rows ;k++) // line 24
{

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.