what am I doing wrong here? 
Author Message
 what am I doing wrong here?

#include <stdio.h>

main(void)

{
 int ll,ul,ans,x,y,flag;

printf ("What is your lower limit please: ");
scanf ("%i", &ll);
printf ("What is your upper limit please: ");
scanf ("%i", &ul);
{
 for (x = ll; x < ul; x++)
 flag = 1;
{
 for (y = 2; y < ul; y++);
 {
  ans = x % y;
 if (ans==0) flag = 0;
 }
 if (flag==0) printf("not prime %i",x);
 else printf ("is prime %i",x);
 }
return;

Quote:
}
}



Sat, 21 Jul 2001 03:00:00 GMT  
 what am I doing wrong here?


Quote:
> #include <stdio.h>

> main(void)

> {
>  int ll,ul,ans,x,y,flag;

> printf ("What is your lower limit please: ");
> scanf ("%i", &ll);
> printf ("What is your upper limit please: ");
> scanf ("%i", &ul);
> {
>  for (x = ll; x < ul; x++)
>  flag = 1;
> {
>  for (y = 2; y < ul; y++);
>  {
>   ans = x % y;
>  if (ans==0) flag = 0;
>  }
>  if (flag==0) printf("not prime %i",x);
>  else printf ("is prime %i",x);
>  }
> return;
> }
> }

Curious_abc - check the comments

/* code re-indented so I can read it */

#include <stdio.h>

main(void)      /* this implies a (correct) integer return, but you are not
returning a value */

{
    int ll,ul,ans,x,y,flag;

    printf ("What is your lower limit please: ");
    scanf ("%i", &ll);
    printf ("What is your upper limit please: ");
    scanf ("%i", &ul);
    {                   /* this opens a 'local scope' for no apparent reason */
        for (x = ll; x < ul; x++)
            flag = 1;   /* flag = 1 is executed (ul - ll) times, then the
loop terminates. */
        {                       /* I'll bet you meant this to open the loop, but it just opens
a local scope. */
            for (y = 2; y < ul; y++);        /* This loop simply sets y to ul and
terminates, because of the semicolon */
            {           /* Yet another local scope - NOT a loop opening brace! */
                 ans = x % y;   /* So basically, this code is executed only
once. So is all subsequent code. */
                 if (ans==0) flag = 0;
            }
            if (flag==0) printf("not prime %i",x);
            else printf ("is prime %i",x);
        }
        return; /* ideally, you'll want to return a value to the calling
process or OS */
    }

Quote:
}

Apart from that, it's fine. Not the whizziest prime number detector I ever
saw, mind you.

--
Richard H

North Bucks, England

Any perceived sarcasm in this reply is probably unintended by the author -
unless you're one of those people who advertises in languages newsgroups...



Sat, 21 Jul 2001 03:00:00 GMT  
 what am I doing wrong here?
Here's your code with unnecessary {} removed (they weren't doing anything)
and some basic formatting done to it.   Can you see what's wrong with it
now?

#include <stdio.h>

main(void)
{
     int ll, ul, ans, x, y, flag;

     printf ("What is your lower limit please: ");
     scanf ("%i", &ll);
     printf ("What is your upper limit please: ");
     scanf ("%i", &ul);

     for (x = ll; x < ul; x++)
          flag = 1;

     for (y = 2; y < ul; y++);
     {
          ans = x % y;
          if (ans==0)
               flag = 0;
     }

     if (flag==0)
          printf("not prime %i",x);
     else
          printf ("is prime %i",x);

Quote:
}

Eric



Quote:
> #include <stdio.h>

> main(void)

> {
>  int ll,ul,ans,x,y,flag;

> printf ("What is your lower limit please: ");
> scanf ("%i", &ll);
> printf ("What is your upper limit please: ");
> scanf ("%i", &ul);
> {
>  for (x = ll; x < ul; x++)
>  flag = 1;
> {
>  for (y = 2; y < ul; y++);
>  {
>   ans = x % y;
>  if (ans==0) flag = 0;
>  }
>  if (flag==0) printf("not prime %i",x);
>  else printf ("is prime %i",x);
>  }
> return;
> }
> }



Sat, 21 Jul 2001 03:00:00 GMT  
 what am I doing wrong here?
1.  You are asking C questions in comp.lang.c++.
2.  You need to indent your code (find a copy of
    Kernigan and Ritchie if you are using C or
    Bjarne Stroustrups book, if C++ and learn to
    indent your code like they do).
     Here, I'll do it for you just this once.

main(void)
{
    int ll,ul,ans,x,y,flag;

    printf ("What is your lower limit please: ");
    scanf ("%i", &ll);
    printf ("What is your upper limit please: ");
    scanf ("%i", &ul);
    {
        for (x = ll; x < ul; x++)
            flag = 1;
        {
            for (y = 2; y < ul; y++);
            {
                ans = x % y;
                if (ans==0) flag = 0;
            }
            if (flag==0) printf("not prime %i",x);
            else printf ("is prime %i",x);
         }
         return;
    }

Quote:
}

     What this shows is that you have a totally spurious
     block after the second scanf.  You then for every value
     of x between ll and xl, set flag to 1.
     You then execute the next block (factors the value in
     ul) and return out of it.

3.  main returns an int, and you have a {*filter*} "return"
    at the end which isn't proper.
