remove index

O

oLgAa25

Hello all,
I am back
I have this question
everything compiles fine, but I just need to check for an index that is
more than the array size. Although it says out of bound, but it is
still deleting the last element. Can someone help please
Thank you all
Olga

I just wanted to test my function,so don't bother with main, and print
functions
/*******************************************************************************
Write a function to, removeAt, that takes three parameters: an array
of *
integers, the length of the array,, and an integer say(index). The
function *
should delete the array element indicated by the index. If index is
out of *
range or the array is empty, output an appropriate message. (Note
that after*
deleting the element, the array size is reduced by 1.) assume the
array is *
unsorted
*
/******************************************************************************/
#include <iostream>

using namespace std;

void print (int [], int);
int removeAt(int [], int&, int);

int main()
{
int list[100] = {1, 2, 3, 6, 10, 14, 20, 25, 30, 40};
int listSize = 10, index;
print (list, listSize);
cout << "The Item to be removed indicated by the index: " ;
cin >> index;
removeAt(list, listSize, index);
print (list, listSize);
system ("PAUSE");
}


void print(int list[], int listSize)
{
for (int i = 0; i < listSize -1; i++)
{
cout << list << ", ";
}
cout << list[listSize-1] << endl;
}


int removeAt(int list[], int& listLength, int index)
{
int j;
if (index < 0 || index > listLength - 1)
cout << "List is out of bound " << endl;

for(j = index; j < listLength -1; j++)

list[j] = list[j+1];
listLength--;



}
 
J

Jaspreet

oLgAa25 said:
Hello all,
I am back
I have this question
everything compiles fine, but I just need to check for an index that is
more than the array size. Although it says out of bound, but it is
still deleting the last element. Can someone help please
Thank you all
Olga

I just wanted to test my function,so don't bother with main, and print
functions
/*******************************************************************************
Write a function to, removeAt, that takes three parameters: an array
of *
integers, the length of the array,, and an integer say(index). The
function *
should delete the array element indicated by the index. If index is
out of *
range or the array is empty, output an appropriate message. (Note
that after*
deleting the element, the array size is reduced by 1.) assume the
array is *
unsorted
*
/******************************************************************************/
#include <iostream>

using namespace std;

void print (int [], int);
int removeAt(int [], int&, int);

Why is the return type int ? Make it void unless you want to return the
status of the remove operation (say 0 for success and 1 for failure). I
would rather make it void.
int main()
{
int list[100] = {1, 2, 3, 6, 10, 14, 20, 25, 30, 40};
int listSize = 10, index;
print (list, listSize);
cout << "The Item to be removed indicated by the index: " ;
cin >> index;
removeAt(list, listSize, index);
print (list, listSize);
system ("PAUSE");
}


void print(int list[], int listSize)
{
for (int i = 0; i < listSize -1; i++)
{
cout << list << ", ";
}
cout << list[listSize-1] << endl;
}


int removeAt(int list[], int& listLength, int index)


I would prefer:
void removeAt(int list[], int& listLength, int index)
{
int j;
if (index < 0 || index > listLength - 1)
cout << "List is out of bound " << endl;

Add 1 more statement inside if to return from here.
if (condition...) {
cout statement;
return;
}
for(j = index; j < listLength -1; j++)

list[j] = list[j+1];
listLength--;



}
 
D

Daniel T.

"oLgAa25 said:
Hello all,
I am back
I have this question
everything compiles fine, but I just need to check for an index that is
more than the array size. Although it says out of bound, but it is
still deleting the last element. Can someone help please
Thank you all
Olga

I just wanted to test my function,so don't bother with main, and print
functions
/*****************************************************************************
**
Write a function to, removeAt, that takes three parameters: an array
of *
integers, the length of the array,, and an integer say(index). The
function *
should delete the array element indicated by the index. If index is
out of *
range or the array is empty, output an appropriate message. (Note
that after*
deleting the element, the array size is reduced by 1.) assume the
array is *
unsorted
*
/*****************************************************************************
*/


You function says it returns an int, what does that int mean? If the int
returned is the new size of the array, then 'listLength' probably
shouldn't be a reference.
int removeAt(int list[], int& listLength, int index)
{
int j;
if (index < 0 || index > listLength - 1)
cout << "List is out of bound " << endl;

for(j = index; j < listLength -1; j++)

list[j] = list[j+1];
listLength--;

Your indentation above is very misleading. Note that the line above
executes no matter what happens in the function.
 
H

Howard

Jaspreet said:
{
int j;
if (index < 0 || index > listLength - 1)
cout << "List is out of bound " << endl;

Add 1 more statement inside if to return from here.
if (condition...) {
cout statement;
return;
}
for(j = index; j < listLength -1; j++)
list[j] = list[j+1];
listLength--;
}

Personally, I prefer to put the intended actions of the function first, and
to also only have one exit point. Something more like:

if (everything is ok)
{
do the stuff this function is intended for
}
else
{
report that everything is NOT ok
}

-Howard
 
O

oLgAa25

Thank you all,
I fixed the problem. I really appreciate your help.
I think This summer I will be refering to this web site more often.
although I am taking C++, but I don't think that I am doing good
enough. So I hope to find the help I will need
Thank you all
 
R

red floyd

Victor said:
Web site? I thought I was on Usenet...

[RANT type="old man"]
You know those young'uns today. They think that the whole bloody
Intarweb is all thar is. Why back in my day...
rassum-fassum-mumble-grumble darned kids....
[/RANT]
 
O

oLgAa25

How about this. If you have some constructive criticism, please say it.
If not, then Just keep quiet. Silence is Golden.
When I found the group I went through google.com and then chose groups,
and through there I got to http://groups.google.com/group/comp.lang.c++
and for your Info. I am not a kid, I am a mother of two kids. so some
respect is appreciated.
I don't recall being rude towards any body here or any where. So just
be Quiet, and don't answer my questions if you can't help
 
D

Daniel T.

"oLgAa25 said:
How about this. If you have some constructive criticism, please say it.
If not, then Just keep quiet. Silence is Golden.
When I found the group I went through google.com and then chose groups,
and through there I got to http://groups.google.com/group/comp.lang.c++
and for your Info. I am not a kid, I am a mother of two kids. so some
respect is appreciated.
I don't recall being rude towards any body here or any where. So just
be Quiet, and don't answer my questions if you can't help

oLgAa25,

Please don't take offense. You may be as old as I am, but you are new to
usenet and that is what "kid" referred to in this context.

Some of us have been posting to, and reading from, usenet for almost 20
years now so allow us our "old man rants" now and then. Really, no
offense was intended. :)
 
O

oLgAa25

Hmmm, if you have been on for 20 years then hmm. you are older ;-).
But thanks for your kind words ;-)
Thank you all
 
R

redfloyd

[complaint redacted]

[HUMOR]
In the future, all my jokes will have [HUMOR] tags to assist the
humor-impaired, in compliance with the Americans with Disability Act.
[/HUMOR]
 
V

Victor Bazarov

[complaint redacted]

[HUMOR]
In the future, all my jokes will have [HUMOR] tags to assist the
humor-impaired, in compliance with the Americans with Disability Act.
[/HUMOR]

And the future is NOW!
 
O

oLgAa25

hahahahaha, you guys are so cool.
I am sorry to not to take a joke, although I love jokes, but life's
stress and demands are killing my sense of humor. need I to say more;)

anyways, I will be back to this "Web Site" and I will be seeking more
help.;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top