Thread: Can someone take a look at my code? please

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Talking Can someone take a look at my code? please

    Use a one-dimensional array to solve the following problem. A company pays its salespeople on a commission basis. The salespeople receive $200 perweek pluse 9 percent of their gross sales for that week. For example, a salesperson that grosses $3000 in sales in a week receives $200 plus 9 percent of $3000, or a total of $470. Write a C program (using an array of counters)that determines how many of the salespeople earned salaries in each of the following ranges(assume that each salesperson's salary is truncated to an integer amonut).

    I'm not having people to do my homework, I try to write it myself, the program works, the only problem I have is that I should only need array[8] not [9], but for some reason the -1 that user enter was count it to array number[8], so if I only have one salesperson that have make over $1000, my array counter [8] will print 2 instead of 1. So I set the array to [9] to take that -1, but didn't output to the screen.

    Can anyone tell me what did I do wrong in my code?

    thank you........

    int main(void)
    {
    int array[9];
    int sale = 0;
    float comm;
    int i;

    for(i=0; i<9;i++)
    {
    array[i]=0;
    }
    while(sale != -1)
    {
    printf("\nPlease enter employee gross sales enter -1 to quit: ");
    scanf("%d", &sale);
    comm =( sale*9/100) + 200;
    if(comm >200 && sale!=-1)
    {
    printf("Employee Commission is $%.2f\n", comm);
    }

    if(comm>200 && comm <=299)
    array[0]++;
    else if(comm>=300 && comm<=399)
    array[1]++;

    else if(comm>=400 && comm<=499)
    array[2]++;

    else if(comm>=500 && comm<=599)
    array[3]++;

    else if(comm>=600 && comm<=699)
    array[4]++;

    else if(comm>=700 && comm<=700)
    array[5]++;

    else if(comm>=800 && comm<=899)
    array[6]++;

    else if(comm>=900 && comm<=999)
    array[7]++;

    else if(comm>1000)
    array[8]++;
    else
    array[9];
    }

    printf("\nNumber of employees in the range of:\n\n");
    printf("$200-299: %d\n", array[0]);
    printf("$300-399: %d\n", array[1]);
    printf("$400-499: %d\n", array[2]);
    printf("$500-599: %d\n", array[3]);
    printf("$600-699: %d\n", array[4]);
    printf("$700-799: %d\n", array[5]);
    printf("$800-899: %d\n", array[6]);
    printf("$900-999: %d\n", array[7]);
    printf("Over $1000: %d\n", array[8]);

    return;
    }

  2. #2
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    Subscript out of range

    Dear yukon,

    There is problem with your code. You are accessing array[9] element, which is not into existance. You created an array of size 9, then it's from array[0] - array[8]. The element array[9] is not at all into existance. Remove array[9] from the code alongwith the else part. Your rest of al code works fine.

    would luv to solve your queries.

    Regards,
    Mangesh.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Be very careful of overflowing the bounds of an array. Whatever was in the next sizeof(array) bytes of memory now has been overwritten. When/if this memory is needed it could crash the system / exe.
    Sometimes I will allocate an extra element to an array 'justin case'.

    As to your code,

    You could try a DO WHILE loop as this will always execute once and test the sale==-1 at the end. You must 'prime' the loop properly. (get the imput before you enter the loop for the first time. Then again at the end.

    Another way is to stop as soon as you get the -1.

    printf("\nPlease enter employee gross sales enter -1 to quit: ");
    scanf("%d", &sale);
    if(sale==-1) continue;//will go to test the while condition

    or more simply (I needed to kill a few mins at work, so you got the LONG version, sorry)

    printf("\nPlease enter employee gross sales enter -1 to quit: ");
    scanf("%d", &sale);
    comm =( sale*9/100) + 200;
    if(comm>200 && sale!=-1)
    {
    //rest of code not just the printf
    }
    //now the final printout

    (add to the array a string holding the range("$200-299:" ect) and use a for loop to print the final amounts to the screen)
    for(i=0;i<9;i++)
    {
    printf("%s %d\n",array[i].sTitle,array[i].iSales);
    }

    Just to be picky,
    (The comm>200 is redundant as only if sale is 0 or -ve CAN comm be less than 200. You have not allowed for the sale of $0-$199 (bad salesman))

    Is this a typo?
    else if(comm>=700 && comm<=700)
    (The second logical operator should be comm<=799)

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Smile thanks

    Hey guys,

    Thank you so much for your help,
    I'm at work right now, but, I will try to
    correct my code tonight and see how it run.
    Since this is my first C programing class, takes
    me a while to understand the coding.

    Once again, thank you thank you...........

    p.s. novacain, yes that's a typo

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    1
    the problem is with your calculation express !!!!

    u r dividing 9/100 actuall when u r converting result of this statement to float u should always use floats
    ie 9.0/100 which will give u the result in floating points !!!!!
    just make this change and your prg will run smoothly !!!!!

    bye anymore quries send it to [email protected]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM