Thread: beginner

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    8

    Thanks

    ok, now it writes 0 in even numbers, but how to get them deleted. i could rewrite the array somehow? and why do I need (int argc, char *argv[]) and #include <stdlib.h>?

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    and one more question - what is deffensive programming?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>how to get them deleted. i could rewrite the array somehow?

    Yup, that's one way, and probably the easiest. You could also keep track of the number of elements in the array. Then start at index 0 and find and even number (or zero or whatever) and slide every number with a higher index than the current index to the left by 1 and decreasing the number of elements by 1, repeating the process until you run out of indexes to check. There are bound to be other ways as well.

    argc is the number of arguments passed to main by the command line and the argv is an array of strings of size argc where each of those arguments is stored. Frequently, the default values of argc and argv are used by leaving the parenthesis blank.

    stdlib is a library of commonly used functions. If you use iostream without the .h extension, you should probably use cstdlib, which is the updated version of stdlib.h just as iostream is the updated version of iostream.h

    When I think of defensive programming I think of trying to include safegaurds to prevent the program from hanging or crashing from dumb mistakes that either I or the end user could make.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i could rewrite the array somehow?
    Yes, something like this would be a good start:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      int array[] = {0,1,2,3,4,5,6,7,8,9};
      int size = 10;
      int i, j;
    
      for (i = 0, j = 0; i < 10; i++) {
        if (array[i] % 2) {
          --size;
          array[j++] = array[i];
        }
      }
      for (i = 0; i < size; i++) {
        cout<< array[i] <<endl;
      }
    }
    >and why do I need (int argc, char *argv[])
    You don't unless your program is accepting command line arguments.

    >and #include <stdlib.h>?
    The system function is declared in stdlib.h. But for it to be strictly correct, the header should be <cstdlib>.

    >what is deffensive programming?
    Writing code that handles errors gracefully, has debugging hooks and generally protects itself rather than relying on outside forces to do the "right thing".
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    and I also need the code to write numbers in the array by itself not manually, but inthis way I have an error -parse error before'?
    If I have to write the code in defensive programming, then what should I change? and what is the right way to write comments?
    #include <iostream>

    using namespace std;

    int main()
    {
    int i, j, list[10]

    for (i=0, i<10, i++){
    list[j] = i;
    };
    for(i=0; i < 10; i++)
    {
    cout << list[i] << " ";

    if(i != 0 && i % 2 != 0)
    list[j++] = list[i];
    }

    cout << "\n\nList without even ones" << endl;

    for(i=0; i < j; i++)
    cout << list[i] << " ";
    cout << endl;

    system("PAUSE");
    return 0;
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >int i, j, list[10]
    Don't forget to terminate a declaration with a semicolon. And j should be given an initial value because you use it immediately in the forthcoming loop. As it is, the value is indeterminate[1].

    >for (i=0, i<10, i++){
    The separator for the parts of a for loop is the semicolon.

    >If I have to write the code in defensive programming, then what should I change?
    The simplest way to start is to use if statements to double check your assumptions. You can get as complex as you want though. For such a small program you don't need production quality defenses though.

    >and what is the right way to write comments?
    There are two ways. The first comment style starts at a // and ends at the nearest newline. The second comments style starts at a /* and ends at the nearest */. The second can spn multiple lines:
    Code:
    // Single line comment
    
    /* Single line comment */
    
    /*
      Multiple
      line
      comment
    */
    [1] indeterminate: adj The state in which a variable tries its best to force your program into a submission with undefined behavior. Usually results in fatal errors, violation errors, nasal demons and embarrassing emails. Should be read as "Bend over so this program can do naughty things with your lower torso." Synonyms: damaging, dangerous, deleterious, detrimental, disobedient, hurtful, injurious, ill-behaved, naughty, ruinous, unhealthy, corrupt, criminal, delinquent, evil, sinful, vicious, vile, villainous, wicked, wrong.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Best Free Beginner Online Books/Tutorials?
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2004, 05:52 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM