#getting rid of globals

97 messages · Page 1 of 1 (latest)

tame pewter
#

My old code

#include <stdio.h>
#include "temps.h"

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

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];
        }
    }
}

void find_min(const int array[], int size)
{
    for (int i = 1; i < size; i++)
    {
        if (array[i] < minimum)
        {
            minimum = array[i];
        }
    }
}

void press(int n, char c)
{
    for (int i = 0; i < n; ++i)
        printf("%c", c);
}


void draw_graph(const int array[])
{
    for (int i = 0; i < size; i++)
    {
        if (array[i] < 0)
        {
            int space_count = array[i] - minimum;
            press(space_count, ' ');
            press(array[i] * -1,'*');
            printf("%c\n", '|');
        }else
        {
            press(minimum * -1, ' ');
            printf("%c", '|');
            press(array[i], '*');
            printf("\n");
        }
        
    }
}

int main()
{
    without_no_value(size);
    find_min(temperatures_cval, size);
    draw_graph(temperatures_cval);
    return 0;
}

trying to make it better

sharp pineBOT
#

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.

tame pewter
#
#include <stdio.h>
#include "temps.h"

constexpr int size = sizeof(temperatures) / sizeof(int);


int* without_no_value(int size)
{
    int temperatures_cval[size];
    int temp = temperatures[0] == no_value ? 0 : temperatures[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];
        }

    }
    return temperatures_cval;
}

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

void press(int n, char c)
{
    for (int i = 0; i < n; ++i)
        printf("%c", c);
}


void draw_graph(const int array[])
{
    for (int i = 0; i < size; i++)
    {
        if (array[i] < 0)
        {
            int space_count = array[i] - find_min(without_no_value(),size);
            press(space_count, ' ');
            press(array[i] * -1,'*');
            printf("%c\n", '|');
        }else
        {
            press(find_min(temperatures_cval,size) * -1, ' ');
            printf("%c", '|');
            press(array[i], '*');
            printf("\n");
        }

    }
}

int main()
{
    without_no_value(size);
    find_min(temperatures_cval, size);
    draw_graph(temperatures_cval);
    return 0;
}
#

#ifndef RECODEX_CEPLOMERIS_TEMPS_H
#define RECODEX_CEPLOMERIS_TEMPS_H

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

#endif
#

i want to get rid of minimum global

#

which i did and i think correctly

#

and now the global list that im creating

#

well these two i want to get rid of

#

minimum i got

#

now the temperatures_cval

#

ok so

#

if you look at the old code

#

i got these

int temperatures_cval[size];
int minimum;
#

so in the updated version i got rid of the int minumum

#

now im trying to get rid of int temperatures_cval[size];

#

so i dont have these two globals

#

i dont want to use pointers

#
int* without_no_value()
{
    int temperatures_cval[size];
    int temp = temperatures[0] == no_value ? 0 : temperatures[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];
        }

    }
    return temperatures_cval;
}
#

can this work

surreal slate
#

int temperatures_cval[size]; is a VLA presumably

sharp pineBOT
#
What Is a VLA And Why Is It 'Bad'?

A Variable Length Array (VLA) is an array where the size is not constant and depends on a variable.
VLAs have poor compiler support and can lead to inefficient code. The core issue with VLAs is that the compiler doesn't know the size of the stack frame. Without warning flags like -Wvla (Note: -Wvla is not turned on by -Wall nor any other warning flag) it can be easy to create a VLA by accident, even in C++ with some compilers.

Compiler Support

✅ available since C99 ⛔ not available in C++ at all ⛔ was never supported by MSVC ⚠ optional feature since C11 ⚠ supported as non-standard extension by GCC, clang

surreal slate
#

so no

#

and returning a pointer to it wouldn't work either

#

can't you just use std::vector

tame pewter
#

i dont want to use other stuff i just want to make the temperatures_cval not global with what i have

#

thats all

surreal slate
#
int remove_val(int data[], int size, int val)
{
    return std::remove(data, data + size, val) - data;
}

that's what you would normally do, in fact you don't need the function if you just call std::remove

tame pewter
#
#include <stdio.h>
#include "temps.h"

constexpr int size = sizeof(temperatures) / sizeof(int);


int* without_no_value(int size)
{
    int temperatures_cval[size];
    int temp = temperatures[0] == no_value ? 0 : temperatures[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];
        }

    }
    return temperatures_cval;
}

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

void press(int n, char c)
{
    for (int i = 0; i < n; ++i)
        printf("%c", c);
}


void draw_graph(const int array[])
{
    for (int i = 0; i < size; i++)
    {
        if (array[i] < 0)
        {
            int space_count = array[i] - find_min(without_no_value(size),size);
            press(space_count, ' ');
            press(array[i] * -1,'*');
            printf("%c\n", '|');
        }else
        {
            press(find_min(without_no_value(size),size) * -1, ' ');
            printf("%c", '|');
            press(array[i], '*');
            printf("\n");
        }

    }
}

