Split Numerical Array of 100 Integer Values 0-99

AI Thread Summary
The discussion revolves around creating a split function for an array of 100 random integers ranging from 0 to 99. The user seeks assistance in implementing a function that separates these integers into two arrays: one for numbers 0-49 and another for numbers 50-99. Participants suggest methods for determining the sizes of the new arrays, either by allocating maximum sizes or by counting the numbers in the original array. They emphasize the importance of using a single loop to efficiently categorize the numbers based on their values. The conversation concludes with a clearer understanding of how to implement the split function effectively.
lcam2
Messages
28
Reaction score
0

Homework 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. I need help creating the split function and making it to split of numbers over and under 50.
Any Help is appreciated, thanks in advance. :smile:

Homework Equations






The Attempt at a Solution

:confused:

code
[
#include <iostream>
#include <time.h> //to generate Random Numbers

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



int main ()
{
const int MAX_SIZE = 100;
int numbers [MAX_SIZE]; //Array size 100
int A, over50, sizeOver;

loadArray ( numbers, MAX_SIZE); //Calling loadArray function
printArray ( numbers, MAX_SIZE); //Calling printArray Function

sizeOver = splitOver50 ( A[] , over50[] , MAX_SIZE );

cout << sizeOver << endl;

return 0;
}


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

}
//Functions
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= randomNumber;

}
return 0;
}


int splitOver50 ( int A[], int over50[], int MAX_SIZE )
{

if (A > 50 )
{
over50 =A;
}


return over50[];

}
]
 
Physics news on Phys.org
Traverse the original array (start at the first slot and end at the last slot). What happens if the current slot is 0-49? 50-99? Provide more thought into it and you should get what to do.
 
Thanks for you fast reply gb7nash, honestly, I'm new in this field of programming and i didn't get exactly what you mean. when i run the code without the last function it gives me 100 random numbers. When trying to create the split function is the part where i get lost. I understand the part where you point out that it has to be from 0 t0 49 and the second array form 50 to 99 what i don't really understand is the part of the split. Thanks
 
First off, we want to "split" this into two arrays. We first thing we need to figure out what size we want to allocate for these two arrays. There are two ways to do this (possibly more):

1) Allocate the largest size possible for each array (what is the maximum size possible?)

2) Traverse the original array and keep a count of how many items are 0-49 (call it countA) and how many items are 50-99 (call it countB). After you have done this, declare arrayA with size countA and arrayB with size countB. This method requires multiple loops though, so that's up to you.

After we have decided the size of each array, we need to traverse the original array. If the current slot is 0-49, what should do with that number? If the current slot is 50-99, what should do with that number?

edit: I saw your original post and it looks like you know array syntax.
 
Yes, we want to "split" into two arrays.
The book doesn't tell the size of the array since the teacher modified the problem and didn't tell us either.
can a for loop be used to count the numbers greater than 50 in the main array and make that the array size?
I don't expect you to do my homework, but i really need help with those details.
 
lcam2 said:
can a for loop be used to count the numbers greater than 50 in the main array and make that the array size?

Yes. You can also do this to count the numbers less than 50 to make the second array size.
 
Ok, i just wrote this function it does makes a lot of sense to me now.
This is my new functions I am not sure if I am returning the correct values.

[
int splitUnder ( int A[], int size, int MAX_SIZE )
{
int countA, arrA;
countA = 0;

for (int i=0; i< 99; i++)
{
while ( A < 49 )
{
countA = i;
}
}

arrA = countA;

return size ;

}

int splitOver (int A[], int size, int MAX_SIZE)
{
int countB, arrB;
countB = 0;

for ( int i=0; i< 99; i++)
{
while ( A >50)
{
countB = i;
}
}

arrB = countB;

return size;
}

]
 
Can someone help me?? I am really lost
 
You're making this way too difficult for yourself. Consolidate this into one function called Split

Code:
for (int i=0; i< 99; i++)
 {
	 while ( A[i] < 49 )
	{ 
		countA = i;
	}
 }

I'm not really sure what you're doing here. You want to count the number of elements in A that are between 0 and 49. You also want to count the number of elements in A that are between 50 and 99. Instead, use:

Code:
countA = 0;
countB = 0;

for (int i = 0; i < 100; i++)
{
     if (A[i] < 50)
     {
           <enter code here>
     }
     else
     {
           <enter code here>
     }
}
 
  • #10
Thanks , it is very clear.
Im sorry, I am not very good with computers.
 
Back
Top