int n, o = 2;
cout << "n="; cin >> n;
for(int i = 1; i <= n; i++) {
for(int j = n; j >= i; j--) {
cout << o << " ";
o += 2;
}
cout << endl;
}
My code outputs (when n is 4)
2 4 6 8
10 12 14
16 18
20
How would I align it to output this?
2 4 6 8
10 12 14
16 18
20
I am trying to align the output to the right in such a way that the numbers are aligned one below another (for example the 4 is aligned to the space between the 10 and 12 and I want to align it to 10). Thanks.
The header <iomanip> provides many input/output manipulators you can play with, one beeing std::setw, which sets the width parameter of the stream for the next item.
Changing the line in the posted snippet that outputs the numbers into
std::cout << std::setw(3) << o;
The output becames
2 4 6 8
10 12 14
16 18
20
Now, to produce the desired output, we can notice that the first number of each row can either be printed with an increasing "width" (3 the first row, 6 the second and so on) or after a loop that prints the right amount of spaces (0 the first row, 3 the second and so on).
just edited the inner loop of your code and this is the edited code:
#include <iostream>
using namespace std;
int main() {
int n, o = 2;
cout << "n="; cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j >= i) {
cout << o << "\t";
o += 2;
}
else
{
cout << "\t";
}
}
cout << endl;
}
}
and this is some example output:
n=4
2 4 6 8
10 12 14
16 18
20
what I did is to make the inner loop, loops from 1 till n like the outer loop and only check if the current column is greater than or equal to the current row, if so, then I print the number and increment it, if not then I just cout a space or a tab to be more specific
Related
The task:
Write a C++ program which will print (half pyramid) pattern of natural numbers.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
I have tried using this code, but its not giving the output:
#include <iostream>
using namespace std;
int main()
{
int rows, i, j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
OUTPUT:
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
j in your inner loop is the 1 based index of the element in the current row (1,2,3 etc.).
Instead of printing it, you should print a counter that is increased over all the iterations.
Something like:
#include <iostream>
int main()
{
int rows, i, j;
std::cout << "Enter number of rows: ";
std::cin >> rows;
int n = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
std::cout << n << " ";
n++; // for next iteration
}
std::cout << "\n";
}
return 0;
}
Output example:
Enter number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
I don't want to reverse or shift the array. What I want is to write the array from right to left.
I did something like
int arr[5] = {0};
for (int i = 0; i < 5; i++)
{
cout << "enter a number : ";
cin >> arr[i];
for (int j = 0; j < 5; j++)
{
if (arr[j] != 0)
{
cout << arr[j] << " ";
}
else
cout << "X ";
}
cout << endl;
}
and on the output screen I see this
enter a number : 5
5 X X X X
enter a number : 4
5 4 X X X
enter a number : 3
5 4 3 X X
enter a number : 2
5 4 3 2 X
enter a number : 1
5 4 3 2 1
Press any key to continue . . .
but i want to see this
enter a number : 5
X X X X 5
enter a number : 4
X X X 5 4
enter a number : 3
X X 5 4 3
enter a number : 2
X 5 4 3 2
enter a number : 1
5 4 3 2 1
Press any key to continue . . .
how can I do that?
I will be glad if you help.
Just change the inner for loop for example the following way
int j = 5;
for ( ; j != 0 && arr[j-1] == 0; --j )
{
std::cout << 'X' << ' ';
}
for ( int k = 0; k != j; k++ )
{
std::cout << arr[k] << ' ';
}
I think you are trying to write into the last index each time, and then move each number backwards in the array?
To write into the last index use cin >> arr[4].
Then After printing out the array copy each value into the previous index. Make sure that your copy only when the J index is less than 4
int arr[5] = {0};
for (int i = 0; i < 5; i++)
{
cout << "enter a number : ";
//write into the last position
cin >> arr[4];
for (int j = 0; j < 5; j++)
{
if (arr[j] != 0)
{
cout << arr[j] << " ";
}
else
cout << "X ";
//don't go out of bounds. This should move each number to the previous index
if(j<4){
arr[j]=arr[j+1];
}
}
cout << endl;
}
Write a program to display the below pattern with n rows, where n is
in the range between 1 and 100. The variable n should be entered by
the user. If the user input is between 1 and 100 then output the
pyramid as given below, otherwise prompt the user to enter n again.
Here is the sample output: Enter the number of rows: 6
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
6 7 8 9 10 11
(enter image description here)
this is my code it shows a close answer but not correct.
int num=1 , counter=1;
cout << "Enter the number of rows: " ;
cin>>num;
for(int i=0;i<=num;i++)
{
for(int j=0;j<=i;j++)
{
cout<<counter<<" ";
counter++;
}
cout<<endl;
}
int num = 1, counter = 1, temp = 1;
cout << "Enter the number of rows: ";
cin >> num;
for (int i = 0; i < num; i++)
{
for (int j = 0; j <= i; j++)
{
cout << temp << " ";
temp++;
}
counter++;
temp = counter;
cout << endl;
}
The variable temp serves as a counter for the rows, meanwhile the variable counter is responsible for the starter numbers in the first column.
you should either set counter in each run of outer loop, right before entering the inner one with something like counter = i+1, the way you wrote this code value of this variable carries over to the next iteration and keeps going only upwards.
Alternate solution would be to print j instead and work on inner loop, it should then start with i and the first number that wouldn't qualify would be 2*i this way for i equal 2 the sequence would be 2 3,
either way you should also rework your outer loop, as right now it starts with 0 and ends with num meaning it goes through num+1 iterations
int rows, i, j = 0, number = 0, counter = 0;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++)
{
while (j != 1 * i)
{
if (counter <= rows)
{
cout << i+j << " ";
counter++;
}
++j;
}
number=counter=j=0;
cout << endl;
}
I am having some troubles with this problem presented from my lab. My goal is to produce an addition table that looks something like this -
(From range(1-5)) :
+ 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
Mine is looking like this, however :
+ 1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
My code looks like this :
if (choice == ADD) {
cout << "+";
for (int i = 0; i < max; i++) {
cout << "\t";
for (int j = min; j <= max; j++) {
cout << i + j << "\t";
}
}
}
(For reference, int max = maximum number in range, int min = minimum number in range, and choice is the decision for user to do either an addition or multiplication table). How can I change my code to fit the proper format? I can't seem to figure it out. Any hints/help would be greatly appreciated :)
#include <iostream>
using namespace std;
int main(){
int max = 5;
int min = 1;
if (true){
cout << "+\t";//print out the initial +
for(int i = min; i <= max; i++) cout << i << "\t";//print out the entire first row
cout << "\n"; //start the next row
//here is the main loop where you do most of the logic
for(int i = min; i <= max; i++){
cout << i << "\t"; //this prints out the first column of numbers
for(int j = min; j <=max; j++){
cout << j+i << "\t"; //this line fills in the body of your table
}
cout << "\n";//creates the space between each row
}
}
}
This code builds the table as explained:
for (int i = 0; i <= max; i++) {
if (i == 0)
cout << '+';
else
cout << i;
cout << '\t';
for (int j = min; j <= max; j++) {
cout << i + j << '\t';
}
cout << '\n';
}
Tip: when you want to print only a character, it is more efficient to use single quotes like '+' or '\t'. Double quotes are more expensive because they represent a const char*.
I was testing vector to vector initialization, I used range based for loop it gives different output.
with respect to Normal for loop.
vector<int> intVector;
for(int i = 0; i < 10; i++) {
intVector.push_back(i + 1);
cout << intVector[i] << endl;
}
cout << "anotherVector" << endl;
vector<int> anotherVector(intVector);
for(auto i : anotherVector)
cout << anotherVector[i] << endl;
//for(unsigned int i = 0; i < anotherVector.size(); i++) {
// cout << anotherVector[i] << endl;
//}
this range based for loop gives the output - Linux ubuntu
Output
STLTest Cunstructor Called.
1
2
3
4
5
6
7
8
9
10
anotherVector
2
3
4
5
6
7
8
9
10
81
2.
vector<int> intVector;
for(int i = 0; i < 10; i++) {
intVector.push_back(i + 1);
cout << intVector[i] << endl;
}
cout << "anotherVector" << endl;
vector<int> anotherVector(intVector);
//for(auto i : anotherVector)
// cout << anotherVector[i] << endl;
for(unsigned int i = 0; i < anotherVector.size(); i++) {
cout << anotherVector[i] << endl;
}
This gives different output.
output
STLTest Cunstructor Called.
1
2
3
4
5
6
7
8
9
10
anotherVector
1
2
3
4
5
6
7
8
9
10
why both for loop acting differently?
for(auto i : anotherVector)
cout << anotherVector[i] << endl;
This code doesn't do what you think it does. The range-based for loop iterates over values, not over indices. In other words, the loop variable (i) is assigned all ements of the vector in turn.
To replicate the functionality of your first loop, you need this:
for (auto i : anotherVector)
cout << i << endl;
What your original code was doing was take an element of the vector, and use it to index into the vector again. That's why the numbers were off by one (since the vector held number n + 1 at position n). Then, the final output (81 in your case) was effectively random and the result of Undefined Behaviour—you were reaching past the end of the vector.