lots of questions about this file

Hi, I'm a beginner at c++, and really stupid at it. I have so many questions about this file, tried my best to google answers but still really can't figure them out by myself. Please, if you can help me!
The following code kinda works, and it produce a '.txt' file. However, there are several numbers in '.txt' being '1.#INF00 ', while all the other numbers are fine. Also, if I change fprintf(fout,"%f\n",T[i-1]); into fprintf(fout,"%d\n",T[i-1]); in the end, the code is not working properly. And if I change data type 'double' into 'long double', the code is not working properly either.
Another trivial thing is, I want to use
1
2
3
r = RAND_MAX;
rand1 = rand()/r;
rand2 = rand()/r;

instead of my quite complicated
1
2
3
r = 33000;rr=33000.0;
rand1 = rand()%r; r1=rand1/rr;
rand2 = rand()%r; r2=rand2/rr;

but it doesen't work :(
At the beginning I was trying to put my 19 pairs of data into an input file and then read it in this file, but I failed, dunno what to do. T-T
So that's my code:

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctime>
int main (int argc, const char* argv[]) {
	int i,j,k; double T[19];
    FILE *fout;
	fout = fopen("doublefinal.txt","w");
	int t[19], e[19], max[19], min[19], m[19], sd[19], p,q;
    t[1]=1; e[1]=0;
    t[2]=1; e[2]=800;
    t[3]=1; e[3]=0;
    t[4]=1; e[4]=350;
    t[5]=1; e[5]=350;
    t[6]=1; e[6]=364;
    t[7]=1; e[7]=50;
    t[8]=1; e[8]=150;
    t[9]=1; e[9]=50;
    t[10]=1; e[10]=50;
    t[11]=1; e[11]=33;
    t[12]=1; e[12]=50;
    t[13]=1; e[13]=40;
    t[14]=1; e[14]=0;
    t[15]=1; e[15]=12;
    t[16]=2; max[16]=45; min[16]=1;
    t[17]=2; max[17]=33; min[17]=1;
    t[18]=2; max[18]=30; min[18]=10;
    t[19]=3; m[19]=80; sd[19]=10;
    
	srand(time(NULL));
	for(k=1;k<=200;k++)
	{T[0]=0.0;
	for(j=1;j<=300;j++)
	{
		for(i=1;i<20;i++)
		{if(t[i]==1){T[i]=T[i-1]+e[i];}
		else if (t[i]==2){p=min[i];q=max[i]-min[i]+1;T[i]=T[i-1]+(rand()%q)+p;}
		else{
			double x, r1, r2,rr; int r, rand1, rand2;
			double pi = 3.14159265358979323846;
			r = 33000;rr=33000.0;
			rand1 = rand()%r; r1=rand1/rr;
			rand2 = rand()%r; r2=rand2/rr;
			
			x = m[i]+sd[i]*sqrt(-2*log(r1))*cos(2*pi*r2);
			
		T[i]=T[i-1]+x;}	}			T[0]=T[19];	
	}
	fprintf(fout,"%f\n",T[i-1]);	}	
	fclose(fout);
}
Last edited on
if I change 'fprintf(fout,"%f\n",T[i-1]);' into 'fprintf(fout,"%d\n",T[i-1]);' in the end, the code is not working properly.
The expression after the % tells fprintf() what paramter type it expects. If you change the expression after the % you need to change the parameter as well. eg fprintf(fout,"%f\n",T[i-1]); -> fprintf(fout,"%d\n",int(T[i-1]));
See:

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

several numbers in '.txt' being '1.#INF00'
That happens in case of an illegal math operation. Such as devision by zero. For instance: rand1 may be zero and hence r1.

Further more arrays start with 0. That means for t that the first element is t[0] and the last t[18]. Accessing t[19] is illegal

Please use code tags: [code]Your code[/code]
See:

http://www.cplusplus.com/articles/z13hAqkS/
Thanks sooo much! Thats brilliant! I've changed my post by using code tags now.
But did u mean my defining int t[19] makes t[19] illegal cos the array start with 0? I think thats fine cos I just changed 19 into 20 and it gives the same result. Anyway there's no more 1.#INF00 now. :) :)
So do you know why is data type long double not working?
And how to put data in another file then read it in this file so that I don't need to have any data in this file?
Last edited on
I think thats fine cos I just changed 19 into 20 and it gives the same result.
What's the point in using the false 1 rather than the correct 0? You have still some out of bounds issues in your code...

if you want to improve this:
1
2
rand1 = rand()%r; r1=rand1/rr;
rand2 = rand()%r; r2=rand2/rr;


you can write:
1
2
r1 = double(rand()%r) / r;
r2 = double(rand()%r) / r;


And how to put data in another file then read it in this file so that I don't need to have any data in this file?
What?

EDIT:
So do you know why is data type long double not working?

Read this
The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G).
in http://www.cplusplus.com/reference/clibrary/cstdio/printf/
(I guess you mean the fprintf problem)
Last edited on
Hi bluebell09,

just wondering why you need a long double?

Ordinary double give at least 16 significant figures of precision - why does your app need more than that?

I think you should avoid putting 2 statements on 1 line - it's harder to read.

You have a lot of array's have you initialised (set values) all of them, before the values are used?

The math lib has a defined constants in it, M_PI is pi. You have included the math library so you can just use M_PI whever you need it. There are other math constants as well.

x = m[i]+sd[i]*sqrt(-2*log(r1))*cos(2*pi*r2);

This code is a mixture of int and double variables, the compiler does the right thing, but it can be a problem, especially here where the ints are undefined, so they will default to 0. It also asks the question whether they should be doubles to start with. I personally always do an explicit cast with (double) if I really need to change an int to a double.

