All code must be in C++ using G++ compiler Write a program that asks the user to
ID: 3810453 • Letter: A
Question
All code must be in C++ using G++ compiler
Write a program that asks the user to enter the name and ID numbers (ints) for a group of 10 users and stores them in parallel arrays. To make sure there are no duplicate IDs, perform a linear search over the previously entered IDs and ask the user to re-enter the ID if it has already been entered. At the end of the program, print the IDs that the user entered, along with the students' names. Example: Enter student name: BUI Gates Enter the ID for Bill Gates: 74927 Enter student name: Steve Jobs Enter the ID for Steve Jobs: 74927 That ID has already been entered for another student! Enter the ID for Steve Jobs: 83643 ... Students: 74927 Bill Gates 83643 Steve Jobs ...Explanation / Answer
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
bool linearSearch(int ids[], int n, int id){
for(int i=0; i<n; i++){
if(ids[i] == id){
return true;
}
}
return false;
}
int main() {
string names[10];
int ids[10];
for(int i=0; i<10; i++){
cout<<"Enter the student name: ";
cin >> names[i];
cout<<"Enter the ID for "<<names[i]<<": ";
cin >> ids[i];
if(linearSearch(ids, i, ids[i])) {
cout<<"ID is already existed. "<<endl;
i--;
}
}
cout<<"Students: "<<endl;
for(int i=0; i<10; i++){
cout<<ids[i]<<" "<<names[i]<<endl;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the student name: Suresh
Enter the ID for Suresh: 111
ID is already existed.
Enter the student name: ^C
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the student name: Suresh
Enter the ID for Suresh: 111
Enter the student name: Sekhar
Enter the ID for Sekhar: 222
Enter the student name: Anshu
Enter the ID for Anshu: 111
ID is already existed.
Enter the student name: Anshu
Enter the ID for Anshu: 333
Enter the student name: Revathi
Enter the ID for Revathi: 444
Enter the student name: qqqq
Enter the ID for qqqq: 555
Enter the student name: wwwww
Enter the ID for wwwww: 666
Enter the student name: tttttt
Enter the ID for tttttt: 777
Enter the student name: uuuu
Enter the ID for uuuu: 888
Enter the student name: jjjjj
Enter the ID for jjjjj: 999
Enter the student name: bbbb
Enter the ID for bbbb: 100
Students:
111 Suresh
222 Sekhar
333 Anshu
444 Revathi
555 qqqq
666 wwwww
777 tttttt
888 uuuu
999 jjjjj
100 bbbb
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.