Exam Question: Write a program to print an empty square with side 10 units using *.
Standard Solution (from the manual) -
- int main()
- {
- for(int a=1;a<=10;a++)
- {
- cout<<"*";
- }
- cout<<endl;
- for(int b=1;b<=8;b++)
- {
- for(int c=1;c<=1;c++)
- {
- cout<<"*";
- }
- for(int d=1;d<=8;d++)
- {
- cout<<" ";
- }
- cout<<"*"<<endl;
- }
- for(int e=1;e<=10;e++)
- {
- cout<<"*";
- }
- return 0;
- }
My solution-
- int main(){
- for(int x=0;x<10;x++){
- for(int y=0;y<10;y++)
- if(y==0||y==9||x==0||x==9
)printf("*"); - else printf(" ");
- printf("\n");
- }
- }
Result - 3/20.
Conversation with my prof-
Me: Sir why did you give me such low marks.
Prof: Your code is wrong.
Me: Sir, how could it be wrong? It prints the exact output required!!!
Prof: But your code is only 6 lines long. The correct code is about 25 lines. Refer to the manual.
Me: But sir, what has the length of code got to do with its correctness? My code prints the exact output required.
Prof: Do you know how long I have been teaching here? 25 years. Do you think you are the first student trying to trick me? The correct code has 5 for loops. Your code has only 2. Don't try to act over smart with me . Now get out before I deduct 2 more marks from your total.