# beginner

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-05-2004
jennilla
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.
• 06-05-2004
Salem
> 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
• 06-05-2004
electRONix
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
• 06-05-2004
Codeplug
>> hope this helps
you get a good grade on your homework.

Homework Policy

gg
• 06-09-2004
jennilla
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>?
• 06-09-2004
jennilla
and one more question - what is deffensive programming?
• 06-09-2004
elad
>>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.
• 06-09-2004
Prelude
>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".
• 06-09-2004
jennilla
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;
}
• 06-09-2004
Prelude
>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.
• 06-09-2004
jennilla
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;
};
• 06-09-2004
Prelude
>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++){`
• 06-09-2004
jennilla
ok, now it works, but works wrong, it writes in wrong numbers.
• 06-09-2004
sand_man
Code:

```for (i=0, i<10, i++){ list[j] = i; };```
; not needed, but not illegal.
• 06-09-2004
Prelude
>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`
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last