I'm not getting the correct output of my elab question. Here's the question:
There are faculties in the department, you have to arrange the list of faculties depending upon ID numbers.
TEST CASE 1
INPUT
5 Ram 101 Rahul 95 Ashwin 75 Ahamed 106 Saurav 110
OUTPUT
After Sorting Name ID Ashwin 75 Rahul 95 Ram 101 Ahamed 106 Saurav 110
Here's the code I implemented using structures:
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct faculty{
string name;
int id;
};
int main(){
int n;
struct faculty arr[n];
for(int i=0;i<n;i++){
cin>>arr[i].name;
cin>>arr[i].id;
}
cout<<"After Sorting"<<endl;
cout<<"Name ID"<<endl;
//insertion sort
for(int i=1;i<n;i++){
struct faculty key=arr[i];
int j=i-1;
while(j>=0 && arr[j].id>key.id)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
//printing
for(int i=0;i<n;i++){
cout<<arr[i].name<<" "<<arr[i].id;
}
return 0;
}
My output:
After Sorting
Name ID
5 0
Can someone help me? I can't figure out what the mistake is.
First, some analysis for your code:
#include<bits/stdc++.h>
. This is non-compliant C++ code. It will not work with other compilersusing namespace std;
. Always prefer using the scope explicitlyarr[n]
) -std=c++14 -Wall -Wextra -Wpedantic
std::vector
Concrete errors:
std::vector
So, your fixed software would look like this:
#include<iostream>
#include <string>
#include <vector>
struct faculty {
std::string name;
int id;
};
int main() {
int n;
std::cin >> n;
std::vector<faculty> arr(n);
for (int i = 0; i < n; i++) {
std::cin >> arr[i].name;
std::cin >> arr[i].id;
}
std::cout << "After Sorting" << std::endl;
std::cout << "Name ID" << std::endl;
//insertion sort
for (int i = 1; i < n; i++) {
faculty key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j].id > key.id)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
//printing
for (int i = 0; i < n; i++) {
std::cout << arr[i].name << " " << arr[i].id << '\n';
}
return 0;
}
But this is not the preferred solution.
You should tackle your problem in a different way to come to a better solution.
First, we need to initialize WHAT to do.
std::cin
must be possiblestd::cout
Next, we need to think on HOW we can fullfill the requirements. We do not write any code yet. Ok, lets's see:
std::string
for the name and an unsigned int
for the ID, because, we assume that the ID will never be negative.>>
. So, we can read the data now from any stream.<<
.if
- statement and check, if that worked.std::vector
for that.OK, we clarified the WHAT and the HOW. Now (not before), we can start coding.
Unfortunately, there are millions of possible solutions. I will show a more advanced version, but you can implement anything according to your needs.
Please see the below example:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
struct Faculty {
// 1. Our data. Initiliaze with default
std::string name{};
unsigned int id{};
// 2. Define extractor operator for this data
friend std::istream& operator >> (std::istream& is, Faculty& f) {
return is >> f.name >> f.id;
}
// 3. Define inserter operator for this data
friend std::ostream& operator << (std::ostream& os, const Faculty& f) {
return os << f.name << '\t' << f.id;
}
};
int main() {
// 4. Get the number of members that we should read, and check, if input worked
size_t numberOfMembers{};
if (std::cin >> numberOfMembers) {
// 5. Here we will store all data
std::vector<Faculty> data{};
// 6. Copy all data from std::cin into our data vector
std::copy_n(std::istream_iterator<Faculty>(std::cin), numberOfMembers, std::back_inserter(data));
// 7. Sort according to ID
std::sort(data.begin(), data.end(), [](const Faculty& f1, const Faculty& f2) { return f1.id < f2.id; });
// 8. Output
std::copy(data.begin(), data.end(), std::ostream_iterator<Faculty>(std::cout, "\n"));
}
return 0;
}
To be compiled with C++14 enabled.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.