Thread: Array Program

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    32

    Array Program

    I have to write a program that deals with arrays. I think I am doing this totally wrong. Do I need a for loop to do the increment?

    Code:
        11
        12  Write a C program (named pe4.c) will prompt
        13  the user for  10 ints, each in the range
        14  0 - 9 inclusive.
        15
        16  Compute a histogram for these 10 numbers (e.g.
        17  determine how many times each number occurred.
        18  Display the list in the format shown below.
        19  See 'pe4.exe' for the output format.
        20
        21  HINT: to solve this problem, create an int array
        22  of size 10. Initialize the array to all zeroes.
        23  When a value is entered, increment the array
        24  element whose subscript is the same as the value entered.
        25  Finally, display the resulting array.
        26  After the display of the histogram,
        27  you program should output: PROGRAM ENDS followed
        28  by a newline character.
        29
        30
        31  ASSUMPTIONS: all values entered are C ints in
        32  the range 0-9 inclusive.  You do not have to
        33  check that values are in this range.
        34
        35
        36  EXAMPLE:
        37
        38  Enter value: 9
        39  Enter value: 0
        40  Enter value: 0
        41  Enter value: 1
        42  Enter value: 1
        43  Enter value: 8
        44  Enter value: 2
        45  Enter value: 8
        46  Enter value: 8
        47  Enter value: 0
        48  Histogram: 3 2 1 0 0 0 0 0 3 1
        49  PROGRAM ENDS
        50
        51  This example shows that the user entered
        52  3 zeroes, 2 ones, 1 two, no threes, no fours,
        53  no fives, no sixes, no sevens, 3 eights, and 1
        54  nine.
    I have this and I think I went in the wrong direction:

    Code:
         7  #include <stdio.h>
         8  #define N 10
         9
        10  int main (void)
        11
        12  {
        13
        14  int a[10] = {0};
        15
        16  printf("Enter value: ");
        17  scanf("%d", &a[0]);
        18  printf("Enter value: ");
        19  scanf("%d", &a[1]);
        20  printf("Enter value: ");
        21  scanf("%d", &a[2]);
        22  printf("Enter value: ");
        23  scanf("%d", &a[3]);
        24  printf("Enter value: ");
        25  scanf("%d", &a[4]);
        26  printf("Enter value: ");
        27  scanf("%d", &a[5]);
        28  printf("Enter value: ");
        29  scanf("%d", &a[6]);
        30  printf("Enter value: ");
        31  scanf("%d", &a[7]);
        32  printf("Enter value: ");
        33  scanf("%d", &a[8]);
        34  printf("Enter value: ");
        35  scanf("%d", &a[9]);
        36
        37  printf("a[%d]");
        38  printf("PROGRAM ENDS\n");
        39
        40  return 0;
        41  }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to read about loops
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    by the way, the point of
    Code:
    #define N 10
    is so you can do this
    Code:
    int a[N] = {0};
    It's bad practice to hard code constants. Now, as the above poster said, go read about loops like you already should have...

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by vart View Post
    you need to read about loops
    Exactly, lol
    =========================================
    Everytime you segfault, you murder some part of the world

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Hi Alex. I spent quite a bit of time composing a "for loop" tutorial for you in this thread. Please go back and review.

    http://cboard.cprogramming.com/showthread.php?t=101294

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Your printf("a[&#37;d") is missing a variable in which to read the integer value to print.

    printf("a[%d", x);

    is more like it
    =========================================
    Everytime you segfault, you murder some part of the world

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM