0

I have been programming a little program in C++ with VisualStudio and while I didn't find a nice way to find an element of an array, I just made my own function to get the biggest value out of an array. But I couldn't do that either because the size() function doesn't work there.

I used size in the int main() and it works fine but in my function int biggest_num(int tmp_array[]) it wouldn't work because it says no instance of overloaded function matches the argument list. I tried do initialize the variable before but it still doesn't work.

#include <iterator> 

int biggest_num(int tmp_array[] = {}) {
    int q = 0;
    int array[] = tmp_array; // Error: C++ initialization with '{...}' expected for aggregate object
    for (int i = 0; i < size(array); i++) q = (q < array[i]) ? q : array[i]; //size(array) Error: <error-type>
    return q;
}

int main(){
    int nums[] = {23, 34, 6, 2, 3, 456, 32, 76, 24, 7, 9, 47};
    cout << biggest_num(nums) << endl;
 }

Without initialization:

#include <iterator>
int biggest_num(int tmp_array[]){
    int q = 0;
    for (int i = 0; i < size(tmp_array); i++) q = (q < tmp_array[i]) ? q : tmp_array[i]; // size() Error:  no instance of overloaded function matches the argument list 
    return q;
}

int main(){
    int nums[] = {23, 34, 6, 2, 3, 456, 32, 76, 24, 7, 9, 47};
    cout << biggest_num(nums) << endl;
 }

SOLUTION:

#include <iostream>
using namespace std;

int biggest_num(const int tmp_array[], size_t n)
{
    int q = tmp_array[0];

    for (size_t i = 1; i < n; i++)
    {
        if (q < tmp_array[i]) q = tmp_array[i];
    }

    return q;
}
int main() {
    char yn = ' ';
    int nums[] = { 1, 2, 3 };
    cout << biggest_num(nums, size(nums)) << endl;
}
RubyKanima
  • 11
  • 3

2 Answers2

0

You cannot determine the length of an array that is passed to you like that. You need to also pass the length as an argument.

Having said that, life will be much much easier if you use std::vector. (cue the 'we are not allowed to use vector' response)

pm100
  • 48,078
  • 23
  • 82
  • 145
  • I am (obviously) new to C++ sooo... you cannot pass any array or just not the length? – RubyKanima Feb 27 '22 at 22:59
  • @RubyKanima You can pass an array all you want. The usefulness, however, is questionable unless that data within contains some sort of termination logic (ex: the nullchar terminator of a native C-style string). Otherwise, you also need a magnitude provided (somehow). – WhozCraig Feb 27 '22 at 23:05
  • @RubyKanima you can pass an array but c++ cannot tell the length of an array passed as a pointer to a function. All it has is the start address and the size of the elements, it doesnt know when it ends. This is what std::vector is for, its what every experienced c++ dev will use when a dynamically sized array is needed. In you example you have a fixed size, so pass that size as an arg, or change to vector – pm100 Feb 27 '22 at 23:05
0

This function declaration

int biggest_num(int tmp_array[]){
    for (int i = 0; i < size(tmp_array); i++) int q = (q < tmp_array[i]) ? q : tmp_array[i]; // size() Error:  no instance of overloaded function matches the argument list 
    return q;
}

is adjusted by the compiler to the following declaration

int biggest_num(int *tmp_array){
    for (int i = 0; i < size(tmp_array); i++) int q = (q < tmp_array[i]) ? q : tmp_array[i]; // size() Error:  no instance of overloaded function matches the argument list 
    return q;
}

That is actually the parameter tmp_array within the function has the pointer type int *.

And the standard function std::size is not defined for pointers.

Moreover the body of the function in any case does not make a sense because the variable q is uninitialized and has the scope of the for loop outside which it is not alive.

What you need is the following

template <size_t N>
int biggest_num( const int ( &tmp_array )[N] )
{
    int q = tmp_array[0];

    for ( size_t i = 1; i < N; i++ )
    {
        if ( q < tmp_array[i] ) q = tmp_array[i];
    }

    return q;
}

Here is a demonstration program

#include <iostream>
#include <iterator>

template <size_t N>
int biggest_num( const int ( &tmp_array )[N] )
{
    int q = tmp_array[0];

    for ( size_t i = 1; i < N; i++ )
    {
        if ( q < tmp_array[i] ) q = tmp_array[i];
    }

    return q;
}

int main()
{
    int nums[] = {23, 34, 6, 2, 3, 456, 32, 76, 24, 7, 9, 47};
    std::cout << biggest_num(nums) << std::endl;
 }

Or the function can be declared the following way

int biggest_num( const int tmp_array[], size_t n )
{
    int q = tmp_array[0];

    for ( size_t i = 1; i < n; i++ )
    {
        if ( q < tmp_array[i] ) q = tmp_array[i];
    }

    return q;
}

In this case it is called like

cout << biggest_num( nums, std::size( nums ) ) << endl;

Though in this case the function will be more safer if will return a pointer to the maximum element in the array because in general the user can pass as the second argument a value of zero.

That is the function should be declared like

const int * biggest_num( const int tmp_array[], size_t n )
{
    const int *q = tmp_array;

    for ( size_t i = 1; i < n; i++ )
    {
        if ( *q < tmp_array[i] ) q = tmp_array + i;
    }

    return q;
}

and called like

cout << *biggest_num( nums, std::size( nums ) ) << endl;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I recognized the problem with the q, already edited it before ;D I tried your solution but now my whole code broke. The only part not broken is the function. – RubyKanima Feb 27 '22 at 23:17
  • @RubyKanima I do not know what is your "whole code". It seems your "whole code" has defects as your initial function definition. See teh demonstration program provided in my answer. – Vlad from Moscow Feb 27 '22 at 23:21
  • It works alone, in a new file, but it doesn't in my already written code, as it says I should use `cout;` and detects << as an error... I'll try to figure out what happened – RubyKanima Feb 27 '22 at 23:39
  • it says "identifier 'biggest_num' is undefined" at `cout << *biggestnums(nums, size(nums) << endl;` – RubyKanima Feb 27 '22 at 23:48
  • @RubyKanima For starters you need to add a closing parenthesis cout << *biggestnums(nums, size(nums) ) << endl; And the error message means that the linker does not see th function definition. It has nothing common with your question and the answer. – Vlad from Moscow Feb 27 '22 at 23:55