problem with my array question

This is a discussion on problem with my array question within the C Programming forums, part of the General Programming Boards category; Hi i am back with another question .... 6.10 Use a single subscript array to sove the following problem.A company ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Question problem with my array question

    Hi i am back with another question ....

    6.10 Use a single subscript array to sove the following problem.A company pays its sales people on a comision basis.The salespeople recive $200 per week plus 9 percent of their gross sales for that week.For example, a salesperson whos grosses $3000 in sales in a week receives $200 plus 9 percent of $3000, or a total of $470.Write a c program ( ussing a counter of arrays) 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 amount).

    1.200-299
    2.300-399
    3.400-499
    4.500.599

    and so on ..

    Okay the question is.What do they mean by Using a counter of arrays and assume that each persons salary is truncated to an integer amount.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A counter array:

    int paybracket[SOME_SIZE] = {0};

    Where paybracket[0] represents the first pay slot, paybracket[1] equals the next range, and so on.

    Thus, when ted shows up, you simply find what bracket his pay goes in and increment that spot:

    paybracket[x]++;

    I could give you the formulate to find where his pay falls, but it's so easy, you should be able to figure it out.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Is this what u mean by a counter array ..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 7
    
    int main()
    {
       int face, roll, frequency[ SIZE ] = { 0 };    
    
       srand( time( NULL ) );
    
       for ( roll = 1; roll <= 6000; roll++ ) {
          face = rand() % 6 + 1;
          ++frequency[ face ];    }  
    
       printf( "%s%17s\n", "Face", "Frequency" );
    
       for ( face = 1; face <= SIZE - 1; face++ )
          printf( "%4d%17d\n", face, frequency[ face ] );
    
       return 0;
    }
    Like in this code i could have used a switch statment right, but i used an array..Is this what u call an array counter????I.f this is the method I have tried my level best and i am still stuck need some help..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    face = rand() % 6 + 1;
    ++frequency[face]; }
    Do you see your problem? If not, answer two questions:

    1) What is the valid range of face?
    2) What is the valid range of frequency's indexing?

    Arrays go from 0 to size-1. As such, you're going out of bounds of your array. Lose the "+1" and it should work fine.

    And yes, that's what I meant by a counter array. Basicly it's an array who's indexes store a count value for a range of counters.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    help quazh

    Yo Q..I was refering to the question 6.10) that i asked in the beginning in the post ..I cant do it i have tried every thing but ??I dont know ..help me out ..The problwm is that how can i say that
    ++array [ 200 <= 299 ] ????

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's an easy formulae. You just have to think about it. Very simple stuff. Analyze your pattern:

    1.200-299
    2.300-399
    3.400-499
    4.500.599
    Ok, so we start at 200. This means that our first cell must represent the first range, which is 200-299.

    To make sure the values line up right with the bucket (each bucket holding a range of 100 numbers), you should subtract 200 from the initial value.

    Now then, to ignore the last two digits of the data, how about dividing by 100? That makes sense, right?

    Say I input 303.

    303 - 200 = 103
    103 / 100 = 1

    Thus:

    array[1]++;

    See? Like I said, if you put a few minutes thought into it, the forumlae is simple.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    i did it

    I found the formula...but i guess urs is better

    hear

    Code:
    int y = (total)/100;
    ++a[y-2];
    If total is 499 it removes the 2nd last integers and now y is 4 then y-2 gives us 2 which means ++a[2]; is incremented ..how was that ..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define SIZE 9
    
    int main()
    {
     int a[SIZE]= {0};
     int  total1,total, gross, sal=200, y;
    
     printf ( "Enter you gross sales in a week:$ ");
     scanf ("%d", &gross );
     while ( gross != -1 ){
     total1 = (gross * 9) /100;
     total = total1 +sal;
      y = floor ( total ) /100;
     ++a[y-2];
     printf ("Enter your gross sales in a week:$ ");
     scanf ( "%d", &gross );
     }
    
     printf ("According to the price\n");
     printf (" 200-299: %d\n"
             " 300-399: %d\n"
             " 400-499: %d\n"
             " 500-599: %d\n"
             " 600-699: %d\n"
             " 700-799: %d\n"
             " 800-899: %d\n"
             " 900-999: %d\n"
             " 1000 and above: %d", a[0], a[1], a[2], a[3], a[4], a[5], a[6],a[7], a[8]);
    
    
          system("PAUSE");
          return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM
  4. 2d array problem
    By LiLgirL in forum Windows Programming
    Replies: 1
    Last Post: 03-15-2004, 01:23 PM
  5. Replies: 6
    Last Post: 10-21-2003, 09:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21