Posts by Lacobus • 13,510 points
596 posts
-
1
votes2
answers329
viewsA: Size of a dynamic array
The idiomatic sizeof(teste) / sizeof(int) is only able to calculate the size in bytes of statically allocated buffers: int teste[ 123 ]; printf("%d\n", sizeof(teste) / sizeof(int) ); /* 492 / 4 =…
-
2
votes1
answer54
viewsA: "Compress" values from an array of shorts, to an array of integers
You can do something like: short a[ ARRAY_MAX_TAM * 2 ]; int b[ ARRAY_MAX_TAM ]; for( i = 0; i < ARRAY_MAX_TAM * 2; i++ ) ((short*)b)[i] = a[i]; Follow a tested code that can solve your problem:…
-
1
votes1
answer69
viewsA: Is there a difference between "new" and "reinterpret_cast" for a memory buffer?
Basically, the difference is in the object constructor call T, which does not happen in the case of reinterpret_cast. When you use the reserved word new to initialize an object T in a buffer…
-
0
votes2
answers2142
viewsA: Create list with column contents
Maybe this will help: projetos_eleitos = [] for i in autor1: projetos_eleitos.append( i['valor_projeto'] )
-
2
votes1
answer309
viewsA: Return of python file read function
How about: arquivos = ['a.txt', 'b.txt', 'c.txt'] lista_geral = [] for nome in arquivos: with open( nome, "r") as arq: a = [] for linha in arq: a.append(linha.strip()) lista_geral.append(a) print(…
-
1
votes2
answers307
viewsA: C Cast vs C++ Cast
The static_cast of the pattern C++ is more restrictive and only allows conversions between compatible types. This compatibility is validated at build time: char c = 123; int * p =…
-
3
votes2
answers2331
viewsA: Compare Latitude and Longitude in Python
You can use an object handling library on the Cartesian plane called Shapely: from shapely.geometry import Point from shapely.geometry.polygon import Polygon def area_contem_cliente( a, c ): ponto =…
-
0
votes2
answers2331
viewsA: Compare Latitude and Longitude in Python
I suggest replacing the polygonal area of 8 vertices with a circle. You can use the Haversine’s formula to calculate the distance between two geographical coordinates: Once the distance between the…
-
4
votes2
answers893
viewsA: How to remove part of a JSON file
json2csv.py: import csv import json # Abre arquivo de entrada para leitura with open("entrada.json", "r") as infile: # Carrega arquivo JSON de entrada data = json.load( infile ) # Abre arquivo de…
-
1
votes1
answer133
viewsA: C++: Addition of ". cpp" implementations in Visual Studio and GCC
The error message is not a build error. It is a linkage: /tmp/ccQVvF4r.o: In function 'main': main.cpp:7: undefined reference to 'Subsistema::Mt::soma(int, int)' collect2: error: ld returned 1 exit…
-
2
votes1
answer368
viewsA: Checklist and Dialog with external file - Shell Script
contasemail.txt: [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] checklist.sh: #!/bin/bash filename="$1" tags=() while read -r tag; do…
-
0
votes1
answer63
viewsA: Difference of calling a function in Dllmain by Createthread or calling directly
It’s bad practice to make calls from API within the DllMain()! The function CreateThread() is one of the only functions that can be called within the DllMain(), however, this should be done very…
-
2
votes1
answer176
viewsA: Splitting string in C
Here is an example capable of converting a string containing space-separated data and aramazena it into a vector: #include <stdio.h> #include <stdlib.h> #include <string.h> char **…
-
2
votes2
answers255
viewsA: Possibility challenge in c,c++ or Matlab
Despite being a computationally inefficient technique, equality (xa * xb) / (ya * yb) = 1700 / 57 can be verified by força bruta. Are approximately 8 Bilhões of possibilities that need to be tested:…
-
2
votes1
answer5984
viewsA: Convert C code to Assembly MIPS?
If you are on a machine with architecture MIPS native, you can use the compiler GCC passing the argument -S so that it manages the code in assembly equivalent to the program written in C, for…
-
0
votes1
answer244
viewsA: Error in convert_from() | SQL_ASCII
You need to identify the encoding of all its inputs and outputs to avoid confusion and not lose the original data along the way. First, you need to identify the encoding in which your file .txt was…
-
1
votes2
answers1349
viewsA: Query to list number of occurrences
Assuming a structure similar to yours: CREATE TABLE IDENTIFICACAO_PESSOA ( PESSOA_FK BIGINT, CATEGORIA_IDENTIFICACAO_FK character varying(1) ); With the following test data: INSERT INTO…
-
1
votes2
answers1591
viewsA: Show the runtime of an algorithm in milliseconds in python?
You can more accurately calculate the running time of a particular operation by sampling. You repeat the same operation by N times and calculates the total time spent, with this, you are able to…
-
3
votes3
answers196
viewsA: Query listing the dates
You can generate a series of dates through the function generate_series(), and then filter only the dates that fall on Sunday using the clause WHERE. Since the version 8.4 of Postgresql, the…
-
2
votes1
answer382
views -
1
votes1
answer256
viewsA: Error in running order of pthreads
You are right: A ordem de criação and the ordem de execução threads are not the same! And this is not a mistake, but the natural behavior of pthreads. It’s not good practice to keep making…
-
2
votes1
answer315
viewsA: How to include header and cpp without resulting in LNK2005 error in Visual Studio
Avoid #include's of files that are not headers, this creates a "mess" without size! It makes no sense to use #pragma once/#ifndef-#define in archives .cpp; Do not mix #pragma once and…
-
0
votes1
answer77
viewsA: exercise on pointers
Solution #1: void power_ref( int * x, int y ) { int i = 0; int n = 1; for ( i = 0; i < y; i++ ) n *= (*x); *x = n; } Solution #2: #include <math.h> void power_ref( int * x, int y ) { *x =…
-
1
votes1
answer1171
viewsA: Save image to POSTGRES database with DELPHI Tokyo by Android App
You can use the function pg_read_binary_file() to read binary files stored in data_directory postgres. Consider the table: CREATE TABLE tb_imagem ( id BIGINT, nome TEXT, imagem bytea ); Images can…
-
5
votes3
answers211
viewsA: How to take data from an object and do mathematical operation with another object in C++?
To calculate the distance between two points in a Cartesian plane the following formula is applied: Based on the example of your question, follow a possible solution to your problem: #include…
-
2
votes1
answer352
viewsA: How to implement a copy constructor for a two-dimensional matrix in C++?
Follows a tested and improved solution able to take advantage of all the power of the overload of C operators++: Matriz.h #ifndef MATRIZ_H #define MATRIZ_H #include <iostream> class Matriz {…
-
2
votes2
answers1562
viewsA: Import JSON to Database
Starting with version 9.2 of Postgressql the type was introduced JSON, that supports data storage and manipulation in JSON format. You can use the function pg_read_file() to read text files stored…
-
0
votes2
answers1368
viewsA: Error: "cannot Convert 'int*' to 'int**"
How about: #include <iostream> using namespace std; void somadiferenca( int vetor[2] ) { int x = vetor[0] + vetor[1]; int y = vetor[0] - vetor[1]; if( y < 0 ) y = -y; cout << "Soma: "…
-
3
votes2
answers169
viewsA: How to read data from a file in c and treat as a string?
You can use the functions fread() and realloc() within a loop. Every iteration of the loop, fread() is responsible for reading a small block of the input file, while realloc() is responsible for…
-
2
votes1
answer1168
viewsA: Function with vectors in C
Here is an (tested) example of how to calculate the quantidade de elementos contained in a vector that are acima da média aritmética of that same vector. For greater readability, clarity and…
-
5
votes2
answers1681
viewsA: Suitable type to use with CPF is numerical or character?
I understand that a CPF is not a numerical type, but a string of digits (There are Cpfs that start with zeros to the left), so a better abstraction of the data would be the use of a type character…
-
1
votes2
answers68
viewsA: IF always gives me the same answer!
How about: #include <iostream> #include <string> using namespace std; int main(){ string x; cout << "Digite 'bebida' " << endl; cin >> x; if( x == "bebida" ) {…
-
2
votes1
answer108
viewsA: Read array of a file diagonally
Follows a program capable of reading words arranged horizontally, vertically and diagonally in a word search contained in a text file. Words can be accessed through their coordinates: #include…
-
1
votes1
answer1009
viewsA: Bulk encoding of files on linux
How about: #!/bin/bash DIR=$1 if [ ! -d "${DIR}" ]; then echo -e "Informe um diretorio\nEx:\n${0} <diretorio>" exit 1; fi for ARQ in $( find ${DIR} -name '*.txt' -type f ); do CONVERT=$( file…
-
0
votes2
answers85
viewsA: error: use of Deleted Function Fight::Fight()'
To be possible the passage of the object Lutador the correct implementation of the "Rule of Three". Wikipedia: An informal rule in object-oriented development in C++ calls out that if a class or…
-
2
votes2
answers928
viewsA: Segmentation failure (recorded core image)
Like the pointer p->c points to an undefined place when trying to record the data v in position p->topo, your program accessed a "prohibited" memory position and received a "Segmentation…
-
3
votes3
answers701
viewsA: How to take the last four months given given date
Suggestion JavaScript: function foobar( dt ) { var array = []; var d = new Date( dt ); for( var i = 0; i < 4; i++ ) { var m = d.getMonth(); d.setMonth( m - 1 ); if( d.getMonth() != m - 1…
-
0
votes2
answers248
viewsA: What is the best method to access a class member?
class Foobar { public: Foobar( void ) {}; virtual ~Foobar( void ) {}; void xpto( int n ) {}; static void xyz( int n ) {}; }; int…
-
2
votes1
answer395
viewsA: How to vector code in C++?
The problem is that the parole if contained within the second loop does not allow it to be optimized by the compiler: for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ d[i][j] = (c[i][j]+e[i][j])/2;…
-
0
votes3
answers827
viewsA: Create serial number generator button
HTML/Javascript Solution: <html> <head> <script> function gerar_string_aleatoria( tam, charset ) { var serial = ""; for( var i = 0; i < tam; i++ ) serial += charset.charAt(…
-
2
votes3
answers80
viewsA: Deleting parts of a Text
String Manipulation (Bash): #!/bin/bash txt='/home/meucliente/public_html' aux=${txt%/*} aux=${aux##*/} echo $aux Regular Expressions (Bash): #!/bin/bash txt='/home/meucliente/public_html' [[ $txt…
-
1
votes2
answers229
viewsA: Dynamic SQL result sorting
You can "mount" your query using a StringBuilder: public String montarQueryConsulta( int ordem ) { StringBuilder sb = new StringBuilder(); sb.append("SELECT c.idade, c.nome, c.cpf FROM tb_cadastro…
-
0
votes2
answers118
viewsA: Incorrect value in subtraction of binary numbers using strings
In C++98 you can solve this problem as follows: #include <iostream> #include <bitset> #include <string> using namespace std; string bin( unsigned int n ) { string bin; while( n ) {…
-
0
votes4
answers123
viewsA: Is there a smaller way to resolve the question below? and know if it is correct
Solution in C: #include <stdlib.h> #include <stdio.h> #include <string.h> int qsort_crescent( const void * a, const void * b ) { return *((int*)a) - *((int*)b); } int…
-
2
votes4
answers123
viewsA: Is there a smaller way to resolve the question below? and know if it is correct
In C++98 you can do something like: #include <iostream> #include <algorithm> #include <vector> using namespace std; int nums[] = { 8, 2, 7, 3, 6 }; void exibir( vector<int>…
-
0
votes2
answers103
viewsA: Problems in Reading File
It follows a implementation of a program capable of reading and processing a file line by line CSV with the format you specified: #include <stdio.h> #include <stdlib.h> #include…
-
1
votes2
answers590
viewsA: Is it possible to develop a C++ application in Visual Studio to run on Linux?
Even with the ease of compilation multiplatform offered by Visual Studio 2017, still yes it would be necessary a specific implementation for each operating system. This happens because the operating…
-
1
votes1
answer938
viewsA: How to create a dynamic matrix using chained list in C language?
Follow an implementation capable of solving your problem: #include <stdio.h> #include <stdlib.h> typedef struct elemento_s elemento_t; typedef struct matriz_s matriz_t; struct elemento_s…
-
1
votes1
answer620
viewsA: Operator overload in C++
Here is a functional example based on your code illustrating how to implement the multiplication operator operator* and the shift operators operator<< and operator>>: #include…
-
0
votes2
answers1092
viewsA: Separating small strings from a giant string
I suggest you use the function strtok() to extract the tokens contained in the line and the functions malloc(), realloc(), strdup() and free() to allocate a completely dynamic string list. Here is…