1. Write a function named "countPositiveNegative" that accepts
an array of integers, its size and return the number of positive
and negative in the array through the use of parameters. In
addition, it also returns the number of 0s in the array as the
return value.
For example, array of {10, 20, -30, -40, 50, 0, 60} will return 4
for positives, 2 for negatives and 1 for 0's as the return
value.
2. Write a function named "rotateRight" that accepts an array of
integers and its size. It will rotate all the values to the right
and move the value of the last element in the array to the
first.
For example, array of {10, 20, 30} will become {30, 10, 20}.
3. Write a function named "countNonAlpha" that accepts a string.
It will return the number of non-alphabet characters (excluding
blanks) in the string.
For example, if the string is "Hello, World!", it will return 2 for
',' and '!" in the string.
4. Write a function named “makeItAscending” that accepts an
array of 3 numbers. It will make sure that these numbers are in the
right ascending order (swapping position if necessary). It will
return a boolean value: true if the numbers have been moved to make
it ascending and false if there is no movement (do not print in the
function).
Write main program that calls and test this function and print out
values before and after the function call to show they are in
ascending after calling function regardless how they are ordered
before the call.
Writing the solutions in C++14
#include <iostream>
#include <ctype.h>
using namespace std;
int countPositiveNegative(int arr[], int n, int &pos, int
&neg){
int zero=0;
pos=0; neg=0;
for (int i=0; i<n; i++){
if (arr[i] > 0){
pos++;
}
else if (arr[i] < 0){
neg++;
}
else{
zero++;
}
}
return zero;
}
void rotateRight(int arr[], int size) {
int i=0 ;
int temp = arr[size-1];
for(i=size-1;i>=1;i--)
{
arr[i] = arr[i-1];
}
arr[0] = temp;
}
int countNonAlpha(string st){
string s = st;
int count = 0;
for (int i=0; i < s.length(); i++){
if (!isalpha(s[i]) && s[i] != ' '){
count++;
}
}
return count;
}
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
bool makeItAscending(int arr[3])
{
int flag = 0;
if(arr[0]<=arr[1] && arr[1]<=arr[2])
{
flag = 0;
}
else
{
if (arr[0] > arr[1]) swap(&arr[0], &arr[1]);
if (arr[1] > arr[2]) swap(&arr[1], &arr[2]);
if (arr[0] > arr[1]) swap(&arr[0], &arr[1]);
flag = 1;
}
if(flag == 1) return true;
else return false;
}
int main() {
int arr1[5] = {1, -3, 2, 0, 0};
int positive, negative;
cout << "Number of zeros : " <<
countPositiveNegative(arr1, 5, positive, negative) <<
endl;
cout << "Number of positive Numbers: " <<
positive << endl;
cout << "Number of negative Numbers: " <<
negative << endl;
int arr2[3] = {10,20,30};
rotateRight(arr2,3);
cout<<"The array after shifting right: ";
for(int i=0;i<3;i++)
cout<<arr2[i]<<" ";
cout<< endl;
string s = "Hello, World!";
cout<< "Number of non aplhabetic characters are
: "<< countNonAlpha(s)<<endl;
int arr3[3] = {10,20,30};
cout<< " The array now is in ascending order
with the status : " << makeItAscending(arr3);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.