4.  This has got to be the worst algorithm for factoring
    a prime I've ever seen.
5.  It might be nice to put out a newline (or a least
    a space) between your printfs
6.  you never actuall attain your upper limit.
7.  x and y aren't very intelligible names.

#include <stdio.h>

int main() {
    int ll, ul, num, factor;
    bool prime;

    printf("Enter lower limit:\n");
    scanf("%i", &ll);
    printf("Enter upper li mit:\n");
    scanf("%i", &ul);

    for(num = ll; num <= ul; num++)  {
        prime = true;

        for(factor = 2; factor < num/2; factor++) {
            if((num % factor) == 0) {
                prime = false;
                break;
            }
        }

        if(prime) printf("%d is prime.\n", num);
        else printf("%d is not prime (%d is a factor)\n", num, factor);
    }
    return 0;

Quote:
}



Sat, 21 Jul 2001 03:00:00 GMT  
 what am I doing wrong here?
Computing primes is a bit tricky. So is accepting numbers from a user. So is
designing loops. Here is a way to avoid all these problems:

#include <stdio.h>
#include <math.h>

int getInteger(char *prompt,int minimum, int maximum)
{
 char buff[128];
 int r,converted;
 do {
  printf("%s: ",prompt);
  fgets(buff,128,stdin);
  converted = sscanf(buff,"%d",&r);
  if(converted != 1) {
   printf("Please try again.\n");
  }
  else if ((r < minimum) || (r > maximum)) {
   converted = 0;
   printf("Please enter a number between %d and %d.\n",minimum,maximum);
  }
 }
 while(converted != 1);
 return r;

Quote:
}

int main()
{
 int lower,upper,x,q,noFactor;
 lower = getInteger("Please enter a lower limit",1,10000);
 upper = getInteger("Please enter an upper limit",1,10000);
 for(x = lower; x <= upper;x++) {
  q = 2;
  while(((q * q) <= x) && (noFactor = (x % q))) {
   q++;
  }
  if(noFactor) {
   printf("%5d is prime.\n",x);
  }
 }
 return 0;

Quote:
}

Please study this code very carefully. It reliably gets numbers from a user,
without breaking, and assures the numbers will fall in an acceptable range.

Please notice something else about this code. The prime test for any number
x need only be conducted between 2 and the square root of x. There is no
point testing numbers higher than this. The time spent squaring q is more
than made up for by the pointless tests that are avoided.

Paul Lutus

Quote:

>#include <stdio.h>

>main(void)

>{
> int ll,ul,ans,x,y,flag;

>printf ("What is your lower limit please: ");
>scanf ("%i", &ll);
>printf ("What is your upper limit please: ");
>scanf ("%i", &ul);
>{
> for (x = ll; x < ul; x++)
> flag = 1;
>{
> for (y = 2; y < ul; y++);
> {
>  ans = x % y;
> if (ans==0) flag = 0;
> }
> if (flag==0) printf("not prime %i",x);
> else printf ("is prime %i",x);
> }
>return;
>}
>}



Sat, 21 Jul 2001 03:00:00 GMT  
 what am I doing wrong here?

Since you did not tell us what you meant to do or in what way your
results diverged from expectation, it's hard to tell just what your
problem is.  Here, however, is a version of your code in legal and
relatively safe C:

#include <stdio.h>
#include <stdlib.h>

int getint(char *s)
{
    char buf[256];
    int i;
    if (s)
        printf("%s", s);
    fflush(stdout);
    if (!fgets(buf, sizeof buf, stdin))
        return 0;
    if (sscanf(buf, "%d", &i) != 1)
        return 0;
    return i;

Quote:
}

int main(void)
{
    int ll, ul, x, y, *flag;
    ll = getint("What is your lower limit please: ");
    ul = getint("What is your upper limit please: ");
    if (!(flag = malloc(ul * sizeof *flag))) {
        fprintf(stderr, "Could not allocate flags array.\n");
        exit(EXIT_FAILURE);
    }
    for (x = ll; x < ul; x++)
        flag[x] = 1;
    {
        for (y = 2; y < ul; y++)
            if (!(x % y))
                flag[x] = 0;
        printf("%d is%s prime.\n", x, flag[x] ? "" : " not");
    }
    return 0;

Quote:
}

> #include <stdio.h>

> main(void)

> {
>  int ll,ul,ans,x,y,flag;

> printf ("What is your lower limit please: ");
> scanf ("%i", &ll);
> printf ("What is your upper limit please: ");
> scanf ("%i", &ul);
> {
>  for (x = ll; x < ul; x++)
>  flag = 1;
> {
>  for (y = 2; y < ul; y++);
>  {
>   ans = x % y;
>  if (ans==0) flag = 0;
>  }
>  if (flag==0) printf("not prime %i",x);
>  else printf ("is prime %i",x);
>  }
> return;
> }
> }

--




Sun, 22 Jul 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. what am i doing wrong?

2. What am I doing wrong?

3. What am I doing wrong?

4. WHAT AM I DOING WRONG?

5. what am i doing wrong!

6. What am I doing wrong (part 2)

7. What am i doing wrong?

8. What am i doing wrong in this code?

9. What am I doing wrong here

10. What am I doing Wrong?

11. what am i doing wrong

12. what am i doing wrong???

 

 
Powered by phpBB® Forum Software