Sunday, December 4, 2016

How are students in Indian colleges taught programming?

Exam Question: Write a program to print an empty square with side 10 units using *.

Standard Solution (from the manual) -
  1. int main()
  2. {
  3.      for(int a=1;a<=10;a++)
  4.         {
  5.             cout<<"*";
  6.         }
  7.             cout<<endl;
  8. for(int b=1;b<=8;b++)
  9.     {
  10.     for(int c=1;c<=1;c++)
  11.         {
  12.                 cout<<"*";
  13.         }
  14.         for(int d=1;d<=8;d++)
  15.               {
  16.                 cout<<" ";
  17.               }
  18.                 cout<<"*"<<endl;
  19.        }
  20. for(int e=1;e<=10;e++)
  21. {
  22. cout<<"*";
  23. }
  24. return 0;
  25. }

My solution-
  1. int main(){
  2. for(int x=0;x<10;x++){
  3. for(int y=0;y<10;y++)
  4. if(y==0||y==9||x==0||x==9)printf("*");
  5. else printf(" ");
  6. printf("\n");
  7. }
  8. }

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.
Me: WTF!!!

*Note*: - This article has been copied from Quora.com

No comments:

Post a Comment