Does anyone know how to add a vector into an array?
I'm given a array table but to solve collisions, i need to implement vectors.
I am provided with this code:
void generateTable(string DNA, int N, int K, int *&table) {
int tableSize = 1;
for (int i = 0; i < K; i++)
table[i] = 0;
I had planned to implement the hashing as such:
for(int i = 0; i < N - K; i++) {
temp = DNA.substr(i, K);
for (int j = 0; j < K; j++) {
value = value * 10 + convert(temp[j]) //this is just to convert the substr into int.
if (table[value % tableSize] == 0)
table[value % tableSize] = value;
else {
//this is where i plan to use vectors to solve collisions
thank you so much for your help. Do let me know if there is any confusion about my question
Related
I'm trying to learn c++ and wrote this algorithm and I am wondering if there is a faster way to do the same thing. This is assuming that the input is valid. I was trying to think of how to remove the nested for loop but decided that it is fine since it is not exponential. Is this correct? Thanks
void DigitSort(int* arr, int size)
{
int counts[10] = { 0,0,0,0,0,0,0,0,0,0 };
int k = -1;
while (++k < size)
counts[arr[k]]++;
k = -1;
for (int j = 0; j < 10; ++j)
for (int i = 0; i < counts[j]; ++i)
arr[++k] = j;
}
There is no benchmark, but here is a (probably) faster solution, using std::fill_n.
void DigitSort(int* arr, int size)
{
int counts[10] = { 0,0,0,0,0,0,0,0,0,0 };
int k = -1, sum_count = 0;
while (++k < size)
counts[arr[k]]++;
for (k = 0; k < 10; ++k) {
std::fill_n(arr + sum_count, counts[k], k);
sum_count += counts[k];
}
}
When I say "probably", it's because the compiler can optimize the std::fill_n to a memset-like instruction.
I'm trying to implement a quick program to solve a system of linear equations. The program reads the input from a file and then writes the upper-triangular system and solutions to a file. It is working with no pivoting, but when I try to implement the pivoting it produces incorrect results.
As example input, here is the following system of equations:
w+2x-3y+4z=12
2w+2x-2y+3z=10
x+y=-1
w-x+y-2z=-4
I expect the results to be w=1, x=0, y=-1 and z=2. When I don't pivot, I get this answer (with some rounding error on x). When I add in the pivoting, I get the same numbers but in the wrong order: w=2,x=1,y=-1 and z=0.
What do I need to do to get these in the correct order? Am I missing a step somewhere? I need to do column swapping instead of rows because I need to adapt this to a parallel algorithm later that requires that. Here is the code that does the elimination and back substitution:
void gaussian_elimination(double** A, double* b, double* x, int n)
{
int maxIndex;
double temp;
int i;
for (int k = 0; k < n; k++)
{
i = k;
for (int j = k+1; j < n; j++)
{
if (abs(A[k][j]) > abs(A[k][i]))
{
i = j;
}
}
if (i != k)
{
for (int j = 0; j < n; j++)
{
temp = A[j][k];
A[j][k] = A[j][i];
A[j][i] = temp;
}
}
for (int j = k + 1; j < n; j++)
{
A[k][j] = A[k][j] / A[k][k];
}
b[k] = b[k] / A[k][k];
A[k][k] = 1;
for (i = k + 1; i < n; i++)
{
for (int j = k + 1; j < n; j++)
{
A[i][j] = A[i][j] - A[i][k] * A[k][j];
}
b[i] = b[i] - A[i][k] * b[k];
A[i][k] = 0;
}
}
}
void back_substitution(double**U, double*x, double*y, int n)
{
for (int k = n - 1; k >= 0; k--)
{
x[k] = y[k];
for (int i = k - 1; i >= 0; i--)
{
y[i] = y[i] - x[k]*U[i][k];
}
}
}
I believe what you implemented is actually complete pivoting.
With complete pivoting, you must keep track of the permutation of columns, and apply the same permutation to your answer.
You can do this with an array {0, 1, ..., n}, where you swap the i'th and k'th values in the second loop. Then, rearange the solution using this array.
If what you were trying to do is partial pivoting, you need to look for the maximum in the respective row, and swap the rows and the values of 'b' accordingly.
So for my homework I have to create a function for adding two matrices, the numbers are given by the user, and I think I have the function somewhat down, I just have no idea what to return because I know you can't return an array, so I tried making a variable to set it equal to but it's not working? Can someone help me out with this?
This is the part I'm stuck on:
int add_matrix(int a[][10], int b[][10], int c[][10], int Anum_rows, int Anum_cols)
{
int matrix_total = c[0][0];
for (int i = 0; i < Anum_rows; ++i) {
for (int j = 0; j < Anum_cols; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
c[i][j] = matrix_total;
}
return matrix_total;
}
The last j in the c array is underlined with red also in visual studio.
Your code already does what it needs, it stores the resulting matrix in c, which, since arrays are passed as pointers, will still contain the updated values when add_matrix() returns. You just have extra code in the function, which should really not be needed to complete the matrix add operation:
void add_matrix(int a[][10], int b[][10], int c[][10], int Anum_rows, int Anum_cols) {
for (int i = 0; i < Anum_rows; ++i) {
for (int j = 0; j < Anum_cols; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
Good point about Anum_cols. There is no need to pass in the number of columns when it is specified in the argument definition and it could lead to future errors.
#define NUM_COLS 10
void add_matrix(int a[][NUM_COLS], int b[][NUM_COLS], int c[][NUM_COLS], int Anum_rows)
{
for (int i = 0; i < Anum_rows; ++i) {
for (int j = 0; j < NUM_COLS; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
I have this program that is trying to determine how many unique items are within some intersecting sets. The amount of input entirely depends on the the first value n, and then the amount of sets entered afterward. For example, if I start with entering n = 2, I am expected to enter 2 integers. The program then determines how many intersections there are between n items (this is like choosing 2 items from n items). This goes on as k increments. But that's kind of beyond the point. Just some background info.
My program adapts correctly and accepts the proper amount of input, but it stops working properly before the first for loop that is outside of the while loop. What I have tried to do is make a vector of integer vectors and then add every other row (when index starts at 0 AND index starts at 1). But I am guessing I have constructed my vectors incorrectly. Does anybody see an error in my vector logic?
#include <iostream>
#include <vector>
using namespace std;
int fact (int m) {
if (m <= 1)
return 1;
return m * fact(m - 1);
}
int comb (int n, int k) {
return fact(n)/(fact(n-k)*fact(k));
}
int main() {
int n = 0;
int k = 2;
int sum = 0;
int diff = 0;
int final = 0;
vector <vector <int> > arr;
cin >> n;
while (n > 0) {
vector <int> row;
int u;
for (int i = 0; i < n ; ++i) {
cin >> u;
row.push_back(u);
}
arr.push_back(row);
n = comb(row.size(), k);
k++;
}
for (int i = 0; i < arr.size(); i+2)
for (int j = 0; j < arr[i].size(); ++j)
sum += arr[i][j];
for (int i = 1; i < arr.size(); i+2)
for (int j = 0; j < arr[i].size(); ++j)
diff += arr[i][j];
final = sum - diff;
cout << final;
return 0;
}
for (int i = 0; i < arr.size(); i+=2)
^
You want to do i+=2 or i=i+2, else the value of i is never changed, leading to an infinite loop.
That is my function:
int main() {
double data[100];
int num;
cout<<"num= ";
cin>>num;
for(int i = 1; i <= num; i++) {
cout<<i<<" element = ";
cin>>data[i];
}
Sort(data, num);
for (int i = 1; i <= num; i++) {
cout<<data[i]<<endl;
}
return 0;
}
void Sort(double data[], int n) {
int i,j,k;
double min;
for(i = 0; i < n-1; i++) {
k = i;
min = data[k];
for(j = i+1; j < n; j++)
if(data[j] < min) {
k = j;
min = data[k];
}
data[k] = data[i];
data[i] = min;
}
}
if I write for exp. three elements: 8,9,1 again cout 8,9,1?
for(int i = 1; i <= num; i++) { // WRONG
I think you mean:
for(int i = 0; i < num; i++) { // RIGHT
Arrays in C are 0-indexed remember.
Your sorting function is fine. The only problem is that you enter elements at positions 1 through n, inclusive, while you should use 0 through n-1, inclusive, in both loops of the main() function.
If you need to print numbers 1 through n, use
cout<<(i+1)<<" element = ";
You should get used of the 0 index begin in the for loop
for(int i = 0; i < N; ++i)
so fixing these two index errors will make your code run properly.
the reason is:
if you write data to data[] using 1 as the begining, your data array's first item will be a random number:
if you insert 3 elements, the array will be like this:
data[0] = ??? // maybe a very very big number
data[1] = 8
data[2] = 9
data[3] = 1
and in your Sort function, your index begins at 0 and ends before num, that means your code would only sort data[0], data[1], data[2].
if you use: num = 3, 3 2 1 as your input data for the origin code you could see that 3 and 2 is sorted
I guess your Sort code is googled from somewhere, please try to understand it.
Good online algorithm course: https://www.coursera.org/course/algs4partI
a very good algorithm online book: http://algs4.cs.princeton.edu/home/
btw, for(j = i+1; j < n; j++) in the Sort function would be better if it has { } braces.