如果条目大于 90,则打印输出字符串的布尔函数

发布于 2024-12-21 10:49:39 字数 2079 浏览 2 评论 0原文

我正在尝试编写一个程序,允许用户输入他们选择的数量。我希望数字按升序显示并显示平均值。我完成了这一部分,但如果至少有一个数字高于 90,我还想打印一个字符串,如果没有高于 90 的数字,则打印另一个字符串。我认为我非常接近完成这项工作,但是每当我通过编译器运行它时,它都会说我必须使用的布尔函数不接受 1 个参数。我真的不知道该怎么办。

#include <iostream> 
using namespace std; 
#include <iomanip>  
#include <cstdlib>

int compare(const void* pa, const void* pb) 
{ 
  const int& a = *static_cast<const int*>(pa); 
  const int& b = *static_cast<const int*>(pb); 
  if (a < b) return -1; // negative if a<b 
  if (a > b) return 1; // positive if a>b 
  return 0; // 0 for tie 
} // compare 

double getAverage(int* score, int n) 
{ 
  int sum = 0; 
  int i = 0; 
  for (i = 0; i < n; i++) 
    sum += score[i]; 
  double average = double(sum) / n; 
  return average; 
} // getAverage 
//Boolen Function to see if there are A grades present 
bool getAGrades(int* score, int n) 
{ 
    int i = 0;
    for (i = 0; i < n; i++){
        if (score[i] >= 90){
            return true;
            cout << "there is at least one A"<<endl;
         }else {
             return false;
             cout<<" No A Grades "<<endl;
         }
    }
}
int main() 
{  
    int size;
    cout << "How many scores? ";
    cin >> size;
    cin.ignore(1000, 10);
    int* score = new int[size];  

    int i; // loop counter 
    for (i = 0; i < size; i++) 
    { 
        cout<< "Enter a number: ";
        cin >> score[i]; 
        cin.ignore(1000, 10); 
    } // for 

    qsort(score, size, sizeof(int), compare); 
    for (int i = 0; i < size; i++) {
        cout << score[i] << ' ';
    }
    cout <<endl;
    cout << "Lowest score  = " << score[0] << endl;
    cout << "Highest score = " << score[size-1] << endl;
    cout << fixed << setprecision(1);
    cout << "Average = " << getAverage(score, size) << endl; 
    //this is where i want it to print if there are a grades or not
    getAGrades(score);

    return 0; 
} // main

I'm trying to write a program that allows users to enter in an amount of their choosing. I want the numbers displayed in ascending order and have the average displayed. I got this part done, but i also want to have a string printed out if there is at least one number higher than 90 and another string if there is not a number higher than 90. I think im pretty close to making this work, but whenever I run it though the compiler, it says that the boolean function that I have to use does not take 1 arguments. I really don't know what to do.

#include <iostream> 
using namespace std; 
#include <iomanip>  
#include <cstdlib>

int compare(const void* pa, const void* pb) 
{ 
  const int& a = *static_cast<const int*>(pa); 
  const int& b = *static_cast<const int*>(pb); 
  if (a < b) return -1; // negative if a<b 
  if (a > b) return 1; // positive if a>b 
  return 0; // 0 for tie 
} // compare 

double getAverage(int* score, int n) 
{ 
  int sum = 0; 
  int i = 0; 
  for (i = 0; i < n; i++) 
    sum += score[i]; 
  double average = double(sum) / n; 
  return average; 
} // getAverage 
//Boolen Function to see if there are A grades present 
bool getAGrades(int* score, int n) 
{ 
    int i = 0;
    for (i = 0; i < n; i++){
        if (score[i] >= 90){
            return true;
            cout << "there is at least one A"<<endl;
         }else {
             return false;
             cout<<" No A Grades "<<endl;
         }
    }
}
int main() 
{  
    int size;
    cout << "How many scores? ";
    cin >> size;
    cin.ignore(1000, 10);
    int* score = new int[size];  

    int i; // loop counter 
    for (i = 0; i < size; i++) 
    { 
        cout<< "Enter a number: ";
        cin >> score[i]; 
        cin.ignore(1000, 10); 
    } // for 

    qsort(score, size, sizeof(int), compare); 
    for (int i = 0; i < size; i++) {
        cout << score[i] << ' ';
    }
    cout <<endl;
    cout << "Lowest score  = " << score[0] << endl;
    cout << "Highest score = " << score[size-1] << endl;
    cout << fixed << setprecision(1);
    cout << "Average = " << getAverage(score, size) << endl; 
    //this is where i want it to print if there are a grades or not
    getAGrades(score);

    return 0; 
} // main

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走走停停 2024-12-28 10:49:39

首先,你的问题:函数getAGrades接受一个int* Score和一个int n。从 main 返回之前要做的最后一件事是调用 getAGrades(score);,但没有为其指定大小。您还必须传递大小:getAGrades(score, size)

第二:为什么你的比较函数采用void*?毫无理由地这是危险的。为什么不使用带有安全比较函数的类型安全 std::sort

第三:getAGrades 在每个cout 之前返回,因此它永远不会显示任何内容。它应该cout,然后然后返回。
编辑:Keven 还注意到,当发现不是 A 的成绩时,getAGrades 返回正如他所解释的,这样的函数应该在找到 A 等级时返回,或者如果它检查了所有等级而没有找到任何等级。删除 for 循环之后“No A Grades” 块。

第四:您的 cin.ignore 没有做任何有用的事情,因为您只有格式化输入。另外,使用 '\n' 而不是 10,否则你会让人感到困惑,而且它更便于移植。

第五:优先选择标准容器而不是分配您自己的数组。因为您的代码泄漏了 score 数组。

First of all, your question: The function getAGrades takes an int* score, and a int n. The last thing you do before you return from main is call getAGrades(score);, but you don't give it a size. You have to also pass the size: getAGrades(score, size).

Second: why does your compare function take void*? That's dangerous for no reason. Why aren't you using the type-safe std::sort with a safe comparison function?

Third: getAGrades returns right before each cout, so it will never ever display anything. It should cout, and then return.
Edit: Keven also noticed that getAGrades returns when it finds a grade that isn't an A. As he explained, such a function should return when it finds an A grade, or if it checked ALL of the grades without finding any. Remove the "No A Grades" block to after the for loop.

Fourth: Your cin.ignore doesn't do anything useful, since you only have formatted input. Also, use '\n' instead of 10, otherwise you're going to confuse people, and it's more portable.

Fifth: Prefer standard containers to allocating your own arrays. Because your code leaks the score array.

悸初 2024-12-28 10:49:39
bool getAGrades(int* score, int n) 

getAGrades 需要两个参数 - 分数和 n。

getAGrades(score);

当您调用 getAGrades 时,您仅传入一个参数。您还应该传递 n 的参数。

getAGrades(score, size);

您的 getAGrades 函数体也有一些错误。假设您传入一个数组 [1,100]。在 for 循环的第一次迭代中,它看到 Score[0] == 1,因此返回 false。它永远不会到达 Score[1] 来查看它是否高于 90。因此,您应该延迟返回 false,直到迭代整个数组之后。

bool getAGrades(int* score, int n) 
{ 
    int i = 0;
    for (i = 0; i < n; i++){
        if (score[i] >= 90){
            return true;
            cout << "there is at least one A"<<endl;
        }   
    }
    return false;
    cout<<" No A Grades "<<endl;
}

另一件事 - 您的 cout 语句在您返回值之后发生,因此它们永远不会被执行。您应该将它们放在 return 语句之前。

bool getAGrades(int* score, int n) 
{ 
    int i = 0;
    for (i = 0; i < n; i++){
        if (score[i] >= 90){
            cout << "there is at least one A"<<endl;
            return true;
        }   
    }
    cout<<" No A Grades "<<endl;
    return false;
}
bool getAGrades(int* score, int n) 

getAGrades requires two arguments - score and n.

getAGrades(score);

When you call getAGrades, you only pass in one argument. You should also pass in an argument for n.

getAGrades(score, size);

Your getAGrades function body also has some errors. Suppose you passed in an array [1,100]. In the first iteration of your for loop, it sees that score[0] == 1, so it returns false. It never gets to score[1] to see that it is above 90. For this reason, you should delay returning false until after you have iterated over the entire array.

bool getAGrades(int* score, int n) 
{ 
    int i = 0;
    for (i = 0; i < n; i++){
        if (score[i] >= 90){
            return true;
            cout << "there is at least one A"<<endl;
        }   
    }
    return false;
    cout<<" No A Grades "<<endl;
}

One more thing - your cout statements occur after you return a value, so they never get executed. You should put them before the return statements.

bool getAGrades(int* score, int n) 
{ 
    int i = 0;
    for (i = 0; i < n; i++){
        if (score[i] >= 90){
            cout << "there is at least one A"<<endl;
            return true;
        }   
    }
    cout<<" No A Grades "<<endl;
    return false;
}
黎夕旧梦 2024-12-28 10:49:39

使用 std::sortstd::vector
我知道这并不是这个问题的真正答案。
但是当您想使用 C++ 时,您不应该尝试使用这些 C 函数,例如 qsort
使用 std::sort 也可以对 C 数组进行排序。

qsort(score, size, sizeof(int), Compare); 变为 std::sort(score, Score+size); (或 std::sort (score, Score+size, Compare);)

C++ 方式(使用 std::vectorstd::sort,固定 getAGrades 函数):

 #include <iostream> 
 #include <iomanip>
 #include <vector>
 #include <algorithm>  

 using namespace std;

 double getAverage(const vector<int>& score) 
 { 
    int sum = 0;
    for(int i = 0; i < score.size(); ++i)
    {
        sum += score[i];
    }
    return sum / double(score.size()); 
 }


 bool getAGrades(const vector<int>& score) 
 { 
     for (int i = 0; i < score.size(); ++i)
     {
         if (score[i] >= 90)
         {
            cout << "there is at least one A"<<endl;
            return true;
         }
     }
     cout << "No A Grades"<<endl;
     return false;
 }

 int main() 
 {  
     int size;

     cout << "How many scores? ";
     cin >> size;
     cin.ignore(1000, 10);

     vector<int> score(size);

     for (int i = 0; i < size; i++) 
     { 
         cout<< "Enter a number: ";
         cin >> score[i]; 
         cin.ignore(1000, 10); 
     }

     sort(score.begin(), score.end());
     for (int i = 0; i < size; i++)
     {
         cout << score[i] << ' ';
     }
     cout <<endl;
     cout << "Lowest score  = " << score[0] << endl;
     cout << "Highest score = " << score[size-1] << endl;
     cout << fixed << setprecision(1);
     cout << "Average = " << getAverage(score) << endl; 

     getAGrades(score);

     return 0; 
 }

Use std::sort and std::vector.
I know it's not really the answer to this problem.
But when you want to use C++ you shouldn't try to use these C-functions like qsort.
With std::sort you can sort c arrays too.

qsort(score, size, sizeof(int), compare); becomes std::sort(score, score+size); (or std::sort(score, score+size, compare);)

C++ way (use of std::vector and std::sort, with fixed getAGrades function):

 #include <iostream> 
 #include <iomanip>
 #include <vector>
 #include <algorithm>  

 using namespace std;

 double getAverage(const vector<int>& score) 
 { 
    int sum = 0;
    for(int i = 0; i < score.size(); ++i)
    {
        sum += score[i];
    }
    return sum / double(score.size()); 
 }


 bool getAGrades(const vector<int>& score) 
 { 
     for (int i = 0; i < score.size(); ++i)
     {
         if (score[i] >= 90)
         {
            cout << "there is at least one A"<<endl;
            return true;
         }
     }
     cout << "No A Grades"<<endl;
     return false;
 }

 int main() 
 {  
     int size;

     cout << "How many scores? ";
     cin >> size;
     cin.ignore(1000, 10);

     vector<int> score(size);

     for (int i = 0; i < size; i++) 
     { 
         cout<< "Enter a number: ";
         cin >> score[i]; 
         cin.ignore(1000, 10); 
     }

     sort(score.begin(), score.end());
     for (int i = 0; i < size; i++)
     {
         cout << score[i] << ' ';
     }
     cout <<endl;
     cout << "Lowest score  = " << score[0] << endl;
     cout << "Highest score = " << score[size-1] << endl;
     cout << fixed << setprecision(1);
     cout << "Average = " << getAverage(score) << endl; 

     getAGrades(score);

     return 0; 
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文