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.
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.
> 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.
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....Originally Posted by jennilla
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
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.Code:int myarray[10];
If you want to declare and initiate your array all in one step you can do this
there are other ways to initialize your array but that's the simplest.Code:int myarray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
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
hope this helpsCode:#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
you get a good grade on your homework.
Homework Policy
gg
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>?
and one more question - what is deffensive programming?
>>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.
>i could rewrite the array somehow?
Yes, something like this would be a good start:
>and why do I need (int argc, char *argv[])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; } }
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.
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;
}
>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:
[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.Code:// Single line comment /* Single line comment */ /* Multiple line comment */
My best code is written with the delete key.
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;
};
>it still says that there is an error
That's because you ignored my second suggestion.
Change it toCode:for (i=0, i<10, i++){
Code:for (i=0; i<10; i++){
My best code is written with the delete key.
ok, now it works, but works wrong, it writes in wrong numbers.
; not needed, but not illegal.Code:for (i=0, i<10, i++){ list[j] = i; };
>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.