Manav
If we were in the realm of before C++11 3 3 3 was a possibility which can't happen now since a sequence is ensured but not the order
Manav
Do keep in mind i have simplified it a bit so there might be errors, do correct me if i am wrong somewhere
void
So in your case there's six possible outcomes depending on what gets executed first. 1 1 3 if a then a++ then ++a 1 2 2 if a then ++a then a++ 1 2 3 if a++ then a then ++a And so on
i got 1 2 2 when compiled instead of 1 2 3, compiler warned about some assignation issue, i assume ++a doesnt take effect like that in printf
Manav
i got 1 2 2 when compiled instead of 1 2 3, compiler warned about some assignation issue, i assume ++a doesnt take effect like that in printf
Well there six possibilities, 113, 122, 123, 133, 222 and 223 It could be anything based on what is evaluated first
Tafajjul
Hii
Hi guys , is it possible to print into console from left to right ? (with C)
artemetra 🇺🇦
Hi guys , is it possible to print into console from left to right ? (with C)
printf("left to right\n"); output: left to right what do you mean?
really
I just to print * character but start from the left to the right with a loop
artemetra 🇺🇦
i didn't understand the question characters are printed from left to right by default
ah sorry from right to left
artemetra 🇺🇦
ah sorry from right to left
there we go do you mean actually print from right to left or maybe just " *"?
R
We are given two strings P and Q, each consisting of N lowercase English letters. For each position in the strings, we have to choose one letter from either P or Q, in order to construct a new string S, such that the number of distinct letters in S is minimal. Our task is to find the number of distinct letters in the resulting string S. For example, if P = "ca" and Q = "ab", S can be equal to: "ca", "cb", "aa" or "ab". String "aa" has only one distinct letter ('a'), so the answer is 1 (which is minimal among those strings). Write a function: int solution(char *P, char *Q); that, given two strings P and Q, both of length N, returns the minimum number of distinct letters of a string S, that can be constructed from P and Q as described above. Examples: 1. Given P = "abc", Q = "bcd", your function should return 2. All possible strings S that can be constructed are: "abc", "abd", "acc", "acd", "bbc", "bbd", "bcc", "bcd". The minimum number of distinct letters is 2, which be obtained by constructing the following strings: "acc", "bbc", "bbd", "bcc". 2. Given P = "axxz", Q = "yzwy", your function should return 2. String S must consist of at least two distinct letters in this case. We can construct S = "yxxy", where the number of distinct letters is equal to 2, and this is the only optimal solution. 3. Given P = "bacad", Q = "abada", your function should return 1. We can choose the letter 'a' in each position, so S can be equal to "aaaaa". 4. Given P = "amz", Q = "amz", your function should return 3. The input strings are identical, so the only possible S that can be constructed is "amz", and its number of distinct letters is 3. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..200,000]; strings P and Q are both of length N; strings P and Q are made only of lowercase letters (a−z); strings P and Q contain a total of at most 20 distinct letters. How to solve this code ?
Manav
We are given two strings P and Q, each consisting of N lowercase English letters. For each position in the strings, we have to choose one letter from either P or Q, in order to construct a new string S, such that the number of distinct letters in S is minimal. Our task is to find the number of distinct letters in the resulting string S. For example, if P = "ca" and Q = "ab", S can be equal to: "ca", "cb", "aa" or "ab". String "aa" has only one distinct letter ('a'), so the answer is 1 (which is minimal among those strings). Write a function: int solution(char *P, char *Q); that, given two strings P and Q, both of length N, returns the minimum number of distinct letters of a string S, that can be constructed from P and Q as described above. Examples: 1. Given P = "abc", Q = "bcd", your function should return 2. All possible strings S that can be constructed are: "abc", "abd", "acc", "acd", "bbc", "bbd", "bcc", "bcd". The minimum number of distinct letters is 2, which be obtained by constructing the following strings: "acc", "bbc", "bbd", "bcc". 2. Given P = "axxz", Q = "yzwy", your function should return 2. String S must consist of at least two distinct letters in this case. We can construct S = "yxxy", where the number of distinct letters is equal to 2, and this is the only optimal solution. 3. Given P = "bacad", Q = "abada", your function should return 1. We can choose the letter 'a' in each position, so S can be equal to "aaaaa". 4. Given P = "amz", Q = "amz", your function should return 3. The input strings are identical, so the only possible S that can be constructed is "amz", and its number of distinct letters is 3. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..200,000]; strings P and Q are both of length N; strings P and Q are made only of lowercase letters (a−z); strings P and Q contain a total of at most 20 distinct letters. How to solve this code ?
Just do it.
Anonymous
Uhh there's too much jargoan with explanations here and none touching the actual problem of interleaved instructions which are largely the root cause of the UB in this case.
I have explained Undefined Behavior, Implementation Defined Behavior and Unspecified Behavior in great detail in the Resources section. Previously we used to point out to that but now we just expect them to search for it in the Resources section
Manav
I have explained Undefined Behavior, Implementation Defined Behavior and Unspecified Behavior in great detail in the Resources section. Previously we used to point out to that but now we just expect them to search for it in the Resources section
Well I didn't see the why being explained anywhere which irked me a bit. Not that i did any better job at explaning :D. Could have talked about sequence point for pre C++11 behaviout but ah well. Terms like side effect and value computations are already difficult for beginners imo. I feel it makes much more sense in showing the range of ambiguity of translations that can happen by taking a hypothetical case (or two, which can happen) for a better understanding.
VD
Well I didn't see the why being explained anywhere which irked me a bit. Not that i did any better job at explaning :D. Could have talked about sequence point for pre C++11 behaviout but ah well. Terms like side effect and value computations are already difficult for beginners imo. I feel it makes much more sense in showing the range of ambiguity of translations that can happen by taking a hypothetical case (or two, which can happen) for a better understanding.
You explained it very well. Sequence points however were a pain in the backside and the 'sequenced before' and 'happens before' relationships post C++11 are a definite improvement and are much easier to understand. No one needs to understand sequence points anymore as a recent survey suggested that only 5% of C++ code uses C++03. These are mostly legacy systems that just work and people don't feel the need to migrate them as they serve the purpose they were developed for and don't need to be changed anymore.
Ahsi
Best project for beginners in c++
Anonymous
Best project for beginners in c++
Please check the link in the Resources channel above. There are links to a lot of open source projects that document how the code was developed from scratch. https://t.me/cpp20programming/202
Darshan
Is there anything like this for JAVA??
Manav
Is there anything like this for JAVA??
It's offtopic but the language and the vm spec is public and more accessible than C/C++. You can just read those and you'll have a very good idea about the language and how it generates bytecode. Also we have an offtopic group so go there if you wanna know more.
NIKKI
Hello guys
Naveen
Hello guys, Can anyone please help to solve this problem in O(N) time complexity? Problem Statement You are given an array(Arr) of N Distinct integers. You have to find the sum of minimum element of all the subarrays (continous) in that array. See Sample for better understanding. Input The first line of input contains N, the size of the array The second line of input contains N space-separated integers Constraints 2 ≤ N ≤ 100000 1 ≤ Arr[i] ≤ 1000000000 Output The output should contain single integers, the sum of minimum element of all the subarrays in that array. Example Sample Input 3 1 2 3 Sample Output 10 Explaination all subarrays [1] [1,2] [1,2,3] [2] [2,3] [3] Sum of minimums : 1 + 1 + 1 + 2 + 2 + 3 = 10
Buffer
If the array is sorted then it is int Sum=0; int n= sizeof(A)/sizeof(A[0]); for(int i=0;i<n;i++) Sum+=A[i]*(n-i);
Naveen
Array is not sorted
yes array is not sorted
vndtta
Hello guys, Can anyone please help to solve this problem in O(N) time complexity? Problem Statement You are given an array(Arr) of N Distinct integers. You have to find the sum of minimum element of all the subarrays (continous) in that array. See Sample for better understanding. Input The first line of input contains N, the size of the array The second line of input contains N space-separated integers Constraints 2 ≤ N ≤ 100000 1 ≤ Arr[i] ≤ 1000000000 Output The output should contain single integers, the sum of minimum element of all the subarrays in that array. Example Sample Input 3 1 2 3 Sample Output 10 Explaination all subarrays [1] [1,2] [1,2,3] [2] [2,3] [3] Sum of minimums : 1 + 1 + 1 + 2 + 2 + 3 = 10
i guess we go from left to right. let us assume our SUM is counted for all arrays in [0,n] range, we have a list of minimums m1<m2<m3... A[m1]<A[m2]<A[m3]... now we check next element (n+1). so our index i now set to n+1, we set A[i] to end of list and delete all elements which are equal or higher than A[i] ( each element is compared about 2 times) next we go and add to SUM m1 arrays {k..i}, 0<=k<=m1 with minimum A[m1], m2-m1 arrays {k..i} m1<k<=m2 arrays with minimum A[m2], and etc go first step worst case we add all elements to list
C
https://pastecode.io/s/it8ggsjj can anyone help me correct this code? its to find kth smallest element using heap
Pavel
https://pastecode.io/s/it8ggsjj can anyone help me correct this code? its to find kth smallest element using heap
Can you specify what exact problem you have with the code? Compilation error, crash, logic error, etc?
Anonymous
https://pastecode.io/s/it8ggsjj can anyone help me correct this code? its to find kth smallest element using heap
You are inserting -1 in the vector and then dont account for it. You don't have to sift up and sift down constantly while inserting each element. You can get all the elements and construct the heap in place. In other words, you can construct the heap in Arr itself. You are deleting an element from the beginning of the vector which is a bad practice and what is worst is that you assume the vector's size doesn't change because of this operation. Typically to remove the max or min element from the heap, you swap the root element with the last child element. The last element (which was actually the root previously) is removed which reduces the heap size by 1. Now the root element is shifted down to maintain heap property
Anonymous
Can someone tell me what "streams" means when we talk about input and output?
Anonymous
Can someone tell me what "streams" means when we talk about input and output?
It is just an abstraction that also conveys that input and output are sequential in nature. The abstraction also ensures that programmers don't need to be concerned about where the input/output is happening from. Streams are an abstraction of top of files which itself is an abstraction layer. Files offer the notion of a sequence of bytes while streams offer formatted input/output operations on top of it.
Kenshin
👍🏾
Anonymous
And can you explain to me what *FILE and (stdin) have to do with all this, they are the things that confuse me the most.
As I understand *FILE is the representation of the (streams) but in the manual gnu they say "When you want to do input or output to a file".... what functions like printf do is print something to a file? So what exactly are things like stdin?
vndtta
Every time you open a file, system associates it with some descriptor(unsigned). stdin stdout stderr names associated with descriptors for console corresponding to 0 1 2.
Anonymous
And can you explain to me what *FILE and (stdin) have to do with all this, they are the things that confuse me the most.
Ok. So you are talking about Streams concept in C. Streams in C++ is different abstraction which is what I was talking about earlier. Streams in C is a generic concept that allows us to perform I/O operations on it. Generally when using C on *nix environments, you can work with files using a file descriptor that the OS returns to you or you can use a FILE* object. The functions that you call to work on the descriptor and the FILE* object are different. The C standard library and the POSIX library allows us to get a descriptor from a FILE* object and vice versa. The FILE* object is what is commonly referred to as a stream in the C world. stdin, stdout and stderr are all FILE* objects. As for the difference between a FILE (stream) and a naked file descriptor, the FILE object is provided by the C library which adds additional things like buffering, error detection, EOF detection and so on to make I/O more performing and easier to use. When you work with naked file descriptors, you generally work at the level of system calls and rely on OS. Calls like fflush are used to flush the C library buffers, while calls like fsync are used to flush the kernel buffers. You can think of streams (FILE* objects) as an abstraction on top of the naked file descriptors that help improve the speed and ease of reading and writing to and from files. They minimize the overhead of multiple system calls Streams in C++ is an abstraction at a slightly higher layer than streams in C. They help with formatted input, output and provide slightly more features than the ones provided by the C library like for ex automatic closure of the underlying file when the fstream object leaves the scope and so on. That is about it.
I_Guess_You_Are_
Please i want to get this output "20220294719" As my output is it possible. The code was error. Can anyone guide. Write your matric number using C language.
I_Guess_You_Are_
Please i want to get this output "20220294719" As my output is it possible. The code was error. Can anyone guide. Write your matric number using C language.
compilation infoprog.c: In function ‘getinput’: prog.c:4:8: warning: missing terminating " character printf("what is your matriculation number? ^ prog.c:4:8: error: missing terminating " character printf("what is your matriculation number? ^~~ prog.c:5:32: warning: missing terminating " character scanf("%d"&matriculation_number"); ^ prog.c:5:32: error: missing terminating " character scanf("%d"&matriculation_number"); ^ prog.c:5:11: error: invalid operands to binary & (have ‘char *’ and ‘int’) scanf("%d"&matriculation_number"); ^ prog.c:5:32: error: expected ‘)’ before ‘printf’ scanf("%d"&matriculation_number"); ^ ) printf("My matriculation number is %d", matriculation_number); ~~~~ prog.c:6:63: error: expected ‘)’ before ‘}’ token printf("My matriculation number is %d", matriculation_number); ^ ) } ~ prog.c:6:63: error: expected ‘;’ before ‘}’ token printf("My matriculation number is %d", matriculation_number); ^ ; } ~  stdout
I_Guess_You_Are_
Am using my phone. I use C Compiler. Cause it was Assignment
I_Guess_You_Are_
#include <stdio.h> void getinput() { int matriculation_number; printf("what is your matriculation number?'; scanf("%d"&matriculation_number); printf("My matriculation number is %d", matriculation_number); }
klimi
oh and the scanf too
I_Guess_You_Are_
error on line printf("what is your matriculation number?';
I have a low understanding. What is meant to be code there?
klimi
I have a low understanding. What is meant to be code there?
you end the string with different character ' vs " the scanf is missing a parameter delimiter ,
I_Guess_You_Are_
I have a low understanding. What is meant to be code there?
Can u correct that. It was 1st class 1st assignment
klimi
Can u correct that. It was 1st class 1st assignment
sorry, but i don't take "it was my assignment" as a valid request
I_Guess_You_Are_
I_Guess_You_Are_
?? that's not correct
What's the correct code
Tommaso
#include <stdio.h> void getinput() { int matriculation_number; printf("what is your matriculation number?”); scanf("%d",&matriculation_number); printf("My matriculation number is %d", matriculation_number); }
klimi
What's the correct code
i would advice you to retake the lesson
I_Guess_You_Are_
i would advice you to retake the lesson
No 1 is taking us, the lecturer just give us notes (theory aspect) told us to do the rest as assignment 🤦‍♂️
I_Guess_You_Are_
#include <stdio.h> void getinput() { int matriculation_number; printf("what is your matriculation number?”); scanf("%d",&matriculation_number); printf("My matriculation number is %d", matriculation_number); }
compilation infoprog.c: In function ‘getinput’: prog.c:4:8: warning: missing terminating " character printf("what is your matriculation number?”); ^ prog.c:4:8: error: missing terminating " character printf("what is your matriculation number?”); ^~~~~~~~~~ prog.c:5:34: error: expected ‘)’ before ‘;’ token scanf("%d",&matriculation_number); ^ ) prog.c:5:1: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion] scanf("%d",&matriculation_number); ^~~~~~~ In file included from /usr/include/stdio.h:867, from prog.c:1: /usr/include/x86_64-linux-gnu/bits/stdio2.h:105:32: note: expected ‘const char * restrict’ but argument is of type ‘int’ printf (const char *restrict fmt, ...) ~~~~~^~~ prog.c:7:1: warning: format not a string literal and no format arguments [-Wformat-security] } ^ prog.c:6:63: error: expected ‘;’ before ‘}’ token printf("My matriculation number is %d", matriculation_number);
C
https://pastecode.io/s/it8ggsjj can anyone help me correct this code? its to find kth smallest element using heap
i corrected this code and now all test cases pass.. for anyone interested https://pastecode.io/s/very1f5x
Danya🔥
/ban @ZxKDK0111 PMing
Anonymous
Hello, how do I print coordinates (x,y) in the cartesian plane in the console? I have an exercise that asks to print the coordinates on the plane.
Ilya
Because of this if (status=='F') printf("Full-Time\n");
izzaaa
then how do i correct that?
izzaaa
it is compulsory in my source code as it is to identify whether theyre a part timer or a full timer
Ilya
if (status=='F') { printf("Full-Time\n"); if (Year_serviced < 5) { new_salary= (0.04*salary)+ salary; printf("your new salary is %.2f\n", new_salary); } if (Year_serviced >= 5) { new_salary= (0.05*salary)+ salary; printf("your new salary is %.2f\n", new_salary); } }
Danya🔥
then how do i correct that?
Use {} and formatting for god sake