Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Intro to C

Name: Anonymous 2007-10-15 5:26

What does /prog/ think of my information on number code? These are the only tools we were allowed to use plus for loops but I felt like using while when doing this.

#include <stdio.h>

int main()

{

int num;
int down;
int up;
int count;
int fix;

while (num != -9876)
        {
printf("\n-------------------------------------------------------\n\n");

printf("Enter a number to learn about or enter -9876 to quit: ");
scanf("%d", &num);
if (num == -9876)
{
return 0;
}
if (num < 0)
{
printf("\n\nPlease choose a positive number.\n\n");
}
else
{
printf("You chose the number: %d\n", num);
down = num-1;
up = num+1;
printf("The number before %d is %d\n", num, down);
printf("The number after %d is %d\n", num, up);
if (num % 2 == 0)
{
printf("%d is even.\n", num);
}
if (num % 2 != 0)
{
printf("%d is odd.\n", num);
}
count = 1;
fix = num;
printf("The next five numbers are: ");
while (count <=5)
{
count = count+1;
fix = fix+1;
printf("%d ", fix);
}
printf("\n"); 
if (num%5==0)
{
printf("%d is a multiple of five.\n", num);
}
fix = num;
fix = fix * fix;
printf("%d squared is %d.\n", num, fix);
count=0;
fix = 0;
up = 0;
while (fix <= num)
{
up = up + 1;
if ((num / (up*7)) > 0)
{
count = count + 1;
}
else
{
(fix = num + 1);
printf("Seven fits into %d %d time(s).\n", num, count); 
}
}
fix = 0;
count=0;
while (count < num)
{
    fix = fix+1;
if (fix*fix==num)
{
printf("%d is %d squared.\n", num, fix);
count=num+1;  
}
if (fix*fix>num)
{
printf("%d is not a perfect square.\n", num);
count = num + 1;
}
}
count=0;
up=1;
while (count < num)
{
up = up+1;
if (num % up == 0 && up != num)
{
printf("%d is not prime.\n", num);
count = num + 1;
}
else
{
if (up >= num)
{
printf("%d is prime.\n", num);
count = num + 1;
}
}
}      
}
}
return 0;
}

Name: Anonymous 2007-10-15 5:28

really bad code.

Name: Anonymous 2007-10-15 5:39

not ENTERPRISE enough

Name: Anonymous 2007-10-15 5:50


{
        {
{
}
{
}
{
{
}
{
}
{
}
{
}
{
{
}
{
}
}
{
{
}
{
}
}
{
{
}
{
{
}
}
}     
}
}
}

Name: Anonymous 2007-10-15 5:55

That's why the forced indentation of the code is a good thing.

Name: Anonymous 2007-10-15 5:55

Needs more forced indentation of code thread over.

Name: Anonymous 2007-10-15 6:01

I actually just erased the indentation in notepad when copying it to piss you guys off.

Name: Anonymous 2007-10-15 6:11

On the off chance this is real... comments in CAPS

#include <stdio.h>

int main () //USE A PROPER DECLARATION
{
  int num;
  int down;
  int up;
  int count;
  int fix;

  while (num != -9876)
  //USES NUM UNITIALIZED THE FIRST TIME
  //OTHERWISE WILL NEVER FAIL, STOP USING USELESS CONDITIONS
  {
    printf ("\n-------------------------------------------------------\n\n");

    printf ("Enter a number to learn about or enter -9876 to quit: ");
    //MERGE THE TWO PRINTFS

    scanf ("%d", &num);
    //NICE ERROR CHECKING

    if (num == -9876)
    //WHAT A FUCKING IDIOTIC END CONDITION
    {
      return 0;
      //BREAK OUT OF THE LOOP, DON'T RETURN
      //STRUCTURED PROGRAMMING MOTHERFUCKER
    }

    if (num < 0)
    {
      printf ("\n\nPlease choose a positive number.\n\n");
      //READABILITY COULD BE IMPROVED BY USING CONTINUE HERE
    }
    else
    //THAT WOULD MAKE THIS ELSE SUPERFLUOUS
    {
      printf ("You chose the number: %d\n", num);
      down = num - 1;
      up = num + 1;
      //NICE USELESS VARIABLES
      printf ("The number before %d is %d\n", num, down);
      printf ("The number after %d is %d\n", num, up);
      if (num % 2 == 0)
      {
        printf ("%d is even.\n", num);
      }
      if (num % 2 != 0)
      //MAKE THIS AN ELSE RETARD
      {
        printf ("%d is odd.\n", num);
      }

      count = 1;
      fix = num;
      //ANOTHER USELESS VARIABLE
      printf ("The next five numbers are: ");
      while (count <= 5)
      //GUESS WHY THEY INVENTED FOR LOOPS
      {
        count = count + 1;
        fix = fix + 1;
        printf ("%d ", fix);
      }
      printf ("\n");
      if (num % 5 == 0)
      {
        printf ("%d is a multiple of five.\n", num);
      }
      fix = num;
      fix = fix * fix;
      //REALLY, STOP USING TEMPORARY VARIABLES

      printf ("%d squared is %d.\n", num, fix);
      count = 0;
      fix = 0;
      up = 0;
      while (fix <= num)
      {
        up = up + 1;
        if ((num / (up * 7)) > 0)
        {
          count = count + 1;
        }
        else
        {
          (fix = num + 1);
          printf ("Seven fits into %d %d time(s).\n", num, count);
        }
      }
      //DIVISION IDIOT, USE IT

      fix = 0;
      count = 0;
      while (count < num)
      //FUCKING USELESS CONDITION, USE BREAKS
      {
        fix = fix + 1;
        if (fix * fix == num)
        {
          printf ("%d is %d squared.\n", num, fix);
          count = num + 1;
        }

        if (fix * fix > num)
        //ELSE IF MOFO
        {
          printf ("%d is not a perfect square.\n", num);
          count = num + 1;
        }
      }
      //IMPROVE THE ALGORITHM, IT LOOKS LIKE SHIT

      count = 0;
      up = 1;
      while (count < num)
      //USELESS CONDITION
      {
        up = up + 1;    //YOU REALLY NEED TO CHECK ALL EVEN NUMBERS AMIRITE
        if (num % up == 0 && up != num)
        //UP != NUM IS A USELESS CHECK
        {
          printf ("%d is not prime.\n", num);
          count = num + 1;
        }
        else
        {
          if (up >= num)
          //PUT ELSE IF ON ONE LINE
          //ALSO CHECK IF UP * UP > NUM
          {
            printf ("%d is prime.\n", num);
            count = num + 1;
          }
        }
      }
      //CHOOSE A BETTER ALGORITHM
      //OR AT LEAST MERGE IT WITH THE PREVIOUS ONE
    }
  }

  return 0;
}

Name: Anonymous 2007-10-15 6:30

>>8
Fuck me.

Name: Anonymous 2007-10-15 9:39

printf ("\n-------------------------------------------------------\n\n");
Could be

printf("\n%55c\n\n", '-');


if (num % 2 == 0)
Should be

if(num == num/2*2)

I'd suggest &1 but i think you'll have endianess issues with that.

And what >>8 said.

Name: Anonymous 2007-10-15 10:49

Not sure why no one else picked up on this but:


a = a + 1;

//Better written as this:
++a;

Name: Anonymous 2007-10-15 10:52

>>11
.....................................

Name: Anonymous 2007-10-15 10:57

yazdigi > u

Name: Anonymous 2007-10-15 11:25

>>10
I'd suggest &1 but i think you'll have endianess issues with that.
What

Name: Anonymous 2007-10-15 11:30

>>14
well suppose 00 00 00 01
Depending on endianess it could be 01 00 00 00 or 00 00 00 01
Now, 1 would be 01 00 00 00 or 00 00 01 oh WAIT SNAP no endianess issues.
fuck.

use &1

Name: Anonymous 2007-10-15 11:31

>>15
also it does not have to be 4 octets, i just gave an exammple
i hate myself

Name: Anonymous 2007-10-15 13:45

>>15
...

Has anyone checked the output of GCC (with -O2/O3) to see if there's any difference in the code generation between & 1 and % 2? It seems to me the compiler would optimize those to the same assembly.

Name: Anonymous 2007-10-15 14:27

>>17
short reply:
the compiler cannot do that.
long reply:
some other anon.

Name: Anonymous 2007-10-15 15:43

>>18
the compiler cannot do that.
Why can't the compiler convert between two mathematically equivalent expressions, exactly?

Name: Anonymous 2007-10-15 17:06

>>18
you don't understand sag- erm, I mean, compilers. yes.

Name: Anonymous 2007-10-15 17:14

Okay, >>10,15-18 , you fucking angered an EXPERT PROGRAMMER.

Binary and (&) is an operation on numbers, not bytes, endianness has nothing to do with it. Compiler writers are ten times as smart as you, so yes they always do simple optimizations like shifting or anding instead of division or modulus where applicable. Note that this is often not possible on signed types.

Here's a link to a related question in the comp.lang.c faq, which you should all read btw: http://c-faq.com/misc/shifts.html

Name: Anonymous 2007-10-15 17:18

>>17,18
hotaru@olorin~$ cat >test1.c
#include <stdio.h>

int main(){
 unsigned int x=arc4random();
 x&=1;
 return x;
}
hotaru@olorin~$ cat >test2.c
#include <stdio.h>

int main(){
 unsigned int x=arc4random();
 x%=2;
 return x;
}
hotaru@olorin~$ gcc -S -O2 -o test1.s test1.c
hotaru@olorin~$ gcc -S -O2 -o test2.s test2.c
hotaru@olorin~$ diff test1.s test2.s
1c1
<       .file   "test1.c"
---
      .file   "test2.c"
hotaru@olorin~$

Name: Anonymous 2007-10-15 17:24

The compiler cannot do it in signed types.
If your compiler does it in signed types as well, it's broken.
Also, i was reffering to OPs code when i said "it can't"
Notice the OP is using plain int's (signed int's)
An int is always signed except if specified otherwise OR inside bitfield.
C89 had negative % n left unspecified.

>>22
Oh you leet haqr!
your code needs more non-std functions and invalid main prototypes.

Name: Anonymous 2007-10-15 17:25

>>23 here
>>21
That FAQ is outdated and has some inaccuracies.
I've contacted S.Summit but he does not seem to care.

Name: Anonymous 2007-10-15 17:26

>>22
in before !hoTarufiRE

Name: Anonymous 2007-10-15 22:58

$ cat >test1.c
#include <stdio.h>

int main(){
 int x=arc4random();
 return x&1==0;
}
$ cat >test2.c
#include <stdio.h>

int main(){
 int x=arc4random();
 return x%2==0;
}
$ gcc -S -O2 -o test1.s test1.c
$ gcc -S -O2 -o test2.s test2.c
$ diff test1.s test2.s
1c1
<       .file   "test1.c"
---
      .file   "test2.c"
13c13,15
<       xorl    %eax, %eax
---
      testb   $1, %al
      sete    %al
      movzbl  %al, %eax
$

Name: Anonymous 2007-10-15 23:18

>>22
ha visto a olorin? mide como este tamaño y salio de mi trasero!

Name: Anonymous 2007-10-16 3:20

Stop using
faggot
 {
  braces
 }

and do it
like {
 this
}

Name: Anonymous 2007-10-16 5:40

>>26
Binary AND (&) has a higher precedence than "equal to" (==). They did that for backward compatibility with B.

x & 1 == 0
x & (1 == 0)
x & 0
0


Maybe you should add -Wall to your CFLAGS.

Name: Anonymous 2010-12-17 1:39

This post brought to you by the Gay Nigger Association of America

Name: Anonymous 2011-02-03 1:52

Name: Anonymous 2011-02-04 19:40

Don't change these.
Name: Email:
Entire Thread Thread List