Hello, Im looking for help writing the code for the following C Progamming assig
ID: 641906 • Letter: H
Question
Hello, Im looking for help writing the code for the following C Progamming assignment. Thank You!
1. In this problem, you are asked to implement a program that checks whether any of the digits in a number
appear more than once. In addition, you need to print out the repeated digits with the number of appearances
also. The order of numbers in the output does not matter.
INPUT: One integer
OUTPUT: The repeated digits and their appearances.
For example,
Input: 22652611
Output: The repeated digits are:
Number Appearances
2 3
6 2
1 2
2. This problem asks for an extension of problem 1. In this case, the input integer can be extremely large,
for example 332203066899111000430 or 55989784651631231548974894512132021085, which can not
be held by an integer type or long integer type. The input and output format remains the same as in problem
1. There is one more constraint that you should be able to validate the input. If the input is not a number,
you should generate some message stating it.
For example,
Input: 2few265gg2611
Output: Please enter a number only.
Another example,
Input: 55989784651631231548974894512132021085
Output: The repeated digits are:
Number Appearances
0 2
1 6
2 4
3 3
4 4
5 6
6 2
7 2
8 5
9 4
Explanation / Answer
Program code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//function prototypes
void checkRepeatDigits(int[], int[], int);
void printRepeatDigits(int[], int);
//defintion of the main function
int main(void)
{
//declare two arrays.
int a[20], b[20];
//declare other necessary arrays
int n, t;
int count = 0, remainder;
int x;
//prompt the user to enter a number
printf("Enter a number: ");
scanf("%d", &x);
//divide the given number into the digits
while (x)
{
remainder = x % 10;
a[count] = remainder;
x /= 10;
count++;
}
//declare another array to store the values of the digits repeated
int c[count];
//pass the value of count into tempraray variable t;
t = count;
//Since, the values of array a are stored int the reverse order so put the
//values array into array b so that the values are in the order of number
// given by the user
for (n = 0; n < count; n++)
{
b[n] = a[t - 1];
c[n] = 0;
t--;
}
//call the checkRepeatDigits function to count the repeated digits
checkRepeatDigits(b, c, count);
//call the printRepeatDigits function to print the value which are repeated more than once
printRepeatDigits(c, count);
return 0;
}
//defintion of checkRepeatDigits contains the logic to count the
//repeated digits
void checkRepeatDigits(int b[], int c[], int count)
{
int v;
int n;
for (n = 0; n < count; n++)
{
if (b[n] == 0)
c[0] = c[0] + 1;
else if (b[n] == 1)
c[1] = c[1] + 1;
else if (b[n] == 2)
c[2] = c[2] + 1;
else if (b[n] == 3)
c[3] = c[3] + 1;
else if (b[n] == 4)
c[4] = c[4] + 1;
else if (b[n] == 5)
c[5] = c[5] + 1;
else if (b[n] == 6)
c[6] = c[6] + 1;
else if (b[n] == 7)
c[7] = c[7] + 1;
else if (b[n] == 7)
c[8] = c[8] + 1;
else if (b[n] == 9)
c[9] = c[9] + 1;
else
v = 0;
}
}
//defintion of printRepeatDigits contains the logic to print
//only the count value of digits which are repeated more than once.
void printRepeatDigits(int c[], int count)
{
int n;
for (n = 0; n <count; n++)
{
if (c[n] > 1)
{
printf("%d %d ", n, c[n]);
}
}
}
void sortOrder(int c[],int count)
{
int i,j;
int temp;
for(i=0;i<count;i++)
{
for(j=0;j<count-i;j++)
{
if(c[j]<c[j+1])
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
for (i = 0; i < count; i++)
printf("digit %d ", c[i]);
}
Sample output:
Enter a number: 22652611
1 2
2 3
6 2
Press any key to continue...
-------------------------------------------------------------------------------------------------------------------------
2)
Program code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define yes 1
#define no 0
//function prototypes
void checkRepeatDigits(int[], int[], int);
void convertStringToDigit(char [], int[], int);
void printRepeatDigits(int[], int);
int checkforDigit(int [], int);
//defintion of the main function
int main(void)
{
//declare two arrays.
int a[256];
//declare other necessary arrays
int n, t;
int count = 10;
char string[256];
//prompt the user to enter a number
printf("Enter a number: ");
fgets ( string, 256, stdin );
int i;
n=strlen(string);
convertStringToDigit(string, a, n);
int f;
f=checkforDigit(a, n);
if(f==0)
{
printf(" Please enter a number only");
fgets ( string, 256, stdin );
}
convertStringToDigit(string, a, n);
//declare another array to store the values of the digits repeated
int c[10];
for(i=0;i<count;i++)
c[i]=0;
//pass the value of count into tempraray variable t;
t = n;
//call the checkRepeatDigits function to count the repeated digits
checkRepeatDigits(a, c, n);
//call the printRepeatDigits function to print the value which are repeated more than once
printRepeatDigits(c, count);
return 0;
}
//defintion of checkRepeatDigits contains the logic to count the
//repeated digits
void checkRepeatDigits(int b[], int c[], int count)
{
int v;
int n;
for (n = 0; n < count; n++)
{
if (b[n] == 0)
c[0] = c[0] + 1;
else if (b[n] == 1)
c[1] = c[1] + 1;
else if (b[n] == 2)
c[2] = c[2] + 1;
else if (b[n] == 3)
c[3] = c[3] + 1;
else if (b[n] == 4)
c[4] = c[4] + 1;
else if (b[n] == 5)
c[5] = c[5] + 1;
else if (b[n] == 6)
c[6] = c[6] + 1;
else if (b[n] == 7)
c[7] = c[7] + 1;
else if (b[n] == 7)
c[8] = c[8] + 1;
else if (b[n] == 9)
c[9] = c[9] + 1;
else
v = 0;
}
}
//defintion of printRepeatDigits contains the logic to print
//only the count value of digits which are repeated more than once.
void printRepeatDigits(int c[], int count)
{
int n;
for (n = 0; n <count; n++)
{
printf("%d %d ", n, c[n]);
}
}
void convertStringToDigit(char s[], int a[], int n)
{
int i;
for(i=0;i<n;i++)
{
a[i]=s[i]-'0';
}
}
//checkforDigit wil check for the digit value;
int checkforDigit(int string[], int n)
{
int i=0;
int flag=0;
for(i=0;i<n;i++)
{
if(string[i]>=0 && string[i]<=9)
flag = 1;
}
return flag;
}
void sortOrder(int c[],int count)
{
int i,j;
int temp;
for(i=0;i<count;i++)
{
for(j=0;j<count-i;j++)
{
if(c[j]<c[j+1])
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
for (i = 0; i < count; i++)
printf("digit %d ", c[i]);
}
Sample output:
Enter a number: 55989784651631231548974894512132021085
0 2
1 6
2 4
3 3
4 4
5 6
6 2
7 2
8 0
9 4
Press any key to continue...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.