Click here to Skip to main content
15,077,641 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm new to c programming so I'm not really familiar with it as much, please help

The purpose of the code is to see and count the number of occurrences of repeated digits; the task was to modify our code so the user can input as many numbers as they can and when they input 0 or less the program gets terminated.

I have tried making one which worked fine so far till I noticed the inputs getting added up each time. How to solve this?

What I have tried:

C
#include <stdio.h>

int main(void)
{
    int count[10] = {0};
    long digit;
    int num1;
    bool valid = true;

    while (valid == true)
    {
        printf("\nInput any digits from 1-10; input 0 to stop: ");
        scanf("%d", &digit);

        if (digit <= 0) {
            valid = false;
            break;
        }

        while (digit > 0) {
            num1 = digit % 10;
            count[num1]++;
            digit /= 10;
        }

        printf("Digits:   \t");
        for (digit = 0; digit < 10; digit++) {
            printf(" %d ", digit);
        }

        printf("\n Occurrences:");
        for (digit = 0; digit < 10; digit++) {
            if (count[digit > 0]) {
                printf(" %d ", count[digit]);
            } else {
                printf("  ");
            }
        }
    }
    return 0;
}


output:
Input any digits from 1-10; input 0 to stop:1233222
 Digits:     0  1  2  3  4  5  6  7  8  9
 Occurrences:   1  4  2  0  0  0  0  0  0
Input any digits from 1-10; input 0 to stop:12345
 Digits:     0  1  2  3  4  5  6  7  8  9
 Occurrences:   2  5  3  1  1  0  0  0  0
Input any digits from 1-10; input 0 to stop:
Posted
Updated 28-Feb-21 20:19pm
v4

You can reset the counters at the start of every pass of the loop.
   
Comments
merano99 28-Feb-21 17:55pm
   
"the task was to modify our code so the user can input as many numbers as they can"

Not shure, if scanf("%d", &digit) can realy do this. What happens if you type more than a long can handle?

There are some more errors in the code. e.g. count[digit > 0] does strange things.

I tried "2223334445566677888999222222222441345436257676776573" in VS and the result was -1. The code does not work for digits that are to much.
Better use a loop, read chars and count them.
Rick York 28-Feb-21 18:15pm
   
Why does that have to be a numeric value? You could accept the input has just a string of digits and do all of your processing of the input as characters. That would allow you to accept a 'number' that is as big a string as you want to accept. I see no reason that has to be an actual number - numeric characters should work just fine.
You have written nice code, but now you have to start the debugging process of your code. Read this Debugger tutorial to learn some information how to do it.

tips:
- write some test code, so you start with a clear scenario
- make some output and remove it later
- make output, when some error case may occur

good luck, you are on the right track ;-)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900