簡體   English   中英

該程序接受用戶輸入,應該使用氣泡排序對其進行排序,但它輸出字母和數字,我不知道為什么

[英]This program takes user input and is supposed to sort it using bubbleSort but its outputting letters and numbers and I'm not sure why

在輸入任何一組整數后運行此代碼並以標記號 999 結束循環時,我得到的輸出始終為 0x7ffd85bc43b0,我不知道為什么。 我想我在某處遺漏了一些重要的代碼,但我不確定在哪里? 這個程序最終也應該能夠找到輸入數字的平均值和中位數。

// HouseholdSize.cpp - This program uses a bubble sort to arrange up to 300 household sizes in
// descending order and then prints the mean and median household size. 
// Input:  Interactive.
// Output:  Mean and median household size. 

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <any>
#include <stdio.h>
using namespace std;
void swapping(int &a, int &b) {      //swap the content of a and b
   int temp;
   temp = a;
   a = b;
   b = temp;
}
void display(int householdSizes, int size) {
   for(int i = 0; i<size; i++){

   }
}
void bubbleSort(int householdSizes[], int size) {
   for(int i = 0; i<size; i++) {
      int swaps = 0;         //flag to detect any swap is there or not
      for(int j = 0; j<size-i-1; j++) {
         if(householdSizes[j] > householdSizes[j+1]) {       //when the current item is bigger than next
            swapping(householdSizes[j], householdSizes[j+1]);
            swaps = true;    //set swap flag
         }
      }
      if(swaps== false)
         break;       // No swap in this pass, so array is sorted
   }
}
int main() 
{
   // Declare variables.
        
   const int SIZE = 300;    // Number of household sizes
   int householdSizes[SIZE];    // Array used to store 300 household sizes
   int x; 
   int limit = SIZE;
   int householdSize = 0;
   int pairsToCompare;
   bool switchOccurred; 
   int temp;
   double sum = 0;
   double mean = 0;
   double median = 0;

   // Input household size      
   cout << "Enter household size or 999 to quit: ";
   cin >> householdSize;
   
   // This is the work done in the fillArray() function
   x = 0;
   while(x < limit && householdSize != 999)   
   {
      // Place value in array.
      householdSizes[x] = householdSize;
      // Calculate total of household sizes
      
      x++;    // Get ready for next input item.
      cout << "Enter household size or 999 to quit: ";
      cin >> householdSize;
   }  // End of input loop.
        
   
   // Find the mean
   
   // This is the work done in the sortArray() function
   int n = sizeof(householdSizes)/sizeof(householdSizes[0]);
   bubbleSort(householdSizes, n);
   cout <<householdSizes;
   
   // This is the work done in the displayArray() function

   // Print the mean

   // Find the median
   
   // Print the median
            
   return 0;
} // End of main function





不能使用cout << householdSizes;直接打印數組cout << householdSizes; 由於數組可以轉換為指針,這就是幕后隱式完成的工作。 更改cout << householdSizes; 到:

for(int i = 0; i < x; ++i){
    std::cout << householdSizes[i] << " ";
}

PS也按照@0x5453說的做。 他是對的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM