How to write this table in a for loop? - c++

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

Related

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 print this table in combined form C++? [closed]

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.

How to calculate sum of each row in 2D array and place in vector?

I am trying to calculate the sum of each row in the array and place it in a vector,
You can find my attempt below,
For that, it prints the same values for the first 4 and a different for the last 1, 215 215 215 215 316
What I want is for example
x1 2 4 4 6 7 Sumx1=??
x2 1 2 3 4 5 etc
x3 1 2 3 4 5
x4 1 2 4 5 6
and place the value Sumx1 in a vector.
Here's my attempt
#include <time.h>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string name;
srand(time(NULL));
int pay[5][4];
vector<string> names;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
pay[i][j] = rand() % 51 + 50;
cout << pay[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
vector<int> totals;
for (int i = 0; i < 5; i++) {
for (int c = 0; c < 4; c++) {
totals.push_back((pay[i][0] + pay[i][1] + pay[i][2] + pay[i][3]));
}
cout << totals[i] << " ";
}
return 0;
}
for(int i=0; i<5; i++){
for(int c=0; c<4; c++){
totals.push_back((pay[i][0]+pay[i][1]+pay[i][2]+pay[i][3]));
}
cout<<totals[i]<<" ";
}
Try removing the inner loop (the one with variable c). Should work.
You add the sum pay[i][0]+pay[i][1]+pay[i][2]+pay[i][3] to the vector four times due to your inner loop c.
What you really want is the following,
for(int i=0; i<5; i++){
int sum = 0;
for(int c=0; c<4; c++)
sum += pay[i][c];
totals.push_back(sum);
cout<<totals[i]<<" ";
}

How do you make a rectangle and triangle shape in C++

How do i achieve this format when i keyed in int of ' 245 ':
(where if it is odd number it will be a rectangle, and even number will be triangle)
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
This is my code so far:
(I can't seemed to output triangle and rectangle at the same time)
int n;
int lastDigit;
do
{
cout << "Enter a positive integer: ";
cin >> n;
}while ( n <= 1 || n == '0');
cout << endl;
// If even digit - tri
do
{
lastDigit = n%10;
if (lastDigit / 2 ==0)
{
for (int i = 1; i <= lastDigit; ++i)
for (int tri = 1; tri <= i; ++tri)
cout << "\t" << tri;
cout << endl;
}
// if odd digit - rect
else if (lastDigit / 2 != 0)
{
for (int i = 1; i <= lastDigit; i++)
{
for (int rect = 1; rect <= i; rect++)
cout << "\t" << rect;
cout << endl;
}
n = n/10;
}
cout << endl;
}while (lastDigit != 0);
n = n/10;
cout << endl;
return 0;
And, how should i code when keyed in the int, the compiler will extract the first digit (From left to right) and output it accordingly?
Any help would be appreciated!
Following is the complete code.
Step1: Take user input and check whether it's odd or even.
Step2: If it is odd then perform triangle else rectangle.
#include<iostream>
int main () {
int n;
cout<<"Enter number: ";
cin>>n;
if (n % 2 != 0)
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= n; j++)
{
cout<<j<<" ";
}
}
}
else
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= i; j++)
{
cout<<j<<" ";
}
}
}
return 0;
}
Both of your print is similar. I suggest:
#include<iostream>
using namespace std;
int main () {
int n;
cout << "Enter number: ";
cin >> n;
int i = 1;
int& rowLim = ((n % 2) ? n : i);
for(i = 1; i <= n; i++)
{
cout << endl;
for(int j = 1; j <= rowLim; j++)
{
cout<<j<<" ";
}
}
return 0;
}
Simplest would be to use a string, iterate char by char and output accordingly. For instance
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
for (char c : s) {
int n = c - '0';
bool k = n % 2;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= (k ? n : i); ++j)
cout << " " << j;
cout << endl;
}
cout << endl;
}
return 0;
}
Output
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
See a DEMO