Syntax Errors

I'm a beginner in programming and I'm unsure on how to neatly program code. Can someone check for indentation errors and redundancies in the 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
53
54
55
56
57
58
59
  double random(unsigned long int & seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double (seed)/MODULUS;
    
}
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    const int SIZE = 10000;
    unsigned long int seed;
    srand(time(0));
    double x,y,z,f1,f2,f3,new_t;
    double old_t=100;
  
    seed = rand();
    f1= random(seed);
    f2= random(seed);
    f3= random(seed);
    
    x= 15*f1/(f1+f2+f3);
    y= 15*f2/(f1+f2+f3);
    z= 15*f3/(f1+f2+f3);
    old_t= sqrt(8*8 + x*x)/3 + y/5 + sqrt(6*6 + z*z)/4;
    
    for (int i=0; i<SIZE; i++)
{
    f1= random(seed);
    f2= random(seed);
    f3= random(seed);
    
    x= 15*f1/(f1+f2+f3);
    y= 15*f2/(f1+f2+f3);
    z= 15*f3/(f1+f2+f3);
    new_t= sqrt(8*8 + x*x)/3 + y/5 + sqrt(6*6 + z*z)/4;
    
    if(new_t < old_t)
{
    cout << setprecision(2);
    cout << "x = " <<  setw(5) << x << endl;
    cout << "y = " <<  setw(5) << y  << endl;
    cout << "z = " <<  setw(5) << z  << endl;
    cout << "t = " <<  setw(5) << old_t << endl;
    cout << endl;
    old_t=new_t;
}
}
    return 0;
}
there are no such thing as indention errors, indention is just for the human.
that said, lines 55+ contain braces that are not indented, its not clear what they belong to. 36 should be pushed in, 47 as well, 55 and 56 are also in to match.


put # includes at the top unless you have a good reason not to (and, there are almost no good reasons for it in most code).

using namespace std is frowned upon. avoid this and put std:: in front of everything as a habit.

may be worth making f1+f2+f3 a single variable. The compiler should catch this, but its redundant. Also, that for loop maybe could be a do-while to avoid the repeated block. Or maybe run the for loop an extra time. There should be a way to avoid repeating all that, but I didn't dig into that just noted in passing.
Last edited on
Hello cash,

Since you are new to programming this may help.

As jonnin says:
there are no such thing as indention errors
Technically speaking to the compiler the code could be one very long line and it would still compile, because the compiler does not care about white space or blank lines. We write code with blank lines and indentation to make it easier for us to read.

Functions are generally put between the "#includes" and "int main()". Or you can put a prototype here and put the function after the closing brace of "main" or even in a different file. In this case the placement of the function may work without any problem because there is nothing in the function requires any header files, but do not get use to this.

As jonnin said:
using namespace std is frowned upon
This may seem easy right not having to write things like: "std::cout", "std::cin" and "std::endl" in your program, but someday it WILL get you in trouble when the compiler puts "std::" in front of a function that you have written and it turns out it is the same name as a function in the standard name space. The parameters of the function may or may not cause an error at compile time. In the end you make changes to the function that you wrote only to find out that the compiler is not using your function, but the one in the standard name space.

In "main" I usually write the "srand" as srand(static_cast<size_t>(time(nullptr))); recently I learned that "nullptr" is the better choice for "time", but you could also use zero. When you look at the function for "srand()" it is defined as (void srand (unsigned int seed);). "size_t" usually is a typedef for an "unsigned int", but I have see a setup where "size_t" was typefeded as a "long". If you are not sure what "size_t" is typedefed as just use "unsigned int". It is better to give the function what it requires rather than to have a conflict with different types. Most times this just produces a warning and does not affect how the program runs.

Lines 26 - 33 are the same as lines 37 - 44 with the only difference being the variable the gets the result. A function could be made that returns the result to the two different variables.

In lines 26 - 28 and again in lines 37 - 39 you are passing "seed" by reference. I am not sure if you are aware of this, but every time you call the function "random" you change the value of seed in the function and this is reflected in "main".

For line 48 you may find this useful: std::cout << std::fixed << std::showpoint << std::setprecision(2);. The first choice is "fixed" or "scientific". The "showpoint" will display ".00" if it occurs.

Here is your code rearranged with a more proper indenting:
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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

double random(unsigned long int & seed)
{
	const int MODULUS = 15749;
	const int MULTIPLIER = 69069;
	const int INCREMENT = 1;
	seed = ((MULTIPLIER*seed) + INCREMENT) % MODULUS;
	return double(seed) / MODULUS;
}

int main()
{
	const int SIZE = 10000;
	unsigned long int seed;
	srand(static_cast<size_t>(time(nullptr)));
	srand(time(0));
	double x, y, z, f1, f2, f3, new_t;
	double old_t = 100;

	seed = rand();
	f1 = random(seed);
	f2 = random(seed);
	f3 = random(seed);

	x = 15 * f1 / (f1 + f2 + f3);
	y = 15 * f2 / (f1 + f2 + f3);
	z = 15 * f3 / (f1 + f2 + f3);
	old_t = sqrt(8 * 8 + x * x) / 3 + y / 5 + sqrt(6 * 6 + z * z) / 4;

	for (int i = 0; i < SIZE; i++)
	{
		f1 = random(seed);
		f2 = random(seed);
		f3 = random(seed);

		x = 15 * f1 / (f1 + f2 + f3);
		y = 15 * f2 / (f1 + f2 + f3);
		z = 15 * f3 / (f1 + f2 + f3);
		new_t = sqrt(8 * 8 + x * x) / 3 + y / 5 + sqrt(6 * 6 + z * z) / 4;

		if (new_t < old_t)
		{
			std::cout << std::fixed << std::showpoint << std::setprecision(2);
			//cout << setprecision(2);
			cout << "x = " << setw(5) << x << endl;
			cout << "y = " << setw(5) << y << endl;
			cout << "z = " << setw(5) << z << endl;
			cout << "t = " << setw(5) << old_t << endl;
			cout << endl;
			old_t = new_t;
		}
	}

	return 0;
}

Keep in mind that the code tags sometimes will exaggerate the indentation, but when copied and put in an IDE it will look more normal.

Hope that helps,

Andy
Topic archived. No new replies allowed.