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
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
void getData(int size, int [], char [][6]);
void sortData(char [][6], int[], int);
void printData(char [][6], int[]);
int searchData(char [][6], int, int[], char []);
void main(){
int scores[5];
char students[5][6];
char key[6];
char answer[4] = "yes";
int size;
int index;
cout << "Enter size for arrays: ";
cin >> size;
getData(size,scores,students);
printData(students,scores);
sortData(students, scores, 5);
while (strcmp(answer, "yes") == 0) {
cout << "Enter a student name as search key: ";
cin >> key;
index = searchData(students, size, scores, key);
if (index == -1)
cout << "Not found." << endl;
else
cout << "Test score for "<< students << "is " << index + 1 << endl;
// end if
cout << "Do you want another search? yes/no: ";
cin >> answer;
} // end while
}
void getData(int size, int testScore[],char studentName[][6]) {
int index;
for (index = 0; index <= size -1; index++){
cout << "Enter " <<(index +1) << "th student name: "
<< ": ";
cin >> studentName[index];
cout << "Enter test score for " << studentName[index]
<< ": ";
cin >> testScore[index];
while (0 > testScore[index] || testScore[index] > 100){
cout << "Re-enter test score for " << studentName[index]
<< ": ";
cin >> testScore[index];
}// end while
}// end for
}
void sortData(char studentName[][6],int testScore[],int size){
bool swap;
char temp[6];
do {
swap = false;
for (int count = 0; count < (size - 1); count++) {
if (strcmp(studentName[count], studentName[count+1]) > 0) {
strcpy_s(temp,6, studentName[count]);
strcpy_s(studentName[count],6, studentName[count+1]);
strcpy_s(studentName[count+1],6, temp);
swap = true;
} // end if
} // end for
} while (swap);
}
void printData(char studentName[][6], int testScores[]){
cout << "ARRAYS BEFORE SORTING" << endl;
cout << "NAME" << "\t\t" << "SCORE" << endl;
for (int i = 0; i < 5; i++)
cout << studentName[i] << "\t\t" << static_cast<int>(testScores[i]) << endl;
sortData(studentName, testScores, 5);
cout << "ARRAYS AFTER SORTING :" << endl;
cout << "NAME" << "\t\t" << "SCORE" << endl;
for (int i = 0; i < 5; i++)
cout << studentName[i] << "\t\t" << static_cast<int>(testScores[i]) << endl;
int ok;
cin >> ok;
}
int searchData(char studentName[][6], int size, int testScores[], char *key){
int first, last, mid;
bool found = false;
int position = -1;
first = 0;
last = size - 1;
while (first <= last && ! found) {
mid = (first + last) / 2;
if (strcmp(studentName[mid], key ) == 0) {
position = mid;
found = true;
}
else if (strcpy(studentName[mid], key) > 0)
last = mid - 1;
else
first = mid + 1;
// end if
} // end while
return position;
}
|