-3

So I have tried everything to figure this code out. I have written the bubble sort code but I do not know how to make it not print grades under 60 percent. I have to somehow only print the grades above 60 but at the end print how many people did not get above 60. this is the code I have for right now if anyone could please help me it would be great.

#include <bits/stdc++.h>
using namespace std;

void bubbleSort(int arr[], int n)
{
    if (n <= 1)
        return;
    
    for (int i = 0; i < n - 1; i++)
        if (arr[i] > arr[i + 1])
            swap(arr[i], arr[i + 1]);
    
    bubbleSort(arr, n - 1);
}

void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
int main()
{
    int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
    int n = sizeof(arr) / sizeof(arr[0]);
    bubbleSort(arr, n);
    printf("Sorted array : \n");
    printArray(arr, n);
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 2
    This code is full of "competitive programming" anti-patterns. Consider learning from a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of "coding" garbage websites. – Evg Nov 01 '21 at 18:20
  • My TA helped me code this that is why it is filled with competitive programing – Chakib Ar Nov 01 '21 at 19:06
  • 1
    @ChakibAr You should _never_ include that `bits/stdc++.h` header. it's an implementation specific header that includes _a lot_ of unnecessary stuff (and often not all the necessary stuff), making it slower to compile your programs. It also makes your programs non-portable. – Ted Lyngmo Nov 01 '21 at 19:28
  • 1
    got it will not include it in the future thank you – Chakib Ar Nov 01 '21 at 20:56
  • @ChakibAr Great! Did you have any success using any of the suggestions in my answer? If you want me to explain anything in more detail, just ask. – Ted Lyngmo Nov 01 '21 at 21:31
  • This is pure C code. Has nothing to do with C++. – A M Nov 02 '21 at 18:10
  • @TedLyngmo I did thank you very much for your help. – Chakib Ar Nov 02 '21 at 23:05
  • @ChakibAr Great! You're welcome! – Ted Lyngmo Nov 02 '21 at 23:06

1 Answers1

0

You could add a condition for printing the score:

void printArray(int arr[], int n) {
    int below60 = 0;                           // counter for scores below 60

    for (int i = 0; i < n; i++) {
        if(arr[i] < 60) ++below60;             // < 60, just count
        else printf("%d ", arr[i]);            // >= 60, print it
    }
    printf("\n");

    printf("%d were below 60\n", below60 );    // print the number of scores < 60
}

An alternative:

void printArray(int arr[], size_t n) {
    size_t below60 = 0;
    // loop for as long as the score is below 60
    for (; below60 < n && arr[below60] < 60; ++below60) {}

    // print the scores >= 60
    for (size_t idx = below60; idx < n; ++idx) std::cout << arr[idx] << ' ';
    std::cout << '\n';

    // and print the count of scores below 60 
    std::cout << below60 << " were below 60\n";    
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • You could also take `i`’s declaration out of the loop, `break` as soon as you hit a failing mark, and use subtraction to print the number of ‘F’s. – Dúthomhas Nov 01 '21 at 18:29
  • @Dúthomhas Yes, that would work too. I added a similar alternative. – Ted Lyngmo Nov 01 '21 at 18:46