so I'm actually trying to do this exercise;
input two integer number, with a >= 1 and b <= 2 power of 32. find the value of a + b
I know this is a simple task but the things is, for small numbers value it worked correctly ( like 20 + 9 = 29), but for value like 1788909566 + 2626399043 the result is incorrect (it should be 4415308609 but 120341313 came out)
this the code that I worked on :
int main() {
unsigned int a, b;
scanf("%u %u", &a, &b);
printf("%u + %u = %u", a, b, a + b);
return 0;
}
Always use long long data type while dealing with large numbers."%llu" is the format specifier for long long.
unsigned long long is of 8 bytes and stores number in range of 0 to 18,446,744,073,709,551,615
int main() {
unsigned long long a, b;
scanf("%llu %llu" , &a, &b);
printf("%llu + %llu = %llu", a, b, a + b);
return 0;
}
because the result wrapped around.
You need to store the result in the larger integer type, or cast to the larger integer type. You need also to use the correct printf format to print unsigned long long number.
int main() {
unsigned int a, b;
scanf("%u %u", &a, &b);
printf("%u + %u = %llu", a, b, (unsigned long long)a + b);
return 0;
}
int main() {
unsigned int a, b;
unsigned long long c;;
scanf("%u %u", &a, &b);
c = (unsigned long long)a + b;
printf("%u + %u = %llu\n", a, b, c);
return 0;
}
Related
I have the following code in c
#include <stdio.h>
int main(void)
{
int a, b, c, sum;
double d, p;
sum = a + b + c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
d = sum / 3;
printf("3个整数的平均值是:d=%.2f", d);
p = a / sum * 100;
printf(",第一个数占和的比例是:p=%4.2f%%", p);
}
It is about entering three integers from the keyboard, average them, and calculate the ratio of the first number to the sum of the three numbers. The output result retains 2 decimal places .
I cannot find where is wrong.
The two main issues are:
You calculate sum with uninitialized values for a, b and c. Move that calculation to after a successful scanf() to ensure those variables are set.
You probably want to do the calculations of d and p with double, rather than integer, precision. I make use of automatic type conversion via fractional constants. The other two options are to change the type of sum from an int to a double, or explicitly use a type cast (see answer by #FeihuLiu).
Minor issues:
Original code was formatted poorly (since fixed by one of our friends :-).
Optional for main() but it's a good idea to return an integer as your declaration said you would.
(not fixed) If you don't use p or d for anything else, consider just eliminating them in favor of doing the calculation call to printf()
It's generally a good idea to reduce the scope of variables so I moved those definitions to just before they are used.
#include <stdio.h>
int main(void) {
printf("请输入三个整数:");
int a, b, c;
if (scanf("%d %d %d", &a, &b, &c) != 3) {
// TBD: translate error message
printf("scanf failed\n");
return 1;
}
int sum = a + b + c;
double d = sum / 3.0;
printf("3个整数的平均值是:d=%.2f", );
double p = 100.0 * a / sum;
printf(",第一个数占和的比例是:p=%4.2f%%", p);
return 0;
}
Besides the sum problem, integer division will result in integer. You can use the following code:
#include <stdio.h>
int main(void) {
int a, b, c, sum;
double d, p;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
sum = a + b + c;
d = (double) sum / 3;
printf("3个整数的平均值是:d=%.2f", d);
p = (double) a / sum * 100;
printf(",第一个数占和的比例是:p=%4.2f%%", p);
}
I need to print any value that's valid to be the size of the fourth side of a quadrilateral whose other 3 sides are given.
Problem link: https://codeforces.com/contest/1422/problem/A
So below is my program for it but somehow the online judge won't accept it, giving me this for the test case#2
"wrong answer : Maximum value no less than sum other values on 96"
Now the problem is I couldn't see the input 96 on test case#2 and thus don't understand what's the problem with my code. What should I do here?
#include<stdio.h>
long long int max(long long int x, long long int y);
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
long long int a,b,c, d, num;
scanf("%llu %llu %llu", &a, &b, &c);
num = max(a,max(b,c));
printf("%llu\n", num+1); //valid fourth side size - max(a,b,c) + 1
}
return 0;
}
long long int max(long long int x, long long int y)
{
return (x > y) ? x : y;
}
I could solve it with some other valid values for the fourth side like a+b+c-1 but I think I need to know why is the above code failing.
I have this code:
#include <stdio.h>
int func(unsigned int *a) {
printf("(func) Value: %d\n", *a);
}
int main() {
unsigned char a = 255;
printf("Value: %d\n", a);
printf("Bytes: %d %d %d %d\n\n", *&a, *(&a + 1), *(&a + 2), *(&a + 3));
func((unsigned int *) &a);
return 0;
}
I have the output of this program:
Value: 255
Bytes: 255 0 22 234
(func) Value: -367656705
Why i have negative func value, though the type is unsigned int?
Why i have negative func value, though the type is unsigned int?
int func(unsigned int *a) {
printf("(func) Value: %d\n", *a);
// ^^
}
Because %d does not match type of *a
Because sizeof(unsigned char) and sizeof(unsigned int) are different and can also be platform dependent. For example, unsigned char can be 1 byte. unsigned int can be 4 bytes. When you do your pointer arithmetic, you are possibly looking at other things in the stack frame. C provides you lots of rope to hang yourself with.
#include <stdio.h>
typedef unsigned char*pointer;
void show_bytes(pointer start, size_t len)
{
size_t i;
for (i = 0; i < len; i++)
printf("%p\t0x%04x\n",start+i, start[i]);
printf("\n");
}
int main()
{
double a = 4.75;
printf("Double demo by %s on %s %s\n", "Toan Tran", __DATE__, __TIME__);
printf("Double a = %.2f (0x%08x)\n", a, a);
show_bytes((pointer) &a, sizeof(double));
}
Output:
Double demo by Toan Tran on Nov 8 2018 11:07:07
Double a = 4.75 (0x00000100)
0x7ffeee7a0b38 0x0000
0x7ffeee7a0b39 0x0000
0x7ffeee7a0b3a 0x0000
0x7ffeee7a0b3b 0x0000
0x7ffeee7a0b3c 0x0000
0x7ffeee7a0b3d 0x0000
0x7ffeee7a0b3e 0x0013
0x7ffeee7a0b3f 0x0040
For this line:
printf("Double a = %.2f (0x%08x)\n", a, a);
I want it to print out the result of start[i]
The return hexadecimal is not the right value for double.
I want it to return 0x40130000000000...
Please help.
The %x format specifier is expecting an unsigned int argument, but you're passing in a double. Using the wrong format specifier invokes undefined behavior.
To print the representation of a double, you need to print each individual byte as hex using a character pointer. This is exactly what you're doing in show_bytes, and is the proper way to do this.
Also, when printing a pointer with the %p format specifier, you should cast the pointer to void *, which is what %p expects. This is one of the rare cases where a cast to void * is needed.
You might be tempted to do something like this:
printf("%llx", *((unsigned long long *)&a));
However this is a violation of the strict aliasing rule. You would need to use memcpy to copy the bytes to the other type:
static_assert(sizeof(unsigned long long) == sizeof(double));
unsigned long long b;
memcpy(&b, &a, sizeof(a));
printf("%llx", b);
You can also do this with a union:
union dval {
double d;
unsigned long long u;
};
union dval v;
v.d = d;
printf("%llx", v.u);
To allow printing a hex dump of any object, pass its address and length.
void show_bytes2(void *start, size_t size) {
int nibble_width_per_byte = (CHAR_BIT + 3) / 4; // Often 2
unsigned char *mem = start;
// Highest bytes first
for (size_t i = size; i>0; ) {
printf("%0*x", nibble_width_per_byte, mem[--i]);
}
printf("\n");
// Lowest bytes first
while (size--) {
printf("%0*x", nibble_width_per_byte, *mem++);
}
printf("\n");
}
Use "%a" to print the significand of the double in hexadecimal.
int main() {
double a = 4.75;
printf("Double a = %a %e %f %g\n", a, a, a, a);
show_bytes2(&a, sizeof a);
}
Output
Double a = 0x1.3p+2 4.750000e+00 4.750000 4.75
4013000000000000 // I want it to return 0x40130000000000...
0000000000001340
can any one please elaborate how to find largest of four numbers without using conditional operator.for 3 numbers i have done but for four numbers how to write different comparisons.
There is a standard way to compute min or max in 2's complement arithmetics without using conditionals:
int max(int a, int b){
unsigned diff = b - a; // negative if a > b
int sign = -(diff >> (sizeof(int) * CHAR_BIT - 1)); // -1 if a > b, 0 otherwise
return (a & sign) | (b & ~sign);
}
it can be easily scaled.
void main()
{
int a, b;
printf("Enter a and b:");
scanf("%d %d", &a, &b);
printf("Maximum number is %d", max(a, b));
getch();
}
int max(int a, int b)
{
int c, temp;
c = a - b;
temp = c + abs(c);
// To check if the difference is negative or not
if(temp) //As suggested by R..
return b;
else
return a;
}
This code is for compare two numbers. Make this comparison for all numbers.
you can find max of two number a,b by using following trick:
(abs(a+b)+abs(a-b))/2
Extend the trick for as many numbers you want.