In: Computer Science
In all cases we have an array of int or char of some fixed size. The program will prompt the user to enter some values, such as:
Enter 7 integers to be stored in the array: 5 13 8 5 1
2
The questions that could then be asked of this data might be:
Similarly we might prompt for character input:
Enter 7 characters to be stored in the array:
azaleas
The questions that could then be asked of this data might be:
Your code to do the task will need to be in a function.
#include<stdio.h>
#include<string.h>
int countNumbers(int arr[],int n);
int oddCount(int arr[],int n);
int lastCharCount(char chArray[]);
int userCharCount(char chArray[],char ch);
int main(){
//code for integer array
int n,i;
int lsthanLast,oddNumCount;
printf("Enter size of array : ");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
lsthanLast =countNumbers(arr,n);
printf("less than last number count :
%d\n",lsthanLast);
oddNumCount=oddCount(arr,n);
printf("odd numbers count : %d\n",oddNumCount);
//code starts for character array
char chArray[100];
int lCharCount,uCharCount;
printf("Enter character array : ");
scanf("%s",&chArray);
lCharCount=lastCharCount(chArray);
printf("last character count :
%d\n",lCharCount);
char ch;
printf("Enter character to find frequency :
");
scanf(" %c",&ch);
uCharCount=userCharCount(chArray,ch);
printf("user character count : %d",uCharCount);
return 0;
}
//for finding less than last number count
int countNumbers(int arr[],int n){
int i,last=arr[n-1],count=0;
for(i=0;i<n-1;i++){
if(arr[i]<last)
count++;
}
return count;
}
//for finding count of odd nubmers
int oddCount(int arr[],int n){
int i,oCount=0;
for(i=0;i<n;i++){
if(arr[i]%2!=0){
oCount++;
}
}
return oCount;
}
//for finding last character count
int lastCharCount(char chArray[]){
int i,len,lcount=0;
char lchar;
len=strlen(chArray);
lchar=chArray[len-1];
for(i=0;i<len-1;i++){
if(lchar==chArray[i]){
lcount++;
}
}
return lcount;
}
//for finding user character count
int userCharCount(char chArray[],char ch){
int i,len,ucount=0;
len=strlen(chArray);
for(i=0;i<len;i++){
if(ch==chArray[i]){
ucount++;
}
}
return ucount;
}