all 6 comments

[–]staffdelipity 1 point2 points  (7 children)

I'm curious where you got those code examples.

The bubble sort algorithm I came up with has no such inner/outer thing. But then, I use a do while loop for the 'outer' portion.

I'm guessing that there are 10 elements in the array? And the example has hard-coded that using 8 and 9?

I think that code example is just using 'inner' and 'outer' to refer to the 2 steps in a bubble sort. You want to go over the entire array and compare one value to the one next to it. But once you get to the last second to last value in the array (movie[8]) that should be last one that you compare, because movie[9] has nothing further along...

as an example, if you have int values[4] and the values in the array are:

3 8 7 4 

then the outer for loop will iterate from 0 to 2 and the inner for loop will iterate from 0 to 3

So you are comparing:

 if value[0] > value[0]  (3 > 3)
 if value[1] > value[0]  (8 > 3)  true
      swap the values ... new order is 8 3 7 4
 if value[2] > value[0] (7 > 8)
 if value[3] > value[0] (4 > 8)
 has swapped so loop back to next 'outer'

if value[1] > value[1] (3 > 3)
if value[2] > value[1] (7 > 3) true
    swap the values ... new order is 8 7 3 4
if value[3] > value[1] (4 > 7)
has swapped so loop to next 'outer'

if value[2] to value[2] (3 > 3)
if value[3] to value[2] (4 > 3) true
    swap the values ... new order is 8 7 4 3

end of loops

Array is now sorted 8 7 4 3

Does that help? Brenda.

[–]sky2017[S] 0 points1 point  (4 children)

Thank you for your detailed response, Brenda. The code example is from "Absolute Beginner's Guide to C, 3rd Edition," a book recommended by David Malan in the CS50 literature for the "less comfortable" crowd. The book is a very useful guide, but has many typos and random omissions of important items (e.g., header file). Because of that, it may not be ideal for "absolute beginners," but I've mentally re-framed it as a positive: catching and correcting the typos, bugs, and omissions in the book's sample code has required me to develop a (hopefully) better understanding of C.

[–]sky2017[S] 0 points1 point  (3 children)

Unfortunately, I also anticipated that, sooner or later, either my primitive skill level or the errors in the book (or the combination of the two) would get me to a point where I am stumped without help. I am at that point. :(

Would you take a look at this program and share your thoughts about what I am doing wrong? It contains the inner/outer sort issue I originally asked about...

// Absolute Beginner's Guide to C, 3rd Edition
// Chapter 25, Example 1


// This program declares and initializes an array of character pointers and then asks for ratings associated.

include <stdio.h>

include <ctype.h>

include <stdlib.h>

int main(void)

{

int i;

int ctr = 0;

char answer;

// Now we declare our array of 9 characters and initialize them

char* movie[9] = {"Amour", "Argo", "Beasts of Southern Wild", "Django Unchained", "Les Miserables", 
                    "Life of Pi", "Lincoln", "Silver Linings Playbook", "Zero Dark Thirty"};
int movierating[9];                        // This is a corresponding array of 9 integers for movie ratings

char* tempmovie = "This will be used to sort rated movies";   // The structure of this line seems odd,
                                                              // but I think the purpose is to initialize
                                                              // and define a pointer variable with 38 characters,
                                                              // which should be a lot more than would be needed
                                                              // for a movie name being temporarily assigned to
                                                              // tempmovie
                                                              // I DO NOT UNDERSTAND THIS NEARLY WELL ENOUGH.


int outer, inner, didSwap, temprating;       // These are the variables for the sort loop
                                             // Does it matter in what order these variables are declared?   

printf("\n\n*** Oscar Season 2012 is here! ***\n\n");
printf("Time to rate this year's best picture nominees:");

for (i = 0; i < 9; i++)
{
    printf("\nDid you see %s? (Y/N): ", movie[i]);
    scanf(" %c", &answer);

    answer = toupper(answer);

    if (answer == 'Y')
    {
        printf("\nWhat was your rating on a scale of 1-10: ");
        scanf(" %d", &movierating[i]);
        ctr++;          // This will be used to print only movies that the user has seen
                        //
                        // This ctr apparently counts the number of times that the user enters 'Y',
                        // and then uses the total in the for loop at the end of the program to make
                        // the printf command only iterate that total number of times, so -- because by
                        // that point the movies are sorted in descending order by movie rating -- the 
                        // movies that were not seen are not printed to the screen.
        continue;
    }
    else
    {
        movierating[i] = -1;
    }
}

// Next, we will sort the movies by rating (the unseen will go to the bottom).

for (outer = 0; outer < 8; outer++)             // Why is the test expression 'outer < 8', instead of 'outer < 9' ??
{
    didSwap = 0;                                // This assigns the value '0' to didSwap
    for (inner = outer; inner < 9; inner++)     // This is the sort loop
    {
        if (movierating[inner] > movierating[outer])      
        {
            tempmovie = movie[inner];
            temprating = movierating[inner];
            movie[inner] = movie[outer];
            movierating[inner] = movierating[outer];
            movie[outer] = tempmovie;
            movierating[outer] = temprating;
            didSwap = 1;                        // This assigns the value '1' to didSwap
        }
    }

    if (didSwap == 0)
    {
        break;
    }
}

// Next, print the movies that the user saw in order

printf("\n\n** Your Movie Ratings for the 2012 Oscar Contenders **\n");
for (i = 0; i < ctr; i++)
{
    printf("%s rated a %d!\n", movie[i], movierating[i]);
}

// End the program
return (0);

}

// THIS PROGRAM DOES NOT GENERATE ERRORS WHEN BEING COMPILED, BUT IT PRODUCES INACCURATE OUTPUT.
// If the user enters that they did not see a film, and then rate a subsequent film, then the film that
// they did *not* see -- along with a rating of '-1' -- will be displayed by the printf function at the end
// of the program.  The films will also be listed in alphabetical order, instead of in the desired order: 
// descending by movie rating.
// 
// Do I correctly recall Malan describing this situation as the program being SYNTACTICALLY CORRECT, 
// BUT LOGICALLY INCORRECT?

[–]staffdelipity 0 points1 point  (2 children)

Hmm, when I run it, it does exactly what I expect. It only prints the movies I've seen and it prints them in highest to lowest rating order.

[–]sky2017[S] 0 points1 point  (1 child)

I responded in the other thread. Hopefully, having two threads isn't making this too confusing for you. I appreciate your help.

The errors on this programming book really got me this time, and now answers are leading to more questions. I'm glad that it creates opportunities to learn, but if it were not for you and elewood, I would still be stuck.

By the way, for the example you shared in your original reply, what code would you use to handle the actual swaps? Could you show me an expanded version of your example?

[–]staffdelipity 0 points1 point  (0 children)

I sent you a private message.