Fetheddine
Idk if my question is clear!
olli
Why in the fonction we used a pointer.
You basically want to pass your array to the function and void foo(int*) is the same as void foo(int[]) because when passing an array you lose information about its size and it decays into a pointer.
Fetheddine
Thank uuu
Sam
void enqueue(); void dequeue(); void display(); void peek(); void main(){ int opt, choice = 1; while(choice == 1){ printf("\n1.Enqueue \n2.Dequeue \n3.Display \n4.Peek"); printf("\nEnter a option: "); scanf("%d", &opt); switch(opt){ case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: peek(); break; } printf("\nDo you want to continue(1): "); scanf("%d", &choice); } } void enqueue(){ struct node *newnode = (struct node*)malloc(sizeof(struct node)); printf("\nEnter a data: "); scanf("%s", newnode->data); newnode->next = NULL; if(front == 0 && rear == 0){ front = newnode; rear = newnode; } else{ rear->next = newnode; rear = newnode; } } void dequeue(){ if(front == 0 && rear == 0){ printf("\nQueue is Empty"); } else{ struct node *temp = front; printf("\nDequeued data: %s",front->data); front = front->next; free(temp); } } void display(){ struct node * ptr; ptr = front; if(front == 0 && rear == 0){ printf("\nQueue is Empty"); } else{ while(ptr != NULL){ printf("%s ", ptr ->data); ptr = ptr->next; } } } void peek(){ if(front == 0 && rear == 0){ printf("\nQueue is Empty"); } else{ printf("Peeked data: %s", front->data); } }
Sam
can anyone help me to change this linear queue to cicular queue plz🥲
mayway
I've learned c++ from aman dhattarwal playlist upto oop ..I think it's all about mugging concepts..can anyone help me where should i learn my oop concept and dsa (yt, website, course.. any resources)
Anonymous
I've learned c++ from aman dhattarwal playlist upto oop ..I think it's all about mugging concepts..can anyone help me where should i learn my oop concept and dsa (yt, website, course.. any resources)
im learning from multiple resources. sometimes the geekforgeeks course( i have it in my mega. text me if you need it), If i still have doubts, i go to codewithharry's playlist. if i have doubts in harry's playlist, then i go to aman's playlist. this trick usually works
ᴹᵒʰᵃᵐᵐᵃᵈ
am = fopen("C:\\Users\\mohammad_z\\Desktop\\create_test\\graph_out1.txt", "w+"); Hello friends This line works in this project, but does not allow the rest of the code to be executed, and after executing this line, the program ends, the same line in a test project I tested works well and my debugging goes well, but here the debugging gives an error and the project does not work properly, commenting on this function works properly in other parts of the project
Amirhossein
hi guys i have a question. char s1[3] = "hi"; s1 = "test"; in the above example i can't do this because s1 is a static array ok? but look below example char s1[3] = "hi"; strcpy(s1,"test"); now no error and now s1 = "test" how is this possible? tnx
Anonymous
Am getting an error(overloaded function with no contextual type information). anyone knows where i have done something wrong? #include <iostream> using namespace std; float areaofcircle(float radius); int main() { float radius; cout<< "Enter radius"<<endl; cin>> radius; cout<< "arae is "<<areaofcircle(); } float areaofcircle() { float pi=3.14; float radius; areaofcircle=pi*radius*radius; return 0; }
Pavel
Am getting an error(overloaded function with no contextual type information). anyone knows where i have done something wrong? #include <iostream> using namespace std; float areaofcircle(float radius); int main() { float radius; cout<< "Enter radius"<<endl; cin>> radius; cout<< "arae is "<<areaofcircle(); } float areaofcircle() { float pi=3.14; float radius; areaofcircle=pi*radius*radius; return 0; }
Well this code have several mistakes First, you defined your function not as you have declared it before (you've missed float radius argument). Second, you assign result of an expression to areaofcircle (which is function, so you assign a float to a function, which doesn't make sense). Third, you read radius from cin and don't use it in the function call. Fourth, you return zero from areaofcircle, so the program will always output "area is 0"
Anonymous
Any one
Amirhossein
the array is not static, but C does not allow you to assign strings or arrays that way. You have to use functions to copy the buffer.
are you sure ? i searched that on the web https://dotnettutorials.net/lesson/static-vs-dynamic-array/#:~:text=Static%20array%20means%20the%20size,its%20size%20cannot%20be%20modified. if you see this is a static array 🤷‍♂️🤦🏻‍♂️
Peace
Is String a class ?
Nils
What is better? https://github-dotcom.gateway.web.tr/andreiamatuni/zstdpp Or just raw zstd?
shriman_deepak
//Multiplication Of two Matrices #include<stdio.h> int main() { int row1,col1,sum=0; printf("Enter the no. of rows and column "); scanf("%d %d",&row1, &col1 ); int M1[row1][col1]; printf("Enter the elements of matrices "); for (int i = 0; i < row1; i++) { for (int j = 0; j < col1; j++) { scanf("%d", &M1[i][j]); } } for (int i = 0; i < row1; i++) { for (int j = 0; j < col1; j++) { printf("%d ", M1[i][j]); } printf("\n"); } int row2,col2; printf("Enter the no. of rows and column "); scanf("%d %d",&row2, &col2 ); int M2[row2][col2]; printf("Enter the elements of matrices "); for (int i = 0; i < row2; i++) { for (int j = 0; j < col2; j++) { scanf("%d", &M2[i][j]); } } for (int i = 0; i < row2; i++) { for (int j = 0; j < col2; j++) { printf("%d ", M2[i][j]); } printf("\n"); } int P[row1][col2]; if (col1 != row2) { printf("This cannot be executed "); } else for (int i = 0; i < row1; i++) { for (int j = 0; i < col2; j++) { for (int k = 0; k <col2; k++) { sum += M1[i][k] * M2[k][j]; } sum = P[i][j] ; sum = 0; } } for (int i = 0; i < row1; i++) { for (int j = 0; j < col2; j++) { printf("%d", P[i][j]); } printf("\n"); } return 0; }
shriman_deepak
can anyone plzz check this code
shriman_deepak
And i am trying to multiply 2 matrixes
Talula
What is paste bin ?
https://pastebin.pl
𝔼𝕃𝔸𝕀𐇲
hey guys
𝔼𝕃𝔸𝕀𐇲
what is the best speech recognition library ?
Talula
And i am trying to multiply 2 matrixes
The error is for (int j = 0; i < col2; j++) it should be for (int j = 0; j < col2; j++)
Max
Who can help me with matrix exercise We have matrix[i][j] Program should count similar numbers next by next not from all matrix elements and print that element witch was similar most times or if it is two or more such elements print the last Example 3 4 5 4 4 5 6 7 7 Print: 7 Because in second line is two 4 but in third line is two 7 and it is the last witch was similar two times
Max
Thank you Who will help me
Pavel
Thank you Who will help me
Hi, you need two loops one inside another (one for rows and one inside it for elements inside row), then you need to store variables for previous number, current repetitions, number with max repetitions and max amount of repetitions. Then when iterating check if current number is not different from previous then, increase current repetitions, else reset. Each time check if current > max, and if it is, then update the max.
Max
now i have something like that
Max
#include <stdio.h> int main(int argc, char const *argv[]) { int rows = 0, cols = 0; scanf("%d%d", &rows, &cols); const int NUM_ROWS = rows; const int NUM_COLS = cols; int matrix[NUM_ROWS][NUM_COLS]; for (int i = 0; i < NUM_ROWS; i++) { for (int j = 0; j < NUM_COLS; j++) { scanf("%d", &matrix[i][j]); } } printf("\n"); // алгоритм и вывод результата здесь int sum = 0; int count = 0; int temp = 0; for (int i = 0; i < NUM_ROWS; i++) { for (int j = 0; j <= NUM_COLS / 2; j++) { if( j <= i && j <= NUM_ROWS - i - 1){ printf("%d ", matrix[i][j]); sum+=matrix[i][j]; } } printf("\n"); } for (int i = 0; i < NUM_ROWS; i++) { for (int j = i + 1; j < NUM_COLS; j++) { if (matrix[i][j]==matrix[i][j+1]) { count++; } } } printf("%d %d %d",sum,count,temp); return 0; }
Anshul
While using unordered map for a user defined key type, I need to overload== operator in the class that is the type of key. And the other thing is I need to create my own hash function and in this class I need to write a method that returns a value of type size_t. But how is it taken care that the value returned by this function is always in bounds of the hash-table size
Anonymous
While using unordered map for a user defined key type, I need to overload== operator in the class that is the type of key. And the other thing is I need to create my own hash function and in this class I need to write a method that returns a value of type size_t. But how is it taken care that the value returned by this function is always in bounds of the hash-table size
In unordered_map, the hash you return is further processed by the map implementation itself to determine the bucket in which the element has to go. This is typically a modulus operation. So you don't have to worry about whether the size_t value you return falls within the bounds because the bounds are implementation details and the interface provided shields you from it.
Anonymous
//Multiplication Of two Matrices #include<stdio.h> int main() { int row1,col1,sum=0; printf("Enter the no. of rows and column "); scanf("%d %d",&row1, &col1 ); int M1[row1][col1]; printf("Enter the elements of matrices "); for (int i = 0; i < row1; i++) { for (int j = 0; j < col1; j++) { scanf("%d", &M1[i][j]); } } for (int i = 0; i < row1; i++) { for (int j = 0; j < col1; j++) { printf("%d ", M1[i][j]); } printf("\n"); } int row2,col2; printf("Enter the no. of rows and column "); scanf("%d %d",&row2, &col2 ); int M2[row2][col2]; printf("Enter the elements of matrices "); for (int i = 0; i < row2; i++) { for (int j = 0; j < col2; j++) { scanf("%d", &M2[i][j]); } } for (int i = 0; i < row2; i++) { for (int j = 0; j < col2; j++) { printf("%d ", M2[i][j]); } printf("\n"); } int P[row1][col2]; if (col1 != row2) { printf("This cannot be executed "); } else for (int i = 0; i < row1; i++) { for (int j = 0; i < col2; j++) { for (int k = 0; k <col2; k++) { sum += M1[i][k] * M2[k][j]; } sum = P[i][j] ; sum = 0; } } for (int i = 0; i < row1; i++) { for (int j = 0; j < col2; j++) { printf("%d", P[i][j]); } printf("\n"); } return 0; }
In your 2nd for loop, you are checking for i < col2 which is not correct. It should be j < col2 And k iterates in the 3rd for loop from 0 to col2 which is wrong. It should be 0 to col1.
Anshul
P.S. why the function needs to be a const function? I compiled just now and got error because I didn't make it const function
Anshul
Which function?
The function inside hash function class.
Anonymous
The function inside hash function class.
If it is not a const function, the implication is that it stores some state which gets altered everytime you call it. The basic underlying assumption behind hashing is that it always returns the same value everytime you call it with the same key. If it is not a const function, this assumption stands violated.
Anshul
class Student { public: string firstName; string lastName; string rollNo; Student(string f,string l,string roll) { firstName=f; lastName=l; rollNo=roll; } Student() { } /*bool operator==(const Student s) const { return rollNo==s.rollNo; }*/ }; bool operator==(const Student &s1,const Student &s2) return s1.rollNo==s2.rollNo; } class HashFn { public: size_t operator()(const Student &s) const { return s.firstName.length()+s.lastName.length(); } }; int main() { Student s1("Anshul","Pareek","2001"); Student s2("Anshul","Pareek","3001"); Student s3("Ayushi","Sijeria","2011"); Student s4("Nishant","Bhat","2031"); unordered_map<Student,int,HashFn> Student_map; Student_map[s1]=85; Student_map[s2]=68; Student_map[s3]=15; Student_map[s4]=78; for(auto it=Student_map.begin();it!=Student_map.end();it++) { cout<<it->first.firstName<<" "<<it->first.rollNo<<" Marks: "<<it->second<<endl; } /* for(auto x:Student_map) { cout<<x.first.firstname<<" "<<x.first.rollNo<<" Marks: "<<x.second<<endl; }*/ }
J
Hello German student trying to solve a douple liked list task here :D
Anonymous
I don't understand. A const function is something that will not change data member of that particular class. Now assume this to be my hashfn then it'll return the same value for same student.
Assuming you have a hash template specialization which ddoes not make operator () const. then this specialization can have data members which can be used to calculate the hash value. Now if this method is not const, then it can change the data members (they can be changed if these members are mutable in which case const also doesn't help but this is the developer's mistake). If these data members are changed, then future calls to hash won't return the same value which defeats the purpose.
Amu
does anyone know how to do multithreaded programming using pthreads
Amu
if so please dm me
Amu
/* * ------------------------------------------------------------------ * startSimulation -- * Create a new Simulation object. This object will serve as * the shared state for the simulation. * * Create the following threads: * - 1 supplier generator thread. * - 1 customer generator thread. * - numSuppliers supplier threads. * - numCustomers customer threads. * * After creating the worker threads, the main thread * should wait until all of them exit, at which point it * should return. * * Hint: Use sthread_join. * * Results: * None. * * ------------------------------------------------------------------ */ static void startSimulation(int numSuppliers, int numCustomers, int maxTasks, bool useFineMode) { // // TODO: Your code here. // sthread_t supplier_generator_thread; // sthread_t customer_generator_thread; // sthread_t supplier_thread[numSuppliers]; // sthread_t customer_thread[numCustomers]; // pthread_create(&supplier_generator_thread, NULL, &supplierGenerator, NULL); // pthread_create(&customer_generator_thread, NULL, &customerGenerator, NULL); // for (int i = 0; i < numSuppliers; i++) // { // pthread_create (&supplier_thread[i], NULL, &supplier, NULL); // } // for (int i = 0; i < numCustomers; i++) // { // pthread_create (&customer_thread[i], NULL, &customer, NULL); // } // pthread_join(supplier_generator_thread, NULL); // pthread_join(customer_generator_thread, NULL); // for (int i = 0; i < numSuppliers; i++) // { // pthread_join (supplier_thread[i], NULL); // } // for (int i = 0; i < numCustomers; i++) // { // pthread_join (customer_thread[i], NULL); // } } int main(int argc, char **argv) { bool useFineMode = false; // Seed the random number generator. // You can remove this line or set it to some constant to get deterministic // results, but make sure you put it back before turning in. srand(time(NULL)); if (argc > 1) useFineMode = strcmp(argv[1], "--fine") == 0; startSimulation(10, 10, 100, useFineMode); return 0; }
Amu
i’ve commented out my code because im not entirely sure if i did it right, basically im supposed to create supplier threads and customer threads for a customer - supplier multithreaded problem in c++
Anonymous
i’ve commented out my code because im not entirely sure if i did it right, basically im supposed to create supplier threads and customer threads for a customer - supplier multithreaded problem in c++
This seems fine, but I don't understand why you need a supplier_generator_thread and a custom_generator_thread when you are creating all those threads anyway from the main thread. And the code for join also seems fine. The only things now left to do are to see if the supplier and the customer functions have been written correctly
Amu
This seems fine, but I don't understand why you need a supplier_generator_thread and a custom_generator_thread when you are creating all those threads anyway from the main thread. And the code for join also seems fine. The only things now left to do are to see if the supplier and the customer functions have been written correctly
/* * ------------------------------------------------------------------ * supplier -- * * The main supplier thread. The argument is a pointer to the * shared Simulation object. * * Dequeue Tasks from the supplier queue and execute them. * * Results: * Does not return. * * ------------------------------------------------------------------ */ static void* supplier(void* arg) { // TODO: Your code here. // Simulation* sim = reinterpret_cast<Simulation*>(arg); // sim->supplierTasks.dequeue(); return NULL; // Keep compiler happy. }
Amu
this is how i attempted to write the supplier queue, basically i took the simulation object which contains a pointer to a taskqueue which contains enque deque capabilities. The supplier generator generates and enques these files using the estore functions while supplier deques these and implements them
Amu
I used monitors (false monitors of course since this isn’t java) using pthread api
Anonymous
sorry how do i share the pastebin link?
Just post the link here. And ask specific questions if you have any. Posting a big chunk of code and asking people to check if it is correct won't get you many answers.
Amu
https://pastebin.com/db6dbdTL
olli
are you sure ? i searched that on the web https://dotnettutorials.net/lesson/static-vs-dynamic-array/#:~:text=Static%20array%20means%20the%20size,its%20size%20cannot%20be%20modified. if you see this is a static array 🤷‍♂️🤦🏻‍♂️
I see what you mean. I think the name static array might not be the best here as static is a keyword in c with a different meaning. In the below example both arrays are fixed sized but one is static. void foo() { static int x[10]; // Static array int y[10]; // non-static array } But either way, you cannot copy assign a string. To copy its content you have to use strcpy. When using these functions make sure your target buffer is big enough.
tanu kumari
How can we use graphics.h header file in vs code
Anonymous
https://pastebin.com/db6dbdTL
Is there a question?
Amirhossein
I see what you mean. I think the name static array might not be the best here as static is a keyword in c with a different meaning. In the below example both arrays are fixed sized but one is static. void foo() { static int x[10]; // Static array int y[10]; // non-static array } But either way, you cannot copy assign a string. To copy its content you have to use strcpy. When using these functions make sure your target buffer is big enough.
aha so its better we call them fixed array not static array ok tnx i found the answer bro see this example please : char s1[3] = "dd"; char* p = s1; *(p + 2) = 'd'; *(p + 3) = 'd'; cout<<s1<<endl; as you see the output is "dddd" so 99% strcpy use this way :) but tnx for your reply 🌹
Peace
Un-buffered standard error stream (cerr) , what does this buffer means ?
Anonymous
Hello guys, i am from Indonesian, nice to meet you all
klimi
Un-buffered standard error stream (cerr) , what does this buffer means ?
Stdout is buffered so when you are writing bytes, they are not immediately printed but they are stored in buffer and then written all at once
Itx
Hi
Anonymous
Anunay
#ot
Anunay
Go there for off-topic chats
Luca
Hi all guys ..I'm planning to build a P2P network...any advice from there?
Talula
Hi all guys ..I'm planning to build a P2P network...any advice from there?
P2P network is 2 devices talking to each other right?