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.
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.
if you want the numbers 1-10 in the array and you don't want to do it this way
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
you can do it this way
for (i=0, i<10, i++)
{
list[j] = i;
};
but the latter will put the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 in your array. To get 1-10 in the array using the latter format you can change the value of i so it ranges from 1 and <= 10 or from 1 to < 11. Or you can leave the range of i as you have it and change the value assigned to list[j] by always adding 1 to i before the assignment.
Last edited by elad; 06-09-2004 at 11:19 AM.
excactly, should it be list[i] = i;?