# Array Program

• 04-15-2008
alex1067
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  }```
• 04-15-2008
vart
• 04-15-2008
sand_man
by the way, the point of
Code:

`#define N 10`
is so you can do this
Code:

`int a[N] = {0};`
• 04-15-2008
JFonseka
Quote:

Originally Posted by vart

Exactly, lol
• 04-15-2008
Dino
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/c-programming/101294-help-writing-program.html

Todd
• 04-15-2008
JFonseka
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