I personally, really dislike single letters for variable names. The problem is Kernighan & Ritchie, who invented the C language, did it all the time.

It's not so bad if you declare each variable on it's own line and comment it with a description - this is good practice. However it is better to give variable a name that is a meaningful word or words joined together, so anyone reading the code will understand straight away.

You have a T[] array and a t[] array - isn't that confusing.

Here's an example of what can happen:

Someone was doing code with a 2d array using i and j for the subscripts, and couldn't see the problem. I suggested doing a find replace changing i to Row and j to Col. She saw the problem straight away- there was a [Row][Row] when it should have been [Row][Col].

Hope all this helps you to be a better programmer.

Thanks for helping me :)
I want to store the 19 pairs of data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
t[1]=1; e[1]=0;
    t[2]=1; e[2]=800;
    t[3]=1; e[3]=0;
    t[4]=1; e[4]=350;
    t[5]=1; e[5]=350;
    t[6]=1; e[6]=364;
    t[7]=1; e[7]=50;
    t[8]=1; e[8]=150;
    t[9]=1; e[9]=50;
    t[10]=1; e[10]=50;
    t[11]=1; e[11]=33;
    t[12]=1; e[12]=50;
    t[13]=1; e[13]=40;
    t[14]=1; e[14]=0;
    t[15]=1; e[15]=12;
    t[16]=2; max[16]=45; min[16]=1;
    t[17]=2; max[17]=33; min[17]=1;
    t[18]=2; max[18]=30; min[18]=10;
    t[19]=3; m[19]=80; sd[19]=10;

in a file, say, a.cpp, then read a.cpp in my original file. But I don't know how a.cpp should be written like, nor do I know how to read data from a.cpp
Last edited on
what I want to do is kinda similar to what this guy was doing
http://www.cplusplus.com/forum/beginner/72241/
but in the end of that post it was not easy for me to understand. Could any of you please explain it a bit more detail for me?
in a file, say, a.cpp, then read a.cpp in my original file. But I don't know how a.cpp should be written like, nor do I know how to read data from a.cpp


You can either write your code to prompt for information, then store it in an array, or you can read it from text file.

You don't read your info from a .cpp file, you have your data in say "MyData.txt" file say.

The .cpp file is your progam code.

The format of the text file is easy - separate the values with spaces. you could have the t values in the first column, and the e values in the second column. Are max, min, m, sd, values calculated from the t and e values ? - if so these could just be ordinary variables or arrays that you put values in once they are calculated. Also why does e[] only go up to 15 where as t[] goes to 19?

With variable names again: StandardVariationT is a much better name than sd, for example.

Hope this helps.
I forgot to mention about reading from files.

The C approach is to use a function called fscanf, this is really quite simple - Google it. "C++ fscanf example" the example given are often simplistic because they don't make use of the return value to do error checking. Error checking is important because, say you are expecting a number, but the file has something else. To do this


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
/* fscanf example */
#include <stdio.h>

int main ()
{
  char str [80];
  float f;
  FILE * pFile;
int FScanfReturnValue = 1;

  pFile = fopen ("myfile.txt","w+");
  fprintf (pFile, "%f %s", 3.1416, "PI");
  rewind (pFile);

  FScanfReturnValue = fscanf (pFile, "%f", &f);
      if (FScanfReturnValue == 0) {
                 printf( "Problem reading float from file \n");  //printf() does the same thing as cout
                  fclose (pFile);
      }
 }
  fscanf (pFile, "%s", str);
  fclose (pFile);
  printf ("I have read: %f and %s \n",f,str);
  return 0;
}


It would be better to have a function float ReadFloat() so you don't have to repeat the code above every time you read something from the file.

The C++ approach to to use filestreams, which also fairly simple. Google this also. "C++ filestreams example"
Also, can you post the actual question for the assignment?

I think you would benefit from doing a basic design. I do it all the time.

You start with a blank cpp file that has main() in it.
Write a bunch of comments that describe how your going to do your program. Describe things like, individual actions that you need to do, with a view to what functions you are going to have. Here is an example for a basic calculator that i have been helping with:

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

//include files

//function declarations

int main() {
//variable declarations

     //while (not quit){ 
         //Getnumber
         //GetOperator
         //Getnumber
         //CalcAnswer
     }  //end of while loop

} //end of main

//function definitions

//Getnumber function

//GetOperator function

//CalcAnswer function 


So you can see here the thought process is logically setout.

What you do next is to go through and decide what the functions need as arguments and what they are going to return. Also what variables do you need of what type? In your case, what arrays do you need, of what size? Some of the variables might be local to the functions, instead of putting them all in main.

Remember what I said about variable names, this is a key thing for understanding

So then you get to start putting in code, leave the comments in, because they serve as documentation.

Now you can see how there is a logical & organised process to develop your code.

It sounds boring, but it can save you writing 200 lines of code, that becomes an unintelligible mess, when you might have had 50 lines of code that is really easy for anyone to understand.

Again I am doing this so you can become a better programmer.

Cheers

Thanks a lot for answering so patiently! ( Went out for 4 days so couldn't reply in time.)
My question about the assignment is:
After I store the data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1 0
1 800;
1 0;
1 350
1 350
1 364
1 50
1 150
1 50
1 50
1 33
1 50
1 40
1 0
1 12
2 45 1
2 33 1
2 30 10
3 80 10

in a file, say 'data.txt', I want to read this file and assign them into arrays a[i][j], so that I can use them. For example a[4][2] will equal to 350. But I don't know how to do that.
Last edited on
I finally managed to do it. Thanks a lot still!
Topic archived. No new replies allowed.