Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
排序数组';结构类型 我正在为C++类工作任务(在我的Test.CPP文件底部粘贴了任务)。直到最后一个要求之前,我都正确地实现了所有功能(使用sort_name函数,根据学生的姓氏对学生结构进行排序,对于每个学生,根据课名对他或她正在学习的课程进行排序。通过从main调用Display函数来显示已排序的学生列表)。我试图通过一开始只对学生进行排序来实现我的排序功能,但我不断出现错误并陷入困境(当前错误是:839a4 Vasilkovskiy.exe中0x777a15de处的未处理异常:0xC00000FD:堆栈溢出)。任何帮助都将不胜感激_C++_Arrays_Sorting_Struct - Fatal编程技术网

排序数组';结构类型 我正在为C++类工作任务(在我的Test.CPP文件底部粘贴了任务)。直到最后一个要求之前,我都正确地实现了所有功能(使用sort_name函数,根据学生的姓氏对学生结构进行排序,对于每个学生,根据课名对他或她正在学习的课程进行排序。通过从main调用Display函数来显示已排序的学生列表)。我试图通过一开始只对学生进行排序来实现我的排序功能,但我不断出现错误并陷入困境(当前错误是:839a4 Vasilkovskiy.exe中0x777a15de处的未处理异常:0xC00000FD:堆栈溢出)。任何帮助都将不胜感激

排序数组';结构类型 我正在为C++类工作任务(在我的Test.CPP文件底部粘贴了任务)。直到最后一个要求之前,我都正确地实现了所有功能(使用sort_name函数,根据学生的姓氏对学生结构进行排序,对于每个学生,根据课名对他或她正在学习的课程进行排序。通过从main调用Display函数来显示已排序的学生列表)。我试图通过一开始只对学生进行排序来实现我的排序功能,但我不断出现错误并陷入困境(当前错误是:839a4 Vasilkovskiy.exe中0x777a15de处的未处理异常:0xC00000FD:堆栈溢出)。任何帮助都将不胜感激,c++,arrays,sorting,struct,C++,Arrays,Sorting,Struct,注:我知道向量对排序更有用,但我们的教授不希望我们使用它们 #include <iostream> #include <string> #include <stdio.h> #include <string.h> #include <iomanip> using namespace std; struct Class { string title; int units; char grade; }; st

注:我知道向量对排序更有用,但我们的教授不希望我们使用它们

#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <iomanip>
using namespace std;


struct Class
{
    string title; 
    int units;
    char grade;

};
struct Student
{
    string name;
    double gpa;
    Class classes[500];
};

int const SIZE = 50;
void initStudent(Student[], int);
void readStudent(Student[], int,  int&);
void gpaCalculate(Student&);
void print(Student[], int);
void sort_name(Student[], int);
void swapStus(Student&, Student&);
void stringToCharArray(string, char[]);
int locactionOfFirstName(Student[], int, int);

int main()
{   
    int numberOfStudents = 0;
    Student students[SIZE];
    initStudent(students, SIZE);
    readStudent(students, SIZE, numberOfStudents);

    for(int i = 0; students[i].name != "";i++)
        gpaCalculate(students[i]);
    print(students, numberOfStudents);
    sort_name(students, numberOfStudents);

    system("pause");
    return 0;
}
void initStudent(Student st[], int s)
{
    for(int i = 0; i < s; i++)
    {
        st[i].gpa = 0.0;
    }

}

void readStudent(Student st[], int s, int& nStus)
{
    for(int i = 0; i < s; i++)
    {   
        string tmpName;

        cout << "Enter student name: ";
        getline(cin, tmpName);
        if(tmpName == "")
            break;
        st[i].name = tmpName;
        nStus++;


        for(int j = 0; j < 500; j++)
        {   
            string tmpTitle;
            cout << "Enter class title: ";
            getline(cin, tmpTitle);
            if (tmpTitle == "")
                break;
            st[i].classes[j].title = tmpTitle;

            cout << "Enter units for " << st[i].classes[j].title << ": " ;
            cin >> st[i].classes[j].units;
            cout << "Enter grade for " << st[i].classes[j].title << ": " ;
            cin >> st[i].classes[j].grade;
            cin.ignore();
        }   
    }
}


void gpaCalculate (Student& s)
{
    double unitsByPoints = 0;
    double totalUnits = 0;

    for (int i = 0; s.classes[i].title != ""; i++)
    {

        int grade = 0;
        char ltrGrade = s.classes[i].grade;
        switch (ltrGrade)
        {
        case 'A':
            grade = 4;
                break;
        case 'B':
            grade = 3;
                break;
        case 'C':
            grade = 2;
                break;
        case 'D':
            grade = 1;
                break;
        case 'F':
            grade = 0;
                break;
        }
        unitsByPoints += s.classes[i].units*grade;
        totalUnits += s.classes[i].units;

    }
    s.gpa = unitsByPoints/totalUnits;
}

void print(Student st[], int size)
{

    for (int i = 0; i < size; i++)
    {
        cout << "Student's name: " <<  st[i].name << endl;
        for (int j = 0; st[i].classes[j].title != ""; j++)
        {
            cout << st[i].classes[j].title << "," << st[i].classes[j].grade << endl;
        }
        cout << fixed << setprecision(2) << "GPA: " <<  st[i].gpa << endl;
    }
}

//void sort_name(Student st[], int size)
//{ 
//  for(int i = 0; i < size; i++)
//  {
//      int smallest = locactionOfFirstName(st, i, size);
//      swapStus(st[i], st[smallest]);
//  }
//
//}

void swapStus(Student& s1, Student& s2)
{
    Student tempStu;
    tempStu = s1;
    s1 = s2;
    s2 = tempStu;
}

void stringToCharArray(string s, char c[])
{
    char tempCharArray[50];
    for(int i = 0; s[i] != '\n'; i++)
    {
        tempCharArray[i] = s[i];
    }
    char * space = strstr(tempCharArray," ");
    strcpy(c,space);
}

bool lastNameCompare(string l1, string l2)
{
    char lName1[50];
    char lName2[50];
    stringToCharArray(l1, lName1);
    stringToCharArray(l2, lName2);
    return (strcmp(lName1, lName2) >=0);
}

int locactionOfFirstName(Student st[],int start, int size)
{
    char lName1[50];
    char lName2[50];
    stringToCharArray(st[0].name, lName1);
    int i;
    for(i = start; i < size;)
    {
        stringToCharArray(st[i].name, lName2);
        if(strcmp(lName1, lName2) >= 0 )
        {
            stringToCharArray(st[i].name, lName1);
        }
    }
    return i;
}

void InsertItem(Student values[], int startIndex, int endIndex)
{
  bool finished = false;
  int current = endIndex;
  bool moreToSearch = (current != startIndex);

  while (moreToSearch && !finished)
  {
    if (lastNameCompare(values[current-1].name, values[current].name))
    {
      swapStus(values[current], values[current-1]);
      current--;
      moreToSearch = (current != startIndex);
    }
    else
      finished = true;
  }
}

void sort_name(Student values[], int numValues)
{
  for (int count = 0; count < numValues; count++)
    InsertItem(values, 0, count);
}
/*



Define a structure called Class (with uppercase C) with the following data:
title, units and grade.

Define a structure called Student with the following data:
name (full name), gpa, and classes which is an array of Class structures (all the classes the student has taken so far).

Write an initialize function that receives an array of Student structures and its size and sets the gpa of all to 0.0.

In main, create 50 Students and call the above function to initialize the gpa for all 50 Students to 0.0. 

Then, pass the array of student structures and its size to a read function that will read student data from the user and store the entered data in the array of student structures.  The user will enter student name followed by the class title, units and grade received for each class he or she has taken.  When finished entering class information for a student, the user will just press Enter (an empty string) and to end entering more students, he or she will do the same for the student name. 

Example:

Enter student name: Maria Gomez

Enter class title: English 101
Enter units for English 101: 3 
Enter grade for English 101: A

Enter class title: Math 201
Enter units for Math 201: 4
Enter grade for Math 201: B

Enter class title: [User enters RETURN to indicate no more classes]

Enter student name: Kevin Duran

Enter class title: Poly Sci 101
Enter units for Poly Sci 101: 3 
Enter grade for Poly Sci 101: A

Enter class title: Math 201
Enter units for Math 201: 4
Enter grade for Math 201: B

Enter class title: [User enters RETURN to indicate no more classes]

Enter student name: [User enters RETURN to indicate no more students]

Once all Studnets have been entered, pass each element of the array of Student structures (element by element) to a gpa function which will compute and return the gpa for each Student using the classes array within each Student structure which contains the units and grade for each class taken by the student.  Store the gpa returned by the above function in the gpa member of the Student structures.  GPA is calculated by multiplying the number of units for each class by the points received for that class, and then adding all these products together and dividing it by total number of units.  The points received for a class is based on the grade: for A, it's 4; for B, it's 3; for C, it's 2; for D it's 1; and for F it's 0.  For example, if a student has take 3 classes with 3, 4, and 3 units and has received A, B, and C for these classes, respectively, then, the GPA will be 3 x 4 + 4 x 3 + 3 x 2 / 10 = 3.0.

Print all students showing name, followed by all classes taken, the grade received and the gpa using a display function which receives the array and its size as parameters.

Then, using a sort_name function, sort the student structures based on their last names and for each student, sort the classes he or she is taking based on class title.  Display the sorted list of students using the display function by calling it from main.

For example:

Kevn Duran
Poly Sci 101, A
Math 150, B
GPA: 3.0

Maria Gomez:
English 101, A
Math 201, C
GPA: 2.9

Robert Small
Comp Science 801, C
Comp Science 802, D
GPA: 1.9

Tom Wang
Comp Science 808, A
Comp Science 839, B
GPA: 3.5

Then, sort the students based on their GPA's using a sort_gpa function and print the list again using the display function. 

Then, ask what to search for - name or gpa.  If name is selected, read a student name from the user and, using a binary search function that takes the array, its size and the name to search for, finds the student and displays all of his or her information (name, gpa, units and list of classes taken).

Example:

Enter a student name:  Robert Small

Robert Small:
Comp Science 801, C
Comp Science 802, B
GPA: 2.5

If GPA is selected, read a GPA and using another binary search find the student with the given GPA by passing the students array, its size and the GPA to search for.  Display the name of the student with the specified GPA in main.  

Example:

Enter GPA to search for:  2.5

Robert Small was found with the GPA of 2.5

If the name or GPA is not found, tell the user it was not found; e.g.: There was no student with a GPA of 2.5; or Robert Small was not found.

Then, pass the array of student atructures and the size to a stats function which will return the average of the GPA's of all students and through two reference parameters will output the student structures that have the minimum and maximum GPA's.  Print the average GPA, as well as the names of the students who have the minimum and maximum GPA in main, like so:

Average GPA = 3.17

Robert Small has the minimum GPA of 2.5.

Tom Wang has the maximum GPA of 3.5. 

Finally, read a maximum and minimum GPA from the user and pass the array of student structures and its size, as well as two other arrays of student structures of the same size to a function which will store all students with a GPA of above the minimum in the highGPA array and all those with a GPA below the maximum in the lowGPA array.  Display the students stored in these two arrays by passing them each from main to the display function.  In other words, the highlow function receives two uninitalized arrays of student structures and populates them based on the GPA criteria passed to it (minimum GPA and maximum GPA).  Then, upon return to main, main passes each of these arrays to the display function to display them.  For example, if the user enters 2.0 for the maximum GPA, the lowGPA array gets filled out with all those students who have a GPA of less than 2.0.  Likewise, if the minimum GPA is 3.5, the highlow function populates the highGPA array with those students who have a GPA of 3.5 or higher.

Example:

Enter maximum GPA:  2.0

Enter minimum GPA:  3.5

Students with a GPA of lower than 2.0:

Robert Small 1.9

Students with a GPA of 3.5 or higher:

Tom Wang 3.5

When writing the highlow function, take advantage of the fact that the array elements are sorted based on the GPA, so to find all the students with a GPA of equal to or higher than the minimum GPA, it doesn't make sense to start from the first element in the array. Instead you can start from the midpoint. If the midpoint is lower than the minimum GPA, you can increment the index until the midpoint is no longer smaller and then all the GPA's from that point on will be larger and part of the high GPA's.  For low GPA's of course, you'd want to start from the beginning of the array and compare and store each that's lower than the maximum until they are no longer lower.

Functions you must write for this assignment (in addition to main):

initialize, read, display, sort_name, sort_gpa, search-name, search_gpa, stats, highlow.

Upload your cpp and exe files using the Browse and Upload buttons below and click Finish once both have been uploaded to the site.*/
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构类
{
字符串标题;
整数单位;
煤焦品位;
};
体类型
{
字符串名;
双gpa;
类[500];
};
int const SIZE=50;
无效学生(学生[],int);
void readStudent(Student[],int,int&);
无效GPA计算(学生和);
无效打印(学生[],整数);
无效排序名称(学生[],整数);
无效交换(学生和,学生和);
void StringToCharray(字符串,字符[]);
int locactionOfFirstName(学生[],int,int);
int main()
{   
int numberOfStudents=0;
学生[人数];
初始学生(学生,大小);
readStudent(学生人数、大小、学生人数);
对于(int i=0;学生[i]。名称!=“”;i++)
GPA计算(学生[i]);
打印(学生、学生人数);
排序名称(学生、学生人数);
系统(“暂停”);
返回0;
}
void initStudent(Student st[],int s)
{
对于(int i=0;i
问题出在
stringToCharray(字符串s,字符c[])

我的索引
i
到达
104
后才得到“分段错误”。由于所有字符串都有长度
50
,显然你越界了

另一个问题是
srtstr
中返回
NULL
,但这与第一个问题有关


快速查看您的
sort\u name
InsertItem
并没有发现任何错误,至少在“分段错误”字段中是这样,因为不太清楚您想做什么,但至少您正在执行正确的索引检查。

那么您在哪一行得到错误?尝试过调试?当人们在没有解释原因的情况下否决您的问题时,这是非常令人沮丧的。如果您打算否决某项内容,至少给出一些建设性的批评。上的工具提示向下投票按钮是“这个问题没有显示任何研究成果”。我同意。你没有缩小范围,你没有说错误发生在哪里,而且这个问题不太可能在将来帮助任何人。这是你的解释。“我相信错误发生了”还不够好。在我看来,调试技能应该在调试之前学习。这就像在做数学之前学习如何写数字一样。因此,如果你还不知道如何调试,就学习。我不能强调这有多重要。“注意:我知道向量对排序更有用,但我们的教授不希望我们使用它们。”真的吗?他对std::sort的感觉如何?顺便说一句,你可能想学习如何调试,否则你将永远无法面对这个问题。