Home > Experiments > The pleasure of solving!

The pleasure of solving!

It’s been a quite long since I blogged something πŸ™‚ May be a writer’s block* (*acronym for pure laziness,here);) Well, Last Saturday, I went to dxmk9 program conducted by NSS collage of engineering at Kottamaidhanam, Palakkad. I had registered my name for ‘Geek’s Paradise’ competition which is basically a C programming contest. The first task was to answer some objective questions, mainly to predict the output, was not much tough. The second task was to print a number pattern like this:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1

whooo! the problem was not easy for a newbie like me (‘m still in school,sir πŸ˜‰ ), I simply wrote some printf() statements with some ++i,etc. and escaped! Also, I had no much time to think, I had promised to return home before 1 PM.

When I reached home, a sudden thought flashed across my mind “Why shouldn’t I give a try ?”. I booted my ubuntu system ( ‘m eagerly waiting to get Fedora in hands), took newly installed Netbeans IDE and started trying. After some time –Β  LOL πŸ™‚ !! It worked!! The program asked for an integer input and printed the pattern exactly the way it’s meant to be! I know, this is a small problem and has nothing of great speciality or something, but don’t know why – The output gave me some moments of rejoice! A small pleasure of coding πŸ™‚ Ha ha, I would like to share that code with you πŸ™‚

/*
 *      121.c
 *
 *      Copyright 2009 Ershad K <ershad92@gmail.com>
 *
 *      Licensed under GPL Version 3
 */

#include <stdio.h>

main() {

    int n;
    int i = 1;
    int j = 1;
    printf("Enter a number: ");
    scanf("%d",&n);
    
    // Increment....
    for (;i <= n; i++ )
    {
        for(j = 1; j <= i; j++)
        {
            printf(" %d",j);
        }
        --j;

        for (--j; j >=1; j--)
        {
            printf(" %d",j);
        }
        printf("\n");
    }

    // Decrement....

     for (i = n-1;i >= 1; i-- )
    {
        for(j = 1; j <= i; j++)
        {
            printf(" %d",j);
        }
        --j;

        for (--j; j >=1; j--)
        {
            printf(" %d",j);
        }
        printf("\n");
    }
    
}

Patches are always welcome :) All the best, nice day!

Advertisements
  1. Syam
    December 14, 2009 at 7:53 pm

    Just one nitpick πŸ˜‰
    Don’t write a plain main(). Always write “int main(void)” for C and “int main()” for C++. The “return-type defaults to int” rule has been deprecated by c99 (it was never valid in C++). So, even if your compiler still supports this, it’s better to explicitly specify the return type. You don’t strictly require a “return 0;” statement in main() though. The C/C++ standards say that if you don’t have a return statement in main(), it’s equivalent to having “return 0;”.

    Another design suggestion.. how about having a function printSeries(int N) that prints the sequence: 1 2 3 .. N N-1 .. 3 2 1 and then call this function to print individual lines in your required pattern?

    • December 15, 2009 at 4:24 pm

      You are right, Syam etta. I would have used ‘int main()’ instead of ‘main’, I thought, unlike from C++, C doesn’t need any return type for main .The C book that I have never used return type where the same author has used ‘int main’ in his C++ Book – this confused me :(. I will specify return type here after πŸ™‚

      Using functions is a nice idea πŸ™‚

      void printSeries(int n);

      int main() {

      int n;
      printf(“Enter a number: “);
      scanf(“%d”,&n);

      printSeries(n);
      }

      void printSeries(int n)
      {
      int i = 1;
      int j = 1;
      // Increment….
      for (;i <= n; i++ )
      {
      for(j = 1; j =1; j–)
      {
      printf(” %d”,j);
      }
      printf(“\n”);
      }

      // Decrement….

      for (i = n-1;i >= 1; i– )
      {
      for(j = 1; j =1; j–)
      {
      printf(” %d”,j);
      }
      printf(“\n”);
      }

      Thank you very much etta πŸ™‚

      • December 15, 2009 at 10:23 pm

        Errr.. I wasn’t saying that you copy everything in main() and put in a separate printSeries() function (that wouldn’t be much fun).
        I was thinking that the printSeries(N) would print just one line: 1 2 3…N N-1…3 2 1
        Now, from main(), you can call this function repeatedly for different values of N.
        Just a thought..

        • December 16, 2009 at 4:11 pm

          Oops, my mistake – I would try that etta, will post soon πŸ™‚

          • December 17, 2009 at 4:14 pm

            etta, what about this ?

            void printSeries(int n);
            int main()
            {
            int n;
            int i;
            printf(“Enter n value: “);
            scanf(“%d”,&n);
            for (i = 0; i 0; i– )
            {
            printSeries(i);
            printf(“\n”);
            }
            }

            void printSeries(int n)
            {
            int i = 1;
            for (;i 0; i– )
            {
            printf(” %d”,i);
            }
            }

            Thank you very much etta, for helping me a lot πŸ™‚

            • December 17, 2009 at 9:04 pm

              What does that code do? All the comparison operators are removed – possibly mistaken for html elements!
              Anyway, does this code print the pattern in your original post? It doesn’t look like it.

              • December 19, 2009 at 11:35 am

                Oops, many texts have been removed 😦 it’s working properly in the original code πŸ™‚

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: