klimi
Maybe try coding the polish notation first
Anonymous
Maybe try coding the polish notation first
Ohh thats Great, What is that?
Anonymous
How to make a calculator which can perform operations more than 2 numbers? Eg: 2+7*7-8 If anybody have example project Please share
Actually, I can make my C program to follow BODMAS rule in calculations once my this concept is clear.
Sylvester Lim
choice length is one char, why not choice[0] == 'y' tho
I actually have no idea how the code works , saw it online and wondering how it would work
Om
"Input 5 digit number and print the number in the following format eg: 23289 output : 2EVEN 3ODD 2EVEN 8EVEN 9ODD" Guys any one can help me I'm beginner
\Device\NUL
Clue, iterate string and ASCII char
Sylvester Lim
string.compare() returns the boolean value. And 0 is representation of boolean value which means false, and 1 or other number (except 0) for true.
can i know why "y" will return value of 0 at first and how does it not be 0 (to break the loop)
Imam
can i know why "y" will return value of 0 at first and how does it not be 0 (to break the loop)
choice.compare("y") is true. 0 is false. Then is true equal to false? No. The result will be false. So it's stop the looping.
Imam
Based Unix return code
Only for unix based?
\Device\NUL
Only for unix based?
No, Unix error code return 0 if succees
Sylvester Lim
Imam
No, Unix error code return 0 if succees
O i see. Like return 0 in main program
\Device\NUL
use ! operator
%Nikita
Gdb is actually weird. I have a string: const char *s = “qwertyuiopasdfghjklzxcvbnm”; This works fine: printf(“%p”, strchr(s, ‘a’)); But if I try to do the same in gdb: (gdb) p strchr(s, ‘a’) I’ll get segfault Love u, C
Ni
I am recently reading the cache chapter in csapp, I have a problem here: For a read operation, if cache hit happens, but the request read size is bigger than the cache block size. What will happen?
Fenimoure
Hi! Why can't we do string << anotherString in C++?
Markelle
Hello, I have a question on how to know if 2 binary trees are structurally identical. By structurally identical, it means that both trees have the same number of left and right child even if they have different values in c
Alviro Iskandar
I tried, I couldn’t. Could you please explain in more detail ?
I think I have explained it detail enough. Can you be more specific in which part you want more detail?
Alfredo
You could use std::any as return type, then you could use std::any_cast to manipulate that returned value. std::any random_choice(std:: vector<std::any> list) {}
meow
Write a value-returning function that accepts a phrase in Morse code and returns the English-language equivalent. However, if the parameter is invalid, the function should return a message telling that the parameter/input is invalid. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word. Embed it in a program
Alfredo
Well, then your question is not clear. You asked how to return std::any type, not how get the type of the returned value. You could use typeid.
Alfredo
You'll know when you try it.
Sylvester Lim
hey guys, I need help with understanding the game's instruction The game should be played as follows: 1. Player one and two enter their name 2. System shuffle and randomly assigned numbers 1-10 to the cards 3. Player one pick a card 4. System shuffle remaining cards and randomly assigned numbers 1-9 to the cards 5. Player two pick a card 6. Both Player show cards – players with higher type and system precedence wins the round a mark of 10 is given to the winner Player 1 and Player 2 are given free will and are able to pick their own cards right??? Not the program automatically pick a card for them
Sylvester Lim
What?
Pavel
What?
When there are some test tasks sometimes they are accompanied with examples of input and output
Suneeth
Hello Can Someone explain how this code works
Suneeth
#include <stdio.h> #include <stdlib.h> void getArray(int n); void displayArray(int n); int main(void) { int n; printf("enter the limit:"); scanf("%d",&n); getArray(n); displayArray(n); return EXIT_SUCCESS; } void getArray(int n) { int i,j,a[20][20]; printf("enter the array elements:\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } } void displayArray(int n) { int i,j,a[20][20]; printf("Array values are:\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d \t",a[i][j]); } printf("\n"); } }
Suneeth
after the getarray function is executed where the array is stored, the function doesn't return anything
Suneeth
how displayarray function is able to access the array in get arrayfunction
Alviro Iskandar
#include <stdio.h> #include <stdlib.h> void getArray(int n); void displayArray(int n); int main(void) { int n; printf("enter the limit:"); scanf("%d",&n); getArray(n); displayArray(n); return EXIT_SUCCESS; } void getArray(int n) { int i,j,a[20][20]; printf("enter the array elements:\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } } void displayArray(int n) { int i,j,a[20][20]; printf("Array values are:\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d \t",a[i][j]); } printf("\n"); } }
The scanf() in getArray() fills the local variable array a[20][20], then it returns. It doesn't do anything else. In displayArray() function, you are basically printing uninitialized variable. The array in this function hasn't been initialized. You need to know that each function has its own local variable, the local variable in displayArray() is a different thing with the local variable in the getArray(). What you should do is put the local variable in the caller, then pass it to getArray() and displayArray() as an argument. Or, you can use a global variable, but it's not necessary for your case, prefer to use local variable.
Alviro Iskandar
Thanks for the detailed reply. The program is working as i intended, i.e. with getarray() i'm able to input an nxn array and with displayarray() i am able to print nxn array which i inputted through getarray(), but i don't understand how it works
When a function returns, all its local variables are dead, except for the static ones. If you declare the array as a local variable in getArray() function, the lifetime of the array ends when this function returns. Your displayArray() function can't access it.
Suneeth
so i understand array[20][20] is static
Suneeth
thats why the program works
Alviro Iskandar
so i understand array[20][20] is static
It's not, you're basically just invoking undefined behavior. Your displayArray() function reads uninitialized local variable. This variable lives on the stack. The storage of this array may get reused, when it hasn't been clobbered, you may see the same value. But it's not always the case.
Sylvester Lim
https://dpaste.org/P1JMp#L10,12,1 Hey I was wondering if anyone knew a way where i can assign num1,num2,num3,num4,num5 and so on to the same random number generator, so i dont have to type the same code so many times
Sylvester Lim
Maybe array?
Can't use array ... Teacher said only use what was taught 😔
klimi
Can't use array ... Teacher said only use what was taught 😔
Than I would suggest functions, but aren't they even more advanced than arrays
Zaur
I think I have explained it detail enough. Can you be more specific in which part you want more detail?
I did it this way: #include <stdio.h> #include<stdlib.h> #include <pthread.h> #include<unistd.h> void *task2 (void *var); int main() { int count; printf("Enter number:"); scanf("%d",&count); pthread_t threadIDs[count]; for(int i=1;i<=count;i++){ pthread_create(&threadIDs[i],NULL,task2,NULL); for(int j=1;j<=i;j++){ pthread_join(threadIDs[i],NULL); printf("*"); } printf("\n"); } return 0; } void *task2 (void *var) { sleep(1); pthread_exit(0); }
Zaur
[New Thread 0x7ffff7da1700 (LWP 427)] * [Thread 0x7ffff7da1700 (LWP 427) exited] [New Thread 0x7ffff7da1700 (LWP 428)] ** [New Thread 0x7ffff7da1700 (LWP 429)] [Thread 0x7ffff7da1700 (LWP 428) exited] *** [Thread 0x7ffff7da1700 (LWP 429) exited] [Inferior 1 (process 423) exited normally]
Zaur
it outputs like this
\Device\NUL
Tf, use putchar if you want to print single char
● Igor
why setprecision is not giving the expected result? int main() { int i; long l; char c; float f; double d; scanf("%d %ld %c %f %lf", &i, &l, &c, &f, &d); cout << i << endl; cout << l << endl; cout << c << endl; cout << setprecision(3) << f << endl; cout << setprecision(9) << d << endl; } input: 3 12345678912345 a 334.23 14049.30493 output: 3 12345678912345 a 334 14049.3049
● Igor
code wher
i sent by accident
\Device\NUL
i was expecting this output for the last two numbers: 334.230 14049.304930000
> std::setprecision(3) Because the max precision is 3, Only get 3 digit from decimal. 334 > std::precision(10) The max precision is 10, 5 14049 from decimal, 1 . from dot, and 4 from floating point 30493
Alviro Iskandar
so how else can i do it?
What is exactly your goal?
Zaur
What is exactly your goal?
to create as many threads as the number entered from the screen, that is, each line should create a thread. If the number entered from the screen is 3 then: stars will be formed and 3 threads will be created
mito
!report
Leel
ARMAN @Greek1711 Shoaib Amir Hayat @atacza @MohdArmanulHaq Sir
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
Hi guys, I have this data struct: typedef struct _data{ int id; char *string; } data at a certain point, my code features the following lines: data *my_data= malloc(sizeof(data) + strlen(argv[1] ) + 1); strcpy(my_data->string, argv[1]); Where argv[1] it's a string given from the user when starting the code The code will always give me a "core dumped" on this point, the strcpy function, and I really don't get why. I tried by creating a char *random_string; and putting random_string in strcpy instead of my__data->string and it works. Can you please help me? This is driving me insane!
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
yep, I did that malloc on line 7 of my previous message
\Device\NUL
yep, I did that malloc on line 7 of my previous message
Could you share full code ? Honestly i don't understand
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
int main(int argc, char **argv){ if(argc <= 1){ printf("Non ci sono abbastanza argomenti.\n"); return -1; } int n_threads= argc-1; pthread_t tid; for(int i= 1; i < n_threads; i++){ thread_data *my_data; if((my_data= malloc(sizeof(thread_data) + strlen(argv[i]) + 1)) == NULL){ printf("C'è stata una failure da parte di malloc.\n"); return -1; } printf("total lenght of data struct: %lu & only string lenght: %lu", sizeof(my_data), sizeof(my_data->string)); fflush(stdout); my_data->id= i; strcpy(my_data->string, argv[i]); puts("I did the strcpy"); // NOTE: this is NEVER printed because of sef fault on previous line! if(pthread_create(&tid, NULL, thread_function, (void *) my_data) == -1){ printf("C'è stato un problema nella creazione del thread %d-esimo.\n", i); fflush(stdout); return -1; } } printf("Fatto tutto!\n");
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
for now threre's only this
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
oh, of course: // 1.1) Macro: #define PAGE_SIZE 4096 // 1.2) Strutture dati: typedef struct _data{ char vector[PAGE_SIZE]; } data; typedef struct thread{ int id; char *string; } thread_data;
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
So I tried to change the data struct and, instead of char *string, I said "let's give it a char string[4096]", and now it does work
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
so my point is, perhaps instead of doing the malloc like I did, perhaps would be better to do: thread_data *my_data= malloc(sizeof(thread_data)); my_data->string= malloc(strlen(argv[i]) + 1). I'll try and see if this does work
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
Yes, now it works. So I did as it follows: thread_data *my_data; if((my_data= malloc(sizeof(thread_data))) == NULL || (my_data->string= malloc(strlen(argv[i]) + 1)) == NULL){ printf("Malloc faiulre.\n"); fflush(stdout); return -1; }
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
You only allocate the struct memory, but not the struct member string memory
yep, I suppose this is the problem. Furthermore, in the first feature of my code, the string takes the whole struct. In fact, with the string "dino" given as a input, the stract weights 8B, and so does the string
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
so the int id part of the struct is not considered at all...
Boom
char name[9][28]={0};
Boom
char name[9][28]={0};
what is wrong with it? i want to assign all values inside this array as 0
Manuel
what is wrong with it? i want to assign all values inside this array as 0
You have to initialize each individual value with 0 with a double for loop