Hi everyone, I new to C++ and seeking for some advice from c++ experts here for the following task. This post is a continuation of my previous post (
http://www.cplusplus.com/forum/beginner/261746/). Looking at the previous post was too long ago, I decide to write a new post.
Matrix data (10x3) were provided to test the effectiveness of this coding.
0 9 8 7 6 5 4 3 2 1
0 1 2 3 4 5 6 7 8 9
5 4 3 2 1 0 9 8 7 6
The task as follows:
1. Need to choose one random number in the range 3 to 9. (done)
For example, random number (r1) = 3
2. Find position of this random number for each row. (done)
For example,
For row 1, random number 3 was found at position 8.
For row 2, random number 3 was found at position 4.
For row 3, random number 3 was found at position 3.
3. Do neighbor swapping with condition for each row. (Error here line 64-71)
Must swap random number (r1) with it neighbor which is its next position. But can't swap with number (0, 1, 2). If this three number appear. Must swap with position before random number.
For example (Row 1):
0 9 8 7 6 5 4 3 2 1
Random number 3 at position 8. Since next position is number 2. We need to swap number 3 with number 4 (position before number 3).
4. Display new vector after swapping.
For example (Row 1):
0 9 8 7 6 5 3 4 2 1
I don't know how to correct the errors. Hopefully someone can teach me or give some advice. Thank you for your kindness.
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
|
#include <iostream> // cout
#include <fstream>
#include <sstream>
#include <ctime> // time
#include <cstdlib> // srand, rand()
#include <algorithm> // find(), iter_swap()
#include <vector> // vector
using namespace std;
int main()
{
// Initialize random seed:
srand(time(NULL));
// Read data from text file
ifstream txtdata("data.txt");
vector<vector<int>> vec;
for (string line; getline(txtdata, line); )
{
vec.push_back(vector<int>());
istringstream iss(line);
for (int n; iss >> n; )
vec.back().push_back(n);
}
// Print Original Vector
cout << "Original vector :" << endl;
for (size_t i = 0; i < vec.size(); i++)
{
for (size_t j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
// Choose random number in the range 3 to 9
int r1 = rand() % 6 + 3;
cout << "First random number = " << r1 << endl;
cout << "\n";
for (size_t i = 0; i < vec.size(); i++)
{
// Iterator used to store the position of searched element
vector<int>::iterator it, ik;
// Find position of first random number, r1
it = find(vec[i].begin(), vec[i].end(), r1);
if (it != vec[i].end())
{
cout << "For row " << i + 1 << "\n";
cout << "First random number " << r1 << " was found at position ";
cout << it - vec[i].begin() + 1 << "\n";
}
// Condition to swap
// 1. Must swap it with it + 1
// 2. But, if (it + 1) equal to {0, 1 or 2). Swap with it - 1.
if (vec[it + 1] == 0) || (vec[it + 1] == 1) || (vec[it + 1] == 2);
{
ik = it - 1;
}
else
{
ik = it + 1;
}
// Swap them
if (it != vec[i].end() && ik != vec[i].end())
{
iter_swap(it, ik);
}
}
cout << "\n";
// Print New Vector
cout << "New vector after swapping :" << "\n";
for (size_t i = 0; i < vec.size(); i++)
{
for (size_t j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
return 0;
}
|