#Using arrays inside of function

57 messages · Page 1 of 1 (latest)

frigid timber
#

Im trying to like append the correct values inside of temperatures_cval but i dont get the result i would expect? dont know how to fix it tried also const int*


#include <stdio.h>


constexpr int no_value = -999;
constexpr int temperatures[]{ 10, 12, no_value, no_value, 20, 14, 6, -1, -5, 0, no_value, 1, -3 };

constexpr int size = sizeof(temperatures) / sizeof(int);
int temperatures_cval[size];


void without_no_value(const int array[], int size)
{
    int temp = temperatures[0];
    if (temperatures[0] == no_value)
    {
        temp = 0;
    }
    
    for (int i = 0; (i = size); i++)
    {
        if (temperatures[i] == no_value)
        {
            temperatures_cval[i] = temp;
            
        }else
        {
            temperatures_cval[i] = temperatures[i];
            temp = temperatures[i];
        }
    }
}

int main(){
    without_no_value(temperatures, size);
    for (int i = 0; (i = size); i++){
        printf("%i", temperatures_cval[i]);
    }
}
faint tideBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

fallow atlas
#

What's up with that condition in for loop i=size

keen spruce
#

(i = size) is assignment, not comparison

#

the loop will be infinite if size != 0

frigid timber
#

so it goes from 0 up to the size of the list array

#

oh yes i changed it to <

#

and it works

#

great thanks

keen spruce
#

(◠‿◠✿)

frigid timber
#

i also deleted the const int array[] from the function

#

becuase it wasnt needed is there something more i can do to make it more elegant

keen spruce
#

btw

constexpr int size = sizeof(temperatures) / sizeof(int);
int temperatures_cval[size];

can be written as

int temperatures_cval[std::size(temperatures)];

but you'd have to include <array>

#

if you need to access the size, may as well use std::array

#

and

for (int i = 0; (i = size); i++)

can simply be

for (int temp : temperatures)
#

C++ makes a lot of things easier

faint tideBOT
frigid timber
#

@keen spruce i dont really know if i can use all the things that cpp can provide.. as im learning C/CPP to program arduino for my uni course

#

and i think it is done with C but sometimes using some stuff from cpp

keen spruce
#

well, even if you can't use the C++ standard library, you can at least use range-based for loops

#

and writing your own std::size is trivial too, and might come in handy

frigid timber
keen spruce
#

yes

frigid timber
#

@keen spruce is it better to have my find_min function like this or i should have a int minimum variable like i have temperaturs_cval

#include <stdio.h>

constexpr int no_value = -999;
constexpr int temperatures[]{ 10, 12, no_value, no_value, 20, 14, 6, -1, -5, 0, no_value, 1, -3 };

constexpr int size = sizeof(temperatures) / sizeof(int);
int temperatures_cval[size];


void without_no_value(int size)
{
    int temp = temperatures[0];
    if (temperatures[0] == no_value)
    {
        temp = 0;
    }
    
    for (int i = 0; (i != size); i++)
    {
        if (temperatures[i] == no_value)
        {
            temperatures_cval[i] = temp;
            
        }else
        {
            temperatures_cval[i] = temperatures[i];
            temp = temperatures[i];
        }
    }
}

int find_min(int array[])
{
    int minimum = array[0];
    
    for (int i = 0; i < size; i++)
    {
        if (array[i] < minimum)
        {
            minimum = array[i];
        }
    }
    return minimum;
}

int main()
{
    without_no_value(size);
    for (int temp : temperatures_cval)
    {
        printf("%i\n", temp);
    }
    
    printf("%i", find_min(temperatures_cval));
}
keen spruce
#

it should be its own function, but it's very weird that the function isn't pure, but uses a global size

#
int find_min(const int array[], size_t size)
{
    if (size == 0) {
        // TODO: handle empty arrays
    }
    int minimum = array[0];
   
    for (size_t i = 1; i < size; i++)
    {
        if (array[i] < minimum)
        {
            minimum = array[i];
        }
    }
    return minimum;
}
#

it could be written like this

#

and then it's going to work for arrays of any size

frigid timber
#

why is there size_t size?

#

when you are not using it in the function

keen spruce
#

wdym, it's used in size == 0 and i < size

frigid timber
#

yeah i didnt see that mb

faint tideBOT
#

@frigid timber Has your question been resolved? If so, run !solved :)

spark lily
#

;compile

int a[] = {1, 2, 3};
for (int x : a)
    std::cout << x;
fathom cloakBOT
#
Program Output
123
fallow atlas
#

Why do they work though ;o

#

If they decay to pointer it wouldn't work anymore ?

spark lily
#

Correct, but then it's not a c array, it is a pointer

#

It works because there is an overload of std::begin for c arrays

fallow atlas
#

Ah interesting

keen spruce
#

in that context, they're implicitly a pointer

fallow atlas
#

So when I'm making my own container , I can just use template specialisation for std begin and end ?

#

Instead of making member functions

keen spruce
#

specializing anything in the standard library is undefined behaviour unless explicitly allowed

#

if you want to be able to loop over your container, you just need to provide begin() and end() (member) functions

#

but don't specialize std::begin

fallow atlas
#

Right I think my prof showed specialisation of std::hash for user defined class ,is that possible or I got STH wrong

keen spruce
#

yeah, that's because std::hash explicitly allows this

#

it's meant to be used that way

fallow atlas
#

From cppreference
Each specialization of this template is either enabled ("untainted") or disabled ("poisoned")

#

Does this mean it can be specialised ?

#

I don't know where it says it's allowed

spark lily
#

Bonus points
std::ranges::begin will also look for begin function through argument dependent lookup.
So you can define a begin function wothout specializing std::begin

faint tideBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.