Jump to content

Using "C", Need help counting number of integers input.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Dusty43

Dusty43

    Newbie

  • Members
  • Pip
  • 1 posts
Hey guys and girls,
I'm having a bit of an issue with my my program - i would like it to count the number of times a certain integer (between 0-9) has been input by the user. I'm using mainly arrays/loops to do so, however it feels like im just going round in circles ... i know im missing something vital here. Anyway, my code looks like this at the moment:

#include <stdio.h>


int main(int argc, char *argv[]) {

  int arr[10] = {0};

  int i=0;

  int x=0;

  

  printf("Enter ten integers:\n");

  

  for (i=0; i<10; i++) {

    x = scanf("%d", &i);

  }

  

  for (i=0; i<10; i++) {

    printf("%d seen %d times\n", i, arr[x]);

  }

  

  return 0;

}


An example of output:

Enter ten integers:

1 2 3 4 5 0 0 9 8 7

0 seen 0 times

1 seen 0 times

2 seen 0 times

3 seen 0 times

4 seen 0 times

5 seen 0 times

6 seen 0 times

7 seen 0 times

8 seen 0 times

9 seen 0 times


Any help is well appreciated,
Thanks,
John =)

#2
redkid

redkid

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Well, you are right, you were missing something vital. You're not reading the values from the user into the array! Also your counting algorithm is wrong.

I suggest you take a break and go back to it. For a head start...you should read the integers like this:
for (i=0; i<10; i++) {
    scanf("%d", &arr[i]);
}

Makes sense I hope.
Also, as I said, your counting is incorrect, work on it.