How to print this table in combined form C++? [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I need to print this table
2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
9 9 9 9
I have written this code for the following result
#include <iostream>
using namespace std;
int main(){
const int N = 9;
for(int i = 0; i <= N; i += 3){
for (int j = 0; j <= i; j +=3) {
cout << i << " ";
}
cout << endl;
}
cout << "\n";
for(int i = 2; i <= N; i += 2){
for (int j = i; j > 0; j -= 2) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
My Result:
0
3 3
6 6 6
9 9 9 9
2
4 2
6 4 2
8 6 4 2
Required Result:
2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
9 9 9 9

#include <iostream>
using namespace std;
int main(){
const auto n = 4;
auto count = 0;
for (auto i = 2; i <= n * 2; i += 2)
{
for (auto j = i; j > 0; j -= 2)
std::cout << j << " ";
std::cout << std::endl;
for (auto j = 0; j < (i == 2 ? i : i + 2); j += 3)
std::cout << count * 3 << " ";
++count;
std::cout << std::endl;
}
return 0;
}
Edit: Corrected...
A little near to the answer
2
0 ==> true
4 2
0 3 ==> should be 3 3
6 4 2
0 3 6 ==> should be 6 6 6
8 6 4 2
0 3 6 ==> should be 9 9 9

Here is another approach for case when you're providing number of repeats (like in #Ruks answer):
void printSequence(unsigned int repeats)
{
int n = 2;
for(int i = 1; i < repeats; i++)
{
n+=2*i;
}
//n - number of all numbers in sequence for given number of repeats
int step = 0;
int numsPerRow = 1;
for(int i = 0; i < n; i+=step)
{
for(int j = numsPerRow; j > 0; j--)
{
std::cout << 2*j << " ";
}
std::cout << std::endl;
for(int j = 0; j < numsPerRow; j++)
{
std::cout << step+numsPerRow-1 << " ";
}
std::cout << std::endl;
step+=2;
numsPerRow++;
}
}

Use this:
void print_sequence(unsigned long long const repeat = 4, unsigned long long const i = 0)
{
for (auto j = (i + 1ull) * 2ull; j > 2ull; j -= 2ull)
std::cout << j << " ";
std::cout << (repeat > 0ull && repeat < -1ull ? "2\n" : "");
for (auto j = 0ull; j < i; j++)
std::cout << i * 3ull << " ";
std::cout << (repeat > 0ull && repeat < -1ull ? std::to_string(i * 3ull) + "\n" : "");
if (repeat < -1ull && i + 1ull < repeat)
print_sequence(repeat, i + 1ull);
}
Edit: The shortest and strongest method I can think of...
And call it like this:
print_sequence();
If you don't want 4 times:
print_sequence(10)
It will repeat it as many times you want...
Kind regards,
Ruks.

Related

How to create array by user input?

I have a C++ problem:
Input an sequence of digit [ 0 - 9 ] and terminated by three 9 consecutivly, print on standard output the number of subsequences consisting of three consecutive equal digits on standard output.
Example: Given the sequence { 1 2 2 2 2 0 0 3 3 3 7 9 9 9 }, the subsequence are identified:
{ 2 2 2}, { 2 2 2 }, { 3 3 3 } .
Therefore, the program should print on standard output the number 3, equal to sequences present.
I try to use an array. My code ended up like this:
int main(){
int i;
int N = 0, A[100];
while( (A[i] && A[i+1] && A[i+2]) != 9 ){
N++;
for( i = 0; i <= N; i++ ){
cout << "A[" << i + 1 << "]:";
cin >> A[i];
}
for(int i = 0; i <= N; i++ ){
cout << "A[" << i + 1 << "]:" << A[i];
}
}
}
My problem is, I have no idea how to terminate the sequence by three 9's consecutively. So I try to use an array. I hope someone can help me to elaborate the idea.
You can do that by breaking the loop when three consecutive 9 is found.
#include <iostream>
const int ARRAY_SIZE = 100;
int main(){
int i;
int N = ARRAY_SIZE, A[ARRAY_SIZE];
for( i = 0; i < ARRAY_SIZE; i++ ){
std::cout << "A[" << i + 1 << "]:";
std::cin >> A[i];
// stop when three consecutive 9 is found
if (i >= 2 && A[i - 2] == 9 && A[i - 1] == 9 && A[i] == 9){
N = i + 1;
break;
}
}
for(int i = 0; i < N; i++ ){
std::cout << "A[" << i + 1 << "]:" << A[i] << '\n';
}
return 0;
}
Instead of an array with the number of elements equal to the magic number 100 what you need is an array of exactly three elements.
Here is a demonstrative program.
#include <iostream>
int main()
{
const size_t N = 3;
int a[N];
size_t count = 0;
for ( size_t i = 0, j = 0; std::cin >> a[j++]; )
{
j %= N;
if ( i != N - 1 )
{
++i;
}
else
{
size_t k = 1;
while ( k < N && a[k] == a[k-1] ) k++;
if ( k == N )
{
if ( a[0] == 9 ) break;
else ++count;
}
}
}
std::cout << "count = " << count << '\n';
return 0;
}
If to enter the sequence of numbers
1 2 2 2 2 0 0 3 3 3 7 9 9 9
then the program output will be
count = 3
instead of the inner while loop you could use for example the algorithm std::all_of.

making a circle around a two dimensional array

I am having trouble with making a turn inside a two dimensional array to output the elements in spiral. I tried this code, but it is outputting not enough elements, I tried to make some if statements outside of the loop to cover all cases for which the general algorithm doesn't output. Can you help suggesting some way to manage the correct output.
CODE
#include <iostream>
#include <algorithm>
//#include <cmath>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c) / 2; runs--;) {
for (int i = c_beg; i < c_end; i++)
cout << m[r_beg][i] << " ";
for (int i = r_beg; i < r_end; i++)
cout << m[i][c_end] << " ";
for (int i = c_end; i > c_beg; i--)
cout << m[r_end][i] << " ";
for (int i = r_end; i > r_beg; i--)
cout << m[i][c_beg] << " ";
r_beg++;
c_beg++;
r_end--;
c_end--;
}
if (r <= c && c_beg <= c_end) {
for (int i = c_beg; i <= c_end; i++)
cout << m[r_end][i] << " ";
}
else if (r >= c && r_beg <= r_end) {
for (int i = r_beg; i <= r_end; i++)
cout << m[i][c_end] << " ";
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Example:
Input:
3 3
1 2 3
4 5 6
7 8 9
Output: 1 2 3 6 9 8 7 4 5
If you have for example 3x10 matrix. It doesn't output.
Input:
3
7
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
Output: should get to 13, but it stops at 8.
The code, as presented, loops towards center until the smaller of the two dimensions is consumed up. However, if that smaller dimension has odd size, then parts of the central row or column respectively haven't been printed out. You can cover that one with some special case handling after your outer loop:
for (int runs = std::min(r, c) / 2; runs--;)
{
// ...
}
if(c < r)
{
if(c & 1)
{
for (int i = r_beg; i <= r_end; i++)
// ^ (!)
// don't forget to print last element: there's no second loop
// that would print the corner element a second time now!
std::cout << m[i][c_end] << " ";
}
}
else
{
// handles the square matrix case as well
if(r & 1)
{
for (int i = c_beg; i <= c_end; i++)
std::cout << m[r_beg][i] << " ";
}
}
This can be solved by carefully fine-tuning the bail-out conditions / ranges of the for loops:
#include <iostream>
using namespace std; // :-(
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c);;)
{
for (int i = c_beg; i <= c_end; i++)
cout << " " << m[r_beg][i];
++r_beg;
for (int i = r_beg; i <= r_end; i++)
cout << " " << m[i][c_end];
--c_end;
if (!--runs) break;
for (int i = c_end; i >= c_beg; i--)
cout << " " << m[r_end][i];
--r_end;
for (int i = r_end; i >= r_beg; i--)
cout << " " << m[i][c_beg];
++c_beg;
if (!--runs) break;
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Input:
3 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Output:
1 2 3 4 5 6 7 14 21 20 19 18 17 16 15 8 9 10 11 12 13
Live Demo on ideone
Input:
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Notes:
I changed the bail-out of the for loop:
Instead of min(r, c) / 2, I use min(r, c) and decrement/check runs twice in the body.
I adjusted the update of r_beg, r_end, c_beg, and c_end.

Sorting an array— issue

I'm asking for help with a problem implying an array sorting in the following manner: all even numbers must be in front of the odd ones. I've partially made the problem, but I did the sorting in the opposite manner and I can not manage to fix it.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int v[100], n, i, aux = 0, inv;
cout << "Number of elements: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "v[" << i << "]=";
cin >> v[i];
}
do
{
inv = 0;
for (i = 0; i < n; i++)
{
if (v[i] % 2 == 1 && v[i + 1] % 2 == 0)
{
inv = 1;
aux = v[i];
v[i] = v[i + 1];
v[i + 1] = aux;
}
}
} while (inv != 0);
cout << endl;
for (i = 0; i < n; i++)
cout << v[i] << " ";
cout << endl;
system("pause");
return 0;
}
The output for this would be:
n = 8
1 3 2 4 7 8 4 2
Result: 2 4 8 4 2 -858993460 1 3
In the expression v[i + 1], you access v[n] when i = n - 1, this will result in an out-of-bounds memory access which results in undefined behaviour.
You should change the for loop to this:
for (i = 0; i < n - 1; i++)
The output for the given input is:
a.exe
Number of elements: 8
v[0]=1
v[1]=3
v[2]=2
v[3]=4
v[4]=7
v[5]=8
v[6]=4
v[7]=2
2 4 8 4 2 1 3 7

