Thread: help with arrays

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    help with arrays

    this is the code i was given and told to make work but i cant figure it out at all,

    void fill(int array[], int size);

    // This function fills the given array with random integers in the range 0 to 100

    (inclusive).

    // size is the number of elements in array.

    void print(int array[], int size);

    // This function outputs the given array with one element per line. Each line of output
    must contain two // values, the array index and the corresponding element. Here is an
    example:

    // Index Element

    // 0 62

    // 1 95

    // 2 87

    // : :

    void findLargest(int array[], int size, int &max, int &maxIndex);

    // This function receives an array and its size. Then, it finds the largest element and
    returns it in variable

    // max. The function also returns the index of the largest element through maxIndex.

    void findSmallest(int array[], int size, int &min, int &minIndex);

    // This function receives an array and its size. Then, it finds the smallest element and returns it through // min. The function also returns the index of the smallest element
    through minIndex.

    int search(int array[], int size, int key);

    // This is a value-returning function. It receives an array, its size, and a key to search for. The function

    // tries to find the key in the array. If the key is found, its corresponding index is returned; if the key is

    // not found, -1 will be returned.

    double average(int array[], int size);

    // This is a value-returning function, it receives an array and its size. The function
    returns the average of

    // all array elements. The average will be a floating-point number.

    void reverse(int array[], int size); // This function reverses all elements of the given

    array. For example, if array = [10, 20, 30, 40], the // function converts it to [40, 30, 20, 10].

    void statistics(int array[], int size, int &below51, int &above50);

    // This function receives an array and its size. The function counts the number of elements that are

    // below 51 (0 through 50) and also the number of elements that are above 50 (51 through 100).

    //The function will return these counters through variables below51 and above50.
    To test your functions use the following main program.

    int main()
    {
    const int SIZE = 20;
    int array[SIZE];
    double avg; // Average of array elements
    int max, maxIndex; // Largest element
    int min, minIndex; // Smallest element
    int key, keyIndex; // Search item
    int below51, above50; // Counters for statistics

    fill(array, SIZE);

    print(array, SIZE);

    findLargest(array, SIZE, max, maxIndex);

    cout << “Largest = “ << max << “ at index “ << maxIndex << endl;

    findSmallest(array, SIZE, min, minIndex);

    cout << “Smallest = “ << min << “ at index “ << minIndex << endl;

    avg = average(array, SIZE);

    cout << “Average = “ << avg << endl;

    statistics(array, SIZE, below51, above50);

    cout << “Number of elements between 0 and 50 = “ << below51 << endl;

    cout << “Number of elements between 51 and 100 = “ << above50 << endl;

    reverse(array, SIZE);

    cout << “Array after reversing: “ << endl;

    print(array, SIZE);

    // Let’s search my array

    cout << “Enter an element to search for: “;

    cin >> key;

    keyIndex = search(array, SIZE, key);

    if (keyIndex > -1)

    cout << “Found your search element at index “ << keyIndex << endl;

    else

    cout << “Search element was NOT FOUND! “ << endl;

    return 0;
    }

    i know its a lil overwhelming

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Lemme guess, you got a popup asking you to add code tags.

    So your workaround solution was to rip out all the braces from your code and the error went away.

    FAIL!

    Now post your ACTUAL code unmodified, and figure out how to use the code tags.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM