c programs

Pages: 12
my text file looks like this:

lat long jan feb
90 80 45 67
90 34 56 78
90 45 78 56
55 34 56 78
55 67 77 89
55 56 78 90

I am new to c and also weak a bit in programming, I am given this assignment to finish anyhow.I want to access all the rows valuesbased on the value of the first column.Suppose i want to have the values for all the columns based on the first column value ie. within a range 90 to 50 , i should get all the rows printed within this range.
please , I will be highly grateful if some one can provide a c or c++ program as soon as possible.
closed account (o3hC5Di1)
Hi there,

Welcome to the forums, let's break your assignment down:

my text file looks like this:


You will need to open your textfile and read its contents into variables. So you will need at least one variable for every column. Tutorial about working with files: http://www.cplusplus.com/doc/tutorial/files/

I want to access all the rows values based on the value of the first column


Depending on your needs, you will either need to check the first value when you are reading it in to determine whether you will use it, or you can store all values from the text file in a container, like std::vector or a raw array if you can't use any STL containers. The container could then be sorted, allowing you fast search and access of values within a specific range. More information on this: http://www.cplusplus.com/faq/sequences/

Please note that we try not to provide solutions to homework problems. Rather, we try to help people solve them themselves so they learn in the process and understand what they are doing.

Please feel free to come back to us with your attempted code if you require any further assistance.

All the best,
NwN
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[20][10];
int i,j;

FILE *fp;
fp=fopen("jy.txt","r");

