Program for the frequency of a number
Please help me with this code to get clear output. I am a beginner
I have made the program using an array. I don't know whether it is correct or not. Made with my own logic
int count(int a)
{
int c;
while(a>=1)
{
c++;
a=a/10;
}
return c;
}
int main()
{
//program to find frquency of the number
int a,n,d;
int b[100];
int e[100];
scanf("%d",&a);
n=count(a);
for(int i=n;a>0;i--)
{
b[i]=a%10;
a=a/10;
}
for(int i=1;i<=n;i++)
{
d=b[i];
e[d]++;//most probably this part error occurs
printf("%d\n",d); //used this this to confirm that i have correctly stored value in d.
}
for(int i=1;i<=n;i++)
{
printf("%d ",e[i]);
}
return 0;
}
The line int c; should be int c = 0;
The line int e[100]; should be int e[100] = {0};
The following code could work:
#include <stdio.h>
int count(int a) {
int c = 0;
while (a >= 1) {
c++;
a = a / 10;
}
return c;
}
int main() {
// program to find frquency of the number
int a, n, d;
int b[100];
int e[100] = {0};
scanf("%d", &a);
n = count(a);
for (int i = n; a > 0; i--) {
b[i] = a % 10;
a = a / 10;
}
for (int i = 1; i <= n; i++) {
d = b[i];
e[d]++; // most probably this part error occurs
printf("%d\n", d); // used this this to confirm that i have correctly
// stored value in d.
}
for (int i = 1; i <= n; i++) {
printf("%d ", e[i]);
}
return 0;
}
Also, you can do it use snprintf:
#include <stdio.h>
int main() {
int a;
int max = -1;
char buf[100];
int count[10] = {0};
scanf("%d", &a);
snprintf(buf, sizeof(buf), "%d", a);
for (int i = 0; buf[i] != '\0'; ++i) {
int temp = buf[i] - '0';
++count[temp];
if (temp > max)
max = temp;
}
for (int i = 0; i <= max; ++i)
printf("%d ", count[i]);
return 0;
}
Related
This is my last try. I tried to find the most popular element in a string, for do this i've taken the string and converted single character in number and after this i sorted them in int array.
the problem is: when i use the 2d array for keep the frequency of each number: x[1][y]. and which number: x[0][y]. for some reason they are trash number
can someone help me?
(my english is rusty, sry).
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<malloc.h>
void bubble(int a[],int x);
void frequenza(int A[], int n);
int main(){
size_t counter;
char a[] = "asrfujefwaa";
int b[20];
int x;
x = strlen(a);
for(counter = 0; counter != strlen(a); counter ++)
{
b[counter] = a[counter];
}
bubble(b,x);
for(counter = 0; counter != x; counter ++)
{
printf("%d ",b[counter]);
}
puts("\n");
system("pause");
puts("\n");
frequenza(b,x);
return 0;
}
void bubble(int a[],int x)
{
size_t i;
int ord;
int scambio;
scambio = 0;
if(ord == 1)
{
return;
}
else
{
ord = 1;
for(i = 0; i < x - 1; i++)
{
if(a[i] > a[i + 1])
{
scambio = a[i];
a[i] = a[i + 1];
a[i + 1] = scambio;
ord = 0;
}
}
bubble(a,x);
}
}
void frequenza(int A[], int n)
{
int x[2][n];
int z = 0;
int y = 0;
size_t q;
x[0][0] = A[0];
x[1][0] = 1;
for(z = 0; z != n; z++)
{
if(x[0][y] == A[z + 1])
{
x[1][y] += 1;
}
if(x[0][y] != A[z + 1])
{
y++;
x[0][y] = A[z + 1];
}
}
for(z = 0; z != 2; z++)
{
puts("\n");
for(q = 0; q != y;q++)
{
printf("%d ",x[z][q]);
}
}
}
If you want to find the most common character in a string, you don't need to sort it or anything fancy. Just iterate through the characters, recording how many times each one has been seen, and get the maximum such count. You can do it all with a single pass through the string if you keep track of the maximum to date as you go:
#include <limits.h>
#include <stdio.h>
int main(void)
{
char a[] = "asrfujefwaa";
int freqs[UCHAR_MAX + 1] = { 0 }; // Initialize frequency table to all 0's
int most_common = 0;
for (unsigned char *c = (unsigned char *)a; *c; c++) {
if (++freqs[*c] > freqs[most_common]) {
most_common = *c;
}
}
printf("Most common character is %c, with %d occurrences.\n",
most_common, freqs[most_common]);
return 0;
}
I am trying to do a selection sort using function called min().
This is my code:
#include <stdio.h>
#include <conio.h>
void main() {
int i, temp, arr[20], n, loc;
int min(int [], int, int);
printf("Enter a range of the array");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter elements");
scanf("%d", arr[i]);
}
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
min(int arr[], int i, int n) {
int j, loc, temp;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = j;
}
}
return (temp);
}
getch();
}
the compiler is giving one error when compiling.
it saying:
Error SELECTIONSORT.C 22: Expression Syntax.
my line number 22 is min(int arr[],int i, int n) according to my compiler Turbo C++.
Please guide me where I am going wrong.
Thanks for any help.
There are multiple problems in your code:
The function min must be defined outside the body of the main() function.
Note that it is considered bad style to declare function prototypes in a local scope. Either define the function before the main() function or put the prototype before the main() function.
Also the prototype for main() without arguments should be int main(void).
In function min, you must initialize temp to i, or use i directly.
You should print the array contents after the sort, otherwise the program has no effect.
Here is a corrected version:
#include <stdio.h>
#include <conio.h>
int min(int [], int, int);
int main(void) {
int i, temp, arr[20], n, loc;
printf("Enter a range of the array: ");
if (scanf("%d", &n) == 1) {
for (i = 0; i < n && i < 20; i++) {
printf("Enter element %d: ", i);
if (scanf("%d", &arr[i]) != 1)
break;
}
n = i; // n is the actual number of inputs
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
for (i = 0; i < n; i++) {
printf("%d\n" array[i]);
}
}
getch();
return 0;
}
int min(int arr[], int i, int n) {
int j;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
i = j;
}
}
return i;
}
int Rearrange(int a)
{
long int b,j,i=0,num=0,count=0,arr[100];
while(a>0)
{
b=a%10;a=a/10;
arr[i]=b;
i++;
count ++;
}
j=count;
for(i=0;i<=count/2;i++)
{
t=arr[i];
arr[i]=arr[count-i-1];
arr[count-i-1]=t;
count--;
}
for(i=0;i<j;i+=2)
{
num=num*10 + arr[i]%10;
}
return num;
}
I want to write a function in c rearrange which prints the alternate digits of a number it is given.
for example:
input:- 12345
output:- 135
Thank you
Why complicating a simple problem?
If you don't mind an alternative approach, please check the below code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int input = 0;
int len = 0;
int i = 0;
char sinput[64] = {0, };
printf("Enter the number :");
scanf("%d", &input);
sprintf(sinput, "%d", input);
len = strlen(sinput);
printf("Output : ");
for (i = 0; i < len; i+=2)
{
printf("%c\t", sinput[i]);
}
printf("\n");
return 0;
}
Sample i/o:
[sourav#braodsword temp]$ ./a.out
Enter the number :123456
Output : 1 3 5
[sourav#braodsword temp]$
Your for loops are faulty. Change those to:
for(i=0;i<=count/2;i++)
{
int t=arr[i];
arr[i]=arr[j]; /* Use j */
arr[j]=t; /* Use j */
/* count--; Dont decrement */
j--;
}
for(i=0;i<count;i+=2) /* Should be count */
{
num=num*10 + arr[i]%10;
}
Demo
There can be many alternate ways to solve, but I just want to show you how the approach in thought process can be implemented correctly.
In your code problem is with the first for loop.
Please check the below code.
int Rearrange(int a)
{
long int b = 0, j = 0, i = 0, num = 0, count = 0, arr[100];
while (a > 0)
{
b = a % 10; a = a/10;
arr[i] = b;
i++;
count++;
}
j = count;
for (i = 0; i < count/2; i++) // Condition is problematic
{
long int t = arr[i];
arr[i] = arr[count-i-1];
arr[count - i - 1] = t;
// count--; // this is problamatic.
}
for (i = 0; i < j; i += 2)
{
num = num * 10 + arr[i] % 10;
}
return num;
}
#include<stdio.h>
int main()
{
int n,arr[40];
scanf("%d",&n);
printf("%d",n);
int s=0,i=0;
while(n!=0)
{
arr[i]=n%10;
printf("%d",arr[i]);
n=n/10;
i++;
}
for(int j=i-1;j>=0;j-=2)
{
s =s*10+arr[j];
}
printf("\n%d",s);
return 0;
}
int alternatedigits(int n)
{
int a[10],i=0,count=0,sum=0;
while(n!=0)
{
a[i]=n%10;
i++;
count++;
n=n/10;
}
for(int i=count;i>=0;i++)
{
if(i%2==0)
{
sum=sum*10+a[i];
}
}
return sum;
}
I am fairly new to C programming .
If the user enters a number lets say 123.
How can i print out 321?
I tried using bubble and selection sort but i find them very hard to understand at this initial stage!
I was hoping somebody could help me understand it by breaking it out !
Please help
Thank You
Here you go!
#include <stdio.h>
#include <string.h>
int main()
{
int n ;
int m ;
int i, j, jMax, max=0, res = 0;
int len = 0;
printf("Input number\n");
scanf("%d", &n);
m = n;
do // compute length of array that contains our input number
{
n /= 10;
len++;
}
while (n > 0);
n = m; // copy back input number
int * arr = malloc(len*sizeof(int)); // allocate memory for the array
len = 0; // initialize len for loop use
do // fill the array of input number
{
arr[len] = n%10;
n/=10;
len++;
}
while (n > 0);
//sort
int * sort = malloc(len*sizeof(int));
for(i = 0; i< len; i++)
{
for(j = 0; j<len; j++)
{
if(arr[j] > max)
{max = arr[j]; jMax = j;}
}
arr[jMax] = 0;
sort[i] = max;
max = 0;
}
// convert back from array to an int
j = 1;
for (i=0; i< len; i++)
{
res += sort[len-i-1]*j;
j *= 10;
}
printf(" %d", res);
return 0
}
You can take the user input as string, and print the string in reverse order, using a loop or recursion, but since you are new to c , I would say, go with chux's solution.
The below code does both reversing the entered number as well as arranging the entered number in the descending order.
You can try the below code:
int main()
{
int n,m,len,i,j,max=0,x;
printf("Enter the number\n");
scanf("%d",&n);
m = n;
len = 0;
printf("Number in the reverse order\n");
while(n>0)
{
n=n/10;
printf("%d",(n%10));
len++;
}
printf("\n");
printf("Numbers in descending order\n");
int *num = malloc(len*sizeof(int));
i =0;
while(m>0)
{
num[i++] = m%10;
m= m/10;
}
/* Any sorting technique can be used to sort the array */
for(i=0;i<len;i++)
{
max = num[i];
x = i;
for(j=i+1;j<len;j++)
{
if(num[j] > max)
{
max = num[j];
x = j;
}
}
num[x] = num[i];
num[i]= max;
}
/* Priting the entered number in descending order */
for(i=0;i<len;i++)
printf("%d",num[i]);
printf("\n");
return 0;
}
#include <stdio.h>
static inline char *max_c(size_t size, const char array[size]){
const char *p = array;
for(int i=1;i<size;++i){
if((unsigned char)*p < array[i])
p = array + i;
}
return (char*)p;
}
static inline void swap(char *a, char *b){
char tmp = *a;
*a = *b;
*b = tmp;
}
int main(void) {
char digits[32];
int len;
printf("input nums : ");
scanf("%31[0-9]%n", digits, &len);
for(int i = 0; i<len-1 ; ++i){
//swaped with the first element to select an element of maximum
swap(max_c(len - i, digits + i), &digits[i]);
}
printf("%s\n", digits);
return 0;
}
Just converting the int to a string using sprintf then reversing the string would probably be the easiest.
#include <stdio.h>
#include <string.h>
int main(){
int num = 123;
char str[15];
char backwards[15];
int i, j = 0;
sprintf(str,"%d",num);
for(i = strlen(str) - 1; i >= 0; i--){
backwards[j++] = str[i];
}
backwards[j] = '\0';
printf("%s\n",backwards);
return 0;
}
Then if you need to turn it back into an int then using atoi would do the trick.
//Remember to include this if you use atoi
#include <stdlib.h>
//Then just use this after you have reversed the string to
//convert it back to type int
num = atoi(&backwards);
I am trying to write a program in C. The program is supposed to find the GCD (greatest common divisor) of a given array. I am trying to use the smallest number of the array to find the GCD. I was wondering whats wrong with my last loop. I havent figured a way on how to check if the division is giving any decimal points in order to stop the loop. This is my code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int A[10]={112, 160, 180, 240, 288, 32, 480, 96, 60, 72};
int i;
int j;
int minimum = A[0];
int GCD;
int temp;
for (i=1;i<9;i++)
{
if( A[i] < minimum)
{
minimum = A[i];
}
}
for (i=1; i < minimum/2; i++)
{
for (j = 0; j < 9;j++)
{
GCD = 2*i;
temp = ((A[j])/(GCD));
int check = temp%1;
if (check == 0)
break;
}
}
printf("The Greates Common Denominator is: %d", GCD);
return 0;
}
#include <stdio.h>
unsigned gcd(unsigned x, unsigned y){
unsigned wk;
if(x<y){ wk=x;x=y;y=wk; }
while(y){
wk = x%y;
x=y;
y=wk;
}
return x;
}
int gcd_a(int n, int a[n]){
if(n==1) return a[0];
if(n==2) return gcd(a[0], a[1]);
int h = n / 2;
return gcd(gcd_a(h, &a[0]), gcd_a(n - h, &a[h]));
}
int main(void){
int A[10]={112, 160, 180, 240, 288, 32, 480, 96, 60, 72};
int size_A = sizeof(A)/sizeof(*A);
int gcd = gcd_a(size_A, A);
printf("%d\n", gcd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b)
{
int r;
while(a)
{
r=b%a;
b=a;
a=r;
}
return b;
}
int gcd_(int* A, int N)
{
int c=gcd(*A,*(A+1));
int i,g;
for(i=1;i<N-1;i++)
{
g=gcd(c,*(A+1+i));
c=g;
}
return c;
}
int main(void)
{
int A[]={96,60,32,72,84,90};
int N=sizeof(A)/sizeof(A[0]);
int t=gcd_(A,N);
printf("%d",t);
return 0;
}
#include <stdio.h>
static int gcd(int x, int y)
{
int r;
if (x <= 0 || y <= 0)
return(0);
while ((r = x % y) != 0)
{
x = y;
y = r;
}
return(y);
}
int main(void)
{
int A[10] = { 112, 160, 180, 240, 288, 32, 480, 96, 60, 72 };
int g = A[0];
for (int i = 1; i < 10; i++)
g = gcd(g, A[i]);
printf("The Greatest Common Denominator is: %d\n", g);
return 0;
}
The answer is 4. This is clearly correct; it is the GCD of 32 and 60; everything else is divisible by 4.
If desired, you could optimize the loop with:
for (int i = 1; i < 10 && g != 1; i++)
g = gcd(g, A[i]);
When the GCD is 1, it won't get any bigger,
#include <iostream>
using namespace std;
int gcd(int a, int b){
int t;
while(a)
{
t = a;
a = b%a;
b = t;
}
return b;
}
int main(){
int n;
cout<<"how many numbers (Max 10): "; // your choice
cin>>n;
cin.ignore();
cout<<endl;
int arr[n-1];
for (int i = 0; i < n; ++i) {
cin>>arr[i];
}
int ans;
ans = arr[0];
for (int j = 0; j < n; ++j) {
ans = gcd(ans,arr[j]);
}
cout<<"GCD = "<<ans;
//cin.get();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j, n, flag = 0, small, *data;
/* get the number of inputs from the user */
printf("Enter the number of inputs:");
scanf("%d", &n);
/* allocate memory to store n numbers */
data = (int *)malloc(sizeof(int) * n);
/* get n numbers from the user */
for (i = 0; i < n; i++) {
printf("Data[%d]: ", i);
scanf("%d", &data[i]);
}
/* find the smallest of n numbers */
small = data[0];
for (i = 1; i < n; i++) {
if (data[i] < small)
small = data[i];
}
/*
* use the smallest no to find gcd of n numbers.
* Start checking from small to 1 whether the
* same value divides all the given inputs
*/
for (i = small; i > 0; i--) {
for (j = 0; j < n; j++) {
if (data[j] % i != 0) {
flag = 1;
}
}
/* print the result */
if (!flag) {
printf("GCD of given %d numbers is %d\n", n, i);
break;
}
flag = 0;
}
return 0;
}
#include <stdio.h>
#include <conio.h>
int x;
void main()
{
int i,num[10]={0};
printf("How many numbers do you want to enter =");
scanf("%d",&x);
for(i=0;i<x;i++)
scanf("%d",& num[i]);
printf("The hcf of the numbers are = %d ", hcf(num));
}
int hcf(int num[])
{
int count,rem,lv,i,j;
lv=gnum(num);
if(lv==1)
return 1;
for(j=2;j<=lv;j++)
{
count =0;
for(i=0;i<x;i++)
{
rem = num[i]%j ;
if( rem != 0 || num[i]< j )
break;
else
{
count++;
}
}
if(count == x)
{
for(i=0;i<x;i++)
num[i]=num[i]/j;
return(j*hcf(num));
}
}
if(count!= x)
return 1;
}
int gnum(int num[])
{
int i,temp=num[0];
for(i=0;i<x;i++)
{
if(temp >= num[i])
temp = num[i];
}
return (temp);
}
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
int A[2] = {10,30};
int a,b,r,i;
for ( i = 1; i < 2; i++)
{
a=A[0],b=A[i];
while(b!=0)
{
r=a%b;/*remainder*/
a=b;
b=r;
}
}
printf("The Greatest Common Denominator is: %d\n", a);
return 0;
}
/*You can change the **for** loop range according to the array values*\
#include <stdio.h>
#include <stdlib.h>
int main()
{
int A[10]={112, 160, 180, 240, 288, 32, 480, 96, 60, 72};
int i;
int j;
int minimum = A[0];
int GCD;
int temp;
int f;
for (i=1;i<10;i++)
{
if( A[i] < minimum)
{
minimum = A[i];
}
}
for (i=1; i <= minimum; i++)
{
f=0;
for (j = 0; j < 10;j++)
{
if(A[j]%i!=0)
{
f=1;
break;
}
}
if(f==0)
GCD=i;
}
printf("The Greates Common Denominator is: %d ", GCD);
return 0;
}
enter code here