By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

remove index

P: n/a
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[i] << ", ";
}
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--;

}

Apr 7 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a

oLgAa25 wrote:
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[i] << ", ";
}
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--;

}


Apr 7 '06 #2

P: n/a
In article <11*********************@i39g2000cwa.googlegroups. com>,
"oLgAa25" <ol********@yahoo.com> wrote:
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.
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 7 '06 #3

P: n/a
Hello,
I made it void. Thank you
and I had my program running. Thank you all

Apr 7 '06 #4

P: n/a

"Jaspreet" <js***********@gmail.com> wrote in message
{
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


Apr 7 '06 #5

P: n/a
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

Apr 7 '06 #6

P: n/a
oLgAa25 wrote:
I think This summer I will be refering to this web site more often.


Web site? I thought I was on Usenet...
Apr 7 '06 #7

P: n/a
Victor Bazarov wrote:
oLgAa25 wrote:
I think This summer I will be refering to this web site more often.


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]
Apr 7 '06 #8

P: n/a
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

Apr 9 '06 #9

P: n/a
In article <11**********************@e56g2000cwe.googlegroups .com>,
"oLgAa25" <ol********@yahoo.com> wrote:
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. :-)

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 9 '06 #10

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

Apr 9 '06 #11

P: n/a
[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]

Apr 10 '06 #12

P: n/a
re******@gmail.com wrote:
[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!
Apr 10 '06 #13

P: n/a
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.;-)

Apr 12 '06 #14

P: n/a
it smells like a troll

Apr 13 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.