please help me !

hi
i have assiment and i shuld Suppos to send it after a few hours.

the ass is
LAB1-2 Chap10

A) Write a function, removeAt, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, index). The function should delete the array element indicated by index. If index is out of range or the array is empty, output an appropriate message.

B) Write a function, removeAll, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, Item). The function should find and delete all of the occurrences of Item in the array.
NOTE: Use function removeAt in the function removeAll.
C) Write a main function to test your functions



>>>
my solution;





#include<iostream>
using namespace std;
void removeat(int list[],int &leanght,int index)
{

if(leanght==0)
{
cout<<"the array its empty";
else if (index<0 || index>leanght)
cout<<"out of range";

else

{
for (int j=index;j<leanght;j++)
list[j]=list[j+1];
leanght--;
}
}

void removeall(int list[],int &leanght,int item)
{
int loc;
for(int i=0;i<leanght;i++)
if(list[i]==item)
loc=removeat( list,leanght, i);
}
int main();
{
int pos;
int leanght=8;
int list[8]={2,3,5,5,5,4,8,9};
int item;
cout<<list[];
cout<<"enter item";
cin>>item;
pos=removeall( list,leanght, item)
return 0;
}
Last edited on
What is the problem?
i dont know
but there are many erros
and i dont knoe why
what kind of errors? runtime compile time? How do they appear?

One thing at a glance: removeall() returns void hence you cannot assign it to pos (which isn't used anyway)
You should probably begin by reading the errors, referring to the source code and figuring out why.

cout<<list[];

You cannot print out an array in this fashion. You need to use a loop and print out each element in turn.

Your removeall() will fail on arrays where consecutive elements are equal to the item you're searching for.

removeall(() doesn't return a value so pos = removeall(...) doesn't make sense.

Obviously you found the format buttons on the right when you're editing or making a post. The one with <> on it should be used for code.

Saying "I don't know but there are many errors" isn't useful. If you can't understand the errors, list them with the code. Also, the subject is useless.

http://www.cplusplus.com/forum/beginner/1/
now i change it to
:
#include<iostream>
using namespace std;
int removeat(int list[],int &leanght,int index)
{

if(leanght==0)
{
cout<<"the array its empty";
else if (index<0 || index>leanght)
cout<<"out of range";

else

{
for (int j=index;j<leanght;j++)
list[j]=list[j+1];
leanght--;
}
return index;
}

int removeall(int list[],int &leanght,int item)
{
int loc;
for(int i=0;i<leanght;i++)
if(list[i]==item)
loc=removeat( list,leanght, i);
}
int main();
{
int pos;
int leanght=8;
int list[8]={2,3,5,5,5,4,8,9};
int item;
for(int i=0;i<8;i++)
cout<<list[i];
cout<<"enter item";
cin>>item;
pos=removeall( list,leanght, item);
for(int i=0;i<8;i++)
cout<<list[i];
return 0;
}


the errors is




In function 'int removeat(int*, int&, int)':
Line 9: error: expected primary-expression before 'else'
compilation terminated due to -Wfatal-errors.




Line 9: error: expected primary-expression before 'else


Where's the closing } after your if block?
well, after the if(leanght==0) in removeat() there's a { without corresponding }. Remove that {


why don't you listen to cire?
#include<iostream>
using namespace std;
int removeat(int list[],int &leanght,int index)
{

if(leanght==0)
cout<<"the array its empty";

else if (index<0 || index>leanght)
cout<<"out of range";

else

{
for (int j=index;j<leanght;j++)
list[j]=list[j+1];
leanght--;
}
return index;
}

int removeall(int list[],int &leanght,int item)
{
int loc;
for(int i=0;i<leanght;i++)
if(list[i]==item)
loc=removeat( list,leanght, i);
}
int main();
{
int pos;
int leanght=8;
int list[8]={2,3,5,5,5,4,8,9};
int item;
for(int i=0;i<8;i++)
cout<<list[i];
cout<<"enter item";
cin>>item;
pos=removeall( list,leanght, item);
for(int i=0;i<8;i++)
cout<<list[i];
return 0;
}


ok , i do that
but still have errors


Line 31: error: expected unqualified-id before '{' token
compilation terminated due to -Wfatal-errors.
cire wrote:
Obviously you found the format buttons on the right when you're editing or making a post. The one with <> on it should be used for code.



int main(); -> int main() // no ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;
int removeat(int list[],int &leanght,int index)
{

if(leanght==0)
cout<<"the array its empty";

else if (index<0 || index>leanght)
cout<<"out of range";

else

{
for (int j=index;j<leanght;j++)
list[j]=list[j+1];
leanght--;
}
return index;
}

int removeall(int list[],int &leanght,int item)
{
int loc;
for(int i=0;i<leanght;i++)
if(list[i]==item)
loc=removeat( list,leanght, i);
}
int main();
{
int pos;
int leanght=8;
int list[8]={2,3,5,5,5,4,8,9};
int item;
for(int i=0;i<8;i++)
cout<<list[i];
cout<<"enter item";
cin>>item;
pos=removeall( list,leanght, item);
for(int i=0;i<8;i++)
cout<<list[i];
return 0;
}



thanks
still have problems !
int main(); -> int main() // no ;

This means that you should change the thing on the left to the thing on the right. Your code still has the thing on the left.
how i change ?
Is it only one thing you can take from a post? In addition to the code tags, coder777 pointed out you have a semi-colon where you shouldn't have one. Line 29.

thanks. still have problems !


I posted this link earlier which you obviously didn't read:
http://www.cplusplus.com/forum/beginner/1/

Be precise and informative about your problem
•Describe the symptoms of your problem carefully and clearly.
•Describe the environment in which it occurs (machine, OS, application, whatever).
•Describe the research you did to try and understand the problem before you asked the question.
•Describe the diagnostic steps you took to try and pin down the problem yourself before you asked the question.

Do the best you can to anticipate the questions a respondent will ask, and answer them in advance in your request for help.


When asking about code
Don't ask others to debug your broken code without giving a hint what sort of problem they should be searching for.


Use meaningful, specific subject headers
The subject header is your golden opportunity to attract qualified experts' attention. Don't waste it on babble like 'Please help me' Don't try to impress us with the depth of your anguish; use the space for a super-concise problem description instead.

how i change ?


If you do not know how to change the code, you are not ready for programming. You have to type on the keyboard. It is astonishing that you can type how i change ? but you cannot type int main().
Last edited on
Topic archived. No new replies allowed.