1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
// File: split.cpp
// Created By: Colton Haas
// Created On: 11/6/2018
// Synopsis: this program prompts the user to input the length of an array and then input that number of intergers in the array. The program then splits the negative numbers from the positive numbers and displaying them in seperate element lists.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// function prototypes
void count(int array[], int & numn, int & nump, int SIZE);
void split(int array[], int ap[], int an[], int SIZE, int numn, int nump);
void print_array(const string & prompt, int array[], int num);
int main()
{
int SIZE(0);
int numn(0), nump(0);
int x(0), i(0);
int num(0);
cout << "Enter number of elements: ";
cin >> SIZE;
int array[SIZE];
cout << "Enter list: ";
cin >> x;
for(i=0; i<SIZE; i++)
{array[i] = x;
cin >> x;}
count(array, numn, nump, SIZE);
int ap[nump], an[numn];
split(array, ap, an, SIZE, numn, nump);
print_array("Negative elements:", an, numn);
print_array("Non-negative elements:", ap, nump);
return(0);
}
// prompts input of the the numbers in the array up to 20
void count(int array[], int & numn, int & nump, int SIZE)
{
int i(0);
while (i < SIZE)
{if (array[i] < 0)
{numn++;}
else
{nump--;}
i++;
}
}
// outputs the enterd numbers in the array
void split(int array[], int ap[], int an[], int SIZE, int numn, int nump)
{
int i(0), j(0), k(0);
for (i=0; i<SIZE; i++)
{if (array[i] < 0)
{for (j=0; j<numn; j++)
{an[j] = array[i];}
}
else
{for (k=0; k<nump; k++)
{ap[k] = array[i];}
}
}
}
// it finds the max of the numbers in the array
int print_array(const string & prompt, int array[], int num)
{
cout << prompt << endl;
int i(0);
for(i=0; i < num; i++)
{cout << " " << array[i];
}
}
|