Array of Student Record
Apr 2, 2011 at 1:50pm UTC
I'm trying to Write a program, to allow users to Enter Student Records into an Array and means of Selection Sort from the Array. I am trying to sort by the Student Number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <iostream>
#include<string>
#include<sstream>
using namespace std;
#define N_STUDENT 10 //Max size of Student
struct student
{
int student_number; // Student number of student
string studnetname; // Name of student
string Address; //Address of Studnet
int CourseCode ; //Course Code
string CourseName; // Name of Course
}record[N_STUDENT];
void selectSort( int x[] int n);
int main ()
{
string mysrt;
string mysrt1;
int n;
cout <<"Enter Student Number: " ;
getline (cin, mysrt);
cout << "Enter Studnet Name: " ;
getline (cin, record[n].studnetname);
cout << "Enter Student Address: " ;
getline (cin, record[n].Address);
cout << "Enter Course Code: " ;
getline (cin, mysrt1);
cout << "Enter Course Name: " ;
getline (cin, record[n].CourseName);
selectSort(n < N_STUDENT);
for (n= 0; n < N_STUDENT; n++)
cout << "X at pos" << student[n] << endl;
return 0;
}
void selectSort( int x[] int n)
{
int smallPos, smallest;
for (int i=0; i <n; i++)
{
smallPos = i;
smallest = x[ smallPos];
for (int j=i +1; j < n ;j ++)
if (x[j] < smallest)
{
smallPos = j;
smallest = x [ smallPos]
}
}
how can i print out what i type in as well ?
Last edited on Apr 2, 2011 at 1:56pm UTC
Apr 2, 2011 at 2:48pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
template <class dType>
void selectsort(dType temp[], int n)
{
int j = 0, k = 0, small;
if (n > 1)
for (k = 0; k < n - 1; k++)
{
small = k;
for (j = k + 1; j < n; j++)
if (temp[j] < temp[small]) small = j;
if (k != small) swaparr(temp[k], temp[small]);
}
}
Apr 2, 2011 at 4:53pm UTC
your program is only allowing you to enter one student, then you are sorting. That doesnt make sense because one element is sorted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
for (int n = 0; n < N_STUDENT;n++)
{
cout <<"Enter Student Number: " ;
cin >> record[n].student_number
cout << "Enter Studnet Name: " ;
cin >> record[n].studnetname;
cout << "Enter Student Address: " ;
cin >> record[n].Address;
cout << "Enter Course Code: " ;
cin >> record[n].CourseCode;
cout << "Enter Course Name: " ;
cin >> record[n].CourseName;
}
selectsort(record, N_STUDENT);
this will loop through an allow you to enter all the student info
void selectSort( int x[] int n); needs to be void selectSort( int x[], int n); in both places.
start with that...
Last edited on Apr 2, 2011 at 4:55pm UTC
Apr 2, 2011 at 4:57pm UTC
1 2 3 4 5 6 7 8
struct student
{
int student_number; // Student number of student
string studnetname; // Name of student
string Address; //Address of Studnet
int CourseCode ; //Course Code
string CourseName; // Name of Course
}record[N_STUDENT];
should be
1 2 3 4 5 6 7 8
struct student
{
int student_number; // Student number of student
string studnetname; // Name of student
string Address; //Address of Studnet
int CourseCode ; //Course Code
string CourseName; // Name of Course
};
inside main....
1 2 3 4 5 6 7
int main ()
{
student record[N_STUDENT];
string mysrt; //remove this
string mysrt1;//remove this
int n;
Apr 2, 2011 at 8:44pm UTC
thanks for all the reply s
i had the
1 2 3
selectSort(n < N_STUDENT);
for (n= 0; n < N_STUDENT; n++)
cout << "X at pos" << student[n] << endl;
at the end of the main,
cout << "X at pos" << student[n] << endl;
should be allowing me to give out the Sorted Student ID.
When i had
cout << "Enter Course Code: ";
cin >> record[n].CourseCode;
Xcode was giving me errors since that was an int. so i had to use
getline (cin, mysrt1);
which seem to work but not was not declared in the array
Apr 2, 2011 at 9:15pm UTC
The Loop is Working Well allowing me to add 10 Students,
just wondering i can i call the
void selectSort( int x[], int n);
inside the
1 2 3 4 5
else if ( choice == 2)
{
}
here is the full code i have so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#include <iostream>
#include<string>
#include<sstream>
using namespace std;
void selectSort( int x[], int n);
#define N_STUDENT 10 //Max size of Student
struct student
{
int student_number; // Student number of student
string studnetname; // Name of student
string Address; //Address of Studnet
int CourseCode; //Course Code
string CourseName; // Name of Course
};
int main ()
{
student record[N_STUDENT];
int n;
for (int n = 0; n < N_STUDENT;n++)
{
cout <<"Enter Student Number: " ;
cin >> record[n].student_number;
cout << "Enter Studnet Name: " ;
cin >> record[n].studnetname;
cout << "Enter Student Address: " ;
cin >> record[n].Address;
cout << "Enter Course Code: " ;
cin >> record[n].CourseCode;
cout << "Enter Course Name: " ;
cin >> record[n].CourseName;
}
bool Exit = false ;
while (!Exit)
{
int choice;
cout << endl;
cout << "=======================================" << endl;
cout << " " << endl;
cout << " WELCOME TO STUDENT RECORD " << endl;
cout << " " << endl;
cout << "=======================================" << endl;
cout << "Select Action: " << endl;
cout << "1) Display Student Deails" << endl;
cout << "2) Sort Using Insertion Sort" << endl;
cout << "3) Sort Using Merge Sort" << endl;
cout << "4) Search Sorted Array Using Binary Search" << endl;
cout << "5) Add the Array to a Stack" << endl;
cout << "6) Display the Array" << endl;
cout << "7) Exit" <<endl;
cout << "=======================================" << endl;
cout << "Enter choice: " ;
cin >> choice;
if (choice == 7)
{
Exit = true ;
}
else if (choice == 6)
{
const int SIZE; // Array Size
int myArray[SIZE]; //the array
...//initialize values
for (int i=0;i<SIZE;i++) //go through all elements
cout<<i<<'\t' <<myArray[i]<<endl; //print indexnumber and value
}
else if ( choice == 5)
{
}
else if ( choice == 4)
{
}
else if ( choice == 3)
{
}
else if ( choice == 2)
{
}
else if ( choice == 1)
{
cout << "Student Number: " << record[n].student_number << endl;
cout << "Studnet Name: " << record[n].studnetname << endl;
cout << "Student Address: " << record[n].Address; << endl;
cout << "Course Code: " << record[n].CourseCode << endl;
cout << "Course Name: " << record[n].CourseName << endl;
}
return 0;
}
}
template <class dType>
void selectSort( int x[], int n)
{
int smallPos, smallest;
for (int i=0; i <n; i++)
{
smallPos = i;
smallest = x[ smallPos];
for (int j=i +1; j < n ;j ++)
if (x[j] < smallest)
{
smallPos = j;
smallest = x [ smallPos];
}
}
}
Last edited on Apr 2, 2011 at 9:47pm UTC
Topic archived. No new replies allowed.