Write a function that categorizes negative and positive numbers of a given innial array into two separate arrays with sufficient allocated array space. Write the main program using the upper function in two cases:
The input array is a static array with the value of the given elements.
The input array is a dynamic array with the value of elements entered from the keyboard
Displays two arrays of results to the screen.
#include <iostream>
#include <string>
using namespace std;
void categorizesNegativePositiveNumbers(float innialArray[],int size){
float positiveNumbers[100];
int positiveNumbersSize=0;
float negativeNumbers[100];
int negativeNumbersSize=0;
for(int i=0;i<size;i++){
if(innialArray[i]>=0){
positiveNumbers[positiveNumbersSize]=innialArray[i];
positiveNumbersSize++;
}else{
negativeNumbers[negativeNumbersSize]=innialArray[i];
negativeNumbersSize++;
}
}
//Displays two arrays of results to the screen.
cout<<"All positive numbers:\n";
for(int i=0;i<positiveNumbersSize;i++){
cout<<positiveNumbers[i]<<" ";
}
cout<<"\nAll negative numbers:\n";
for(int i=0;i<negativeNumbersSize;i++){
cout<<negativeNumbers[i]<<" ";
}
}
int main(){
//The input array is a static array with the value of the given elements.
float staticArray[]={-54,4,44,87,56,-23,66,50,-8,-48};
categorizesNegativePositiveNumbers(staticArray,10);
//The input array is a dynamic array with the value of elements entered from the keyboard
int size=-1;
cout<<"\n\nEnter the number of elements in the dynamic array: ";
cin>>size;
float* dynamicArray= new float[size];
for(int i=0;i<size;i++){
cout<<"Enter the element "<<(i+1)<<": ";
cin>>dynamicArray[i];
}
categorizesNegativePositiveNumbers(dynamicArray,size);
delete[] dynamicArray;
cin>>size;
return 0;
}
Comments
Leave a comment