Split Numerical Array of 100 Integer Values 0-99

In summary: I don't know if I am doing this right.[int split (int A[], int size, int MAX_SIZE){ int arrA, arrB, countA, countB; countA = 0; countB = 0; for (int i=0; i< 100; i++) { if (A[i] < 50) { countA = countA + 1; } else { countB = countB + 1; } } arrA = countA; arrB = countB; return size ; }]In summary, the conversation discusses creating an array with 100 integer
  • #1
lcam2
28
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
  • #2
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.
 
  • #3
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
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 
  • #7
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;
}

]
 
  • #8
Can someone help me?? I am really lost
 
  • #9
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.
 

Related to Split Numerical Array of 100 Integer Values 0-99

1. What is a "Split Numerical Array of 100 Integer Values 0-99"?

A "Split Numerical Array of 100 Integer Values 0-99" is a data structure that contains 100 integer values ranging from 0 to 99, split into separate arrays or subsets. This type of array is commonly used in computer programming to efficiently store and manipulate large sets of numerical data.

2. How is a "Split Numerical Array of 100 Integer Values 0-99" created?

A "Split Numerical Array of 100 Integer Values 0-99" can be created using various programming languages, such as Python, Java, or C++. The process typically involves defining an array with a size of 100 and filling it with integers ranging from 0 to 99. The array can then be split into smaller arrays based on the programmer's needs.

3. What are the advantages of using a "Split Numerical Array of 100 Integer Values 0-99"?

One advantage of using a "Split Numerical Array of 100 Integer Values 0-99" is that it allows for efficient storage and manipulation of large amounts of numerical data. It also allows for easy sorting and searching of the data, as well as the ability to perform mathematical operations on the entire array or subsets of the array.

4. How is data stored in a "Split Numerical Array of 100 Integer Values 0-99"?

Data is typically stored in a "Split Numerical Array of 100 Integer Values 0-99" in consecutive order, with the first value in the array being 0 and the last value being 99. Depending on the programming language used, the array may also have a designated index for each value, allowing for easy access and manipulation of specific elements.

5. How is a "Split Numerical Array of 100 Integer Values 0-99" different from a regular array?

A "Split Numerical Array of 100 Integer Values 0-99" is different from a regular array in that it is specifically designed to store and manipulate large sets of numerical data in an efficient manner. It is also different in that it is split into smaller arrays or subsets, allowing for more flexibility in handling the data. Regular arrays can contain any type of data, while a split numerical array is limited to only integers.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
Back
Top