Question

1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(Javascript) Write a function that accepts an array. The function adds its value with the value...
(Javascript) Write a function that accepts an array. The function adds its value with the value of the character before to it to generate a number. It returns a string of numbers.
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string. Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. 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 pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
Write a function that accepts an int array and the array’s size as arguments. The function...
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 named data into an...
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
Write a C++ function which accepts two array of integers (like arr1 and arr2) of the...
Write a C++ function which accepts two array of integers (like arr1 and arr2) of the same size (100), then create a new array (like arr3) with the same size (100) and assign the sum of corresponding elements in arr1 and arr2 to the new array (arr3) and return back arr3 from the function. You don't need to write the main function. For example sum of corresponding elements in arr1 and arr2 to be assigned to arr3 should be like:...
Write a method named raiseSalary that accepts two integers as an argument and return its sum...
Write a method named raiseSalary that accepts two integers as an argument and return its sum multiplied by 15%. Write a tester program to test the method. The class name should be your ID(for example: Id12345678). Your answer should include a screenshot of the output. Otherwise, you will be marked zero for this question.
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Write a function named “highestScore” that takes an array of floating point scores and its size...
Write a function named “highestScore” that takes an array of floating point scores and its size as parameters and return the highest of these scores. The function prototype: float highestScore(float scores[], int size);
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT