473,379 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

remove index

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
14 3010

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
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
Hello,
I made it void. Thank you
and I had my program running. Thank you all

Apr 7 '06 #4

"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
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
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
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
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
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
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
[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
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
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
it smells like a troll

Apr 13 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MFRASER | last post by:
How do I go about looping through a hash table and removing items. I know how do this in a collectionbase, but can't iterate through the hash table with out getting an error. Here is my sample...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
9
by: Merlin | last post by:
Hi, My code below doesn't work does anyone have any pointers? All my controls are programically added. Dim i As Int16 For i = 0 To Me.Controls.Count - 1 If Me.Controls(i).Name <>...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
2
by: Kela | last post by:
An interesting problem: I have a ListView with LabelEdit set to TRUE. When I change the label, I want to make some decisions as to whether the ListViewItem (that's just been edited) should stay in...
7
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
6
jlandbw04
by: jlandbw04 | last post by:
Okay. Here's the deal. I have this assignment for college that has me completely puzzled. I need this assignment to do the following: 1. input 12 integers into an array from the user. 2. output...
10
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I wanted to go through each entry(?) of ArrayList and remove some particular entry. So I tried following but it throws exception at runtime: foreach (myEntry entry in myArrayList) {...
6
by: falconsx23 | last post by:
I am trying to write a code for a Phone Directory program. This program is suppose to allow the user to enter a name or directory and then program can either add, save or even delete an entry. Also...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.