Thread: Is my code considered written in Modular style? :)

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    8

    Is my code considered written in Modular style? :)

    Code:
    #include <stdio.h>
    
    
    int main () {
        int a[10];
        int b[10];
        int n;
        int x;
        
        printf("Enter number :");
        scanf("%d",&n);
        
        for(x=1;x<=n;x++){
        printf("Enter number A:");
        scanf("%d",&a[x]);
    }
         
         for(x=1;x<=n;x++){
        printf("Enter number B:");
        scanf("%d",&b[x]);
    }
    
    
     for(x=1;x<=n;x++){
       if(x==n){
       printf("%d,%d",a[x],b[x]);
    }
    else{            
       printf("%d,%d,",a[x],b[x]);
    }
    }





    Alternating the input of two different arrays
    )
    YAY! THANKS For the answers!

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You could isolate the scanf() part, repeated twice, into a function that takes an array as argument.
    But this code is very short, so it doesn't make much sense to waste time trying to factorize it.
    Do you have something more specific in mind?

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Oh, thanks for the reply )
    We were given an activity which requires us to create a program which combines two arrays into one array with its elements arranged alternately.
    Example:
    N=3
    A 1,2,3
    B 4,5,6
    C=1,4,2,5,3,6

    My program works but, my teacher asked me to code it into modular style )
    I have little knowledge in using functionssss )

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your teacher means to break out some logic into separate functions. It is useful if the functions have a name that describes what it does(say read_integer_array()) rather than how it does it (say read_loop()). Your code also has some duplication of logic, and functions can be helpful in reducing that.

    Incidentally, you are aware that array indices in C start at zero? So valid indices of a 10-element array are 0 to 9, not 1 to 10.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Thanks again for the reply
    Oh! Yes, I remember that array in C start at zero
    I'll just try to read articles/posts about use of basic functions specifically in C

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    This website has great tut's on creating functions.

    Functions in C - Cprogramming.com

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    I'm done with my activity! YAY!

    Thank you for all the replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code written in Dev-C++ won't compile on linux
    By FernandoBasso in forum C++ Programming
    Replies: 14
    Last Post: 11-25-2011, 01:56 PM
  2. how would you have written this code?
    By pietrolunz in forum C Programming
    Replies: 11
    Last Post: 08-23-2011, 03:35 PM
  3. Replies: 10
    Last Post: 03-08-2010, 11:30 AM
  4. non-modular to modular program conversion
    By strife in forum C Programming
    Replies: 3
    Last Post: 09-22-2006, 03:13 AM
  5. Re-written code needs help
    By crag2804 in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:43 AM