Array

How can I write this please!!

A function that will fill an integer array of any size with only the odd random numbers in 1-500 range.
A function that will print an integer array of any size.
A function which will search a passed in integer array of any size and use reference parameters to return the largest and smallest value in a passed in array.

This is not homework, I just want to get myself the concept of Arrays because I am teaching myself. Thank you
Last edited on
closed account (ybf3AqkS)
http://www.cplusplus.com/doc/tutorial/arrays/
Thanks, I have read it already but i was confused in these i can program the common ones but these were just confusing me.. :)
function that will fill an integar array of any size with odd random numbers 1-500 (cstdlib, ctime libraries needed):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// must pass the array to the function
void fillArrayWithOddNumbers(int arrayName(&[]))
{
    srand(time(NULL));
    const int sizeOfArray = sizeof(arrayName) / sizeof(*arrayName);
    int randomNumber = rand() % 500 + 1;
    for (int n{0}; n != sizeOfArray; n++)
    {
        do
        {
            if (randomNumber % 2 != 0)
            {
                arrayName[n] = randomNumber;
            }
            else
            {
                randomNumber = rand() % 500 + 1;
            }
        }
        while (randomNumber % 2 == 0);
    }
}


function that will print integar array of any size:

1
2
3
4
5
6
7
8
void printArray(const int arrayName(&[]))
{
    const int sizeOfArray = sizeof(arrayName) / sizeof(*arrayName);
    for (int i{0}; i != sizeOfArray; i++)
    {
        std::cout << arrayName[i] << std::endl; // newline optional
    }
}


function which will search a passed in integar array of any size and uses reference parameters to return the largest and smallest value in the passed array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#define largest 0
#define smallest 1
int largestOrSmallestValueOfArray(const int ArrayName(&[]), const unsigned short int &largestOrSmallest)
{
    const int sizeOfArray = sizeof(ArrayName) / sizeof(*ArrayName);
    int ArrayNameCopy[sizeOfArray];
    for (int n{0}; n != sizeOfArray; n++)
    {
        ArrayNameCopy[n] = ArrayName[n];
    }
    for (int NoRep{0}; NoRep != sizeOfArray; NoRep++)
    {
        for (int index{0}; index != (sizeOfArray - 1); index++)
        {
            if (ArrayNameCopy[index] > ArrayNameCopy[index + 1])
            {
                int temp = ArrayNameCopy[index];
                ArrayNameCopy[index] = ArrayNameCopy[index + 1];
                ArrayNameCopy[index + 1] = temp;
            }
        }
    }
    const int largestNumberInArray = ArrayNameCopy[sizeOfArray - 1];
    const int smallestNumberInArray = ArrayNameCopy[0];
    if (largestOrSmallest == largest)
    {
        return largestNumberInArray;
    }
    else
    {
        return smallestNumberInArray;
    }
}
Last edited on
Thanks man!! I really appreciate it.. :)
@Abhijeet010

Your welcome.

But I just want to say sorry since the above code does not compile. Because of complications of passing arrays to functions. But if you read the code carefully you should get the idea.
closed account (ybf3AqkS)
When passing an array as a parameter...
void print_array(int a[])

is exactly the same as
void print_array(int* a)

both examples are passing a pointer.

With only a pointer you can't know the size of the array. That's why you see a lot of functions that take another argument that specifies the size. The exception is character arrays that expect the array to be null terminated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <limits>
#include <random>
#include <ctime>

using namespace std;

void fill_random(int* a, const int sz)
{
    static default_random_engine re(time(nullptr));
    static uniform_int_distribution<> dist(1, 500);

    for(int i=0; i<sz; ++i)
        while((a[i] = dist(re)) % 2 == 0);
}

void print_array(int* a, const int sz)
{
    for(int i=0; i<sz; ++i)
        cout << a[i] << '\n';
}

void smallest_largest(int* a, const int sz, int& smallest, int& largest)
{
    smallest = numeric_limits<int>::max();
    largest = numeric_limits<int>::min();
   
    for(int i=0; i<sz; ++i)
    {
        if(a[i] > largest)
            largest = a[i];
            
        if(a[i] < smallest)
            smallest = a[i];
    }
}

int main()
{
    constexpr int array_size = 10;
    int a[array_size];
    fill_random(a, array_size);
    print_array(a, array_size);
    
    int smallest, largest;
    smallest_largest(a, array_size, smallest, largest);
    cout << "smallest: " << smallest << " largest: " << largest << '\n';
}
Last edited on
Topic archived. No new replies allowed.