Split Numerical Array of 100 Integer Values 0-99

Click For Summary

Discussion Overview

The discussion revolves around creating a function to split an array of 100 random integers ranging from 0 to 99 into two separate arrays: one for numbers from 0 to 49 and another for numbers from 50 to 99. Participants are seeking assistance with the implementation of this function as part of a homework assignment.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes their initial code setup and requests help specifically with the split function.
  • Another participant suggests traversing the original array to determine how to categorize the numbers based on their value.
  • A participant expresses confusion about the splitting process and seeks clarification on how to implement it.
  • There are suggestions on how to allocate sizes for the two new arrays based on counts of numbers in specified ranges.
  • Multiple participants discuss the possibility of using loops to count numbers in the original array to determine the sizes of the new arrays.
  • One participant shares their attempt at writing the split function but questions whether they are returning the correct values.
  • Another participant advises consolidating the logic into a single function and provides a more straightforward approach to counting elements in the array.

Areas of Agreement / Disagreement

Participants generally agree on the need to split the array into two parts based on the specified ranges. However, there is no consensus on the best method to implement the split function, and various approaches are discussed without resolution.

Contextual Notes

Some participants express uncertainty about the array sizes due to a lack of information from the homework prompt. There are also unresolved issues regarding the implementation details of the split function, including the correct use of loops and array indexing.

Who May Find This Useful

This discussion may be useful for students learning about array manipulation in programming, particularly in the context of splitting data based on specific criteria.

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 );

count << sizeOver << endl;

return 0;
}


void printArray (int A[], int MAX_SIZE) //printArray Function
{
int i;
for ( i=0; i<MAX_SIZE; i++)
count << 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.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
8
Views
4K
Replies
9
Views
2K
Replies
8
Views
2K