int main()
{
    find_min(without_no_value(size), size);
    draw_graph(without_no_value(size));
    return 0;
}

#

can i do it somewhat like this

surreal slate
#

no

tame pewter
#

but i need to use this for an assignment i wrote it like before but i was intructed to remove the global variables

#

so thats what im trying here to do

#

and i should write it in C mostly...

surreal slate
#

if you want to filter out array elements, you'll inevitably have to write up something like std::remove or std::copy_if

#

so it's worth looking into how these things work

#

or you could just filter these elements out while processing the data but keep them in the array

tame pewter
#

so i need to make a new one to change it up

plucky yarrow
tame pewter
#

that should be global

#
#include <stdio.h>
#include "temps.h"

constexpr int size = sizeof(temperatures) / sizeof(int);


int* without_no_value(int size)
{
    int temperatures_cval[size];
    int temp = temperatures[0] == no_value ? 0 : temperatures[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];
        }

    }
    return temperatures_cval;
}

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

void press(int n, char c)
{
    for (int i = 0; i < n; ++i)
        printf("%c", c);
}


void draw_graph(const int array[])
{
    for (int i = 0; i < size; i++)
    {
        if (array[i] < 0)
        {
            int space_count = array[i] - find_min(without_no_value(size),size);
            press(space_count, ' ');
            press(array[i] * -1,'*');
            printf("%c\n", '|');
        }else
        {
            press(find_min(without_no_value(size),size) * -1, ' ');
            printf("%c", '|');
            press(array[i], '*');
            printf("\n");
        }

    }
}

int main()
{
    find_min(without_no_value(size), size);
    draw_graph(without_no_value(size));
    return 0;
}

#

i dont know i tried this but its all wrong

#

error: use of undeclared identifier 'size'
draw_graph(without_no_value(size));

#

dont i have size at the top as a constant

plucky yarrow
#
#include <stdio.h>
#include "temps.h"

- constexpr int size = sizeof(temperatures) / sizeof(int);
- int temperatures_cval[size];
- int minimum;
+ constexpr int array_size(int array[]) {
+    return sizeof(array) / sizeof(int);
+}
- void without_no_value(int size)
+ int[] without_no_value()
{
+   const int size = array_size(temperatures);
+   int temperatures_cval[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];
        }
    }
}

- void find_min(const int array[], int size)
+ int find_min(const int array[])
{
+   const int size = array_size(array);
+   int minimum = 2000000000;
    for (int i = 1; i < size; i++)
    {
        if (array[i] < minimum)
        {
            minimum = array[i];
        }
    }
+   return minimum;
}

void press(int n, char c)
{
    for (int i = 0; i < n; ++i)
        printf("%c", c);
}


void draw_graph(const int array[])
{
+   const int size = array_size(array);
+   const int minimum = find_min(array);
    for (int i = 0; i < size; i++)
    {
        if (array[i] < 0)
        {
            int space_count = array[i] - minimum;
            press(space_count, ' ');
            press(array[i] * -1,'*');
            printf("%c\n", '|');
        }else
        {
            press(minimum * -1, ' ');
            printf("%c", '|');
            press(array[i], '*');
            printf("\n");
        }
        
    }
}

int main()
{
+   int temperatures_cval[size] =  without_no_value();
-   without_no_value(size);
-   find_min(temperatures_cval, size);
    draw_graph(temperatures_cval);
    return 0;
}
#

probably a more structured code

tame pewter
#

it does not work

#

tho

plucky yarrow
#

Compile error or?

plucky yarrow
tame pewter
#

probably my fault

#

but look cant i just edit mine

#

a bit that it would work

plucky yarrow
#

Alright so

#
  1. why are you using C in a cpp file?
#
  1. if your project is C++, why are you not using a std::array or std::vector
tame pewter
#

its my first code in C/CPP so i dont know much but i had an assigment that i need to write in C with some cpp stuff like contexpr and thats probably it

#

not much

#

other then that i use C

#

the first code i posted was my original code it was ok but i have to re do it

plucky yarrow
#

If the code is C, why is the extension .cpp?

#

Are you trying to convert your old C code into C++?

tame pewter
#

no

#

look im using cpp only for like constexpr

#

thats all

#

i want the code to look like the upmost one but i have to re do

plucky yarrow
#

Well

tame pewter
#

the two globals

#

minimum and temperatures_cval

#

for it to be used in a function and not as global

#

i think the minimum works fine now i have to get rid of temperatures_cval

#

which is a pain for me

plucky yarrow
#
#include <stdio.h>
#include "temps.h"

