Re: [Dev-C++] Memory Error in simple array sort
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-10-02 15:11:07
|
How can you call fillArray and say that the size of the array is 10, when
I can clearly see that your array declaration is only of size 1...
/Per W
On Thu, 2 Oct 2003, Opacki, Joseph wrote:
> When I run the program below, I am getting a memory error:
>
> The instruction ...referenced memory...could not be "read".
>
> I don't exactly don't know what is going on hear, so I thoughI could reach
> out to the experts and ask.
>
> The program asks for a list of integers and inputs them into an array. You
> end the list of integers with a negative number, then the app sorts it and
> redisplays your input to you lowest to greatest. When ran I got the error
> above and it crashed. I then put the sytem("pause") statement in and it ran
> fine. After it paused, then it crashed.
>
> Here is the code:
>
> // array sort demonstration
> #include <iostream>
> #include <cstdlib>
>
> void fillArray(int a[], int size, int& numberUsed);
> /*
> numberUsed is the number of values stored in a. a[0] through
> a[numberUsed - 1]
> have been filled with nonnegative integers read from the keyboard
> */
>
> void sort(int a[], int numberUsed);
> /*
> the values of a[0] through a[numberUsed - 1] have been rearranged so
> that
> a[0] <= a[1] <= a[2] ... a[numberUsed - 1]
> */
>
> void swapValues(int& v1, int& v2);
> // interchanges the values of v1 and v2
>
> int indexSmallest(const int a[], int startIndex, int numberUsed);
> /*
> returns the index of i such that a[i] is the smallest of the values
> a[startIndex, a[startIndex + 1] ..., a[numberUsed - 1]
> */
>
> int main()
> {
> using namespace std;
>
> cout << "This program sorts numbers from lowest to highest." << endl;
>
> int sampleArray[1], numberUsed;
>
> fillArray(sampleArray, 10, numberUsed);
> sort(sampleArray, numberUsed);
>
> cout << "In sorted order the numbers are: " << endl;
>
> for ( int index = 0; index < numberUsed; index++ )
> {
> cout << sampleArray[index] << " ";
> }
>
> cout << endl;
>
> system("PAUSE");
> return 0;
> }
>
> void fillArray(int a[], int size, int& numberUsed)
> {
> using namespace std;
>
> cout << "Enter up to " << size << " nonnegative whole number.\n"
> << "Mark the end of the list with a negative number.\n";
>
> int next, index = 0;
> cin >> next;
>
> while ( (next >= 0) && (index < size) )
> {
> a[index] = next;
> index++;
> cin >> next;
> }
>
> numberUsed = index;
> }
>
> void sort(int a[], int numberUsed)
> {
> int index_Smallest;
>
> for ( int index = 0; index < numberUsed - 1; index++ )
> { // place the correct value in a[index]
> index_Smallest = indexSmallest(a, index, numberUsed);
> swapValues(a[index], a[index_Smallest]);
> // a[0] <= a[1] <= ... <= a[index] are the smallest of the original
> array
> // elements. The rest of the lements are in the remaining
> positions.
> }
> }
>
> void swapValues(int& v1, int& v2)
> {
> int temp;
> temp = v1;
> v1 = v2;
> v2 = temp;
> }
>
> int indexSmallest(const int a[], int startIndex, int numberUsed)
> {
> int min = a[startIndex], indexMin = startIndex;
>
> for ( int index = startIndex + 1; index < numberUsed; index++ )
> {
> if ( a[index] < min ) // change to > to change sort arrangement
> {
> min = a[index];
> indexMin = index;
> // min is the smallest of a[startIndex] through a[index]
> }
> }
>
> return indexMin;
> }
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|