mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2005-04-08, 12:40   #1
Vijay
 
Apr 2005

2·19 Posts
Default A very simple mistake...

I just wanted to create a programme in C, which will count up from 1 to 20.
Heres the script, but for some reason, theres a mistake and it won't work, I don't see whats wrong with it.
Can you find the mistake :surprised :

Code:
main()
{
    int Nth;
    int lower, upper, step;
    
    upper = 20;
    lower = 1;
    step = 1;       /* step size */
    
    Nth = lower;
    while (Nth <= upper){
        Nth = Nth + step;
        printf("%d", Nth);
        getchar();
    }
}
Vijay is offline   Reply With Quote
Old 2005-04-08, 12:43   #2
Vijay
 
Apr 2005

2·19 Posts
Default

Don't bother, I found the mistake.
Vijay is offline   Reply With Quote
Old 2005-04-08, 13:02   #3
Wacky
 
Wacky's Avatar
 
Jun 2003
The Texas Hill Country

32·112 Posts
Default

Code:
main()
{
    int Nth;
    int lower, upper, step;
    
    upper = 20;
    lower = 1;
    step = 1;       /* step size */
    
    Nth = lower;
    while (Nth <= upper){
        Nth = Nth + step;
        printf("%d", Nth);
        getchar();
    }
}
"it won't work" isn't a very helpful description of the situation.
Did you try using the C compiler? Did you provide input? What did happen?

I'm guessing that you're hung on buffered I/O. See `man 3 fflush`.

I also notice that it will print 2-21 since you have incremented before the print. And you may find the formatting unsatisfactory.
Wacky is offline   Reply With Quote
Old 2005-04-09, 01:27   #4
ColdFury
 
ColdFury's Avatar
 
Aug 2002

1010000002 Posts
Default

main should return an int
ColdFury is offline   Reply With Quote
Old 2005-04-09, 02:07   #5
Wacky
 
Wacky's Avatar
 
Jun 2003
The Texas Hill Country

32×112 Posts
Default

Quote:
Originally Posted by ColdFury
main should return an int
SHOULD ---
It also should have argc and argv arguments .....

But I perceive that the inquirer is not yet ready for such subtleties.
Wacky is offline   Reply With Quote
Old 2005-04-09, 10:52   #6
Vijay
 
Apr 2005

2·19 Posts
Arrow I know now

Yes, I know I left the arguments out at the very beginning. And I said so.
Vijay is offline   Reply With Quote
Old 2005-04-09, 11:39   #7
Wacky
 
Wacky's Avatar
 
Jun 2003
The Texas Hill Country

32·112 Posts
Default

Quote:
Originally Posted by Vijay
Yes, I know I left the arguments out at the very beginning. And I said so.
You did? I don't see where you declared that omission.
Wacky is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deliberate mistake? Nick Forum Feedback 6 2016-01-14 11:53
Small mistake tomgreep Information & Answers 4 2014-06-09 20:33
Somebody made a numerical mistake Arkadiusz Math 20 2009-12-22 16:07
ECM efforts mistake? 10metreh mersennewiki 1 2008-12-28 13:31
Mistake in List of Primes rogue Sierpinski/Riesel Base 5 1 2007-02-23 00:35

All times are UTC. The time now is 22:12.

Wed May 5 22:12:32 UTC 2021 up 27 days, 16:53, 0 users, load averages: 2.82, 2.79, 2.70

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.