Click here to Skip to main content
15,312,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written this code to solve a math. It is working well when the integers are not too long.
#include<stdio.h>
int main(){
int x, y, a, xa, ya, num;
scanf("%d%d%d", &x,&y,&a);
if( x%a!=0){
xa=(x/a)+1;
}else{
    xa=x/a;

}
if( y%a!=0){
ya=(y/a)+1;
}else{
   ya=y/a;
    }

num=xa*ya;
printf("%d\n", num);
return 0;
}

But when integers are more than 7 or 8 numbers each this code executes a garbage value.
To get away with this problem I have tried to use int array.
see the code:
#include<stdio.h>
int main(){
    int size=1000000000000;
int x[size], y[size], a[size], xa[size], ya[size], num[size];
scanf("%d%d%d", &x,&y,&a);
if( x%a!=0){
xa=(x/a)+1;
}else{
    xa=x/a;

}
if( y%a!=0){
ya=(y/a)+1;
}else{
   ya=y/a;
    }

num=xa*ya;
printf("%d\n", num);
return 0;
}


But my IDE is showing error message.
error: invalid operands to binary % (have 'int *' and 'int *').
I searched for it on google but couldn't find an point to point answer. I think it's my ignorance. I can't manage enough times to do programming but I want to do it badly.
Please, be kind, help me.

What I have tried:

 int size=1000000000000;
int x[size], y[size], a[size], xa[size], ya[size], num[size];
scanf("%d%d%d", &x,&y,&a);
if( x%a!=0){
xa=(x/a)+1;
}
Posted
Updated 18-Nov-21 23:46pm
v2

1 solution

Quote:
But when integers are more than 7 or 8 numbers each this code executes a garbage value.

Look at your code:
C
int size=1000000000000;
int x[size], y[size], a[size], xa[size], ya[size], num[size];
scanf("%d%d%d", &x,&y,&a);
if( x%a!=0){

So you allocate 6 arrays, all one gigabyte times the size of an integer, and then read a single value into the first element of each.
Then you try to check if one array is completely divisible by another ...

What part of that doesn't seem strange to you?

When you declare an array, the name of the array is a pointer to the first element; these two staments are equivelent:
C
int x[100];
int * px = x;
C
int x[100];
int * px = &(x[0]);
Which means that your code is attempting to divide a pointer by another pointer - and you can't do that because it's like trying to divide your mobile phone number mby my mobile phone number: it may be possible to a value of some kind, but it will have no meaning, it is useless for any practical purposes whatsoever.

And taking the address of an array is pointless, as it returns the same pointer value:
C
int a[10];
printf("%p:%p\n",a, &a);

0x7ffcce8972a0:0x7ffcce8972a0


Stop and think about what you are actually trying to do: about what the problem you were having with the earlier version might be related to instead of randomly adding code to bodge round your lack of understanding.

If you were getting weird results from "big numbers" when you input them, the most likely reason is that an integer is not big enough: depending on your compiler, an integer could be 16, 24, or 32 bits - which can hold positive number up to 32768, 8388608, and 2147483648 respectively.
Changing to an array won't help that: the size of an integer doesn't change no matter how many of them you allocate, and you can't read a number "across elements" of an array.
   
v2
Comments
Jahirul Sarker 19-Nov-21 6:56am
   
"Changing to an array won't help that: the size of an integer doesn't change no matter how many of them you allocate, and you can't read a number "across elements" of an array."
Yes, I understand it now. I should use long or long long int. After using long long int, if I want to limit the size of input values, then I can simply use "%1000lld".

"When you declare an array, the name of the array is a pointer to the first element; these two staments are equivelent"
I get it.
Yes, your answer helps me a lot.
Thank you.
OriginalGriff 19-Nov-21 6:58am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900