Numerical Array split

1. The problem statement,
Define an array with 100 integer values, and fill the array with random numbers in the range from 0-99. Then write a function named split () that reads the array and places numbers from 0-50 and another array that places numbers from 50 to 99.

I wrote this code which sets up an array 100 large and generates random numbers every time i run the code. Now i need to create the split function. When writing the split function, im not sure what i have to return adn what i have to display in main.
Please help me,


2. The attempt at a solution

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
91
92
93
#include <iostream>
#include <time.h>			//to generate Random Numbers

using namespace std;
//Funtion Prototype
int loadArray ( int [], int);
void printArray (int [], int);
int split ( int[], int[], int);



int main ()
{
	const int MAX_SIZE = 100;		
	int numbers [MAX_SIZE];	   //Array size 100
	int A, over50, sizeOver;
	int size;
	
	loadArray ( numbers, MAX_SIZE);		//Calling loadArray function
	printArray ( numbers, MAX_SIZE);	//Calling printArray Function
	final = split (numbers, size, MAX_SIZE);

	cout << numbers << endl;
	
	 
	return 0;
}


//Functions

void printArray (int A[], int MAX_SIZE)				//printArray Function
{
	int i;
	for ( i=0; i<MAX_SIZE; i++)
		cout << A[i] << endl;

}

int loadArray ( int  A[], int MAX_SIZE)			//LoadArray Funciton
{
	//fill with random numbers
	
	int i,randomNumber;

	srand (time (NULL) );				//Initialize random nubmer generator

	for ( i=0; i < MAX_SIZE; i ++)
	{
		randomNumber = rand() % 100;		// get random numbers; range 0-99
		
		A[i]= randomNumber;
		
	}
	return 0;
}


int split ( int A[], int size, int MAX_SIZE)
{
	int countA, countB;
	countA = 0;
	countB = 0;

	for (int i = 0; i < 100; i++)
	{
		if (A[i] < 50)
		{
			for (int i=0; i< 99; i++)
			{
				while ( A[i] < 49 )
				{ 
					countA = i;
				}
			}
           
		}
		 else
		 {
			for (int i=0; i< 99; i++)
		    {
				 while ( A[i] < 49 )
				{ 
					countB = i;
				}
			}
		 }
	 }

	return A[i];


}
Last edited on
The split function should have a void return type, so in other words, it won't return anything (directly). The two arrays (and their sizes) should be returned by means of pointers (passing by reference). The two arrays should be allocated on the heap from within the split function.

I think the main function should display the contents of both arrays (along with their counts). The main function should also clean up the arrays as they were allocated on the heap.

Also:

1
2
3
4
5
6
7
8
9
10
if (A[i] < 50)
{
     for (int i=0; i< 99; i++)
     {
          while ( A[i] < 49 )
          { 
                countA = i;
          }
     }
}


You do not need to iterate through the elements again here to get the count. At this point, you already know the current element is less than 50. Instead of the inner two loops, it should just be countA++; (and similarly for the else).

You also said the first array should have any elements between 0-50 and the second array should have any elements between 50-99. I don't think this is quite right since all the 50's will be duplicated. I think the first array should have any elements between 0-49 (inclusive) and the second one should have any elements between 50-99 (inclusive). I suspect that the order of the elements should be the same as they appear in the original array.
Topic archived. No new replies allowed.