Trouble with printf conversion long long - c

I've been working on a project euler problem, which by their very nature coerce you to use data types with big storage.
#include <stdio.h>
#include <conio.h>
#define num 600851475143
int main()
{
long long i, j, count=0, number=num, k;
for(i=2;number!=1;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if((i%j)==0)
{
count++;
}
}
for(k=0;k<100000000;k++)
{}
if(count==2)
{
printf(" %d\n", i);
if(number%i==0)
{
number/=i;
printf(" %d\n", number);
printf("%d\n", i);
i=2;
}
}
}
getch();
return 0;
}
When I compile and run the program, there is nothing printed for number. I have tried various printf conversions %ll, %l, I have changed data types. I am using GNU GCC compiler. What should I do?

You should (re)read the documentation, I guess.
%ll didn't work since ll is not a complete specifier, it's just a modifier for the actual conversion specifier, which should follow.
Try %lld.

The correct format for printf is %lld. Moreover you should use a prefix for your constant num, because this integer constant is too large to be hold in long type.
#define num 600851475143LL
Perhaps should you avoid lower-case macro's identifiers?

Related

I can't use drand48() and srand48() in C

I'm having problems compiling a program in C using the function drand48(). I wanted to know if and how I can fix this issue.
I've written a program in C which should generate random numbers and confront them with 5 input numbers. I wanted to use drand48() (because it's the function our professor wants us to use during our exam) but my ide (Dev C++ 6.3.0) keep telling me:
"[Warning] implicit declaration of function 'srand48'; did you mean 'srand'? [-Wimplicit-function-declaration]"
even though I've included "stdlib.h". I have tried to do the same on many other ide (from Eclipse to CodeBlocks) and they all print the same error and don't compile the program.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(){
srand48(1102003);
int i, num[5], n, count=0;
double random[100], ran, t;
//Input
printf("Digit 5 numbers\n");
for(i=0; i<5; i++) {
scanf("%d", &num[i]);
}
//Sequence Generation
for (n=0; n<100; n++) {
ran=drand48();
random[n]=round(ran*100);
}
//Value check
for(n=0; n<100; n++) {
for (i=0; i<5; i++){
if (num[i]==random[n]) {
count+=1;
}
}
}
printf("You guessed %d numbers\n", count);
system("PAUSE");
}
Here is a workaround:
At the too of your projekt (after includes) add the following lines.
#if !(_SVID_SOURCE || _XOPEN_SOURCE)
double drand48(void) {
return rand() / (RAND_MAX + 1.0);
}
long int lrand48(void) {
return rand();
}
long int mrand48(void) {
return rand() > RAND_MAX / 2 ? rand() : -rand();
}
void srand48(long int seedval) {
srand(seedval);
}
#endif
These will not provide the same values as the rand48 functions (for the same seed), but will behave the same.
From the linux man page:
These functions are declared obsolete by SVID 3, which states that rand(3) should be used instead.
So you may always want to use rand() instead.

C, Arrays, file format not recognized

for our homework we have to compile the program we wrote in the school. I have typed it without mistakes(verified with my colleagues) and the program does not work, I am using DEV C++ and the error log says, file not recognized: File format not recognized.
I tried using integer and not double but it stays the same...I have no idea what is wrong.
#include <stdio.h>
#define VELIKOST 23
int main (void)
{
double dPolje[VELIKOST];
int iStevec,iVecje=0;
printf("Algoritem, ki določi koliko elementov podatkovnega polja imajo vrednosti vecje ali enake od 10 \r\n");
for(iStevec=0;iStevec<VELIKOST;iStevec++)
{
printf("Vnesite %i. stevilo:",iStevec=iStevec+1);
fflush(stdin);
scanf("%lf",&dPolje[iStevec]);
if(dPolje[VELIKOST]>=10)
{
iVecje++;
printf("Element dPolje [%i]=%f.",iStevec,dPolje[iStevec]);
}
printf("%i elementov polja je imelo vecje ali enako vredost 10.",iVecje);
return(0);
}
}
I'm guessing that Dev C++ doesn't support Slovenian.
Create a new file and try this code:
#include <stdio.h>
#define SIZE 23
int main(){
double dField[SIZE];
int i, larger = 0;
printf("This algorithm, determines how many data field items have values greater than or equal to 10.\n");
for (i = 0; i < SIZE; i++){
printf("Enter field number %i:", i + 1); //Note I fixed this original code had i = i + 1
//fflush(stdin); unneeded
scanf("%lf", &dField[i]);
if (dField[i] >= 10){
larger++;
printf("Field number %i = %lf", i, dField[i]);
}
} //Moved this above final output and return
printf("%i field items were greater than or equal to 10 ", larger);
return 0;
}
I expect that to work.
Either way I'd definitely change compilers. Visual Studio Community Is a great fully featured IDE.

