Question

In: Computer Science

Write a function named timesOfLetter that reads an array and returns the number of times of...

Write a function named timesOfLetter that reads an array and returns the number of times of each lowercase letter and each uppercase letter appear in it, using reference parameter. • Write a function named timesOfNumber that reads an array and returns the number of times of each odd number, and each even number appear in it, using reference parameter. • Write a function named isChar() that determines if the input is alphabetic or not during inputting. • Write a function named isInt() that determines if the input is a digit or not during inputting. • Initialize the size of the arrays as 10.

Hints: • Firstly, determine whether the element is a letter or a number, which should not be any other characters. • By passing by reference, the values can be modified without returning them. • You may call a function inside another function.

Solutions

Expert Solution

//function named determines if the input is alphabetic or not
bool isChar(char c)
{
   if((c>='a' && c<='z') || (c>='A' && c<='Z'))
       return true;
   return false;
}

//function that determines if the input is a digit or not
bool isInt(int n)
{
   if(n>='0' && n<='9')
       return true;
   return false;
}

/* function that reads an array and returns the number of times of each
lowercase letter and each uppercase letter appear in it, using reference
parameter.
nl = number of lowercase, nu = number of uppercase
*/
void timesOfLetter(int &nl, int &nu)
{
   char s[10], ch;
int n = 10;
  
   for(int i=0; i<n; i++)
   {
       cin>>ch;
       if(isChar(i)==false)
       {
           cout<<"Not a letter!";
           i--;
           continue;
       }
       s[i] = ch;
   }
      
   nl = nu = 0;
  
   for(int i=0; i<n; i++)
   {
       if(s[i]>='a' && s[i]<='z') nl++;
       else if(s[i]>='A' && s[i]<='Z') nu++;
   }
}

/* function that reads an array and returns the number of times of each odd
number, and each even number appear in it, using reference parameter.
ne = number of even, no = number of odd
*/
void timesOfNumber(int &ne, int &no)
{
   int s[10], a;
   int n = 10;
  
   for(int i=0; i<n; i++)
   {
       cin>>a;
       if(isInt(i)==false)
       {
           cout<<"Not a digit!";
           i--;
           continue;
       }
       s[i] = a;
   }
  
   ne = no = 0;
  
   for(int i=0; i<n; i++)
   {
       if(s[i]%2==0) ne++;
       else no++;
   }
}


Related Solutions

1. Write a function named “Number” that reads in a list of numbers until the user...
1. Write a function named “Number” that reads in a list of numbers until the user enters 0. It will return true if the user has entered more even numbers than odd numbers; otherwise it returns false. 2. Write a code segment to sort an array of students in ascending order of their IDs. Assume the array has been filled with names and assume the following declaration: struct Student { string name; int ID; } Student roster[30]; // Code to...
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the two arrays contain the same values (but not necessarily in the same order), otherwise it returns 0. Your solution must not sort either array or a copy of either array! Also you must not modify either array, i.e., the values in the arrays upon return from the function must be the same as when the function was called. Note that the arrays do not...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns a string containing the number word for the whole numbers 0 - 999. For example:  numberWords(234) would return 'two hundred thirty-four' If the input value is not a whole number between 0 - 999 then the function should return a string equivalent to 'ERROR'.
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the second highest number from the array. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input array will always have length at least two (so there is always a second highest value). The original array must NOT be modified by this function. Example secondHighest([5,3,8,6,2,7,4]) must return 7, and secondHighest([5,3,8,6,2,7,8]) must return 8. I have...
Write a GLM function named getInverse that returns the inverse of A. If A does not...
Write a GLM function named getInverse that returns the inverse of A. If A does not have an inverse, the function returns the identify matrix. Assume A is 3x3.
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT