@Mirelin,
So, you've heard a number of coding standard style issues, all related to brackets and indentation.
I'm not suggesting a style to you, but this is along the lines of what they're saying:
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
|
int main()
{ int a, b, eachRowAverage, specificRow, randomPosition;
srand(time(NULL) );
ifstream dok1;
ofstream dok2;
dok1.open("olas.dat");
dok1 >> a;
dok1 >> b;
dok1.close();
eachRowAverage = (a * 2) / b;
if (b > 0)
{ eachRowAverage = 1;
}
if (eachRowAverage == 0)
{ eachRowAverage = 1;
}
dok2.open("olas.rez");
for(int crate = 1; crate <= a; crate++)
{ cout << crate << ".crate" << endl;
dok2 << crate << ".crate" << endl;
for (int eggrow = 0; eggrow < 2; eggrow++)
{ specificRow = 0;
for (int eggs = 0; eggs < 5; eggs++)
{
if (eachRowAverage == 1)
{ randomPosition = rand() % 4 + 0;
}
else { randomPosition = 6;
}
if((specificRow < eachRowAverage) && (b > 0) )
{ if(randomPosition != 6)
{ if(randomPosition == eggs )
{ cout << " X ";
dok2 << " X ";
specificRow++;
b--;
}
else{ cout << " O ";
dok2 << " O ";
}
}
else { cout << " X ";
dok2 << " X ";
specificRow++;
b--;
}
}
else { cout << " O ";
dok2 << " O ";
}
}
cout << endl;
dok2 << endl;
}
}
dok2.close();
return 0;
}
| |
Coding standards include a number of instructions on idioms, formatting and naming conventions.
I realize it isn't the focus of attention in much early study, but perhaps you can see how more comprehensible the code is merely by having code grouped and indented by some association with the loops and/or logic (if/else).
That said, here's one thing that strike me:
I am wondering if the functions within my code can be optimized |
Since you haven't written any functions (only int main()), this is, at first, a puzzling request.
My response is group some of this code into functions.
Linus Torvalds, from whom I don't take too many recommendations (just the OS code he chairs), I paraphrase a quote of his: "If you have more than 3 indents in your code, you need to fix your code".
He may have several meanings, but a simple interpretation is that by the time you have a 3rd indent, you've probably defined something that should be a function and not indented code.
I sense that optimization may mean something to you we don't recognize. To us, optimization is about increasing speed, reducing memory requirements - that kind of thing. I think you probably aren't referencing that.
Structure is what is needed here, and I think that may be what you're describing here, not optimization as we know it. This code sends a few characters at a time to a stream, which isn't high performance by itself, so optimization is probably not a goal. This is also not likely a challenge to a modern CPU.
There are also more basic issues. What if b comes from the file as zero? That will cause a division by zero (crash) just after you close dok1.close();
You test for b>0 later, after the division by zero crash might have already happened. Think how to structure that so it makes more sense.
It seems the only reason for reading from olas.dat is to feed input to integers a & b. Perhaps this is best a concept wrapped into a function that returns a and b (and so the dok1 stream isn't inside main).
There is an outer most loop that runs through crates. Perhaps everything else inside that loop is best in a function. It is an outer loop, so that's less of a performance concern (calling a function) than code inside the most interior loop. It would, however, show us that there's a loop running through crates.
We had to deduce that by looking to see where the loop ended, way, way down the page.
You're also duplicating the output at every stream statement for cout and dok2. Every time you find a pattern repeated like that you're probably doing too much work, and should make a function that does that.
If you know how to make classes, then perhaps you need a class that does this duplicate output for everything you're streaming.
Maybe that's just a non-member function taking dok2 and your string (cout is global).
If cout duplicates are just for debugging/testing, such a method makes that optional, at will.
Replicate that thinking throughout the code and see where you land.