Thread: beginner

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    8

    beginner

    please help a beginner to start writing the code:
    I need to write a code that writes in a list numbers from 1 to 10 and then delets the even ones. i dont know what to start with. please help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I need to write a code that writes in a list numbers from 1 to 10
    Well this is the step you do first

    > and then delets the even ones.
    When you've got the first step working, then you do this
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User electRONix's Avatar
    Join Date
    Jun 2004
    Posts
    13

    Lightbulb here's what to do =)

    Quote Originally Posted by jennilla
    please help a beginner to start writing the code:
    I need to write a code that writes in a list numbers from 1 to 10 and then delets the even ones. i dont know what to start with. please help.
    Well I'm assuming you can start a new project and have the standard things included in your .cpp file: iostream, main function, namespaces... whatever....

    A list of numbers from 1-10 is probably best put into an array. Since you want an array of integergs your decleration for this array would be something like

    Code:
    int myarray[10];
    You said you're a beginner so I'm not quite sure if you understand that. the "int" part is the data type (you want to hold integers not floats or characters or anything else)... "myaray" is the name of your variable and the "[10]" indicates the capacity of your array.

    If you want to declare and initiate your array all in one step you can do this
    Code:
    int myarray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    there are other ways to initialize your array but that's the simplest.

    to delete the even numbers you want to have a "for loop", again i'm not sure of your experience with code... so i'll put the rest of the code up and you can play around with it until you understand what's happening

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
      int myarray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      
      //print the array
      for(int i = 0; i < 10; i++)
        cout << myarray[i] << " ";
        
      //delete evens
      for(int i = 1; i < 10; i+=2)
        myarray[i] = 0;  // makes the evens 0
    
      cout << endl; // new line 
      
      //print the array again
      for(int i = 0; i < 10; i++)
        cout << myarray[i] << " ";
      
      cout << endl; // new line 
      
      system("PAUSE");	
      return 0;
    }
    hope this helps

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> hope this helps
    you get a good grade on your homework.

    Homework Policy

    gg

  5. #5
    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>?

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

  7. #7
    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.

  8. #8
    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.

  9. #9
    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;
    }

  10. #10
    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.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    it still says that there is an error... confused...
    int main()
    {
    int i,list[10];
    int j=0;

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

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it still says that there is an error
    That's because you ignored my second suggestion.
    Code:
    for (i=0, i<10, i++){
    Change it to
    Code:
    for (i=0; i<10; i++){
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    ok, now it works, but works wrong, it writes in wrong numbers.

  14. #14
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    for (i=0, i<10, i++){
    list[j] = i;
    };
    ; not needed, but not illegal.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but works wrong, it writes in wrong numbers
    Would those numbers be large values with ridiculous digit lengths compared to what you expect?
    Code:
    list[j] = i; // One of these things, is not like the other
    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