How to write this table in a for loop?

Desired output:
2
4 2
6 4 2
8 6 4 2
Here is my code:
for(int i = 0; i <= 7; i += 2) {
for (int j = 0; j <= i; j += 2)
cout << j + 2 << " ";
cout << endl;
}
and I get a wrong answer.
Your approach with the double for loop was a good idea. Here is my generalized attempt, which allow to print what you want in n lines, rather than just 4:
#include <iostream>
using namespace std;
int main() {
int n = 4;
for(int i = 0; i < n; ++i) {
for(int j = i; j >= 0; --j) {
cout << 2 * (j + 1) << " ";
}
cout << "\n";
}
return 0;
}
Output:
2
4 2
6 4 2
8 6 4 2
Live Demo
PS: This approach minimizes (if not exterminates) the usage of magic numbers.
Welcome to Stack overflow.
You just went the wong way around with your inner for loop
#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i <= 7; i += 2){
for (int j = i; j >= 0; j -= 2) {
cout << j + 2 << " ";
}
cout << endl;
}
return 0;
}
this gives you the desired output:
2
4 2
6 4 2
8 6 4 2

For loop with multiple variable

Is it possible for a for loop with 2 variables to stop incrementing only one of the variable when a condition is met ? For example
for(int i = 0, j = 0; i < 5 && j < 10; i++, j++)
{
cout << i << " " << j << endl;
}
and the output would look something like
0 0
1 1
2 2
3 3
4 4
4 5
4 6
4 7
4 8
4 9
This is my actual code. I wanted the condition for both variables
cout << sp.dets.size() << " " << gt.groundtruth.size() << endl;
for (int i = 0, j = 0; i < sp.dets.size() && j < gt.groundtruth.size(); j < gt.groundtruth.size() ? j++ : j, i < sp.dets.size() ? i++ : i)
{
cout << i << " " << j << endl;
}
sp.dets.size = 0
gt.groundtruth.size() = 8
It would be nice if the solution works for any number i.e. i > j or i < j or i = 0 or j = 0
You can use ternary statement to increment variable i value. Like below:
for(int i = 0, j = 0; i < 5 && j < 10; j++, i<4? i++: i)
{
cout << i << " " << j << endl;
}
This will output the expected result.
Something like this may work,
int k = 0;
int val = 4;
for(int i = 0, j = 0; i < 10 && j < 10; i++, j++){
if (i>=val){
k=val;
cout << k << " "<< j << endl;
}
else
cout << i << " " << j << endl;
}
Prints this,
0 0
1 1
2 2
3 3
4 4
4 5
4 6
4 7
4 8
4 9
Don't try to be overly clever by trying to fit all the logic in one line. It't more important that your code can be read by others (and yourself in two weeks' time) than saving a single line. If the incrementing logic of the two variables is more complex than just increasing by one for each loop, put them in seperate lines, like this:
while (i<5 && j<10) {
...
// complicated expression calculating new i
// complicated expression calculating new j
}
In your case, you might also flip the logic around:
for (int i=0; i<max(sp.dets.size(), gt.groundtruth.size(); i++) {
cout << min(i, sp.dets.size()-1)
<< ","
<< min(i, gt.groundtruth.size()-1)
<< endl;
}
You can use ternary operator on both variables. Just be sure to change your condition to break only when both reach their destined value
for (int i = 0, j = 0;
i != 5 || j != 10;)
{
i += i < 5 ? 1 : 0;
j += j < 10 ? 1 : 0;
}
You'll note I moved the increment into the loop body, this improves readability, in my opinion.