1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Numerical Array split

  1. Jul 5, 2011 #1
    1. The problem statement, all variables and given/known data
    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:

    2. Relevant equations




    3. 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[];

    }
    ]
     
  2. jcsd
  3. Jul 5, 2011 #2

    gb7nash

    User Avatar
    Homework Helper

    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.
     
  4. Jul 5, 2011 #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
     
  5. Jul 5, 2011 #4

    gb7nash

    User Avatar
    Homework Helper

    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.
     
  6. Jul 5, 2011 #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.
     
  7. Jul 5, 2011 #6

    gb7nash

    User Avatar
    Homework Helper

    Yes. You can also do this to count the numbers less than 50 to make the second array size.
     
  8. Jul 5, 2011 #7
    Ok, i just wrote this function it does makes a lot of sense to me now.
    This is my new functions im not sure if im 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;
    }

    ]
     
  9. Jul 5, 2011 #8
    Can someone help me?? Im really lost
     
  10. Jul 5, 2011 #9

    gb7nash

    User Avatar
    Homework Helper

    You're making this way too difficult for yourself. Consolidate this into one function called Split

    Code (Text):
    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 (Text):


    countA = 0;
    countB = 0;

    for (int i = 0; i < 100; i++)
    {
         if (A[i] < 50)
         {
               <enter code here>
         }
         else
         {
               <enter code here>
         }
    }
     
     
  11. Jul 5, 2011 #10
    Thanks , it is very clear.
    Im sorry, im not very good with computers.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Numerical Array split
  1. Confused with array (Replies: 3)

  2. Array C++ (Replies: 2)

  3. C - array (Replies: 2)

  4. Function and array (Replies: 26)

Loading...