C++ Applications of Arrays(Searching and Sorting)

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Hello all, I am stuck once again. I created an array with a size of 12 and what I have to do is let the user pick an index and the index they pick removes it from the array. I need to output the final array elements, array size and the array element that was removed.

I haven't gotten to the delete part even though I can't find how to delete an index from an array in the chapter so was going to ask the professor on Monday(unless one of you know how to). Here is what I have so far but giving me 3 errors which I don't see why I am getting them.

Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;

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


	int main() {

		const int len = 1;
		int ylist[len];
		int index = 1;

		removeAt(ylist, len, index);

		return 0;
	}

	void removeAt(int ylist[], int len, int index)
	{

		 len = 12;
		 ylist[len] = {4, 23, 65, 34, 82, 37, 12, 17, 24, 36, 82, 51};


		cout << "Please pick an array index from 0 to " << (len - 1) << ": ";
		cin >> index;

		for (int loc = 0; loc < len; loc++)
			if (ylist[loc] == ylist[index])
			{
				cout << "The array element that you are removing is " << ylist[loc] << endl;
			}

	}

ERRORS:
1>c:\users\welcome back\documents\visual studio 2008\projects\searchandremove\searchandremove\search.cpp(25) : error C2059: syntax error : '{'
1>c:\users\welcome back\documents\visual studio 2008\projects\searchandremove\searchandremove\search.cpp(25) : error C2143: syntax error : missing ';' before '{'
1>c:\users\welcome back\documents\visual studio 2008\projects\searchandremove\searchandremove\search.cpp(25) : error C2143: syntax error : missing ';' before '}'

Why am I getting these? I have the ; where they are needed to be, at least I think so...
 
Solution
It's an integer variable, so it's always going to have some value. Of course, you don't have to display it. If you keep track in the program of how many elements you have deleted, say n, you can choose to not display the last n elements in the array. Alternatively, rather than zero you could give the "blanked elements" an obviously false value (e.g. -999999; but what value could be considered "obviously false" depends upon what constraints there are on the input values) and just not display elements containing that value.

Simple c++ arrays are fixed in size (that's not strictly true but I'm guessing that it's far beyond the level that you are now at to talk about ways of treating them as dynamically sized), so the elements are always...

Ijack

Distinguished
Line 23 is all wrong. You are trying to initialize an integer (ylist[len]) to an array. In any case, you shouldn't be initializing the array within the delete function. Delete line 23 and add the initialization to line 11 (where you declare the array). Also, you've declared the array to be of length 1, so line 10 needs to read "= 12". The program will then compile (but isn't as yet doing anything).

There's only two ways, that I'm aware of, to remove element n from a traditional array: (1) move element n+1 to n, n+2 to n+1, etc., and then blank out the last element. This doesn't strictly remove the e,ement as you still end up with an array of the same length (with a last blank element), but traditional arrays can't be resized. (2) If you really want to resize, copy the elements to a new array.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Oh ok I see thank you. Yea I had a different code, which was allowing the user to pick the array size and entering the values in but the assignment gave us a set array, I forgot to change line 10.

Also I think the move element will work because the assignment says "Remove element at index = 7, then move the elements to index posistions: 7, 8, 9."

 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
How would you move like element 7 to the end and blank it out? Would like ylist[6]+5 move it to the end and move the others up one? I am confused how the move works.

Found this on the internet but it doesn't really make sense to me:

Code:
for (int i = index; i < len; i++)
		{
			ylist[index] = ylist[index+1];
			ylist[len-1] = 0;
		}
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
UPDATE:

Code:
//Jonathan Salina

	#include <iostream>
    #include <iomanip>
    using namespace std;

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


	int main() {

		const int len = 12;
		int ylist[len] = {4, 23, 65, 34, 82, 37, 12, 17, 24, 36, 82, 51};
		int index = 1;

		removeAt(ylist, len, index);

		return 0;
	}

	void removeAt(int ylist[], int len, int index)
	{

		cout << "Please pick an array index from 0 to " << (len - 1) << ": ";
		cin >> index;

		

		for (int loc = 0; loc < len; loc++)
			if (ylist[loc] == ylist[index])
			{
				cout << endl << "The array element that you are removing is " << ylist[loc] << endl;
			}

		for (int i = index; i < len; i++)
		{
			ylist[index] = ylist[index+1];
			ylist[len-1] = 0;
		}

		cout << "Final Array Elements are: ";

		for (int x = 0; x < len; x++)
		{
			cout << ylist[x] << " ";
		}
		cout << endl;

	}

The output isn't coming out right.

"The array element that you are removing is: 17" - This output is correct
"Final Array Elements are: 4 23 65 34 82 37 12 24 24 36 82 0" - This is wrong
 

Ijack

Distinguished
A couple of errors.

In line 37 you are moving ylist[index+1] to ylist[index], but index is a fixed number. It's ylist that you are interested in not ylist[index]. (If you were to debug the program and single-step through the "for" loop whilst watching the values of the variables you'd soon see what was going wrong.)

Line 38. You don't want to zero the final element until you have moved all the others, so move this statement outside and after the "for" loop.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Do I have to have a zero there? is there a way to like still have the 12 elements but have like ylist[11] have nothing in it? So it doesn't show anything not even 0? Just an empty element.
 

Ijack

Distinguished
It's an integer variable, so it's always going to have some value. Of course, you don't have to display it. If you keep track in the program of how many elements you have deleted, say n, you can choose to not display the last n elements in the array. Alternatively, rather than zero you could give the "blanked elements" an obviously false value (e.g. -999999; but what value could be considered "obviously false" depends upon what constraints there are on the input values) and just not display elements containing that value.

Simple c++ arrays are fixed in size (that's not strictly true but I'm guessing that it's far beyond the level that you are now at to talk about ways of treating them as dynamically sized), so the elements are always there. In real-life examples if you needed a structure that could vary in size you would use something like a linked list rather than an array. Again I suspect that this is getting beyond the level that you are currently at, so I wouldn't worry about it, but no doubt you'll learn about such data structures later in your course.
 
Solution

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Then my textbook is stupid. Here's the exercise:

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.(Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted.


So it thinks we can delete an element from an array....
 

Ijack

Distinguished
Not so much stupid as perhaps slightly misleading. In C and C++ the onus is always on the programmer to keep track of the size of an array, in particular to ensure that the program doesn't try to write beyond the bounds of the array. So, even though the storage for the array is defined and it is not an error to write to any of the elements in that storage, the program can still determine how many elements of the array are "active" at any one time. In that, somewhat limited, sense the number of elements in the array can vary as far as the program is concerned.

As I said before, it's normal to use more sophisticated data structures when programming in C++. I'm pretty sure that the C++ Standard Library defines dynamic arrays (but note that the Standard Library is not an inherent part of the language). But for learning purposes you need to get to grips with the basics of the language before worrying about greater sophistication, which is what this exercise is all about.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
I understand, wasn't really thinking I could just not display like you said earlier which is working just fine, was just focused on how the problem said remove the element from the array. Thank you for all the help.