Евгений
It should indent your code
Евгений
not understood how it will work
Try, And then delete that semicolon
Евгений
did nt work
I see. In my visual studio 2022 works
admin
// Decimal to Hexa Conversion #include <bits/stdc++.h> using namespace std; int dec2hex(int dec) { // dec=5386 int a, b, ans; int i = 0; string str[10]; char ch; while (a > 0) { a = dec / 16; // 336,21, b = dec % 16; // 10,0 str[i] = b; dec = dec / 16; // 336 i++; } int x = str[10].size(); for (int j = x - 1; j >= 0; j--) { cout<<str[j]; } } int main() { int dec; cin >> dec; cout << dec2hex(dec) << endl; }
admin
code is not working... plz help anyone.
Ludovic 'Archivist'
Why is that?
Because if you want to use the native APIs or make drivers or use DirectX comfortably it is the choice on Windows
Anonymous
I don't know how it works))
You did copy to some where?
Евгений
You did copy to some where?
Хаха, of course not. I even snowed you two more ways of doing it.
Евгений
You did copy to some where?
Just explore what I've sent you and you'll get the idea
admin
// Decimal to Hexa Conversion #include <bits/stdc++.h> using namespace std; int dec2hex(int dec) { // dec=5386 int a, b, ans; int i = 0; string str[10]; char ch; while (a > 0) { a = dec / 16; // 336,21, b = dec % 16; // 10,0 str[i] = b; dec = dec / 16; // 336 i++; } int x = str[10].size(); //print reverse the number for (int j = x - 1; j >= 0; j--) { cout<<str[j]; } } int main() { int dec; cin >> dec; dec2hex(dec) ; }
admin
code is not working... plz help anyone.
Anonymous
Why did declare out of function?
Danya🔥
Because if you want to use the native APIs or make drivers or use DirectX comfortably it is the choice on Windows
Is there any problems with Clang? It can even use Microsoft STL as standard lib
Danya🔥
Emmanuel
include<stdio.h> int dost(int a, int b, int c); int main() { dost(); return 0; } void dost(int a, int b, int c) { printf ("Enter first number\n"); scanf("%d", &a); printf ("Enter second number\n"); scanf("%d", &b); c = a + b; printf("The sum is : %d", c); }
The problem is not with your return type. The error is, you created a function prototype "int dost" which takes in three arguments. Now, you're calling the same function "dost()" in your main function without supplying it with any parameter. The compiler will flag an error because "dost()" doesn't have enough arguments as prescribed in the prototype.
Ludovic 'Archivist'
Anonymous
#include <stdio.h> int dost(); int main() { dost(); return 0; } int dost() { int a, b,c; printf("Enter a number: "); scanf("%d", &a); printf("Enter a number: "); scanf("%d", &b); c = a+b; printf ("The sum is : %d", c); return c;
Anonymous
This is the right code
𝕷𝖔𝖗𝖊𝖓𝖟𝖔
Hi guys, so I was given the task to write a software that takes nBytes random bytes from /dev/random device (let it be fd2, after a read only open invocation), and writes it on another file opened in write, let it be fd1. This is quite easy, yet there's a complication: fd1 must be filled with shorts (so 2 bytes) . My solution was to read 2 bytes per time (untill I get to nBytes), thus filling a buffer like the one following: char buff[2]; and then writing those 2 bytes on the new file. The question is about what happens while writing: so, I have those 2 bytes written in memory, but will they be considered as a short? Or just two random bytes separated? In class we were only told that a short is 2 bytes, nothing further about memory representation, so I want to make sure that the two bytes I write really are a short, and the next two bytes I write are considered as another individual short. Thanks! Following, the part of the code that does the read from fd2 -> write to fd1 job. I'll quickly resume who's who: 1) fd1 is the destination file (the one I want to write on); 2) fd2 is the source file (the one I opened in reading to get the bytes from); 3) nBytes is the number of bytes (given by the user, I made sure at the beginning of the code that it always is a multiple of 16) the users wants to be written on the file. Notice: not the number of shorts, but the number of the bytes. For istance, if the user wants 2^15 bytes, then, given the fact that a short is 2 bytes, I shall write 2^14 shorts; 4) counter is, as the name suggests, a counter I use in order to understand how many bytes were already copied in fd1. // Rest of the code was not posted because it is not relevant: char buff[2]; unsigned long int counter= 0; do{ if(read(fd2, buff, counter) != 2){ printf("Error in reading!\n"); exit(1); } if(write(fd1,buff, 2) != 2){ printf("Error in writing!\n"); exit(1); } counter += 2; } while (nBytes - counter >= 0);
Anonymous
Hi guys which youtube channel is good for c language
Anonymous
A channel?
Nomid Íkorni-Sciurus
A channel?
sorry, it had to be a joke, I couldn't resist. youtube and learning do not go well together in this field
Nomid Íkorni-Sciurus
what I meant is that you should look for better resources on the web
redfox
can anyone explain templates to me
Nomid Íkorni-Sciurus
can anyone explain templates to me
Imagine you have a Box a Box can contain anything so how can you say two Boxes are equal if they're content is unknown? one thing the templates are used for, is specifying this kind of relationship so that Box<A> and Box<B> will be different but Box<A> and Box<A> will be equal
Nomid Íkorni-Sciurus
this is just one of the applications though. they're a vast concept I advice you to dig deeper
Faramarz
Hi can you help me to figure out how to avoid memory leak when I'm using malloc? Thank you
$ameer
Can someone please guide me pops Nasasity
Anonymous
Codeblocks
CodeBlocks is an IDE , not just a compiler
Nomid Íkorni-Sciurus
Nomid Íkorni-Sciurus
Hi can you help me to figure out how to avoid memory leak when I'm using malloc? Thank you
heh you'll need to track your allocations I'm afraid there are tools that will help you but the best advice is always... check that your free()s match your malloc()s
Anonymous
Precisely it is not
Anshul
/* * Below is the interface for Iterator, which is already defined for you. * DO NOT modify the interface for Iterator. * * class Iterator { * struct Data; * Data* data; * public: * Iterator(const vector<int>& nums); * Iterator(const Iterator& iter); * * // Returns the next element in the iteration. * int next(); * * // Returns true if the iteration has more elements. * bool hasNext() const; * }; */ class PeekingIterator : public Iterator { public: const int* it; int s; const int *a; PeekingIterator(const vector<int>& nums) : Iterator(nums) { // Initialize any member here. // DO NOT save a copy of nums and manipulate it directly. // You should only use the Iterator interface methods. it=&nums[0]; s=nums.size(); a=&nums[0]; } // Returns the next element in the iteration without advancing the iterator. int peek() { return *it; } // hasNext() and next() should behave the same as in the Iterator interface. // Override them if needed. int next() { int const temp=*it; it=it+1; return temp; } bool hasNext() const { if((it-a)>=s) return false; return true; } }; can anyone help why am i getting a run time error? i have checked it a few times but i can't find any error Que: https://leetcode.com/problems/peeking-iterator/
Anshul
the logic that i am applying is: i am keeping an iterator to start of vector, whenever peek is called i just return what's pointed by iterator. for next function, i store what is pointed by iterator, increment the iterator to the next step and then return the stored value. and for hasnext function, i check if it-(starting address of vector) is greater than or equal to vector size, if yes there are no more elements avlbl return false else return true
Anshul
Maybe you should check it 100 times)))
Still not able to find the error
CALVIN
How to filter out a record of a file maybe using one variable....... But let me share my code first where I attempted this....
Natanim
im trying to write the output of a code but its hard unless i show it by an image i have a question about it
Natanim
its when outputting a number pattern in a diamond form, but the pattern should look something like this» > 3 232 12321 232 3
Natanim
its when outputting a number pattern in a diamond form, but the pattern should look something like this» > 3 232 12321 232 3
/*this code outputs from the outside in but im having trouble shifting it so it could output the above one, i need some help here*/ #include <iostream> using namespace std; int main() { int i, j, k, l, rows; cout << "Enter Diamond Number Pattern Row = "; cin >> rows; cout << "Diamond Number Pattern\n"; for(i = 1; i <= rows; i++) { for(j = 1; j <= rows - i; j++) { cout << " "; } for(k = i; k >= 1; k--) { cout << k; } for(l = 2; l <= i; l++) { cout << l; } cout << "\n"; } for(i = rows - 1; i > 0; i--) { for(j = 1; j <= rows - i; j++) { cout << " "; } for(k = i; k >= 1; k--) { cout << k; } for(l = 2; l <= i; l++) { cout << l; } cout << "\n"; } return 0; }
Pita
#include <iostream> using namespace std; main(){ int data, jumlah, cacah; jumlah = 0; data = 0; cacah = 0; while (data != -1) { cout << "Masukkan data angka : "; cin >> data; jumlah += data; cacah++; } cout << " Jumlah data adalah : " << jumlah << endl; cout << "Rata-rata :" << jumlah/cacah; }
三体183号
You are too voluptuous
三体183号
I used to be satisfied with learning for two hours, but now it's painful not to learn for two hours
三体183号
How to make an analysis of this program?
You can print the result of each input
三体183号
You can see their changes every time
Anonymous
/* * Below is the interface for Iterator, which is already defined for you. * DO NOT modify the interface for Iterator. * * class Iterator { * struct Data; * Data* data; * public: * Iterator(const vector<int>& nums); * Iterator(const Iterator& iter); * * // Returns the next element in the iteration. * int next(); * * // Returns true if the iteration has more elements. * bool hasNext() const; * }; */ class PeekingIterator : public Iterator { public: const int* it; int s; const int *a; PeekingIterator(const vector<int>& nums) : Iterator(nums) { // Initialize any member here. // DO NOT save a copy of nums and manipulate it directly. // You should only use the Iterator interface methods. it=&nums[0]; s=nums.size(); a=&nums[0]; } // Returns the next element in the iteration without advancing the iterator. int peek() { return *it; } // hasNext() and next() should behave the same as in the Iterator interface. // Override them if needed. int next() { int const temp=*it; it=it+1; return temp; } bool hasNext() const { if((it-a)>=s) return false; return true; } }; can anyone help why am i getting a run time error? i have checked it a few times but i can't find any error Que: https://leetcode.com/problems/peeking-iterator/
The code looks fine and would work if the vector is not updated after being passed to the PeekingIterator constructor. A problem may arise because the question explicitly asks you not to save nums and work only with the interface. So suppose say something is added to the vector after being passed to the iterator then your pointers may be invalidated which may be causing the problem. You should write peek using Iterator::next and Iterator::hasNext.
Anonymous
But where am I saving nums, I'm accessing it using a pointer to a const int
Yes but if elements are added to the vector after calling your constructor, these pointers may become invalid
Anonymous
The calling code could be like vec<int> tmp; PeekingIterator it(tmp); tmp.push_back(.......);
Anshul
The calling code could be like vec<int> tmp; PeekingIterator it(tmp); tmp.push_back(.......);
Oh yeah I understand it now, it's going to give wrong answer even if it executed but atleast the code should produce some output rather than giving a run time error
Anonymous
Oh yeah I understand it now, it's going to give wrong answer even if it executed but atleast the code should produce some output rather than giving a run time error
It is Undefined Behavior to access invalidated pointers. In all likelihood you may be getting a segmentation fault which is what is being probably shown to you as a RTE by whatever onlineplatform you are coding on.
Anshul
It is Undefined Behavior to access invalidated pointers. In all likelihood you may be getting a segmentation fault which is what is being probably shown to you as a RTE by whatever onlineplatform you are coding on.
Can you please tell me how it is a invalidate pointer? Like even if the pointer points to a garbage and I try to print it, it'll print that garbage P.s. I never came across the term invalidate pointer😅
Anonymous
Can you please tell me how it is a invalidate pointer? Like even if the pointer points to a garbage and I try to print it, it'll print that garbage P.s. I never came across the term invalidate pointer😅
No. It is Undefined Behavior according to the standard. The C++ standard doesn't and can't define what happens when you access invalid memory. An OS/HW may just page fault. Another OS might crash. Yet another OS might just print garbage values. This is precisely what Undefined Behavior means
Anshul
Thanks I understood this And while going through solution to this problem I came across this way to write peek Return (*this).next
Anshul
int peek() { return Iterator(*this).next(); } How does this work?
Anonymous
int peek() { return Iterator(*this).next(); } How does this work?
The way it is supposed to work. Though it is a tad inefficient but. You can implement it a bit more directly. Iterator(*this) creates a new iterator that shares state with your current iterator pointed to by this. You call next on it which returns the element to be peeked at. The temporary is destroyed after that. The original object pointed at by "this pointer" is not changed.
Anshul
I'm trying to understand this
Anonymous
I'm trying to understand this
It is like this vec = {1,2,3}; Iterator a(vec); Iterator b(a); b.next(); //will print 1 a.next(); //will still print 1
Anshul
Anonymous
b and a both are pointing to same memory or a deep copy is created here 😐
That depends on how Iterator class is implemented. In all likelihood they will just have copies of the state they are in i.e. the element they are pointing at. So calling next on on one iterator does not affect the state on the other iterator. And you shouldn't be worried about it either. The question says that Iterator has a copy constructor and so you can assume it is implemented correctly and just use it
Anshul
I am able to understand it now Thank you so much
klimi
(tho if you are working with int you don't have to worry about anything as it is such small size)