I need to count the number of times a certain number shows up in an array after
ID: 3534819 • Letter: I
Question
I need to count the number of times a certain number shows up in an array after it is sorted. Here is the code:
#include<stdio.h>
#include<conio.h>
main()
{
int n =0;
printf("This program counts the number of times distinct input number occurs. ");
printf("How many input numbers are there?. ");
scanf("%d",&n);
int a[n],temp,i,j;
printf("Please Input %d numbers. ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j] <a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(" Bubble Sorted Series --");
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
}
}
Explanation / Answer
for(i=0;i<n;i++)
{
num=a[i] // initializing the starting number of array to num
for(j=i+1;j<n;j++)
{
if(num==a[j]) // comparing the num and all the numbers in the array
{
count++;
}
}
}
printf("the number of times a number is showing up is %d", count+1);
the count will hold the value of the no. of times the number cam up in the array .....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.