I have two arrays each containing 100 digits.I need to add corresponding digits from units place meaning from the end of the array.If there is a carry it should be added in the next index.It should return another array containing the sum.
Here is my code and something seems to be wrong .Help me
void sumOf100DigitNumbers(int num1[100], int num2[100], int sum[101]) {
int i, j = 100, k;
for (i = 99; i >= 0; i--)
{
if (sum[j] == 1)
{
}
else
sum[j] = 0;
k = sum[j] + num1[i] + num2[i];
if (k >= 10)
{
sum[j] = k % 10;
sum[j - 1] = 1;
}
else
{
sum[j] = k;
}
j--;
}
}
Method to test it
[TestMethod, Timeout(1000)]
void Test_sumOf100DigitNumbers2()
{
int a[100] = {6, 8, 3, 7, 8, 1, 2, 4, 6, 7, 2, 0, 6, 6, 7, 0, 0, 9, 4, 8, 2, 9, 6, 3, 1, 7, 2, 3, 0, 4, 4, 5, 0, 9, 7, 0, 8, 9, 2, 6, 9, 2, 8, 8, 0, 2, 8, 2, 6, 5, 3, 0, 5, 2, 2, 5, 8, 8, 6, 6, 2, 3, 6, 0, 7, 0, 9, 9, 0, 4, 6, 4, 0, 4, 5, 1, 9, 5, 1, 5, 3, 6, 6, 3, 2, 4, 0, 7, 7, 8, 4, 6, 8, 7, 8, 9, 1, 6, 9, 2};
int b[100] = { 4, 1, 4, 1, 5, 0, 1, 8, 4, 5, 9, 7, 6, 2, 2, 0, 1, 7, 2, 5, 0, 3, 6, 9, 0, 8, 7, 3, 0, 2, 7, 8, 6, 5, 7, 3, 6, 8, 4, 2, 9, 2, 4, 8, 2, 1, 1, 0, 6, 6, 2, 7, 2, 8, 9, 7, 2, 4, 2, 2, 7, 6, 0, 7, 2, 3, 8, 4, 2, 5, 4, 7, 1, 8, 9, 9, 7, 0, 3, 2, 5, 1, 9, 7, 1, 0, 0, 0, 2, 1, 1, 5, 9, 0, 0, 1, 6, 6, 9, 7 };
int ans[101] = { 1, 0, 9, 7, 9, 3, 1, 4, 3, 1, 3, 1, 8, 2, 8, 9, 0, 2, 6, 7, 3, 3, 3, 3, 2, 2, 5, 9, 6, 0, 7, 2, 3, 7, 5, 4, 4, 5, 7, 6, 9, 8, 5, 3, 6, 2, 3, 9, 3, 3, 1, 5, 7, 8, 1, 2, 3, 1, 2, 8, 8, 9, 9, 6, 7, 9, 4, 8, 3, 3, 0, 1, 1, 2, 3, 5, 1, 6, 5, 4, 7, 8, 8, 6, 0, 3, 4, 0, 7, 9, 9, 6, 2, 7, 7, 9, 0, 8, 3, 8, 9 };
int c[101];
sumOf100DigitNumbers(a, b, c);
Assert::AreEqual(true, areEqualArrays(ans, c, 101), L"sumOf100DigitNumbers() failed", 1, 2);
};
but my output is as follows:
Output
You never initialize c. This means that its contents are indeterminate. You can't predict what values it will contain.
So when you do this:
if (sum[j] == 1)
If one of those indeterminate values happens to be 1 then you're performing a carry you don't intend on doing.
You can initialize it as follows:
int c[101] = { 0 };
Then you can remove this entirely:
if (sum[j] == 1)
{
}
else
sum[j] = 0;
Related
I'm using OpenCV v4.4.0 with gcc v10.1 on Ubuntu 18.04.5. This code snippet:
using namespace std;
using namespace cv;
using namespace cv::viz;
......
vector<Vec3f> points{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {2, 0, 0}, {2, 1, 0}};
vector<int> faces{4, 0, 1, 2, 0, 4, 0, 2, 3, 0, 5, 1, 4, 5, 2, 1};
Viz3d window("Mesh");
WMesh mesh(points, faces);
window.setBackgroundColor(Color::gray());
mesh.setColor(Color::indigo());
mesh.setRenderingProperty(OPACITY, 0.4);
mesh.setRenderingProperty(SHADING, SHADING_FLAT);
mesh.setRenderingProperty(REPRESENTATION, REPRESENTATION_SURFACE);
window.showWidget("mesh", mesh);
window.spin();
creates and displays this planar mesh:
The square on the left is defined as 2 triangles and is shaded uniformly, but the square on the right, which is defined as a single quadrilateral face is not shaded uniformly. Again, the mesh is planar. Why the non-uniformity?
It becomes even more non-uniform when I change shading from SHADING_FLAT to SHADING_GOURAUD:
Can someone explain what is going on here? I know that quadrilateral faces are converted to triangles, but why the shading is non-uniform?
EDIT
As noted by Матвей Вислоух in his answer below, I intended to use:
vector<int> faces{3, 0, 1, 2, 3, 0, 2, 3, 4, 1, 4, 5, 2};
which properly defines two triangular and one quadrilateral face. This solves the problem of artifacts in the left half, but they still remain in the right half:
vector<int> faces{3, 0, 1, 2, 3, 0, 2, 3, 4, 1, 4, 2, 5};
There is a specific rule how opencv unpacks indices from polygon.
if there is a polygon : (5, 1, 4, 5, 2, 0) it consists of 3 triangles: (1,4,5) , (4, 5, 2), (5, 2, 0)
OLD:
vector faces{4, 0, 1, 2, 0, 4, 0, 2, 3, 0, 5, 1, 4, 5, 2, 1};
This means you draw 3 polygons: 2 quads and 1 pentagon.
But by indices I guess that you want to draw 2 triangles, and one quad, so try this:
vector faces{3, 0, 1, 2, 3, 0, 2, 3, 4, 1, 4, 5, 2};
I'm trying to pack 16 bits data to 8 bits by using _mm256_shuffle_epi8 but the result i have is not what i'm expecting.
auto srcData = _mm256_setr_epi8(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);
__m256i vperm = _mm256_setr_epi8( 0, 2, 4, 6, 8, 10, 12, 14,
16, 18, 20, 22, 24, 26, 28, 30,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1);
auto result = _mm256_shuffle_epi8(srcData, vperm);
I'm expecting that result contains:
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
But i have instead:
1, 3, 5, 7, 9, 11, 13, 15, 1, 3, 5, 7, 9, 11, 13, 15,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
I surely misunderstood how Shuffle works.
If anyone can enlighten me, it will be very appreciated :)
Yeah, to be expected. Look at the docs for _mm_shuffle_epi8. The 256bit avx version simply duplicates the behaviour of that 128bit instruction for the two 16byte values in the YMM register.
So you can shuffle the first 16 values, or the last 16 values; however you cannot shuffle values across the 16byte boundary. (You'll notice that all numbers over 16, are the same numbers minus 16. e.g. 19->3, 31->15, etc).
you'll need to do this with an additional step.
__m256i vperm = _mm256_setr_epi8( 0, 2, 4, 6, 8, 10, 12, 14,
-1, -1, -1, -1, -1, -1, -1, -1,
0, 2, 4, 6, 8, 10, 12, 14,
-1, -1, -1, -1, -1, -1, -1, -1);
and then use _mm256_permute2f128_si256 to pull the 0th and 2nd byte into the first 128bits.
Ok so I want to do this operation in Eigen:
float StartMatrix[7][7] = { { 1, 4, 6, 9, 3, 5, 8 }, { 2, 5, 3, 7, 4, 8, 2 }, { 3, 6, 6, 7, 0, 2, 4 },
{ 2, 4, 3, 7, 4, 8, 2 }, { 2, 3, 3, 11, 4, 8, 1 }, { 2, 12, 3, 7, 0, 8, 2 },
{ 2, 2, 3, 4, 4, 11, 2 } };
float TotalMatrix[7] = { 22, 15, 13, 26, 27, 33, 19 };
float CoMatrix[7][7] = { { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 } };
for (int row = 0; row < 7; row++) {
for (int col = 0; col < 7; col++) {
CoMatrix[row][col] = StartMatrix[row][col] / TotalMatrix[col];
}
}
Divide each row by just the column in the TotalMatrix. And then I want to subtract the Identity matrix from the CoMatrix in Eigen and get the inverse on that (just to get an idea why I want to do this).
Problem is, how do I either perform this operation with Eigen, or somehow get the CoMatrix array into a matrix in Eigen so I can do stuff with it (like getting inverse etc).
Thanks!
Your code in Eigen would look something like this (after importing the Eigen namespace, using namespace Eigen;):
MatrixXd StartMatrix(7, 7);
StartMatrix <<
1, 4, 6, 9, 3, 5, 8, 2, 5, 3, 7, 4, 8, 2, 3, 6, 6, 7, 0, 2, 4,
2, 4, 3, 7, 4, 8, 2, 2, 3, 3, 11, 4, 8, 1, 2, 12, 3, 7, 0, 8, 2,
2, 2, 3, 4, 4, 11, 2;
VectorXd TotalMatrix(7);
TotalMatrix << 22, 15, 13, 26, 27, 33, 19;
MatrixXd CoMatrix = MatrixXd::Zero(StartMatrix.rows(), StartMatrix.cols());
CoMatrix = StartMatrix.array() / (TotalMatrix.replicate(1,StartMatrix.cols())).array();
You can continue subtracting the identity matrix with
CoMatrix -= MatrixXd::Identity(CoMatrix.rows(), CoMatrix.cols());
or combine it with the previous expression as:
CoMatrix = (StartMatrix.array() / (TotalMatrix.replicate(1, StartMatrix.cols())).array()).matrix()
- MatrixXd::Identity(CoMatrix.rows(), CoMatrix.cols());
I need to check, whether a letter (in english and russian languages) is alphabetical. A file is supposed to be encoded with UTF-8 by default.
I found out, that the best solution is working with UCS codes.
The way to calculate UCS-code of 2-bytes encoded letter is
#include <stdio.h>
#include <stdlib.h>
char utf8len[256] = {
// len = utf8len[c] & 0x7 cont = utf8len[c] & 0x8
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 - 15
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 16 - 31
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 32 - 47
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 48 - 63
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80 - 95
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 112 - 127
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 80 - 8f
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 90 - 9f
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // a0 - af
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // b0 - bf
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c0 - cf
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // d0 - df
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // e0 - ef
4, 4, 4, 4, 4, 4, 4, 4, // f0 - f7
5, 5, 5, 5, // f8, f9, fa, fb
6, 6, // fc, fd
0, 0 // fe, ff
};
#define UTF8LEN(c) (utf8len[(unsigned char)(c)] & 0x7)
#define UTF8CONT(c) (utf8len[(unsigned char)(c)] & 0x8)
int main (int argc, char *argv[])
{
char *s = "Б№1АГД"; //string which contains cyrillic symbols
while (*s) {
int ucode;
printf ("[%s] %d\n", s, UTF8LEN(*s));
if ((UTF8LEN(*s) == 2) && UTF8CONT(s[1])) {
ucode = ((*s & 0x1f) << 6) | (s[1] & 0x3f); //! HERE I GET UCS CODE
printf ("ucode = 0x%x\n", ucode);
s++;
}
s++;
}
}
It's a half of the solution I'm looking for. This code alows me to work with cyrillic symbols only (as they're encoded with 2 bytes in UTF-8). The problem is, I need to work with latin alphabet as well.
So what should i do to get UCS code for 1-byte symbol (in my case with UTF8LEN(c)=1)?
Upd: Probably, the solution is:
ucode = *s
Will this work?
I have been staring at this code for few hours, tried walkthrough,debugging with autos and breakpoints and it's no solution so far. Maybie someone's fresh look would help me ;) .
#include <iostream>
using namespace std;
int matrix[9][9] = {{0, 0, 6, 0, 0, 0, 1, 0, 5},
{0, 4, 0, 7, 0, 6, 0, 3, 9},
{2, 0, 0, 9, 3, 0, 6, 0, 0},
{7, 0, 0, 1, 8, 0, 5, 0, 4},
{0, 0, 4, 0, 6, 0, 9, 0, 0},
{1, 0, 9, 0, 5, 2, 0, 0, 3},
{0, 0, 1, 0, 9, 3, 0, 0, 7},
{6, 7, 0, 5, 0, 8, 0, 9, 0},
{9, 0, 8, 0, 0, 0, 4, 0, 0}};
bool check(int column ,int row,int checkedValue)
{
//column check
for(int i=0; i<9; i++)
{
if(i==row)continue;
if(checkedValue==matrix[column][i]) return false;
}
//row check
for(int i=0; i<9; i++)
{
if(i==column) continue;
if(checkedValue==matrix[i][row]) return false;
}
return true;
}
int main()
{
cout<<check(4,0,4); //Why does it output 0? There is no "4" in the 5th column and the 1st row.
system("pause");
return 0;
}
The function check(column,row,value) was designed to return 0 when number occurs at least once in the "matrix" two dimensional table. This program is a chunk of sudoku solver.
You mixed the indices up in the if statements. They should be:
if(checkedValue==matrix[i][column]) return false; // not matrix[column][i]
and
if(checkedValue==matrix[row][i]) return false; // not matrix[i][row]
The reason is that the first dimension is the row. You can check this by printing matrix[2][0].
For your matrix, you will get 2 (and not 6).