I need to code this in c++ using nested for loops. click thisAnd this is what I have right now.
#include <iostream>
#include <map>
using namespace std;
int main() {
int i, j, k, n = 9;
for(i = n; i >= 0; i--){
for(j = i; j >= 0; j--)
cout << j<<" ";
cout <<endl;
}
}
how do I flip it.
Currently you are starting with the longest row and going down. Start with the shortest row and go up:
#include <iostream>
int main() {
int n = 9;
for(int i = 0; i <= n; i++){ // start with 0 and go up to n
for(int j = i; j >= 0; j--)
std::cout << j << ' ';
std::cout << '\n';
}
}
To flip in the other direction fill it with spaces:
#include <iostream>
int main() {
int n = 9;
for(int i = n; i >= 0; i--){
for(int j = 2*(n-i); j >= 0; j--) // fill with spaces
std::cout << ' ';
for(int j = 0; j <= i; j++) // start with 0 and go up to i
std::cout << j << ' ';
std::cout << '\n';
}
}
#include <iostream>
using namespace std;
int main()
{
for (int i = 9; i >= 0; i--) { // i is 9 and its decrementing over time
for (int j = 9; j > i; j--) { // j is also 9 and it must be greater than i, otherwise you will have unnecessary one blank space in first row
cout << " "; // This is my way of doing things, i would rather print double blank space
}
for (int k = 0; k <= i; k++) { // k is just incrementing from 0 to i and it has blank space
cout << k << " ";
}
cout << endl;
}
}
Related
How come no matter what I do for rows and columns my columns won't go above two? It should be 8x8 adding the 8 numbers together 8 times. I don't know what I'm doing wrong. Thank you
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main() {
srand(time(0));
int array1[8][8];
int array2[8][8];
int addition[8][8];
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++)
array1[i][j] = rand() % 6;
}
for (int i = 0;i < 7; i++) {
for (int j = 0; j < 7; j++) {
array2[i][j] = rand() % 8;
}
}
{
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
addition[i][j] = array1[i][j] + array2[i][j];
}
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
cout << array1[i][j];
cout << " " << array2[i][j];
cout << " " << endl;
cout << "both previous numbers added together = " << addition[i][j] << endl;
}
}
return 0;
}
}
}
Hi look carefully at the code structure.
you had some extra brackets.
This is the correct structure:
#include <iostream>
//using namespace std; - use this only in simple projects (my opinion).
int main()
{
srand(time(0));
int array1[8][8];
int array2 [8][8];
int addition[8][8];
for (int i = 0;i < 8;i++)
{
for (int j = 0; j< 8;j++)
array1[i][j] = rand() % 6;
}
for (int i = 0;i < 8;i++)
{
for (int j = 0;j < 8;j++)
array2[i][j] = rand() % 8;
}
//{ - you dont need this here
for (int i = 0;i < 8;i++)
{
for (int j = 0;j < 8;j++)
addition[i][j] = array1[i][j] + array2[i][j];
}
for (int i = 0;i < 8;i++)
{
for (int j = 0;j < 8;j++)
{
std::cout << array1[i][j];
std::cout << " " << array2[i][j];
std::cout << " " << std::endl;
std::cout << "both previous numbers added together = " << addition[i][j] << std::endl;
}
}
//} - and you don't need this here
return 0;
}
Take this example and compare to your code to see your mistake. Code just wasn't structured properly.
Your code's logics are absolutely right! However, the mistake was found in improper bracket structuring on for loop. I have corrected your code and mentioned the mistakes as comments.
#include <iomanip>
#include <cstdlib>
#include <iostream> //include this header to use "cout"
using namespace std;
int main() {
srand(time(0));
int array1[8][8];
int array2[8][8];
int addition[8][8];
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++)
array1[i][j] = rand() % 6;
}
for (int i = 0;i < 7; i++) {
for (int j = 0; j < 7; j++) {
array2[i][j] = rand() % 8;
}
}
{
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
addition[i][j] = array1[i][j] + array2[i][j];
}
} //add this bracket
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
cout << array1[i][j];
cout << " " << array2[i][j];
cout << " " << endl;
cout << "both previous numbers added together = " << addition[i][j] << endl;
//} remove this bracket
}
return 0;
}
}
}
Also, to add on, if you want an 8x8 matrix use i<8 and j<8 everywhere in the code. Here you have used i<7 and j<7 which means you get a 7x7 matrix as result.
Logic:
i=0 to i<7 use have => 0,1,2,3,4,5,6 (stopping at 6 because 6<7 is true and 7<7 becomes false naturally). So from 0 to 6 there are totally 7 elements.
Hope this helps! :)
so i want output like this
1
123
12345
123
1
i already make the program but it only output these, and im confused how to output the bottom triangle
1
123
12345
here's my program
#include <iostream>
using namespace std;
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
#Mojtaba's answer is a perffect extension to your approach.
However, I wanted to provide another method that is generally used in creating such strings that are formatted in a particular manner. It is common to create the entire pattern line by line and then print to the console all at once.
I have appropriately commented the code for your reference and it should be easy to understand:
#include <iostream>
#include <vector>
void pattern(int n) {
std::vector<std::string> lines; // store the first n lines to print later
int length = 2*n - 1; // length of each line
for(int i = 0; i < n; i++) {
std::string str = std::string(length, ' ');
for(int j = 1; j <= 2*i + 1; j++) {
str[n - i + j - 2] = j + '0';
// indexing can be figured by observing the pattern
}
lines.emplace_back(str);
}
for(int i = 0; i < n; i++) {
std::cout << lines[i] << std::endl;
}
for(int i = n-2; i >= 0; i--) {
std::cout << lines[i] << std::endl;
}
return;
}
int main() {
int n;
std::cin >> n;
pattern(n);
}
I added another for loop exactly like yours with different order from n-1. I modified your code to this:
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
for (i = n - 1; i >= 1; i--) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
Now it returns:
1
123
12345
123
1
#include <iostream>
#include<fstream>
using namespace std;
int main() {
int a = 1;
if (a == 1) {
int a[1][1];
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= 1; j++) {
cin >> a[i][j];
}
}
cout << endl;
for (int k = 0; k <= 1; k++) {
for (int l = 0; l <= 1; l++) {
cout << a[k][l] << " ";
}
cout << endl;
}
}
return 0;
}
In this program if we enter input as :
1
2
3
4
it gives output :
1 3
3 1
it should give output as:
1 2
3 4
Please help, I am a beginner.
I am coding in code blocks.
Try this
int main()
{
int arr[2][2]; // declares a 2x2 array
cout << "Enter integers : " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cin >> arr[i][j]; //inserts at the index i and j in the array
}
}
cout << "Display array : " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << arr[i][j] << endl; /*displays the value at
index i and j in the array*/
}
}
}
Trying to make the following pattern and have tried to make several changes; however, I'm unable to replicate the exact same pattern can someone help? I've also attached the picture of the pattern!
#include <iostream>
using namespace std;
void triangle(int n)
{
int k = 2 * n - 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++)
cout << " ";
k = k - 1;
for (int j = 0; j <= i; j++) {
cout << " "<<i;
}
cout << endl;
}
}
int main()
{
int n = 5;
triangle(n);
return 0;
}
There are minor issues in your code
#include <iostream>
using namespace std;
void triangle(int n)
{
int k = 2 * n - 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++)
cout << " ";
k = k - 1;
for (int j = 1; j <= i; j++) { //you started loop from j=0
cout << " "<<j; //you need to print 'j' and not 'i'
}
cout<<" 1"<<endl; // for the trailing '1' in every line
}
}
int main()
{
int n = 5;
triangle(n);
return 0;
}
I have commented the changes in the code.
There are few errors in your logic. In the 2nd nested for loop the loop should start as-
for(j=1; j<=i; j++) //you used j=0 here
cout<<" "<<j; //you used i here
and coming out of the loop you need to print an extra one here.
so after this loop use
cout<<" 1"<<endl // it is for the extra 1 at the end of each line;
Hi I'm trying to get my C++ console to output the first image I've attached. I know you have to like count the spaces in like maybe a for loop and decrement it as you go down the rows..However, I'm not exactly sure on how to go about that, pretty new to c++. The solution I'm currently working on is the exact mirror image of it. Any help would be greatly appreciated.
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
cout << "" << endl;
for (int j = 1; j <= i; ++j) {
cout << "*";
}
}
}
Correct Solution:
My Current Output:
You need an extra for loop before the one that is sending the asterisks, like so that will send the spaces to push the asterisks to the right.
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
for (int j = rows - i; j > 0; j--) {
cout << " ";
}
for (int j = 1; j <= i; ++j) {
cout << "*";
}
cout << endl;
}
}
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
cout << "" << endl; // Mistake here, you are putting a empty string
for (int j = 1; j <= i; ++j) {
cout << "*";
}
}
}
You're almost good. You have missed to put the spaces in order to create the right padding of the stars.