Number pattern in C++ - c++

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

Related

VLOOKUL OR OFFSET

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

Convert space separated value text to .csv with c++ then save it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How to convert .asc file (space separated value) into .csv file with c++ then save it as .csv file on pc or android.
Note: this .asc file is for srtm data, so it is large, is 3600 rows and 3600 columns.
Please without software like excel or such.
I searched in google but no result
Edit
See the figure to see the example 1
the .asc file:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 3 3 3 4 4 5 5 4 5 5 6 6 6 6 5 5 4 3 3 2 3 5 6 7 7 8 9 10 10 10 11 12 11 10 10 11 13 14 14 14 13 13 12 12 11 11 11 11 11 10 10 10 10 10 10 11 11 13 14 13 13 13 14 14 14 14 14 15 15 14 14 14 15 15 14 14 16 17 18 20 23 27 31 32 35 38 41 43 44 46 47 48 50 50 49 49 50 51 51 54 55 56 58 59 59 61 62 62 62 61 58 54 53 52 52 51 51 53 54 53 51 50 50 49 49 50 51 52 53 53 52 51 51 52 57 62 61 59 57 57 57 60 60 60 59 58 58 58 58 57 57 57 61 63 64 62 -32768
then new line
and other number follow the same patttern number of data spaced by a space
Note
It look likes as in figure
I know just so:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
  ofstream file;
  file.open ("example.txt");
  file << "Please write this data to a file using C++\n";
  file.close();
  return 0;
}
but not reading then converting
I have created a small C++ program which will solved your requirement.
int main()
{
std::ifstream ascfile;
std::ofstream csvFile;
ascfile.open("source.asc");
csvFile.open("output.csv");
if (ascfile.is_open())
{
std::string line;
while(std::getline(ascfile, line))
{
int p = 0;
while ((p = line.find(' ', p)) != std::string::npos)
{
line.insert(p, ",");
p += 2;
}
csvFile << line <<std::endl;
}
}
else
{
std::cout << "Sorry, the file could not be openend." << std::endl;
return -1;
}
ascfile.close();
csvFile.close();
return 0;
}
add header file iostream, fstream, string
Hi I modified the above program below as above program do not replace the space use the below program instead but must add header file :-
int main()
{
std::ifstream ascfile;
std::ofstream csvFile;
ascfile.open("source.asc");
csvFile.open("output.csv");
if (ascfile.is_open())
{
std::string line;
while(std::getline(ascfile, line))
{
std::replace( line.begin(), line.end(), ' ', ',');
csvFile << line <<std::endl;
}
}
else
{
std::cout << "Sorry, the file could not be openend." << std::endl;
return -1;
}
ascfile.close();
csvFile.close();
return 0;
}

SSE - mismatch between _mm_extract_ps and direct access

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.

Printing a table using nested while loops only

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.

functioning of bitwise and

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.)"