if(fp>0)
{
for(i=0;i<4;i++)
{
for(j=0;j<6;j++)
{
fscanf(fp,"%d" , &a[i][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<6;j++)
{
printf(" " "%d" " ", a[i][j]);
}
printf("\n");
}

fclose(fp);

return 0;
}

The above code works well with a small file as i have created like the one below:




3 2 3 3 4 5
3 4 5 6 7 9
3 7 2 4 3 5
3 7 5 6 6 5
3 8 7 6 8 6
8 7 7 4 7 8
8 4 4 6 6 3
8 5 2 6 5 6
8 4 3 3 2 2
5 2 8 9 0 9
0

Iam able to display them and also i have managed to get their sum row wise by another program: But my file is huge and its not working.Moreover i am running the code in turbo c but its getting abruptly terminated. please help me out .I will be very gratefull to you.




#include<stdio.h>
void main()
{
FILE *fp;
char ch;

fp=fopen("22M.txt","r");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;

printf("%c",ch);
}
fclose(fp);
}


This above code i can read my huge text file .please tell me the changes to get my problem solved using any language.Thank you.
closed account (o3hC5Di1)
Hi there,

You seem to be on the right track, but you declare an array like this:

int a[20][10];

20*10 Is probably not going to be big enough if your file is really huge.
Also, since the file is big, you probably want to consider allocating it on the heap and using a std::vector.

The following code is C++, as this is a C++ forum:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <fstream>
#include <vector>

constexpr int COLUMNS_AMOUNT = 10; //amount of columns in the textfile

int main(int argc, char** argv)
{
    std::ifstream file_stream("jy.txt"); //create filestream to file

    std::vector<std::vector<int>>* matrix; //pointer to multidimensional vector
    matrix = new std::vector<std::vector<int>>(); //allocate new vector on the heap

    int index=0, tmp;

    while (file_stream.good()) //while data in the filestream
    {
        for (int i=0; i<COLUMNS_AMOUNT; i++) //for every row
        {
            if (file_stream.eof()) //check for end of file
                break;

            file_stream >> tmp; //fetch the next value
            (*matrix)[index][i] = tmp; //store the next value
        }
         
        ++index; //increment row-index
    }

    //do whatever you want with the matrix

    file_stream.close();
    delete matrix;

    return 0;
}


Compiles fine (with C++11): http://ideone.com/eUzh4d

More information: http://www.cplusplus.com/doc/tutorial/files/


All the best,
NwN
Last edited on
Compiling NONAME01.CPP:
Error NONAME01.CPP 1: Unable to open include file 'FSTREAM'
Error NONAME01.CPP 7: Declaration syntax error

I got this error while compiling the code sent to me.help me please.thank you.
closed account (o3hC5Di1)
Are you sure you're compiling with a C++ compiler?

All the best,
NwN
I compiled in turbo c/c++.
Compiling NONAME01.CPP:
Error NONAME01.CPP 2: Unable to open include file 'VECTOR'
Error NONAME01.CPP 8: Type qualifier 'std' must be a struct or class name in function main(int,char * *)
Error NONAME01.CPP 8: Statement missing ; in function main(int,char * *)
Error NONAME01.CPP 10: Type qualifier 'std' must be a struct or class name in function main(int,char * *)
Error NONAME01.CPP 10: Statement missing ; in function main(int,char * *)
Error NONAME01.CPP 11: Undefined symbol 'multidimensional' in function main(int,char * *)
Error NONAME01.CPP 11: Statement missing ; in function main(int,char * *)
Error NONAME01.CPP 13: Undefined symbol 'vector' in function main(int,char * *)
Error NONAME01.CPP 13: Statement missing ; in function main(int,char * *)
Error NONAME01.CPP 17: Undefined symbol 'file_stream' in function main(int,char * *)
Error NONAME01.CPP 24: Undefined symbol 'tmp' in function main(int,char * *)
Error NONAME01.CPP 25: Undefined symbol 'matrix' in function main(int,char * *)
Error NONAME01.CPP 25: Undefined symbol 'index' in function main(int,char * *)
Error NONAME01.CPP 34: Undefined symbol 'matrix' in function main(int,char * *)
Warning NONAME01.CPP 37: Parameter 'argc' is never used in function main(int,char * *)
Warning NONAME01.CPP 37: Parameter 'argv' is never used in function main(int,char * *)


These errors i received while compiling next time.please help.
If possible please can you help me by retrieving the row values from jan-dec based on the range of lat and longitude.suppose i want to print the values from jan to dec within the range latitudes( 90-70) and longitudes (180-270).

please help me with this code.I will be very grateful to you.Please help me.
Please please help me in solving the proble.once i can read the values of this range then i can sum it up by own.please help me..........
Turbo is really outdated. What operating system do you have? On Windows, I recommend VC++ or MinGW - they're from within the last decade.
Hello, the problem is solved but i am not able to find the sum of float values.the answer is coming in the form like 11.0000, the exact value is not coming..please help me ,its urgent.
suppose i want to add 22.67800, 34.45678, 14.85670...........what should i do, %f is not working
please help.
suppose i want to add 22.67800, 34.45678, 14.85670...........what should i do, %f is not working
please help.

We can't know why your code isn't working without seeing it.
closed account (o3hC5Di1)
Hi there,

Did you declare the variable(s) you are adding as either of type float or type double?

If so - could you please share your current code?

All the best,
NwN
#include <stdio.h>
#include <stdlib.h>
#include<float.h>
int main()
{
FILE *fp;
float a[20][10];
int i,j;
float sum;
fp=fopen("jy.txt","r");

if(fp>0)
{
for(i=0;i<4;i++)
{
sum=0;
for(j=0;j<4;j++)
{
fscanf(fp,"%f",&a[i][j]);
sum=sum+a[i][j];
}
printf("\nsum for row %d is %f",i,sum);
}
}

fclose(fp);

return 0;
}



the file is


3.4567 2.4564 1.6783 3.3456
3.8999 6.2345 1.3333 6.3334
3.1111 7.3456 2.1234 4.6789
3.1256 8.2345 7.4567 6.5678
8.2345 7.4567 7.4567 4.2345
Last edited on
When posting code, please use code tags to make it readable.
okay next time it will be done.....please solve my problem if possible
hi there, are you able to do,i think we should use strtok....
my program to read the file within a range is below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/******PROGRAM TO READ THE FILE dataset.txt and PRINT output******/
/*****************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
  char *lon,*lat,*jan,*feb,*mar,*apr,*may,*jun,*jul,*aug,*sep,*oct,*nov,*dec;
  float v,l,j,f,mr,a,my,jn,jl,ag,s,o,n,d; 
  char line[300];
  FILE *fp = fopen("dataset.txt","r");
  FILE *fo = fopen("new.txt","w");
fprintf(fo,"Lat    \t Lon   \t  Jan   \tFeb    \tMar    \tApr    \tMay    \t  Jun  \t  Jul \t  Aug\t   Sep \t   Oct   \t Nov \tDec\n",line);
 
  while(fgets(line,sizeof line, fp)!= NULL) 
   {
      lat=strtok(line," ");
      v=atof(lat); 
     if (v>10 && v<35)
       {
         lon=strtok(NULL," ");        
         l=atof(lon);
          if(l>65 && l<100 )  
            {
              jan=strtok(NULL," ");
		 j=atof(jan);

		feb=strtok(NULL," ");
		 f=atof(feb);

		mar=strtok(NULL," ");
		 mr=atof(mar);

		apr=strtok(NULL," ");
		 a=atof(apr);

		may=strtok(NULL," ");
		 my=atof(may);

		jun=strtok(NULL," ");
		 jn=atof(jun);

		jul=strtok(NULL," ");
		 jl=atof(jul);

		aug=strtok(NULL," ");
		 ag=atof(aug);

		sep=strtok(NULL," ");
		 s=atof(sep);

		oct=strtok(NULL," ");
		 o=atof(oct);

		nov=strtok(NULL," ");
		 n=atof(nov);

		dec=strtok(NULL," ");
		 d=atof(dec);


	     fprintf(fo,"%.04f  %.04f  %.04f  %.04f  %.04f  %.04f  %.04f  %.04f  %.04f  %.04f  %.04f  %.04f  %.04f  %.04f\n",v,l,j,f,mr,a,my,jn,jl,ag,s,o,n,d);
	

            }  

        }
    } 
  fclose(fp);
fclose(fo);
  return (0);
}
Pages: 12