Thread: Help with a functions using arrays/if statements

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    14

    Help with a functions using arrays/if statements

    Hi all, basically my assignment is to write a program to alter the salaries of n employees based on the following conditions: If the salary is less than 10,000 then give a 5% bonus etc. If its 10,000-20,000 then another set of changes to the salary are done, anything higher than 20,000 also receives its own changes.

    As you can see, the size of the arrays that contain the values for salary and new salary are dependent on integer n, which is entered by the user. This format does not work on my compiler, as the program itself stops before printing the first line. However when I change s[n] to s[100], l[n] to l[100], the function works. I want to know why I cannot use the s[n] format, instead of just using a random number for the number of salaries/altered salaries after changes. Any help would be greatly appreciated, thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int i,n;
        float s[n],l[n];
        printf("Please enter the number of employees: ");
        scanf("%d",&n);
        for (i=0;i<n;i++)
        printf("Please enter the salary: ");
        scanf("%f",&s[i]);
        if (s[i]<10,000)
        {
            l[i]=s[n]+(0.05*s[i]);
            printf("Salary after changes: %f",l[i]);
        }
        else if ((s[n]>=10,000)&&(s[n]<20,000))
        {
            l[i]=s[i]-(0.03*s[i]);
            l[i]=l[i]-(0.01*l[i]);
            l[i]=l[i]+(.025*l[i]);
            printf("Salary after changes: %f",l[i]);
        }
        else
        {
            l[i]=s[i]-(0.05*s[i]);
            l[i]=l[i]-(0.02*l[i]);
            l[i]=l[i]-(0.01*l[i]);
            l[i]=l[i]+(0.08*l[i]);
            printf("Salary after changes: %f",l[i]);
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > float s[n],l[n];
    > printf("Please enter the number of employees: ");
    > scanf("%d",&n);
    C doesn't predict the future, or revise the past.
    You need to input n BEFORE creating an array.

    > if (s[i]<10,000)
    Beware of the comma operator,
    This isn't what you think it is.
    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
    Jun 2005
    Posts
    6,815
    n is an uninitialised variable. Formally, according to the C standard, accessing its value yields undefined behaviour.

    Your usage does not create an array that will magically resize itself when n is changed. It attempts to access an indeterminate value (an operation that, in itself may fail) and then create an array who's size is that value.

    Assuming your compiler is compliant with the 1999 C standard, initialise n to a valid value (for example, by reading its vale using scanf()) BEFORE defining "float s[n], l[n];" If your compiler is compliant with an older C standard (say the 1989 standard) your usage is invalid, and will yield a compile time error, since arrays can only be created with a size known to the compiler (i.e. not based on data obtained at run time).

    Incidentally, the test "if (s[i] < 10,000)" compares s[i] with the value 10, not 10000. When programming C or C++ the comma operator is not used in constants like that. The condition will therefore always test false (s[i] < 10 is evaluated, the comma operator causes that to be discarded, and the result of the test is zero (000)).
    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.

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    dynamic allocation would be something you could use/research. ie malloc in C, new in C++. dont forget to free after!

    and when getting input from user, by warey of newlines ('\n') left in the input stream

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if statements and functions
    By trowavo in forum C Programming
    Replies: 8
    Last Post: 04-24-2011, 05:08 PM
  2. Using char arrays in branching statements
    By hmanners in forum C Programming
    Replies: 18
    Last Post: 06-08-2009, 04:03 AM
  3. arrays within switch statements
    By divinyl in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2003, 01:56 PM
  4. signing 'if statements' to 'arrays'
    By Machewy in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2003, 05:18 PM
  5. Statements and Functions
    By GeekFreak in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2002, 12:34 PM