jeudi 22 août 2019

Why do my for loops say the variable in the init statement is undeclared? Why won't my pointer address pass to my function?

I'm still in basic programming, learning C++ 11, so this is probably an easy fix, but I seriously need help man.

Basically, my assignment is to come up with a code that creates an array of 12 random integers between 1 and 1000. Then, using another array (of pointers), make it so that each element points to the values from the first array in ascending order via selection sort before displaying a table showing the values in both original and sorted order, side-by-side.

I'm still having a lot of problems with it. Pointers were just introduced to us and my whole class is a little stuck on this assignment, but I think I made some good progress. Unfortunately I can't get with the professor about this, because there are only 2 after class opportunities for teacher to help (as that's how many classes per assignment we get) and I already used up those opportunities.

Anyway, I need to know why the for loops in my functions give me a "variable not declared" error, even though I usually never have this problem with for loops. The other big problem is in passing the pointer array to my selection sort function. I need to swap the pointer's addresses around without affecting the first array, and I think I'm on the right track, but after many different attempts, I still can't figure out how to get the info to pass to the function.

Let me know if I'm making this too hard on myself and/or there's a simple trick to some of the stuff I'm doing too.

I tried using the same variable name as declared earlier in the function, I tried making a new variable name, retyping, checked spelling throughout the whole function.

THE FOLLOWING IS NOT THE WHOLE CODE, JUST THE PARTS THAT HAVE ME STUMPED.

int main(){
    int unsorted[SIZE];                                                     //Make an array called "unsorted" sized the specified size,
    int *sorted[SIZE] = {nullptr};                                          //as well as an array of pointers sized the specified size pointing nowhere in paricular.
    for(int i = 0; i < SIZE; i++){                                          //Then, repeat the following [specified size] number of times:
        sorted[i] = (unsorted + i);                                         //set the [num times repeated] element in the pointer array to the address of the [num times repeated] element in the unsorted array,
    }                                                                       //effectively creating two arrays, one normal unspecified array of [size] elements and an array of pointers pointing at each element sequentually.
    loadArray(unsorted, SIZE);                                              //Then, set all of the values in the original not-pointer array to random values between MIN and MAX using the loadArray function
    selectionSort(sorted, SIZE);                                            //Then, with the selectionSort function, set the pointers in the pointer function to the values in the unsorted function in order from smallest to largest. (without modifying the unsorted array)
    displayResults(unsorted, sorted, SIZE);                                 //Now that all the values have been set and sorted, display the values
}

void selectionSort(int arr[]){                              //selectionSort function that sorts the addresses in unsorted[] array into the sorted[] array via selecton sort according to the value in that address.
    int smallestValue = MAX;                                            //First, make an int called smallestValue to store the smallest value found via scanning the unpacked addresses in the array of pointers (sorted[])
    int currentPointer = 0;                                             //Then, make a counter int variable called currentPointer to represent the current pointer element onward to scan the smallest value for.
    int *smallestValueAddress = nullptr;                                //Lastly, a temporary pointer variable to store the address of the element that holds the smallest value.

    while (currentPointer < SIZE){                                      //Inside a while loop, counting up currentPointer until it reaches the number of pointers, do the following:

        //1. Scan the unpacked values from currentPointer to the last pointer in arr[], setting smallestValueAddress to the smallest value found
        for (counter = 0; counter < SIZE; counter++){
            counter = counter + currentPointer;
            if (*arr[counter] < smallestValue) {smallestValue = *arr[counter]; smallestValueAddress = (arr + counter)}
        }
        //2. Swap the value of the pointer that returned the smallest value with the address of currentPointer.
        swap (smallestValue, arr[currentPointer]);

        currentPointer++;
    }
}

void displayResults(int unsorted[], int *sorted[], int size){                       //displayResults function to display the results to the screen.
    for (count = 0; count < (size - 1); count++){                                   //Make a variable called count and set it to 0, then while it's less than the last subsc element in the arrays, count up and 
        cout << setw(8) << unsorted[count] << setw(10) << sorted[count] << endl;    //c out with width set to 8 the unsorted array's values, then with width set to 10 c out the value of the address in the sorted array.
    }
}

The output is supposed to look something like:

Hello

ORIGINAL    SORTED
==================
     479       100
     493       214
     818       303
     641       375
     689       436
     926       479
     100       493
     303       641
     700       689
     436       700
     214       818
     375       926

Goodbye

But instead I get the following errors:

In function 'int main()': 49    28  [Error] cannot convert 'int**' to 'int*' for argument '1' to 'void selectionSort(int*)'

In function 'void selectionSort(int*)': 77      8   [Error] 'counter' was not declared in this scope

In function 'void displayResults(int*, int**, int)': 89     7   [Error] 'count' was not declared in this scope

Aucun commentaire:

Enregistrer un commentaire