What is the largest prime factor of the number 600851475143?

I want to know that what is the error in my code.
int main () {
long long int number, large_factor=0, i=2;
printf ("Enter a number : ");
scanf ("%ld", &number);
while (number!=1) {
if (number%i==0) {
while (number%i==0) {
printf ("%ld\t", i);
number/=i;
}
large_factor=i;
}
i++;
}
printf ("\n\nThe largest prime factor is : %ld\n\n", large_factor);
return 0;
}
This code is running fine for smaller numbers but why it is failing for the large numbers?
Your format specifier everywhere is for long int you should use "%lld".
When I fix the format specifiers it runs fine for 600851475143 giving the largest prime factor 6857.
There must be something else going on. Assuming your compiler is compliant long long int should (at minimum) be a 64-bit integer and easily large enough to accommodate that value.
Try
printf("long long int max : %lld\n",LLONG_MAX);
Having added #include <limits.h> at the top.
It should produce a value no less than 9223372036854775807 and probably that exact number.
Here:
#include <limits.h>
#include <stdio.h>
int main () {
printf("long long int max : %lld\n",LLONG_MAX);
long long int large_factor=0, i=2;
long long int number=600851475143;
while (number!=1) {
if (number%i==0) {
while (number%i==0) {
printf ("%lld\t", i);
number/=i;
}
large_factor=i;
}
i++;
}
printf ("\n\nThe largest prime factor is : %lld\n\n", large_factor);
return 0;
}

Read and Display matrix

I'm having a small issue with this problem and I could really use another set of eyes with this. I am basically trying to read a matrix that I input and then display the said matrix.
The program always returns a null matrix (0 on all positions). The size of the matrix (columns/rows) is good. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#define MAXN 10
void display_matrix(int n, int m, double a[MAXN][MAXN], char ch)
{
int i, j;
printf("\n MATRIX %c\n", ch);
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++) printf("%8.2lf ",a[i][j]);
printf("\n");
}
}
void read_matrix(int *n, int *m, double a[MAXN][MAXN])
{
int i,j;
printf("\nInput of size and elements of a matrix\n");
printf("\n\tNumber of rows, n=");
scanf("%d", n);
printf("\n\tNumber of columns, m=");
scanf("%d", m);
printf("\n\tThe elements of the matrix\n");
for (i = 0; i < *n; i++)
{
for(j = 0; j < *m; j++)
{
printf("a[%d,%d]=", i, j);
scanf("%lf",&a[i][j]);
}
}
printf("\n");
}
void main()
{
int n, m;
double a[MAXN][MAXN];
read_matrix(&n, &m, a);
display_matrix(n, m, a, 'X');
return 0;
}
Your code seems mostly fine (main() should have a return type of int, and you should always check the return value of scanf()), except possibly for the following line:
printf("%8.2lf ",a[i][j]);
Prior to the C99 standard, the l modifier was not defined in conjunction with the f conversion specifier. Using the two in combination resulted in undefined behaviour. I don't know what the odds are, but it is not inconceivable that you have a pre-C99 C library that either misbehaves or implements %lf to match something entirely different, perhaps long double.
To print a double, you should use the %f conversion specifier:
printf("%8.2f ",a[i][j]);
This is just clutching at straws though. It might not change anything at all for you.
Background
The scanf() function has different conversion specifiers for float (%f) and double (%lf), because it need to know the exact type that the corresponding pointer argument is pointing to.
But when you pass a float to a variadic function, such as printf(), the default argument promotions convert the float to a double. Hence, the printf() function does not need a conversion specifier for float, because there is no way to pass a float to it. The C99 standard added the l modifier just for symmetry with the scanf() function.
References
The C90 Standard: ISO/IEC 9899:1990 (Withdrawn)
The C99 Standard: ISO/IEC 9899:1999 (Withdrawn)
The C11 Standard: ISO/IEC 9899:2011 (Withdrawn)
The C18 Standard: ISO/IEC 9899:2018 (Current)

Typecasting in division in C

#include <stdio.h>
int main(void) {
int t;
long long int a[100000], n, i;
scanf("%d\n", &t);
while(t){
t--;
scanf("%d", &n);
printf("%ld\n", n);
n = n * (n-1);
printf("%ld\n", n);
n = n/2;
printf("%ld\n", n);
}
return 0;
}
Can't figure out the problem in division. It's returning garbage value in the third printf statement. Can you please help me identify where the problem is?
The format specifier for long long int is lld, not ld.
That's especially important in the scanf as using the wrong specifier may end up putting the data in the wrong bytes within the variable.

Resources