Question

In: Computer Science

In all cases we have an array of int or char of some fixed size. The...

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:

  1. Count how many numbers are < the last element
  2. Count how many numbers are odd

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:

  1. Count how many letters occur more than once are equal to the last character
  2. Count how many letters are equal to some user input letter

Your code to do the task will need to be in a function.

Solutions

Expert Solution

#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;
}


Related Solutions

We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int A [ ], int n) { M = A[0]; for i = 1 to n – 1     if (A[i] > M)         M = A[i]        //Update the max return M; } Write a recursive version of this program. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n). Prove by induction that the solution of...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT