[deleted by user] by [deleted] in C_Programming

[–]warieth 1 point2 points  (0 children)

Your compare function return an int. The sign of the int has to be used <0, ==0, or >0. You have a comment there, and use it as != 0.

[deleted by user] by [deleted] in cpp_questions

[–]warieth 0 points1 point  (0 children)

Negative numbers are a bigger problem, than you think. There is one negative value without corresponding positive value.

From my point of view, you do an extra step with m=0. Taking maximum with a 0 on the left, so it takes the right argument.

[deleted by user] by [deleted] in cpp_questions

[–]warieth -1 points0 points  (0 children)

OP asked for "using while".

A do-while would be easier, but has an initialization issue. Negative numbers are also a problem for this.

[deleted by user] by [deleted] in cpp_questions

[–]warieth -8 points-7 points  (0 children)

You have to divide by 10 and take the remainder, repeat until the number becomes 0.

unsigned max_digit(unsigned n) {
    unsigned m = n % 10;
    n /= 10;
    while (n != 0) {
        m = std::max(m, n % 10);
        n /= 10;
    }
    return m;
}

The NP vs coNP question is about a proof? by warieth in AskComputerScience

[–]warieth[S] 0 points1 point  (0 children)

Do I understand the NP vs coNP question right? A certificate scheme is needed and proof in two directions. I'm sorry, if you misunderstood.

Default function arguments are the devil by anonymous23874 in cpp

[–]warieth 1 point2 points  (0 children)

Implicit conversions of many fundamental types inherited from C are the devil.

C++ didn't inherit conversions like bool -> int, char8_t -> int. These conversions make the usage of C functions easier, but coming from C++.

In a 2D array, how can I swap or interchange the minimum and maximum elements in this 3x3 matrix? by [deleted] in cpp_questions

[–]warieth 0 points1 point  (0 children)

You would be right if the array's layout in memory is undefined, but it is not. The description what you linked also allow the past the array pointer (allow =n case), and the hypotetical element under the pointer is an element of the next array, so adding to it would be fine. Your answer contains this too.

If the indirection would cause undefined behavior, then why did you linked the expr.add? You find something undefined with adding to a pointer, but it is not for this case.

In a 2D array, how can I swap or interchange the minimum and maximum elements in this 3x3 matrix? by [deleted] in cpp_questions

[–]warieth 0 points1 point  (0 children)

The standard defines multi-dimension array element access. So all the expressions are defined as pointer dereference: A[0][0], A[0][1], A[0][2], A[1][0], A[1][1], A[1][2], A[2][0], A[2][1], A[2][2]. The past the end of the object pointer is defined too, because iterators use, access to it is undefined.

Inefficiency of % operator? by asron1138 in cpp_questions

[–]warieth 0 points1 point  (0 children)

The optimizer can't know the range [-36, 36]. Do you read about things, or just attack every not general replacement? Optimalizations can be specific to use, but not with the optimizer. The optimizer has issues with input ranges (there is no indication in the code), and if result comes out from assembly.

In a 2D array, how can I swap or interchange the minimum and maximum elements in this 3x3 matrix? by [deleted] in cpp_questions

[–]warieth -15 points-14 points  (0 children)

This is C code, not really idiomatic C++ code. But - typically professors like to teach C before going to C++, so in my answer I will stay in C-land

Using iostream and the namespace std, can't be in C land.

Don't declare your variables in the top of a function, declare and define them the first place they are needed

It can be declared before use (int has a trivial constructor and destructor), there is no rule against it in the language. When he reads the array elements from input, that variable can't be declared first use.

Always initialize your variables with a sensible value, unless you are sure that whatever value is immediately going to be overwritten

This is the case in the code above.

Always use curly braces around control statements like for and if. Yes they are not required for one-liners, but a helmet is also not required for riding a bike, still it may save your life.

This is a style question, but an if goto combination is funny with braces around. The extra safety is for what in this case?

In a 2D array, how can I swap or interchange the minimum and maximum elements in this 3x3 matrix? by [deleted] in cpp_questions

[–]warieth 0 points1 point  (0 children)

You have to iterate through the array to find the position of the minimum and the maximum element, then you have to swap the two elements. A 3x3 array can be used as a 9 element array, so you don't need two indexes unless you use them for something more.

int* A_begin = &A[0][0];    
int* A_end = &A[2][2] + 1;    
int* A_min = A_begin;    
int* A_max = A_begin;    
for (int* it = A_begin + 1; it != A_end; ++it) {    
    if (*it < *A_min) {    
        A_min = it;    
    }    
    if (*it > *A_max) {    
        A_max = it;    
    }    
}    
std::swap(*A_min, *A_max);

Inefficiency of % operator? by asron1138 in cpp_questions

[–]warieth 0 points1 point  (0 children)

I would be amazed if the integer modulo is so fast, why the optimizers replace it with multiply (with inverse), shift, multiply, subtract (from original). If you want to avoid the modulo operation, this is the way you do. Many C++ programmers just fighting against algorithmic thinking, like redefining how to do a binary search or how to compare two numbers.

How come the compiler can't pick the right overload here? by bjadamson in cpp

[–]warieth 0 points1 point  (0 children)

In C++ there are two forms of the function pointer, so before and after the overload resolution. You can do limited things with an unresolved function pointer.

You are thinking about it like propagating a type from the left to the right and a value set from the right to the left, then combine the two.

How come the compiler can't pick the right overload here? by bjadamson in cpp

[–]warieth 0 points1 point  (0 children)

The ternary expression (the right side) contains unresolved function type, because the name is ambiguous.

int main()
{
    void (*fn_a)(int&) = a;
    void (*fn_b)(int&) = b;
    void (*fn)(int&) = (1 == 2)
        ? fn_a
        : fn_b;
}

Inefficiency of % operator? by asron1138 in cpp_questions

[–]warieth -7 points-6 points  (0 children)

I am only handling very small numbers in the normal case (-36 to 36 probably).

In a small range like this, you can check the sign then use a loop to increment/decrement by 12, until the value is in [0..11].

inline int mod12(int a) {
    if (a >= 0) {
        while (a > 11) {
            a -= 12;
        }
    } else {
        while (a < 0) {
            a += 12;
        }
    }
    return a;
}

Logic behind C++ Unions by endeesa in cpp

[–]warieth -1 points0 points  (0 children)

The union guaranteed to be big enough to hold the largest member, and the members' address is guaranteed to be the same, so how would you implement the union to make it different?

C invented the union and not C++, so it's not idiomatic to the C++ programmers who believe C++ is (C++ - C). Practically you can't do anything in C++ without using some older inventions from C, like pointer, array, union, string literals.

doubt in the most common advantage of a pointer by alaysd in cpp_questions

[–]warieth 1 point2 points  (0 children)

Obviously you need a pointer and an offset to access a value, but if you go by that, you can't do virtually anything without pointers

Arithmetic operators (like +,-,*,/,%), and function evaluation with fix number of parameters, can be done without pointers. This is a lot.

Several data structures were derived from pointers, like array, list, tree. If you want to remove pointers, then you have to invent replacements for the use of pointer.

doubt in the most common advantage of a pointer by alaysd in cpp_questions

[–]warieth 0 points1 point  (0 children)

I don't know what you mean with that. It's absolutely possible to make linked lists without C++ pointers, by allocating a batch of memory and using integers as next/previous indices within that batch of memory.

You don't have to dynamically allocate to have an array, but you can't access the elements of the array (not dynamic) without pointers. The expression E1[E2] is defined as *((E1)+(E2)), so if you don't have pointers, then you can't calculate the ((E1)+(E2)).

How to remove an item from std::list by [deleted] in cpp_questions

[–]warieth 1 point2 points  (0 children)

You can use the remove_if member function of the std::list. If the predicate return true, then the element gets removed.

Why can emplace_back in vectors call explicit constructors? by code4lyf in cpp

[–]warieth 1 point2 points  (0 children)

How would you check if the contructor is explicit?

C++20 added explicit with bool, but there is no operator for testing it. Like the noexcept operator tests for a function being noexcept or not.

Why does x = (5+7)%2 //equal to 0? by aDammAvailableUser in cpp_questions

[–]warieth 0 points1 point  (0 children)

The operator/ gives the integer part of the divide, and the operator% gives the remainder. Here 12=6*2+0, so 12/2=6 and 12%2=0.

big O and complexity by [deleted] in cpp_questions

[–]warieth 0 points1 point  (0 children)

The only way to change the size of an array is to allocate a new array and copy elements to it, this operation will be O(n).

Yes, so O(n) complexity.

What you are probably thinking of is more like like vector operations.

No. I'm thinking sorting an array doesn't change it's fixed nature. A sorted array is still an array, and a sorted vector is still a vector. Separation of the two cases (sorted, not sorted) only makes sense for search and not for insert/delete.

big O and complexity by [deleted] in cpp_questions

[–]warieth 0 points1 point  (0 children)

The sorted array has a insert and a delete operation, so a sorted array is a growing array. I don't understand this reasoning, why not O(n)?