473,770 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3048

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************ *********@i39g2 000cwa.googlegr oups.com>,
"oLgAa25" <ol********@yah oo.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************ **********@e56g 2000cwe.googleg roups.com>,
"oLgAa25" <ol********@yah oo.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

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

Similar topics

2
24639
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 code for a collection base for(int i = this.Values.Count; i > 0 ; i--) { //Set local object
4
11497
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 select, the first one in the listbox is always removed.(The listitem with an index of 0. Box is set to single selection mode) I've looked at multiple examples and they all do it this way. What's wrong? (variables are also being set to the values...
9
15343
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 <> "TheOneIWantToKeep" Then Me.Controls.RemoveAt(i) End If
31
4604
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 are more objects that match the first object that i grabbed. If they match then I put them in an array. I would like to remove each match from the arraylist as I find them to speed things up and so that they don't get checked again. If I try...
2
3941
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 the ListView or not. The natural place to inspect this is in the AfterLabelEdit event... If the decision is to take the item out, I used the ListViewItem.Remove(). However - check this out: A) say that the item's index is N (indices are...
7
6609
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 include them in the set that is displayed to the user) but I don't want to delete the corresponding row from the database. I've tried using the .Rows.Remove() method but an exception is thrown to say I don't have a 'delete' command in the data...
15
15627
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 output string. But this was trivial. The other option is to use pointers and shift all the characters after the space by one space to the left. I did this program using pointers and then using array too and I get segmentation fault. What is going...
6
2062
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 the numbers back to the screen showing what the user input. 3. ask the user what integer they would like to remove. 4. use a loop to remove ALL occurrance of that integer, and the output the new numbers back to the screen for the user to see the...
10
18079
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) { // do something... if (entry.fieldA == 0)
6
4255
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 this program has more then one class and also uses an interface. Right now I am working on ArrayBasedPD class. I am trying to write a code for the remove method (line 158) that allows the user to enter a name, once the program sees that the name is...
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.