-6
該代碼初始化一個帶有9個值(電影名稱)的指針數組,然後鼓勵用戶輸入每部電影的評分。在爲每部電影輸入評分後,氣泡排序代碼應該將每部電影從最低到最高排序。最後,電影列表應該按照從最低到最高的順序打印出來。不幸的是,代碼的最後部分是打算按評級順序打印電影不起作用。爲什麼是這樣?我的泡泡排序有什麼問題?
int i;
int ctr = 0;
char ans;
char * movies[9] = {"Amour", "Argo","Beasts of the Southern Wild","Django Unchained","Les Miserables","Life of Pi", "Lincoln","Silver Linings Playbook","Zero Dark Thirty"};
int movieratings[9];
char *tempmovie ;
int didSwap, temprating;
printf("\n\n*** Oscar Season 2012 is here and the nominees are: ***\n\n");
for (i=0;i<9;i++)//FOR LOOP TO PRINT OUT VALUES IN movies
{
printf(" %s\n",movies[i]);
}
printf("Time to rate this year's best picture nominees:");
for (i=0; i< 9; i++)//for loop to rate movies
{
printf("\nDid you see %s? (Y/N): ", movies[i]);
scanf(" %c", &ans);
if ((toupper(ans)) == 'Y')
{
printf("\nWhat was your rating on a scale of 1-10:");
scanf(" %d", &movieratings[i]);
ctr++;//counter to recognise how many movies you have seen
continue;
}
else
{
movieratings[i] = -1;
}
}
// Now sort the movies by rating (the unseen will go
// to the bottom)
while(1)//bubble sort to sort movies by rating
{
didSwap = 0;
for (i = 0; i < 9; i++)
{
if (movieratings[i] > movieratings[i+1])
{
tempmovie = movies[i];
temprating = movieratings[i];
movies[i] = movies[i+1];
movieratings[i] = movieratings[i+1];
movies[i+1] = tempmovie;
movieratings[i+1] = temprating;
didSwap = 1;
}
}
if (didSwap == 0)
{
break;
}
}
printf("\n\n** Your Movie Ratings for the 2012 Oscar Contenders **\n");
for (i=0; i<ctr; i++)
{
printf("%s rated a %d \n", movies[i], movieratings[i]);
}
它代替它做了什麼? –
它不打印任何東西。 –
你最後一個for循環有'我