Greeting C gurus!!
I'm trying to write a counting frequency elements in array code but it's not working
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int v[]={1,5,6,7,9,3,3,6,1,5};
int c[10]={0};
int i;int j;int k;
for(k=0;k<10;++k) //intialising the counter of my elements to 1
c[i]=1;
for(i=0;i<strlen(v);++i)
{
for(j=i+1;j<strlen(v);++j)
{
if(v[i]==v[j] && v[i]!='\0')
{
c[i]++;
v[j]='\0';
}
}
}
for(i=0;i<strlen(v);++i)
{
if (v[i]!='\0')
printf("the elements %d is repeated %d times",v[i],c[i]);
}
}
I also found another code while searching in internet
Code:
#include<stdio.h>
int main(){
int n, t, i, j, arr[30],len, halflen,flag=0,count=0;
printf("Enter number of elements to insert in an array:");
scanf("%d",&len);
printf("Enter elements to insert in an array:");
for(i=0;i<len;i++){
scanf("%d",&t);
arr[i]=t;
}
printf("\n");
/*****************************/
for(i=0;i<len;i++){
count=1;
for(j=i+1;j<=len-1;j++){
if(arr[i]==arr[j]&& arr[i]!='\0'){
count++;
arr[j]='\0';
}
}
if(arr[i]!='\0'){
printf("%d is %d times.\n",arr[i],count);
}
}
/*****************************/
getch();
return0;
}
My questions are :
- what's wrong with my code?
- the second code use count as counter for the all elements in array, but how can this possible? the value of count is different for every element??
- what's the use of getchar()?
Many thanks in advance!!