Posts by pmg • 6,456 points
254 posts
-
2
votes1
answer183
views -
0
votes1
answer34
viewsA: How do I enter with several different files
file = fopen("arquivo", "r" ); This instruction of your program will open the name file "arquivo"; will not open the file with the name that the user entered and that is contained in the variable…
-
1
votes1
answer35
viewsA: Problem with the final results
In alocamatriz() the cycle shall be a and not to q char **alocamatriz(int q, int a) { int i; char **matriz; matriz = malloc(a * sizeof *matriz); for(i = 0; i < a ; i++) // ^ a elementos {…
-
5
votes1
answer61
viewsA: FORTRAN and C interoperability
It is possible to "mix" C and Fortran, but not in the same file, either directly or through #include. See, for example, in http://www.cae.tntech.edu/help/programming/mixed_languages The basic idea…
-
1
votes1
answer40
viewsA: Problem with the results
DCT += vetor[i] * cos((PI/N * (j + 0.5)) * i); To formula is poorly implemented. DCT += vetor[j] * cos((PI/N * (j + 0.5)) * i); // ^…
-
1
votes3
answers678
viewsA: data structure problem
main. c #include <locale.h> #include "menu.h" //Programa principal int main() { setlocale(LC_CTYPE, "Portuguese"); // 1 _Aeroporto* cabAeroporto; // 2…
-
1
votes3
answers678
viewsA: data structure problem
The cabAeroporto defined in main. c is a local variable, which belongs to the function only main(). The function menu() defined in menu.c does not know the local variables of other functions. To…
-
2
votes4
answers316
viewsA: "Pure" structs (no pointer) and C error handling
If you cannot change the function to return a pointer and return NULL in case of error, I suggest an element with special value const struct registro especial_erro = {-1, ...}; struct registro…
-
3
votes1
answer132
viewsA: Char count in C
The instruction *ponteiro++ (*(ponteiro++)) increases the pointer and returns the existing value before increment. To increase the pointed value uses (*ponteiro)++ (the value returned is also the…
-
0
votes2
answers1917
viewsA: How to Show all elements of an array declared in a C struct?
To print all elements at once, with a single simple instruction, use 1 function! void cadastro_print(struct cadastro *p) { printf("nome: %s\n", p->nome); printf("idade: %d\n", p->idade);…
-
1
votes3
answers1681
viewsA: Find day of the week of the first day of January from the date of Easter
How about we skip Easter and use the C library? #include <stdio.h> #include <time.h> int main(void) { const char *ds[] = {"Domingo", "Segunda", "Terca", "Quarta", "Quinta", "Sexta",…
-
1
votes2
answers157
views -
1
votes3
answers1681
viewsA: Find day of the week of the first day of January from the date of Easter
The sums must be subtract. If from an Easter Sunday you want to "walk backwards", for the 1st of January ... subtract! if(mes_pascoa == 3) dia_semana = (dia_pascoa - bissexto - 28 - 31) % 7; else…
-
1
votes2
answers390
viewsA: Use of EOF in vectors
Some notes: while (scanf(%d), &sequencia[i] != EOF) quotation mark error and parenthesis sequencia = NULL; /* ... */ &sequencia[i] You’re getting ahead of yourself use of i without assigning…
-
2
votes1
answer134
viewsA: Main for function on linked lists
To add an item at the end of a linked list you need to scroll through the entire list. Adding an element at the beginning of a linked list does not require anything special. To convert an array…
-
1
votes3
answers264
viewsA: Pointer return error in function
char *resultado[30]; static char *str[30]; resultado and str are arrays of 30 pointers! None of the 30 pointers of any of the arrays points to a valid place! When you use pointers you must always…
-
3
votes1
answer86
viewsA: I can’t call a string
Your mistake is in scanf(). scanf("%s", &pesquisa); The correct form is scanf("%s", pesquisa); // ^ sem & You can still limit the possibility of buffer overflow if you enter the maximum…
-
0
votes2
answers183
viewsA: How do I insert complex numbers into C++?
In C (it’s not in the question, but it’s in a tag) I do so #include <complex.h> #include <stdio.h> int main(void) { complex double x, y, z; x = 3.14159 - 2*_Complex_I; y = 2.71828182 -…
-
0
votes2
answers346
viewsA: save a data type in a file in c
aux is a pointer. fwrite(&aux, sizeof(SuperDume) * CONJUNTOSUPERDUME,1,f); // | ^^^^^^^^^^^^^^^^^ tamanho dum ponteiro // \--> endereço dum ponteiro Try if (fwrite(aux, sizeof *aux,…
-
1
votes2
answers346
viewsA: save a data type in a file in c
I can’t find the description of fopen_s in the C Standard; you must be using a Non-standard compiler :-) To description of fopen_s provided by Microsoft and the way you use the value returned by the…
-
2
votes3
answers5893
viewsA: What is operator overload?
Normal operators are also overcharged in C. For example, the operator + serves to add integers (42 + 12), to add floating comma numbers (0.5 + 3.14159), to "advance" a pointer (string + 5), ... But…
-
5
votes1
answer137
viewsA: How to use build options in Ideone?
One of the languages you can choose is C99 Strict
-
1
votes2
answers2555
viewsA: Program in C to generate terms from a P.G
Maybe your pow() be weird and pow(5, 3) == 124.9999999999213453562. Suggestion: termo_pg = init * pow(rate, count-1) + 0.000001; // arredonda para cima or write a function similar to pow work only…
-
3
votes1
answer5250
viewsA: How can I solve an "indefinite reference"?
Put libraries at the end of the command gcc -o alcance alcance.c -lm According to the gcc manual [...] It makes a Difference Where in the command you write this option [...]…
-
3
votes1
answer5806
viewsA: Deleting specific line from a text file
The usual way to handle C files is to make a new file with what you want from the old one. Then delete the old one (or rename it) and rename the new one to the original one. Something like char…
-
8
votes2
answers1688
viewsA: Design Patterns in C?
With regard to header files, code files and prototypes' location, the most convenient is to get the prototypes into the .h and include that .h in the .c. So you only write each prototype once ...…
-
5
votes4
answers8023
viewsA: When to use size_t?
I use basically in 2 situations 1) sizes for allocation size_t len; // calcula len ptr = malloc(len * sizeof *ptr); 2) for array index for (size_t i = 0; i < sizeof array / sizeof *array; i++) {…
-
3
votes2
answers4930
viewsA: Triangular number
When i for 1 the program will do if if (1 * 2 * 3 == numero) Either print "YES N" or print "NO". When the i for 2 if becomes if (2 * 3 * 4 == numero) and the program prints "YES N" or "NO". That is,…
-
9
votes1
answer574
viewsA: Why does a char array support a character like ç and a char variable not?
This has little to do with C or C++ or other programming language. Internally, the computer only knows numbers. To represent letters it uses a coding. Each person can make their own custom encoding.…
-
1
votes1
answer426
viewsA: Insert sub-list C
How could I insert a list of houses within a list of streets struct Casa *lista_de_casas = NULL; struct Rua *exemplo1; exemplo1 = calloc(1, sizeof *exemplo1); // nao esquecer de fazer #include…
-
-1
votes2
answers2880
viewsA: How can I print only the fractional part of the number
You change the value of A in your code // digamos que o utilizador digitou 5.678 A = ceil(A); // A passou a 6 A = ; // a parte fracionaria de 6 'e 0 A = floor(A); // A passou a 6 I suggest you find…
-
1
votes2
answers52
viewsA: Why is one of the cases incorrect?
Because you crossed the line to int of your computer. 5000050000 is more than 2 31-1 (2147483647). Your show suffers from Helpless Behavior. 5000050000(10) == 100101010000001101011010101010000(2) //…
-
1
votes3
answers211
viewsA: Program using malloc twice
why this program [...] uses malloc twice? For numbers with N bits, the cycle while of the program runs N - 1 time. This cycle is not executed when decimal for 0 or 1. while (decimal >= 2) { /*…
-
0
votes2
answers83
viewsA: Problem with random values
See my comments below. Comments relate to the lines marked int inserir_fila(int x) // [1] { char ch; if (fim_fila<=MAX) { x= rand()% 26; ch= x + 'A'; fim_fila++; // [2] fila[fim_fila]=ch; // [3]…
-
2
votes2
answers83
viewsA: Problem with random values
The function rand() fetch a number from a fixed list of random numbers. If you do not choose a specific list, C uses the #1 list. To choose a specific list use srand() passing the number of the list…
-
3
votes2
answers326
viewsA: Segmentation failure when changing a Struct’s value in C
void main(){ Info* info; inserir(info); //O erro acontece na linha de baixo, como se o //que foi feito na função inserir não persistiu de fato //na estrutura printf("%d\n", info->val); } What…
-
1
votes2
answers447
viewsA: How to make a matrix sum using shared memory?
In your code, the child process prints; the parent process calculates. The parent process waits for the child to finish before calculating, so the child process prints zeros. Exchange father and son…
-
1
votes3
answers185
viewsA: Problem with program that prints three numbers increasingly
This code does a funny thing. if (A < B < C) /* ... */; First he compares A and B getting 0 or 1; then compare this value with C if ((A < B) < C) /* ... */; if (0 < C) /* ... */; if…
-
1
votes1
answer3017
viewsA: Copy struct C
Assuming each of the pointers points to a valid site struct whatever {int a; int b; int c;}; struct whatever array[2]; struct whatever *p1, *p2; p1 = array; // p1 aponta para o primeiro elemento do…
-
0
votes1
answer74
viewsA: Program runs but does not respond
Try putting line breaks in your prints: stdout can make "buffering" and hide output printf("A criar socket...\n"); // print com quebra de linha /* ... */ printf("A ligar ao socket...\n"); // a…
-
1
votes3
answers389
views -
0
votes2
answers250
viewsA: Repeat loop is not running
while (num <= n1) { printf ("%d ", vet[num]); num++; } At the end of this first cycle, the value of num will be n1 + 1 while (num <= n1) //roda o laço e caso encontre uma posição do vetor…
-
2
votes2
answers344
views -
3
votes2
answers1206
viewsA: ". exe has stopped working"
MAT_DIAG *d; /* ... codigo que nao atribui valor a d ... */ case 1: criar_matriz(d, ord); inicializar_matriz(d); break; case 2: imprimir_matriz(d); break; /* ... */ On the lines of case 1 and those…
-
4
votes1
answer474
viewsA: Variable loses value in C
First thing I noticed: "Erro! Insira um preco: " needs 24 bytes ... but yours mensagem only room for 20.
-
1
votes2
answers1725
viewsA: Print the largest N notes and names among the values that are in the struct array
You can make a "Selection Sort" only for the first 5 elements; it is not necessary to order the complete array.
-
2
votes1
answer61
viewsA: Fork does not restore variables
You’re not passing the argv[1] for the son execl("/home/Downloads/filho", arg, NULL); // NULL vai para argv[1] // path argv[0] should be execl("/home/Downloads/filho", "nome do filho", arg, NULL);…
-
2
votes3
answers42472
viewsA: Error Segmentation fault (core dumped)
In function preenche() the variable dados 'is an initialized pointer. Pass that pointer to the function aloca(), but it is not changed: in C all parameters are passed by value. Next you want to use…
-
1
votes1
answer478
viewsA: Parameters of the exec function
Maybe the request is something called recursive. // programa 'pai' execl("filho.exe", "filho.exe", "4", 0); and the 'son' program picks up the value, decreases 1 and calls itself to zero. //…
-
3
votes1
answer288
viewsA: Reset pointer address after being incremented several times
Make a copy of the pointer before changing it; then reset to that copy int *pbak = p; // copia de p for (i = 0; i < T_INT; i++) { printf("%d\n", *p); p++; } p = pbak; // resetando p…