Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to get fast input/output in c? i have been using scanf and printf but in programming contest i am getting "Time limit exceeded" .please suggest which functions to use for fast I/O in c.
#include<stdio.h>
#include<math.h>
main()
{
int i,j,flag,num,t,a[500000],k;
unsigned int n;
scanf("%d",&t);
if(t<=10)
{
while(t!=0)
{
scanf("%d",&n);
if(n<=70000)
{
i=1;flag=1;num=3;a[0]=2;k=0;
while(i<n)
{
for(j=3;(j<=sqrt(num))&&(flag==1);j++)
{
if(num%j==0)
{
flag=0;
}
}
if(flag==1)
{
a[i]=num;
num=num+2;
flag=1;
printf("%d",a[k]);
k++;
i++;
}
else
{
flag=1;
num=num+2;
}
}
}
printf("%d",a[k]);
t--;
printf("\n");
}
}
}
this is the code for which i am getting "time limit exceeded"
How do you know, that your program is I/O-bound? Programming contests like http://codility.com usually specify a desired time-complexity like O(N). If you submit an algorithm with O(N^2) complexity, you just cannot win, even if you use the fastest I/O library of the world.
printf/scanf is buffered I/O. It first catch I/O in buffer. You can use fflush() function to flush buffer in memory.
fgets and puts should be substantially faster than formatted IO.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I don't know why, but my code is not taking input.... where did i do mistake???
After running it just prints this:
Type your input (press enter to save and exit).🙂️
Done, your file is saved successfully🤩️
My code is:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fpp;
char Entry;
char sid;
fpp = fopen("sid","w");
if (fpp == NULL)
{
printf("Sorry🙁️ file not created\n");
exit(0);
}
printf("Type your input (press enter to save and exit).🙂️\n");
while (1)
{
putc(Entry,fpp);
if(Entry =='\n')
break;
}
printf("Done, your file is saved succesfully🤩️\n");
fclose(fpp);
return 0;
}
yes, guys I used scanf() instead of putc(). My online tutor said me to write this...
while((ch=getchar())!='\n')
{
putc(Entry,fpp);
}
and I used that.... but now I used this code and it worked.
while (1)
{
scanf("%c",&Entry);
if(Entry =='\n')
break;
}
In order to get this out of the list of unanswered questions I compile an answer from comments.
Credits to the commenters: MikeCAT, 0x5453, Vlad, Daniel Farrell.
Other commenters provided wise input, which I however do not see as immeditate parts of the solution.
Your question is not very specific about what puzzles you about the shown output so here is my guess, along with explanation:
Why does any input you provide not have any influence on program behavior?
Because you do never take any input. It seems that you intend to read input with putc(Entry,fpp);, but as 0x5453 comments
putc is used for output, not effective input.
I.e. with no input reading functions called there is no input.
Why does Done, your file is saved successfully🤩️ occur at all? It should only occur after '\n' input.
Because you have an unconditional break; in your endless loop.
That might be non-obvious, but as Daniel Farrell commetns:
... the semicolon between if(...) and break essentially makes the if a no-op... Thus break is executed the first time the loop runs.
I.e. unconditional immediate break, end of loop, output. End of program.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm witnessing some trouble compiling this code wherein you may see a nice if-else statement in practice but as soon as I compile the code, I get a "statement missing" error in the compiler to basically terminate the "if" statement before the highlighted parenthesis, which is weird but when I perform it, the compiler starts to show another error stating a "misplaced else" statement now.
What is the actual error in here and how should I proceed?
///////////////////// Input Code ////////////////////
int getcount()
{int count=0;
FILE*fp;
fp=fopen("counter.DAT","rb");
If(fp==NULL)
*{
fp=fopen("counter.DAT","wb");
count=2;
If(fp==NULL)
{
printf("\nErorr");
getch();
exit(0);
}
fwrite(&count,sizeof(int),1,fp);
count=1;
}
else
{
fread(&count,sizeof(int),1,fp);
fclose(fp);
fp=fopen("counter.DAT","wb");
If(fp==NULL)
{
printf("\nErorr");
getch();
exit(0);
}
count++;
fwrite(&count,sizeof(int),1,fp);
count--;
}
while(count>2)
{count=count-2;}
fclose(fp);
return count;}
Try a lower-case, "if"... I haven't tested it, but at first glance, that seems to be what's going on here.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So let's say you have a C program, that calls a function int foo() before returning control to main(), then main() uses puts() to write to the console.
What are some ways to prevent puts from writing to the console?
The only #includes you get are stdio and stdlib. You cannot touch any of the code in main(), only foo().
#include <stdio.h>
#include <stdlib.h>
unsigned int foo()
{
//Your code here
return 0;
}
int main()
{
foo();
puts("If you are reading this you have failed");
return 0;
}
Just redirect stdout to somewhere else (like a normal file or a character device like /dev/null) with freopen(). See the sample there
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
puts("stdout is printed to console");
if (freopen("redir.txt", "w", stdout) == NULL)
{
perror("freopen() failed");
return EXIT_FAILURE;
}
puts("stdout is redirected to a file"); // this is written to redir.txt
fclose(stdout);
}
Adapting this to your particular case should be simple
At //Your code here, put exit(EXIT_SUCCESS);.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
which of these codes is better?
do {
fread( ... );
if(!feof(fp)) { ... }
}
while(!feof(fp));
or
while(1){
fread( ... );
if(!feof(fp)) { ... }
else break;
}
Thanks.
Neither. You are better off making the eof test part of the loop condition (at the top).
You can do this:
while (!feof(fp)) {
fread(...);
}
Since fread returns the number of objects read, you could should also do it this way:
while (fread(...) != 0) {
}
The while loop is better since the do while do the same operations but it's calling the feof() function twice.
which is better?
No one is better than another. The only difference in between these two that first one iterate at least once.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else printf("Largest number = %.2f",c);
}
return 0;
}
when I compile. the code will scan for the 3 numbers but wont do anything afterwards. even if i put {} around every if and else statements, it wont change.
As Shubham suggested, try putting something at the end of the program that prevents the windows command line interface from closing instantly.
A getchar() from stdio.h is more appropriate than getch() from conio, because it's in the standard library.
If you run the program from a command line interface and not by double clicking the icon or hitting the run-button in the IDE your program runs fine without the getchar(), as you would expect.
You can also check if your IDE supports an option to leave the command line interface open after the program has terminated.
Another option is setting a breakpoint on the last line of your main() function.
Try putting a getch() before the return 0 statement. Don't forget to #include <conio.h>.
What is happening is that the program displays the result and closes immediately.
EDIT:
Assuming you are on windows
you have include one more header file like conio.h . you may aware of this one....sorry to remember you this basic header file for output window.......then before your return 0; line just write getch();
Sample like:
#include <stdio.h>
#include <conio.h>
int main()
{
/* your code to check conditions for which number is greater among these numbers */
getch();
return 0;
}