Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct _counter {
- int number;
- int count;
- } CountRecord;
- #define MAXNUMBERS 5
- int main()
- {
- int i, j, number;
- // declare and init data array
- CountRecord data[MAXNUMBERS];
- // Fields in the structure are initilized to zero (0)
- for (i = 0; i < MAXNUMBERS; i++)
- data[i].number = data[i].count = 0;
- // Get each number and process it
- for (i = 0; i < MAXNUMBERS; i++) {
- scanf_s("%d", &number); // Get a number
- //If the number is not already in the data array, then add it along with incrementing the count
- for (j = 0; j < MAXNUMBERS; j++) {
- if (data[j].number == 0) {
- data[j].number = number;
- data[j].count++;
- break;
- }
- else if (data[j].number == number) { // The number exists in the data array, increment the count
- data[j].count++;
- break;
- }
- }
- }
- // Print the data array to the console
- for (i = 0;i < MAXNUMBERS;i++) {
- if (data[i].number != 0)
- printf("%d:%d\n", data[i].number, data[i].count);
- else
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment