Question

Please Update my C++ code: Implement array to replace for vector: Example --> input: 100 output:...

Please Update my C++ code:

Implement array to replace for vector:

Example --> input: 100 output: A

1000Pairs.txt looks like this:

{100, A }
{200, A }
{300, B }
{400, C }
{500, D }
{600, E }
{700, F }
{800, G }
{900, H }
{1000, I }
{1100, J }
{1200, K }
{1300, L }
{1400, M }
{1500, N }
{1600, O }
{1700, P }
{1800, Q }
{1900, R }
{2000, S }
{2100, T }
{2200, U }
{2300, V }
{2400, W }
{2500, X }
{2600, Y }
{2700, Z }
{2800, [ }
{2900, \ }
{3000, ] }
{3100, ^ }
{3200, _ }
{3300, ` }
{3400, a }
{3500, b }
{3600, c }
{3700, d }
{3800, e }
{3900, f }

and so on...

//Here is my code below:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <sstream>
using namespace std;

int main(){
ifstream inFile;
string s;
char a, b, c, d, e, f;
int num;
inFile.open("1000Pairs.txt");
if(!inFile){
cerr << "Unable to open file datafile.txt";
exit(1); // call system to stop
}
//Vector to store the data
vector<pair<int, char> > v;

while(getline(inFile, s)) {
string number;
for(int i = 1; i < s.length(); i++) {
if((s[i] ==' ') or (s[i] == '{') or (s[i] == '}'))
continue;
else if(s[i] >= '0' && s[i] <= '9') {
number += s[i];
}
else if(s[i] ==','){
//convert string to number
stringstream ss(number);
num = 0;
ss >> num;
//cout<<"num "<<num<<"\n";
}
else{
char d = s[i];
v.push_back(make_pair(num, d));
}

}

}
inFile.close();
/*for (int i = 0; i < v.size(); ++i)
{
cout<<v[i].first<<" "<<v[i].second<<endl;
}*/
int input;
cout<<"input: ";
cin>>input;
for (int i = 0; i < v.size(); ++i){
if(v[i].first == input) {
cout<<"output: "<<v[i].second<<endl;
}
}
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <vector>

#include <sstream>

using namespace std;

template <typename T>

T *incArray(T *arr, T newEle, int size)

{

T *newArr = (T *)malloc(sizeof(T) * (size + 1));

for (int i = 0; i < size; i++)

{

newArr[i] = arr[i];

}

newArr[size] = newEle;

return newArr;

}

int main() {

ifstream inFile;

string s;

char a, b, c, d, e, f;

int num,size = 0;

inFile.open("1000Pairs.txt");

if (!inFile) {

cerr << "Unable to open file datafile.txt";

exit(1); // call system to stop

}

//Vector to store the data

int *numbers;

char *chars;

while (getline(inFile, s)) {

string number;

for (int i = 1; i < s.length(); i++) {

if ((s[i] == ' ') or(s[i] == '{') or(s[i] == '}'))

continue;

else if (s[i] >= '0' && s[i] <= '9') {

number += s[i];

} else if (s[i] == ',') {

//convert string to number

stringstream ss(number);

num = 0;

ss >> num;

//cout<<"num "<<num<<"\n";

} else {

char d = s[i];

numbers = incArray(numbers, num, size);

chars = incArray(chars, d, size);

size++;

}

}

}

inFile.close();

/*for (int i = 0; i < v.size(); ++i)

{

cout<<v[i].first<<" "<<v[i].second<<endl;

}*/

int input;

cout << "input: ";

cin >> input;

for (int i = 0; i < size; ++i) {

if (numbers[i] == input) {

cout << "output: " << chars[i] << endl;

}

}

return 0;

}

1000Pairs.txt:

{100, A }
{200, A }
{300, B }
{400, C }
{500, D }
{600, E }
{700, F }
{800, G }
{900, H }
{1000, I }
{1100, J }
{1200, K }
{1300, L }
{1400, M }
{1500, N }
{1600, O }
{1700, P }
{1800, Q }
{1900, R }
{2000, S }
{2100, T }
{2200, U }
{2300, V }
{2400, W }
{2500, X }
{2600, Y }
{2700, Z }
{2800, [ }
{2900, \ }
{3000, ] }
{3100, ^ }
{3200, _ }
{3300, ` }
{3400, a }
{3500, b }
{3600, c }
{3700, d }
{3800, e }
{3900, f }

Output:

Add a comment
Know the answer?
Add Answer to:
Please Update my C++ code: Implement array to replace for vector: Example --> input: 100 output:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • Can someone please edit my current code to accept a user input for the text file...

    Can someone please edit my current code to accept a user input for the text file name in lieu of how it currently will read data1.txt. Also if the user enters a data file that isn't ready to be used, cout a statement "cout << "Could not open file " << userInputforData.txt << endl; Another condition that I need to add is if there is a special character anywhere in the word it cannot be an acceptable variable name. could...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • What is the output of the following code snippet? (If there is some kind of syntax...

    What is the output of the following code snippet? (If there is some kind of syntax error indicate this by marking "error" in your answer choice) 1.        int fly = 5; int x;        if (fly-- > 5)               x = 5;        else               x = 2;        if (x++ > 3)               cout << x;        cout << fly << endl; 2.   int i = 0;        bool b = i == 0 || i++ > 0;        if (!b)               cout << i << endl;        else               cout...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT