Question

In: Computer Science

Using Pass-by-Reference for Out Parameters Write a single function that counts the number of positive, negative...

Using Pass-by-Reference for Out Parameters Write a single function that counts the number of positive, negative and zero values in an integer array. Return the number of positive values, the number of negative values and the number of zero values. Use pass-by-reference to use parameters as “out parameter”: these are variables passed by reference into a function with the purpose of holding the result of the function after it terminates. Your function should have the following declaration: void countPosNegZero(int array[], int numElements, int &positive, int &negative, int &zero); where array is the array with the values, numElements is the number of elements in the array, positive will hold the number of positive values in the array, negative will hold the number of negative values in the array and zero will hold the number of zero values in the array. Add a test-main function that tests your program with several different arrays (use the rand() method to create arrays with random values ranging over negative and positive values).

c++

Solutions

Expert Solution

Hi,

Please find the code below and output attached.

#include<iostream>
using namespace std;
#include <ctime> // this is required to use rand function

//the out parameter will be of type out&, i.e passed by reference,it means value that is passed can be modified by the function
// when we pass by reference values modified will also be visible in the main function here.
void countPosNegZero(int array[], int numElements, int &positive, int &negative, int &zero)
{
  
for(int i=0; i<numElements; i++)
   {
       if(array[i]<0)
       {
           negative++;
       }
       else if(array[i]==0)
       {
           zero++;

       }
       else
       {
           positive++;
       }
   }

  
}

int main()
{
  
   int countp=0, countn=0, countz=0, arr[10], i;
   int numOfElements;
   int secondArray[50];
   cout << "Please enter the number of elements" << endl;
   cin >> numOfElements;
   cout << "Now enter "<< numOfElements << "integers" << endl;
   for(i=0; i<numOfElements; i++)
   {
       cin>>arr[i];

   }
   countPosNegZero(arr,numOfElements,countp,countn,countz);
   cout << "Positive Integers count:" << countp << endl;
   cout << "Negative integers count:" << countn << endl;
   cout << "No of zeros:" << countz << endl;
  
   // this is using rand to generate random array of 50 integers.
   srand(time(0));
for(int i=0; i<50; i++)
{
secondArray[i] = rand()%100-20; // -20 just to generate negative numbers also
cout << secondArray[i] << "\t";
}
countp=0, countn=0, countz=0; // initializing them again t 0 to reuse
countPosNegZero(secondArray,50,countp,countn,countz);
   cout << "Positive Integers count using rand :" << countp << endl;
   cout << "Negative integers count using rand:" << countn << endl;
   cout << "No of zeros using rand:" << countz << endl;
  
   return 0;
}

thanks,

kindly upvote.


Related Solutions

Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Q3)   Write a C program that counts the number of odd numbers with using function count()...
Q3)   Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Implement if else loop to find out if the given number is positive or negative or...
Implement if else loop to find out if the given number is positive or negative or zero. You would be hardcoding the number in the below example I used 8. But you can use a different number.
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
Write a function that counts the number of times a given integer occurs in a Linked...
Write a function that counts the number of times a given integer occurs in a Linked List. What is the time complexity of your algorithm? Justify your answer in python
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters...
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters 2 ) ask the user for 2 numbers (should be in main) 3 ) Calculate the sum and product of the numbers using reference parameters (should be in getAnswer function) 4 ) Print the sum and product of the numbers in the following format: (should be in printmessage function) Here is the function you need        Function name | Parameters                       | Return Type    heading |...
C programming review excerises. 1. Write a function that counts the number of lines in a...
C programming review excerises. 1. Write a function that counts the number of lines in a file, using the following declaration: int countLines(char *filename) 2. Write a program that counts the number of words in a text file. Use the command-line arguments to read the name of the file. The syntax: countwords filename.txt 3. Write a program that counts the number of words starting with the first letter ‘T’ in a text file. Using commend line argument for the text...
Write a function in lisp XIT that counts the number of items in each sub-list of...
Write a function in lisp XIT that counts the number of items in each sub-list of a list and returns the count in a list. It should only work if there are two levels of brackets in the parameter (simple lists inside the parent list). Thus:             (XIT '((A B C)))        -> (3)             (XIT '((A) (A B) (A B C))) -> (1 2 3)             (XIT '((1 2)))              -> (2)             (XIT '(1 (2 3)))...
I need to write a function that counts the number of total wins and losses. I...
I need to write a function that counts the number of total wins and losses. I have a text file called analysis_data.txt with 1000 lines written Won Loss Won Loss Won Won ... The function need to do the following: Opens the analysis_data.txt file and reads through all the data, counting number of 'Won' and 'Loss' words stored in the file. Returns two integers: count of wins and count of losses from the text file. **Can't use the break statement
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT