I have a problem with adding an array in to a second column of list contol. The problem is, that the number of filled rows in the second column is the same as the number of filled rows in the first column. My code is :
enter code here
for (int i=0; i<velikost; i++) // velikost = 10 (velikost is size)
{
niz.Format(_T("%d"),polje[i]);
m_listCtrl.InsertItem(i,niz);
}
polje_stevcev = new int[max]; // new array with size of max number
for(int i=0; i<max + 1; i++) // lets say the max is 90
{
polje_stevcev[i] = 0;
niz2.Format(_T("%d"), polje_stevcev[i]);
m_listCtrl.SetItemText(i,1,niz2);
}`enter code here`
We see in the second for loop that the loop is performed 90 times, but in list control in in second column show me only first 10 rows, others are empty.
Picture (in second column should be 95 rows with number 0, but shows only 10) : Here is a screenshot
Thanks.
Related
I develop a project that uses QTableWidget. Below is a small test fragment of code reproducing the problem.
auto tw = new QTableWidget(3,3);
tw->setItem(0, 0, new QTableWidgetItem("test123 test123 test123 test123 test123 test123 test123"));
for (int i = 1; i < 3; ++i)
for (int j = 0; j < 3; ++j)
tw->setItem(i, j, new QTableWidgetItem("test123"));
tw->setSpan(0,0,1,3);
tw->show();
tw->resizeRowsToContents();
There is a table with 3 rows and 3 columns. The first cell of the table contains a long text. I create a column span at this cell to fit the text and avoid changing other columns width.
The issue is occured when I try to resize rows to their contents. All rows except the first one is resized without any problems - they take up minimal space. The first one gets much more higher that it was before (I expected it to have the same height as the other rows). It seems like QTableWidget::resizeRowsToContents() does not respect column span at all so it increases the row height to fit cell contents.
Could you please tell me how to fix this problem and force rows with column span be resized correctly?
I have a 2D array:
A[65][4];
Is it possible to increment all the value of a specific column with looping through the row?
i.e. an equivalent command to:
for (int i = 0 ; i < 65 ; i++)
A[i][2]++;
Thanks
I am trying to create a program in c++ that takes in triangle pattern of numbers into a 2-D array.
Example:
1
3 4
5 9 2
9 4 6 1
The top row is one number(integer) and each row of the triangle has one more number than the row above it.
Once the triangle has been entered and/or examined via for loops, the program needs to traverse the triangle from top to bottom and record all possible sums of each path;
The path taken down the triangle must always be adjacent to the number in the row above it.
While traversing down the triangle each "path" should be stored in a new array so the path can be displayed.
After recording the sum for each path down the triangle the program should compare them and display the path with the smallest sum.
With the changes i made so far thanks to #Beta i have this so far:
int main()
{
int row = 0;
int col = 0;
int A[4][4] = {{2},{8,9},{3,4,5},{6,2,9,1}};
for (row = 0; row < 4; row++)
{
for (col = 0; col <= row; col++)
{
cout << A[row][col] << " ";
}
cout << endl;
My Output is so far is:
2
8 9
3 4 5
6 2 9 1
I think the trick you're looking for is this:
for (col = 0; col <= row; col ++)
I couldn't parse the last part of your question ("After that...").
EDIT:
The problem of making the triangle look symmetrical is a problem of printing spaces at the beginning of each line. With the trick above, you should be able to figure than one out.
As for considering all paths and displaying the one with the smallest sum, what have you tried? If you are not familiar with Breadth-First Search and Depth-First Search, copying arrays and arrays of pointers, you're probably not ready for this exercise.
I'm supposed to multiply two 3-digit numbers the way we used to do in childhood.
I need to multiply each digit of a number with each of the other number's digit, calculate the carry, add the individual products and store the result.
I was able to store the 3 products obtained (for I/P 234 and 456):
1404
1170
0936
..in a 2D array.
Now when I try to arrange them in the following manner:
001404
011700
093600
to ease addition to get the result; by:
for(j=5;j>1;j--)
{
xx[0][j]=xx[0][j-2];
}
for(j=4;j>0;j--)
{
xx[1][j]=xx[1][j-1];
}
xx is the 2D array I've stored the 3 products in.
everything seems to be going fine till I do this:
xx[0][0]=0;
xx[0][1]=0;
xx[1][0]=0;
Here's when things go awry. The values get all mashed up. On printing, I get 001400 041700 093604.
What am I doing wrong?
Assuming the first index of xx is the partial sum, that the second index is the digit in that sum, and that the partial sums are stored with the highest digit at the lowest index,
for (int i = 0; i < NUM_DIGITS; i++) // NUM_DIGITS = number of digits in multiplicands
{
for (int j = 5; j >= 0; j--) // Assuming 5 is big enough
{
int index = (j - 1) - (NUM_DIGITS - 1) - i;
xx[i][j] = index >= 0 ? xx[i][index] : 0;
}
}
There are definitely more efficient/logical ways of doing this, of course, such as avoiding storing the digits individually, but within the constraints of the problem, this should give you the right answer.
I want to layout X buttons.
At the start, Y items can be in a row.
After the first row is laid out, only Y - 1 items can appear in the next row and so on.
So say I have 13 buttons and the first row can have up to 6 buttons, I would need 3 rows. The first would have 6 buttons the second 5 buttons, and the 3ed 2 buttons.
Thanks
What algorithm could be to do:
int getRowCount(int startCols, int numItems);
I know how to do it with MOD if the number of columns is constant but how would you do it if the maximum number of columns decreases with each row?
In situations like this, I try to translate the english into code.
int getRowCount(int startCols, int numItems) {
int currentCols = startCols;
int numRows = 0;
while (numItems > 0) { // as long as items remain
numRows += 1; // add another row
numItems -= currentCols; // reduce the remaining items by the current number of columns
currentCols--; // reduce the number of columns by one
}
}
It's always best to run through the scenario with some edge cases. Ask yourself questions like:
What answer do I get if numItems is 0?
What answer do I get if startCols is 0?
What answer do I get if numItems == startCols?