Thread: Modules

  1. #1
    Registered User
    Join Date
    Mar 2015
    Location
    Toronto, Ontario, Canada
    Posts
    9

    Modules

    I'm a beginner and don't know how to use modules. So I have a task :
    Code:
    Write a main() program that prints a monthly sales report.  This report will list the sales for each store the company owns and the total sales.  To do this it must first call a module / function that takes in the sales for each store.  The sales should be stored in an array of the appropriate data type (assume the dollar value of sales can be really big).  Since there are only 3 stores, the array only needs to store 3 values.  This array is initialised in main(), and any data entered in other module(s) must be stored in that array.  This CANNOT be done using global scope, you must pass the array to the module(s).
    What I wrote :

    Code:
    #include <stdio.h>
    
    
    main()
    {
    
    
            float balance[3];/* the array*/
            int i;
    
    
            for (i = 0; i < 3;)
            {
                    i = i + 1;
                    printf("Enter sales amount for store #%d\n",i);
                    scanf("%f",&balance[i-1]);
    
    
            }
    
    
            printf("MONTHY SALES REPORT (BY STORE NUMBER):\n");
            printf("Store #1 $ %f\n",balance[0] );
            printf("Store #2 $ %f\n",balance[1] );
            printf("Store #3 $ %f\n",balance[2] );
    }
    What I've tried to do
    Code:
    #include <stdio.h>
    
    
    int ILikeCats(float balance[3])
    {  
          for (i = 0; i < 3;)
            {
                    i = i + 1;
                    printf("Enter sales amount for store #%d\n",i);
                    scanf("%f",&balance[i-1]);
    
    
            } 
    }
    main()
    {
    
    
            float balance[3];/* the array*/
    
    
            ILikeCats(float balance[3]);
    
    
            printf("MONTHY SALES REPORT (BY STORE NUMBER):\n");
            printf("Store #1 $ %f\n",balance[0] );
            printf("Store #2 $ %f\n",balance[1] );
            printf("Store #3 $ %f\n",balance[2] );
    }
    Could you help and explain it? Thanks in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > ILikeCats(float balance[3]);
    Just say
    ILikeCats(balance);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Location
    Toronto, Ontario, Canada
    Posts
    9
    Quote Originally Posted by Salem View Post
    > ILikeCats(float balance[3]);
    Just say
    ILikeCats(balance);
    Thanks for the speedy reply! I did as you said but it gives me the error in 21st line where the array begins.
    Code:
    array.c: In function ‘main’:
    array.c:21:11: error: expected expression before ‘float’
    The full code.
    Code:
    #include <stdio.h>
    
    
    
    
    float ILikeCats(float balance[3])
    {
           int i = 0;
            for (i = 0; i < 3;)
            {
            i = i + 1;
            printf("Enter sales amount for store #%d\n",i);
            scanf("%f",&balance[i-1]);
    
    
            }
    }
    main()
    {
    
    
    float balance[3];/* the array*/
    
    
    
    
    ILikeCats(float balance);
    
    
    
    
    printf("MONTHY SALES REPORT (BY STORE NUMBER):\n");
    printf("Store #1 $ %f\n",balance[0] );
    printf("Store #2 $ %f\n",balance[1] );
    printf("Store #3 $ %f\n",balance[2] );
    }
    ~
    For me it seems okay and it should work; however it doesnt work >.>.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    ILikeCats(float balance);
    Look very carefully at what Salem wrote. Because it wasn't this.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    In post # 3, look at line 9 - it has a semicolon - what is the reason for it?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by PaulS View Post
    In post # 3, look at line 9 - it has a semicolon - what is the reason for it?
    Are you referring to this line?

    Code:
    for (i = 0; i < 3;)
    If so, that's standard syntax for the "for()" (the "update" portion is blank, since it's done within the loop body instead ... for some reason).

  7. #7
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    OK, I see that- I guess I am not advanced enough to understand why or if it would affect the remaining code.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    All parts of the "for()" loop are technically optional.

    In fact, one of the accepted ways of writing an infinite loop is:

    Code:
    for( ; ; )
    Note that while the parts might be optional, the semi-colons are not.

  9. #9
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    I have the concept down - I guess I don't see an advantage to using it. Thanks for increasing my knowledge.

  10. #10
    Registered User
    Join Date
    Mar 2015
    Location
    Toronto, Ontario, Canada
    Posts
    9
    Quote Originally Posted by Matticus View Post
    Code:
    ILikeCats(float balance);
    Look very carefully at what Salem wrote. Because it wasn't this.
    Oh yeah! That's true;I made a mistake. Thank you. It works now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with modules
    By Jacob Boyd in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2012, 10:08 AM
  2. modules in C++
    By jamort in forum C++ Programming
    Replies: 1
    Last Post: 06-03-2009, 02:07 AM
  3. Modules
    By Hakim in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2006, 04:53 PM
  4. Modules
    By tim545666 in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:44 PM
  5. Modules
    By Garfield in forum C Programming
    Replies: 4
    Last Post: 09-30-2001, 10:23 AM