Thread: Arrays and Functions

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Arrays and Functions

    Code:

    #include <stdio.h>
    main ()
    {
    int unitssold[10];
    int empindex;
    float basal[] = {158.00, 147.75, 315.00, 162.25, 220.00, 181.60, 376.90, 168.70, 293.00, 214.30}


    printf( "Please enter the Units Sold for Each of the 10 Sales Persons\n" );
    for ( empindex = 0 ; empindex < 10 ; empindex ++ )
    {
    scanf( "%d", &unitssold[empindex] );
    }

    End of Code (For Now )

    Hey guys im kinda new to arrays and functions i need this program to calculate wages based on Commission ie, the more certain emplyees sell the more money they make, i have to use a certain commission rate as follows,
    Number of Units sold Commission Rate
    1 - 5 INclusive 3.80
    6 - 10 Inclusive 5.40
    11 - 15 Inclusive 8.60
    > 15 15.20

    Im just wondering how to implement this into the program, and also if the program so far is looking ok? any advice would be appreciatede guys thanks..

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    Question, why does my compiler give me warnings with this statement?
    Code:
    float basal[] = {158.00, 147.75, 315.00, 162.25, 220.00, 181.60, 376.90, 168.70, 293.00, 214.30};
    But if I change the datatype to double it compiles without warnings.


    Code:
    warning C4305: 'initializing' : truncation from 'const double ' to 'float '
    warning C4305: 'initializing' : truncation from 'const double ' to 'float '
     warning C4305: 'initializing' : truncation from 'const double ' to 'float '
    warning C4305: 'initializing' : truncation from 'const double ' to 'float '

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    /code/
    Code:
    #include <stdio.h>
    main ()
    {
    int unitssold[10];
    int empindex;
    float total_sal[10];
    float basal[] = {158.00, 147.75, 315.00, 162.25, 220.00, 181.60, 376.90, 168.70, 293.00, 214.30};
    float commission[10];
    
    printf( "Please enter the Units Sold for Each of the 10 Sales Persons\n" );
    
    for ( empindex = 0 ; empindex < 10 ; empindex ++ )
    {
      scanf( "%d", &unitssold[empindex] );
    }
    if (unitssold[empindex] >= 1 && unitssold[empindex] <= 5)
    
    { total_sal[empindex] == unitssold[empindex]*3.80 + basal[empindex];
      commission[empindex] == unitssold[empindex]*3.80;
    }
    
    else
    
    if (unitssold[empindex] >= 6 && unitssold[empindex] <= 10)
    
    { total_sal[empindex] == unitssold[empindex]*5.40 + basal[empindex];
      commission[empindex] == unitssold[empindex]*5.40;
    }
    
    else
    
    if (unitssold[empindex] >= 11 && unitssold[empindex] <= 15)
    { total_sal[empindex] == unitssold[empindex]*8.60 + basal[empindex];
      commission[empindex] == unitssold[empindex]*8.60;
    
    }
    
    else
    { total_sal[empindex] == unitssold[empindex]*15.20 + basal[empindex];
      commission[empindex] == unitssold[empindex]*15.20;
    }
    
    printf( " Total salary for no 2 is %f and the Commission is %f" ,total_sal[2] , commission[2]);
    
    }
    /end code/

    Ok this seems ok to me but when it compiles it doesnt run properly, it gives the wrong commission and Total salary in the printf statement at the end of the program, any ideas guys? Probably did something completely stupid

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good try with the //end code though

    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Question, why does my compiler give me warnings with this statement?
    Code:
    float basal[] = {158.00, 147.75, 315.00, 162.25, 220.00, 181.60, 376.90, 168.70, 293.00, 214.30};
    >But if I change the datatype to double it compiles without warnings.
    Code:
    warning C4305: 'initializing' : truncation from 'const double ' to 'float '
    The compiler is just telling you exactly what you are doing.
    6.4.4.2 Floating constants
    • 4 An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    .........
    Join Date
    Nov 2002
    Posts
    303
    Thanks Dave

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    Code:
    #include <stdio.h>
    float commission (float amnt_sld);
    
    main()
    {
    int unitssold[10];
    int empindex;
    float total_sal[10];
    float basal[] = {158.00, 147.75, 315.00, 162.25, 220.00, 181.60, 376.90, 168.70, 293.00, 214.30};
    float commission[10];
    
    for ( empindex = 1 ; empindex < 11 ; empindex ++ )
    {
      printf( "Please enter the Units Sold for Employee No. %d\n", empindex );
      scanf( "%d", &unitssold[empindex] );
    }
    if (unitssold[empindex] >= 1 && unitssold[empindex] <= 5)
    
    { total_sal[empindex] = unitssold[empindex]*3.80 + basal[empindex];
    }
    
    else
    
    if (unitssold[empindex] >= 6 && unitssold[empindex] <= 10)
    
    { total_sal[empindex] = unitssold[empindex]*5.40 + basal[empindex];
    }
    
    else
    
    if (unitssold[empindex] >= 11 && unitssold[empindex] <= 15)
    { total_sal[empindex] = unitssold[empindex]*8.60 + basal[empindex];
    }
    
    else
    { total_sal[empindex] = unitssold[empindex]*15.20 + basal[empindex];
    }
    
    printf( "Emp No.     Commission(Euro)     Salary(Euro)     Units Sold\n");
    printf( "1                  %.2f                 %.2f               %d         \n",x, total_sal[1], unitssold[1]);
    printf( "2                  %.2f                 %.2f               %d         \n",x, total_sal[2], unitssold[2]);
    printf( "3                  %.2f                 %.2f               %d         \n",x, total_sal[3], unitssold[3]);
    printf( "4                  %.2f                 %.2f               %d         \n",x, total_sal[4], unitssold[4]);
    printf( "5                  %.2f                 %.2f               %d         \n",x, total_sal[5], unitssold[5]);
    printf( "6                  %.2f                 %.2f               %d         \n",x, total_sal[6], unitssold[6]);
    printf( "7                  %.2f                 %.2f               %d         \n",x, total_sal[7], unitssold[7]);
    printf( "8                  %.2f                 %.2f               %d         \n",x, total_sal[8], unitssold[8]);
    printf( "9                  %.2f                 %.2f               %d         \n",x, total_sal[9], unitssold[9]);
    printf( "10                 %.2f                 %.2f               %d         \n",x, total_sal[10], unitssold[10]);
    }
    float commission (float amnt_sld)
    {
    Anyone know where to go with this function? Not really sure how to use functions properly, also i still dont understand why it doesnt return anyvalues, just 0s

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM