Thread: Making my code better?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    Making my code better?

    I am required to make a few programs for school. Very simple ones. Any way i could improve that?

    Code:
    #include <stdlib.h>
    
    int main()
    {
    
         int i;
         for (i = 1; i<=10;++i)
         {
             printf("Number : %d - Square : %d \n",i, i*i);
         };
    
    
         system("PAUSE");
          return 0;
    }
    Code:
    #include <stdlib.h>
    
    int main()
    {
    
         int i;
         for (i = 10; i<=45;++i)
         {
             printf("Number : %d - Square : %d \n",i, i*i);
         };
    
    
         system("PAUSE");
          return 0;
    }
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        char *name;
        int counter;
        counter =0;
    
        printf("Enter name ");
        scanf("%s", name);
    
        while (strcmp(name,"STOP") != 0)
        {
              counter++;
              printf("Name : %s \n",name);
              printf("\nEnter name ");
              scanf("%s", name);
    
        }
    
        printf("\nNumber of names entered : %d",counter);
    
        system("PAUSE");
        return 0;
    }
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        int number,total,highest;
        total=0;
    
        printf("Enter number (-ve to stop) ");
        scanf("%d", &number);
        highest = number;
    
        while (number >=  0)
        {
              if (number > highest)
              {
               highest = number;
              }
    
              total+=number;
              printf("Enter number (-ve to stop) ");
              scanf("%d", &number);
    
        }
    
        printf("\nTotal : %d",total);
        printf("\nHighest number : %d\n",highest);
    
        system("PAUSE");
        return 0;
    }
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        int number,total,highest;
    
    
        printf("Enter number (-ve to stop) ");
        scanf("%d", &number);
        highest = number;
        total=0;
    
        if (number >=0)
        {
           do
           {
                   total+=number;
                   printf("Enter number (-ve to stop) ");
                   scanf("%d", &number);
                   if (number > highest)
                      highest = number;
    
            } while (number >=  0)
        }
    
        printf("\nTotal : %d",total);
        printf("\nHighest number : %d\n",highest);
    
        system("PAUSE");
        return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%s", name);
    This will fail. name probably doesn't point to memory that you own, and if it does, it's not likely to be where you want a string written.

    Other than that, at a glance your code seems okay for simple school stuff. You could work on your formatting as it's not very consistent, and bulletproofing can wait until later. Anything else would just be nitpicking.
    My best code is written with the delete key.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    system("PAUSE");
    not a good idea

    Code:
    getchar();
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM