Anonymous
Have edit
Pavel
...
what do you mean?
instead of looping over something and going print("%d, ", n); they could just print out the first element, and then add (", %d", n) later
Anonymous
Pavel
...
pretty much
Anonymous
Typing on the phone is annoying
Anonymous
Char a = 48, what is a
Anonymous
What do you mean?
Anonymous
How i can print a string without length?
Pavel
C or C++?
Anonymous
First convert to int
Anonymous
Just use stoull (or the family of functions as is appropriate for your case) and convert both the strings to numbers and subtract them.
Pavel
To convert a char to a number you can do something like this
int number = character - '0';
To convert back, you need to add '0'.
Anonymous
Aah. So you want to subtract digit by digit the way it is done on a piece of paper. Then you will have to worry about borrowing and stuff. Not hard to implement one.
Or you could use a library like gmp which allows arbitrary precision integers.
Anonymous
I think on the contrary, if you only use the library you are just a kid
Pavel
You need a basic inverse loop from yourString.size()-1 to zero, do yourString.at(index) (or something like this) and convert the result to number as I shown above
Anonymous
Anonymous
Your loop should start from the least significant digit and proceed over to the most significant digit and subtract each digit individually. You should also keep in mind borrowing. I am not sure what is tough about this.
Anonymous
I thought the same*
Anonymous
What method to solving this problem when input very large string, about 100.000?
Anonymous
there is buffer overflow?
Pavel
Looks good except for bounds check, you need to make sure to handle the cases when the strings have different sizes
also carry = s / 10; can be replaced with carry = 1; i think
Anonymous
Subtraction is essentially the same. First check if the subtrahend is bigger or the minuend is bigger. Once you have determined that, arrange to ensure that you are subtracting the smaller number from bigger number and change the sign of the result if you happened to do the opposite of what was asked for.
The loop for subtracting would be similar to addition. When you encounter a situation where you have to subtract a bigger digit from a smaller digit just borrow from the digit next to it (you might need a loop here incase the next digit is 0) and proceed.
I still dont understand what is the confusion.
Ssssss
which course should i prefer for c++?
Anonymous
Hi guys, am new here
.
Pls paster ex code
This for example. I need numbers to be separated by commas and spaces but I want to get rid of the last ones. My code should be as short as possible so I am trying to find the shortest way to do it.
#include <unistd.h>
void ft_print_comb(int a, int b, int c)
{
a = '0';
while (a <= '9')
{
b = '0';
while (b <= '9')
{
c = '0';
while (c <= '9')
{
if ((c != a && c != b && a != b) && (a < b & b < c))
{
write(1, &a, 1);
write(1, &b, 1);
write(1, &c, 1);
write(1, ", ", 2);
}
c++;
}
b++;
}
a++;
}
}
V01D
This for example. I need numbers to be separated by commas and spaces but I want to get rid of the last ones. My code should be as short as possible so I am trying to find the shortest way to do it.
#include <unistd.h>
void ft_print_comb(int a, int b, int c)
{
a = '0';
while (a <= '9')
{
b = '0';
while (b <= '9')
{
c = '0';
while (c <= '9')
{
if ((c != a && c != b && a != b) && (a < b & b < c))
{
write(1, &a, 1);
write(1, &b, 1);
write(1, &c, 1);
write(1, ", ", 2);
}
c++;
}
b++;
}
a++;
}
}
You are missing an & in the final part of the if statement
.
V01D
Np
VENUJAN
#include <stdio.h>
int main()
{
int i = 1, j = 1;
char Ch = '*';
printf("\nPrinting Right Angled Triangle \n \n");
while ( i <= 5)
{
while (j <= i;)
{
printf("%c", Ch);
j++;
}
printf("\n");
i++;
}
return 0;
}
VENUJAN
what's wrong?
Anonymous
This for example. I need numbers to be separated by commas and spaces but I want to get rid of the last ones. My code should be as short as possible so I am trying to find the shortest way to do it.
#include <unistd.h>
void ft_print_comb(int a, int b, int c)
{
a = '0';
while (a <= '9')
{
b = '0';
while (b <= '9')
{
c = '0';
while (c <= '9')
{
if ((c != a && c != b && a != b) && (a < b & b < c))
{
write(1, &a, 1);
write(1, &b, 1);
write(1, &c, 1);
write(1, ", ", 2);
}
c++;
}
b++;
}
a++;
}
}
For your scenario just do
if(a!='7')
write(1,",",2);
Ravi
VENUJAN
VENUJAN
/* C Program to Print Right Angled Triangle Pattern */
#include <stdio.h>
int main()
{
int i = 1, j = 1;
char Ch = '*';
printf("\nPrinting Right Angled Triangle \n \n");
while ( i <= 5)
{
while (j <= i)
{
printf("%c", Ch);
j++;
}
printf("\n");
i++;
}
return 0;
}
VENUJAN
in this code why i could'nt get the expected output.....like an right angled traingle? it's working for 'for loop' but not working for while loop. why?
Ravi
whats the output you r getting then?
Anonymous
Ravi
VENUJAN
VENUJAN
Anonymous
But can we use in while loop?
This is what i meant by moving the initialization of j to within the while loop. Just modified your code.
/* C Program to Print Right Angled Triangle Pattern */
#include <stdio.h>
int main()
{
int i = 1;
char Ch = '*';
printf("\nPrinting Right Angled Triangle \n \n");
while ( i <= 5)
{
int j =1;
while (j <= i)
{
printf("%c", Ch);
j++;
}
printf("\n");
i++;
}
return 0;
}
Anonymous
share your full code on pastebin.com
Anonymous
This was the same error you were getting earlier. But you deleted your code links. How do you expect people to help you then?
Anonymous
Share the link for headerfile.h and also for your main function
Anonymous
Your code compiles fine except for a serious warning in subtract method in line 98. You have used '10' there which is not a char literal.
I am not getting the compiler error that you saw.
Anonymous
Anonymous
This is how the program executed on my system. The answer is wrong for subtraction but the program compiles fine. The warning in subtract method should be heeded to and fixed but. I am not sure why you are getting an error in CodeBlocks. What compiler are you using underneath the IDE?
Anonymous
No. I used gcc and clang and both gave me the same warning.
Anonymous
That is strange.
Amy
Look like it is no issues. Does the compiler have more error or warn messages ?
Anonymous
Try cleaning your project directory and start a fresh build from scratch and see if the problem is resolved.
Anonymous
@Napem24 congrats
Pavel
Is this safe: num1[i-1]
Your i can be zero and then i-1 negative
Anonymous
pay = (num1[i-1] - '0') - ('1');
Why are you subtracting '1'? You should be subtracting 1 and not the char literal '1'.
.
This for example. I need numbers to be separated by commas and spaces but I want to get rid of the last ones. My code should be as short as possible so I am trying to find the shortest way to do it.
#include <unistd.h>
void ft_print_comb(int a, int b, int c)
{
a = '0';
while (a <= '9')
{
b = '0';
while (b <= '9')
{
c = '0';
while (c <= '9')
{
if ((c != a && c != b && a != b) && (a < b & b < c))
{
write(1, &a, 1);
write(1, &b, 1);
write(1, &c, 1);
write(1, ", ", 2);
}
c++;
}
b++;
}
a++;
}
}
Is there a way to add a space for example between a and b? How do I append it to write(1, &a, 1); ?
Anonymous
.
.
klimi
The code seems.... Very weird
klimi
Why are you doing tmp+10?
Anonymous
klimi
Also you are only taking in count the length of the first string, so if it is 2, then you will take the value of '-' and calculate with it
Anonymous
klimi
I still dont believe that your code is correct
Anonymous
Because of Undefined Behavior. Your loop iterates num1.size()-1 times. If num2 has a lesser length you will be iterating over memory that you are not supposed to access.
Moreover there is no logic for borrowing.
klimi
If both numbers have equal length, then how come you say it works for 9 and -5
klimi
We are saying your logic is wrong..
klimi
Why not just take those strings, convert them to int, do the arithmetic, and then print it out?
'''''''
How did he share media?
klimi
'''''''
klimi
So you want infinite numbers? I see...