I'm trying to print the following table in C++:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Using nested while loops only.
I have two main problems here:
I don't understand how to do it without using a non-nested while at the beginning (to print the first line), or an if statement for the first line.
Using setw I have problems trying to align the numbers with two digits.
Here is what I tried
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int k=0;
while(k<=10)
{cout << k << setw(5);
k++;
};
cout << "\n";
int i=1;
while(i<=10){
cout << i << setw(5);
int j=1;
while(j<=10){
cout<< i*j << setw(5);
j++;
}
cout << "\n";
i++;
}
return 0;
}
But, as said, I used a non-nested while at the beginning, and also the output is:
0 1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Where the two-digits numbers are not aligned in the proper way. On the other hand I cannot think a way to modify the loop in order to increase the space only for two digits number, without using an if statement.
So am I missing something or it is not possible to print the table above without using if or non-nested while?
I think you want to hide first 0 value. To hide it, I have used some bit manipulations. There is no if statement and all of the printing in nested while.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i = 0, k = 0;
while (i < 10){
int j = 0;
while (j <= 10){
cout << left << setw(5);
(i || j || k) && cout << j + i * j + !j * (i + 1);
!(i || j || k) && cout << "";
j++;
}
cout << "\n";
i += k++ > 0;
}
return 0;
}
The output is
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Explanation:
I have used j for seed and i * j for new value for (j + 1)-th column.
It works perfectly for from first row second column to last row last column.
For first column, j is always 0. To make it work, I used !j * (i + 1) which prints 0 to 10 for first column.
To hide 0 for first row and first column, all of i, j and k are 0. For all other cells, at least one of them has value other than 0.
If still unclear, I'll explain with example.
Things to consider:
Outputting tabs instead of trying to set the width is much easier,
though you'll have less control over how your output looks (it will
depend in the tab stop width of your console). But your standard tab stops
will handle your range of values.
your problem statement doesn't say how many while loops you can use
You can use a while loop to emulate an if statement like this:
while(j == 0){
cout << i << '\t';
break;
}
I'll leave the rest as an exercise for you.
Related
I have two sheets in an Excel workbook. I need the formula which creates tables by using vlookup.
I have 10 columns and 10 rows like this
1 2 3 4 5 6 7 8 9 10
2
3
4
5
6
7
8
9
10
I have tried to use Vlookup with sum but not get the actual results.
The expected result should be like this
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 42 48 56 64 72 80
9 18 27 36 49 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
I want to print a number pattern:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
...
10 . .. . .
This is my code:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int cols, row, num=1;
for(row=1; row<=10; row++)
{
for(cols = row; cols <= row*10; cols = cols + row)
{
cout << cols << " ";
}
cout << "\n";
}
return 0;
getch();
}
But it gives me the output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
To make that pattern you only really need the number of rows. The number of columns in each row happens to be equal to the row number, and the values are integer-multipliers of that row number.
#include <iostream>
int main()
{
const int rows = 10;
for (int row = 1; row <= rows; ++row)
{
for (int col = 1; col <= row; ++col)
{
int value = row * col;
std::cout << value << ' ';
}
std::cout << '\n';
}
}
Output
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100
The following piece of code:
__m128 var1;
float *a = (float*)malloc(50*sizeof(float));
float *ptr = a;
//Initialise a with some values
for(int i = 0; i < 50; i++)
*(a+i) = i;
//print those values
for(int i = 0; i < 50; i+=4,ptr+=4)
{
var1 = _mm_loadu_ps(ptr);
cout<<(*ptr)<<" "<<var1[0]<<" "<<_mm_extract_ps(var1, 0)<<endl;
cout<<(*ptr+1)<<" "<<var1[1]<<" "<<_mm_extract_ps(var1, 1)<<endl;
cout<<(*ptr+2)<<" "<<var1[2]<<" "<<_mm_extract_ps(var1, 2)<<endl;
cout<<(*ptr+3)<<" "<<var1[3]<<" "<<_mm_extract_ps(var1, 3)<<endl;
}
returns this output:
0 0 0
1 1 1065353216
2 2 1073741824
3 3 1077936128
4 4 1082130432
5 5 1084227584
6 6 1086324736
7 7 1088421888
8 8 1090519040
9 9 1091567616
10 10 1092616192
11 11 1093664768
12 12 1094713344
13 13 1095761920
14 14 1096810496
15 15 1097859072
16 16 1098907648
17 17 1099431936
18 18 1099956224
19 19 1100480512
20 20 1101004800
21 21 1101529088
22 22 1102053376
23 23 1102577664
24 24 1103101952
25 25 1103626240
26 26 1104150528
27 27 1104674816
28 28 1105199104
29 29 1105723392
30 30 1106247680
31 31 1106771968
32 32 1107296256
33 33 1107558400
34 34 1107820544
35 35 1108082688
36 36 1108344832
37 37 1108606976
38 38 1108869120
39 39 1109131264
40 40 1109393408
41 41 1109655552
42 42 1109917696
43 43 1110179840
44 44 1110441984
45 45 1110704128
46 46 1110966272
47 47 1111228416
48 48 1111490560
49 49 1111752704
1.45875e-42 1.45875e-42 1041
0 0 0
My questions is: Isn't _mm_extract_ps the right way of accessing the contents of an __m128 variable? Why does it print values that dont match the actual value, whereas var[0] prints the correct values. As far as I know, accessing the fields of an __m128 variable using var[0] is incorrect and may lead to problems. What exactly is the right approach, at times when I need to debug my code.
Type of a is pointer to float, when you write float == 1.0f, into memory, its representation in hex is 0x3F800000, decimal value is 1 065 353 216, so printed value is valid, _mm_extract_ps returns int, and cout prints it. Hex representation of 2.0f is 0x40000000, in decimal 1 073 741 824. You printed hex representation of float using decimal value.
Hi so I have created the nested list/matrix:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m
1 2 3
4 5 6
7 8 9
I have also identified the middle column in the list:
q)a:m[0;1],m[1;1],m[2;1]
I now want to replace the middle row (4 5 6) with a to finish with m looking like:
q)m
1 2 3
2 5 8
7 8 9
You've already seen you can index into the matrix with syntax like m[0;1], where 0 refers to the first level of nesting and 1 refers to the second level
KDB also allows you to assign to an index of a list in a similar way e.g.
q)l:1 2 3 4
q)l[1]:20
q)l
1 20 3 4
So you can use something similar in this example:
q)m[1]:a
q)m
1 2 3
2 5 8
7 8 9
As an aside, KDB also allows you to leave out an index, in which case it will take all items from the corresponding level of nesting, e.g.
q)m[0] /first level of nesting i.e. first row
1 2 3
q)m[;0] /second level of nesting i.e. first column
1 4 7
Hope that helps
Jonathon McMurray
AquaQ Analytics
You want to generalise for larger matrices (which must also be square) so your answer needs two parts:
how to construct a
how to insert it
for row/col x where x<count m.
The general expression you want is simply m[x;]:m[;x], because m[x;] denotes row x and m[;x] denotes column x.
See Q for Mortals 3.11.3 Two- and Three-Dimensional Matrices
You can make this a function of the index and the matrix:
q)show m:5 5#1_til 26
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
q){y[x;]:y[;x];:y}[3;m]
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
4 9 14 19 24
21 22 23 24 25
Just adding another approach for you.
q)m:8 cut til 64
q)0 0+\:til 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
q)(m)./:flip 0 0+\:til 8
0 9 18 27 36 45 54 63
q)#[m;4;:;(m)./:flip 0 0+\:til 8]
0 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
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q)
For fun, here it is in a function which takes the length&width of the matrix and replaces the 'middle' row with the diagonal values
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}8
0 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
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}5
0 1 2 3 4
5 6 7 8 9
0 6 12 18 24
15 16 17 18 19
20 21 22 23 24
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}4
0 1 2 3
4 5 6 7
0 5 10 15
12 13 14 15
q)
q)#[((1 2 3);(4 5 6);(7 8 9));1;:;(2;5;8)]
1 2 3
2 5 8
7 8 9
Indexing in q can be straight forward and I believe a intermediate can be omitted:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m[1]:m[;1]
q)m
1 2 3
2 5 8
7 8 9
This question was asked in an interview, can someone tell what does the following code do? It gives output 15 for 150, 3 for 160, 15 for 15. What mathematical operation is it performing on 'n'.
int foo(int n)
{
int t,count=0;
t=n;
while(n)
{
count=count+1;
n=(n-1)&t;
}
return count;
}
It seems to calculate the number max(n**2-1, 0), where n is the number of 1 bits in a number's binary representation:
0 0 0b0
1 1 0b1
2 1 0b10
3 3 0b11
4 1 0b100
5 3 0b101
6 3 0b110
7 7 0b111
8 1 0b1000
9 3 0b1001
10 3 0b1010
11 7 0b1011
12 3 0b1100
13 7 0b1101
14 7 0b1110
15 15 0b1111
16 1 0b10000
17 3 0b10001
18 3 0b10010
19 7 0b10011
20 3 0b10100
21 7 0b10101
22 7 0b10110
23 15 0b10111
24 3 0b11000
25 7 0b11001
26 7 0b11010
27 15 0b11011
28 7 0b11100
29 15 0b11101
30 15 0b11110
31 31 0b11111
32 1 0b100000
33 3 0b100001
34 3 0b100010
35 7 0b100011
36 3 0b100100
37 7 0b100101
38 7 0b100110
39 15 0b100111
40 3 0b101000
41 7 0b101001
42 7 0b101010
43 15 0b101011
44 7 0b101100
45 15 0b101101
46 15 0b101110
47 31 0b101111
48 3 0b110000
49 7 0b110001
50 7 0b110010
51 15 0b110011
52 7 0b110100
53 15 0b110101
54 15 0b110110
55 31 0b110111
56 7 0b111000
57 15 0b111001
58 15 0b111010
59 31 0b111011
60 15 0b111100
61 31 0b111101
62 31 0b111110
63 63 0b111111
64 1 0b1000000
65 3 0b1000001
66 3 0b1000010
67 7 0b1000011
68 3 0b1000100
69 7 0b1000101
70 7 0b1000110
71 15 0b1000111
72 3 0b1001000
73 7 0b1001001
74 7 0b1001010
75 15 0b1001011
76 7 0b1001100
77 15 0b1001101
78 15 0b1001110
79 31 0b1001111
80 3 0b1010000
81 7 0b1010001
82 7 0b1010010
83 15 0b1010011
84 7 0b1010100
85 15 0b1010101
86 15 0b1010110
87 31 0b1010111
88 7 0b1011000
89 15 0b1011001
90 15 0b1011010
91 31 0b1011011
92 15 0b1011100
93 31 0b1011101
94 31 0b1011110
95 63 0b1011111
96 3 0b1100000
97 7 0b1100001
98 7 0b1100010
99 15 0b1100011
It is easier to find out the "mathematical operation", when function is changed to recursive:
int foo(int n, int t)
{
if( n )
return foo( (n-1) & t ) + 1
else
return 0;
}
So formula is:
F(0,t) = 0
F(n,t) = F( (n-1) & t, t ) + 1
foo(n) = F(n,n)
I don't have any idea, is that wellknown formula for counting something, or not.
You may find answer from math.stackexchange.com
That is a method known as Brian Kernighan's way to count set bits :
unsigned int v; // count the number of bits set in v
unsigned int c; // c accumulates the total bits set in v
for (c = 0; v; c++)
{
v &= v - 1; // clear the least significant bit set
}
Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only the high bit set, then it will only go once through the loop.
Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)"