Thread: Need Help With Array and Arguements Hw

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Question Need Help With Array and Arguements Hw

    hi, well i need help with my hw, it deals with arrays and arguments but i have no idea how to do it, like im taking a online class and the book is basically useless, so i was wondering if u guys could help out a bit .

    This is the hw problem:

    Problem Description

    Each year the Department of Traffic Accidents receives accident count reports from a number of cities and towns across the country. To summarize these reports, the department provides a frequency distribution printout that gives the number of cities reporting accident counts in the following ranges:
    0 – 99
    100 – 199
    200 – 299
    300 – 399
    400 – 499
    500 or above

    The department needs a computer program to take the number of accidents for each reporting city or town and add one to the count for the appropriate accident range. After all the data have been processed, the resulting frequency counts are to be displayed.

    Problem Requirement
    a) You must create a file and save it as Program7.c file.
    b) You must use an array for the counts of the accident ranges.
    c) The results must be displayed in a tabular format.
    d) The program must have one user-defined function named updateRange with 2 arguments: accident count and the range array. The function should use the accident count to increase the count for the appropriate accident range array.
    e) The program must have another user-defined function named displayRange with the range array as the argument. The function should use a loop to display the content of the range array.
    f) Your program should run correctly with the same inputs and outputs as given in the sample run.




    This is wht i have so far :/ i know its not right

    Code:
    #include <stdio.h>
    
    main()
    {
    
    int index, a, b, c, d, e, f, city;
    printf("Enter current city\n");
    scanf("%lf", city);
    
     for(index=0; index < 99; index++)
    
            {
    
                    printf("Enter number of accidents in current city\n");
                    scanf("%lf",a[index]);
    
            }
    
            for(index=0; index < 100-199; index++)
    
            {
    
                    printf("Enter number of accidents in current city\n");
                    scanf("%lf", b[index]);
    
            }
    
            for(index=0; index < 200-299; index++)
    
            {
    
                    printf("Enter number of accidents in current city\n");
                    scanf("%lf", c[index]);
    
            }
    
     
    
            for(index=0; index < 300-399; index++)
    
            {
    
                    printf("Enter number of accidents in current city\n");
                    scanf("%lf", d[index]);
    
            }
    
            for(index=0; index < 400-499; index++)
    
            {
    
                    printf("Enter number of accidents in current city\n");
                    scanf("%lf", e[index]);
    
            }
    
     
    
            for(index=0; index < 500; index++)
    
            {
    
                    printf("Enter number of accidents in current city\n");
                    scanf("%lf", f[index]);
    
            }
    
     
    
            printf("The accident count for the current city is [index]\n");
    
                     
    
                    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm unclear what format you want for the frequency distribution printout. Could you show what you want? (use code tags so the format will be preserved). What you show doesn't look much like a "tabular format".

    Can you be more specific than "I know it isn't right"? You know that isn't REALLY too helpful, of course.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You need to define arrays. When you do scanf("%lf",a[index]) it looks like you want 'a' to be an array. You must define a[6] or whatever number of elements you need. Also, you need to have '&' (address of) scanf("%lf",&a[index]).
    Also, "%lf" is for type double, yet 'a' is int. You have to decide if you need integer or floating point.

    Syntax for for(index=0; index < 100-199; index++) is wrong. You have to figure out what you want here.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    None of a through f are arrays... you can't index them!

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    this is the code i wrote but i need to write it in array form but i dont know how, ohh the system pause cause a syntax error

    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        int temp;
        int fivehun, fourhun, threehun, twohun, onehun,zero;
        char x;
        
    
    
        fivehun      = 0;
        fourhun      = 0;
        threehun     = 0;
        twohun       = 0;
        onehun       = 0;
        zero         = 0;
            
        printf("Enter a accidents count or enter -1 to stop> \n");
    
        do
        {
            scanf("%d", &temp);
    
            if (temp == -1)
                break;
           if(temp >= 500)
                fivehun++;
                
    printf("Enter a accidents count or enter -1 to stop> \n");
            if(500 < temp && temp <= 400)
                fourhun++;
            if(400 < temp && temp <= 300)
                threehun++;
            if(300 < temp && temp <= 200)
                twohun++;
            if(200 < temp && temp <= 100)
                onehun++;
            if(100 < temp && temp <= 0)
                zero++;
                
            } while (temp != -1);
            
          printf("%d fivehun, %d fourhun and %d threehun, %d twohun %d onehun %d zero\n", fivehun, fourhun, threehun,twohun,onehun,zero);  
            
    system ("pause");  
    
    return (0);
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    if(500 < temp && temp <= 400)
                fourhun++;
    Oh C'mon! That's not even a good try -- work it out with a paper and pencil.

    Here, temp would have to be both greater than 500 and less than or equal to 400.

    Check all your if statements and see if they make sense.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    oo got wrong sign :P so is there a way to write it in array form? a example would be nice

    here its all updated it works and gives the right answer it just isnt in array form which the damn teacher didnt teach us how -.-' example plz?
    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        int temp;
        int fivehun, fourhun, threehun, twohun, onehun,zero;
        char x;
        
    
    
        fivehun      = 0;
        fourhun      = 0;
        threehun     = 0;
        twohun       = 0;
        onehun       = 0;
        zero         = 0;
            
        printf("Enter a accidents count or enter -1 to stop> ");
    
        do
        {
            scanf("%d", &temp);
    
            if (temp == -1)
                break;
           if(temp >= 500)
                fivehun++;
                
    printf("Enter a accidents count or enter -1 to stop> ");
            if(500 > temp && temp >= 400)
                fourhun++;
            if(400 > temp && temp >= 300)
                threehun++;
            if(300 > temp && temp >= 200)
                twohun++;
            if(200 > temp && temp >= 100)
                onehun++;
            if(100 > temp && temp >= 0)
                zero++;
                
            } while (temp != -1);
            
          printf("    Range       Frequency            \n" ) ; 
          printf("    0-99                %d\n ", zero);
          printf("   100-199             %d\n ", onehun);  
          printf("   200-299             %d\n ", twohun); 
          printf("   300-399             %d\n ", threehun); 
          printf("   400-499             %d\n ", fourhun); 
          printf("   500 or above        %d\n ", fivehun); 
    
    system ("pause"); 
    
    return (0);
    
    }
    Last edited by 123456tacos; 04-01-2011 at 08:04 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For a summary, I'd use something like:

    int data[SIZE];

    Where SIZE is a #defined macro in your program, before int main(void).

    Then the data points that would go into zero, would be recorded as:

    data[0]++;

    And those for one hundred would be logged in as

    data[1]++;

    data[4]++ would count up what group of hundreds, then?

    Since you want to sum up 0 through 5, you need int data[SIZE], where SIZE is defined as 6.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    ohh ok i think i got it thanks

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with "shrinking" array
    By believe in forum C Programming
    Replies: 2
    Last Post: 05-11-2007, 12:06 PM
  2. having trouble with searching an array..
    By qubit673 in forum C Programming
    Replies: 9
    Last Post: 01-21-2007, 07:14 PM
  3. Replies: 1
    Last Post: 12-07-2005, 10:23 PM
  4. make array with command line help
    By Brokn in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 05:10 PM
  5. filling an array from a file using fscanf...
    By stodd04 in forum C Programming
    Replies: 2
    Last Post: 03-16-2005, 08:55 AM

Tags for this Thread