- constexpr int size = sizeof(temperatures) / sizeof(int);
- int temperatures_cval[size];
- int minimum;
+ constexpr int array_size(int array[]) {
+    return sizeof(array) / sizeof(int);
+}

- int[] without_no_value()
+ int* without_no_value()
{
+   const int size = array_size(temperatures);
+   int temperatures_cval[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];
        }
    }
}

- void find_min(const int array[], int size)
+ int find_min(const int array[])
{
+   const int size = array_size(array);
+   int minimum = 2000000000;
    for (int i = 1; i < size; i++)
    {
        if (array[i] < minimum)
        {
            minimum = array[i];
        }
    }
+   return minimum;
}

void press(int n, char c)
{
    for (int i = 0; i < n; ++i)
        printf("%c", c);
}


void draw_graph(const int array[])
{
+   const int size = array_size(array);
+   const int minimum = find_min(array);
    for (int i = 0; i < size; i++)
    {
        if (array[i] < 0)
        {
            int space_count = array[i] - minimum;
            press(space_count, ' ');
            press(array[i] * -1,'*');
            printf("%c\n", '|');
        }else
        {
            press(minimum * -1, ' ');
            printf("%c", '|');
            press(array[i], '*');
            printf("\n");
        }
        
    }
}

int main()
{
+   int temperatures_cval[size] =  without_no_value();
-   without_no_value(size);
-   find_min(temperatures_cval, size);
    draw_graph(temperatures_cval);
    return 0;
}
plucky yarrow
tame pewter
#
#include <stdio.h>
#include "temps.h"

constexpr int array_size(int array[]) {
    return sizeof(array) / sizeof(int);
    }

int* without_no_value()
{
    const int size = array_size(temperatures);
    int temperatures_cval[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(const int array[])
{
    const int size = array_size(array);
    int minimum = 2000000000;
    for (int i = 1; i < size; i++)
    {
        if (array[i] < minimum)
        {
            minimum = array[i];
        }
    }
    return minimum;
}

void press(int n, char c)
{
    for (int i = 0; i < n; ++i)
        printf("%c", c);
}


void draw_graph(const int array[])
{
    const int size = array_size(array);
    const int minimum = find_min(array);
    for (int i = 0; i < size; i++)
    {
        if (array[i] < 0)
        {
            int space_count = array[i] - minimum;
            press(space_count, ' ');
            press(array[i] * -1,'*');
            printf("%c\n", '|');
        }else
        {
            press(minimum * -1, ' ');
            printf("%c", '|');
            press(array[i], '*');
            printf("\n");
        }

    }
}

int main()
{
    int temperatures_cval[size] =  without_no_value();
    draw_graph(temperatures_cval);
    return 0;
}}
#

this is the code right?

#

that you have posted

plucky yarrow
#

should be yeah

tame pewter
#

well it still doesnt work

#

dont know why do i need this


constexpr int array_size(int array[]) {
    return sizeof(array) / sizeof(int);
    }
plucky yarrow
#
#include <stdio.h>
#include "temps.h"

constexpr int array_size(int array[]) {
    return sizeof(array) / sizeof(int);
}

int* without_no_value()
{
    const int size = array_size(temperatures);
    int temperatures_cval[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(const int array[])
{
    const int size = array_size(array);
-   int minimum = 2000000000;
+   int minimum = array[0];
    for (int i = 1; i < size; i++)
    {
        if (array[i] < minimum)
        {
            minimum = array[i];
        }
    }
    return minimum;
}

void press(int n, char c)
{
    for (int i = 0; i < n; ++i)
        printf("%c", c);
}


void draw_graph(const int array[])
{
    const int size = array_size(array);
    const int minimum = find_min(array);
    for (int i = 0; i < size; i++)
    {
        if (array[i] < 0)
        {
            int space_count = array[i] - minimum;
            press(space_count, ' ');
            press(array[i] * -1,'*');
            printf("%c\n", '|');
        }else
        {
            press(minimum * -1, ' ');
            printf("%c", '|');
            press(array[i], '*');
            printf("\n");
        }

    }
}

int main()
{
+   int size = array_size(temperatures);
    int temperatures_cval[size] =  without_no_value();
    draw_graph(temperatures_cval);
    return 0;
}}
tame pewter
#

why isnt this fine? ```cpp
constexpr int size = sizeof(temperatures) / sizeof(int);

#

when i know i will always have temperatures there

plucky yarrow
#

You can, but you shouldn't

tame pewter
#

only for the minimum and the list

#

temeperatures_cvla

plucky yarrow
#

You should only use global variables when you're dealing with magic numbers in a project-wide scope

#

or similar usages

tame pewter
#

thansk for help i do appriciate but i think i will try to do it somehow my way i think i dont need to change it up that much just for it to get rid of that global list

tame pewter
#

!close

sharp pineBOT
#

Thank you and let us know if you have any more questions!

tame pewter
#

!close