klimi
Maybe try coding the polish notation first
Anonymous
\Device\NUL
Anonymous
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
\Device\NUL
Clue, iterate string and ASCII char
Imam
\Device\NUL
Sylvester Lim
Imam
Sylvester Lim
\Device\NUL
use ! operator
Zaur
%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
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
meow
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.
Alfredo
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
\Device\NUL
Sylvester Lim
What?
Pavel
What?
When there are some test tasks sometimes they are accompanied with examples of input and output
Sylvester Lim
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.
Suneeth
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
klimi
Sylvester Lim
Maybe array?
Can't use array ... Teacher said only use what was taught 😔
Suneeth
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
\Device\NUL
● Igor
Alviro Iskandar
Zaur
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!
\Device\NUL
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
yep, I did that malloc on line 7 of my previous message
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
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;
}
\Device\NUL
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
so the int id part of the struct is not considered at all...
Boom
char name[9][28]={0};