Create a shape in C++ - c++

I want to create a shape like this:
ccccccc
cccccc
ccccc
cccc
ccc
cc
c
My code is:
#include <iostream>
using namespace std;
int main(){
int i, j;
for(i = 0; i < 7; i++){
for(j = 7; j > 7; j--){
cout << 'c';
}
cout << endl;
}
return 0;
}
But in terminal the output I get is some blank lines.
What am I doing wrong?

for(j = 7; j > 7; j--){ This expression is always false.
You need to write for(j = 7; j > i; j--){

You want this:
#include <iostream>
using namespace std;
int main(){
int i, j;
for(i = 7; i > 0; --i){
for(j = i; j > 0 ; j--){
cout << 'c';
}
cout << endl;
}
return 0;
}
live example
Your original code had a logic error in the inner loop
for(j = 7; j > 7; j--){
here j is 7 but j will never be greater than 7 so it never executes, but even if this was fixed to
for(j = 7; j > 0; j--){
This will just cout 7 'c' 7 times, so what I modified was to change your inner loops starting value so that it then decrements correctly.
for(i = 7; i > 0; --i){
for(j = i; j > 0 ; j--){
^ now initialised by outer loop
So what would happen is that the inner loop never executed but you executed cout << endl; 7 times hence the blank lines

The condition of the loop
for(j = 7; j > 7; j--){
is wrong. That is it always is equal to false because initially i is set to 7 and it can not be greater than 7.:)
I think you mean something like
for(j = 7 - i; j > 0; j--){
The program can be written simpler.
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0-exit): ";
size_t n = 0;
std::cin >> n;
if ( !n ) break;
const char c = 'c';
std::cout << std::setfill( c );
while ( n ) std::cout << std::setw( n-- ) << c << std::endl;
}
return 0;
}
The program output is
Enter a non-negative number (0-exit): 7
ccccccc
cccccc
ccccc
cccc
ccc
cc
c
Enter a non-negative number (0-exit): 0

Related

How do i flip a number half triangle in c++?

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;
}
}

making a triangle diagram however iam unable to make make the final pattern

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;

Fill an array diagonally

I have following program. with Input 3 5
3 rows
5 growth of numbers
The output should be:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
But my program gives:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Here is what I have tried so far
int main() {
int n, m, c = 0;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
}
What I am doing wrong or missing?
About the spaces: Can't find reason for such behavior(first spaces are ignored), displayed on screenshot. Tried to run in different IDE's with different compilers and had such problem only in testing system.
Hi try to use tab instead.
#include <iostream>
using namespace std;
int main() {
int n, m, c = 0;
cin >> n >> m;
int *a = new int[n * m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i * n + j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << "\t" << a[i * n + j];
cout << endl;
}
delete[] a;
return 0;
}
Can't remember how I solved this problem in secondary school, but with n less than m, the following code works:
#include <iostream>
using namespace std;
void nextij(long n,long m,long& i,long& j) {
if (i==n-1) { //bottom row
if (j<n-1) { //in the left square
j = i+j+1;
i = 0;
}
else { //out of the left square
i = j-(n-1)+1;
j = m-1;
}
}
else { //other rows
if (j==0) { //left most column
j = i+1;
i = 0;
}
else { //other columns
i++;
j--;
}
}
}
int main() {
long n = 3;
long m = 5;
long a[3][5];
long i = 0;
long j = 0;
long c = 1;
while (c<=n*m) {
a[i][j] = c;
nextij(n,m,i,j);
c++;
}
for (i=0; i<n; i++) {
for (j=0; j<m; j++)
cout <<a[i][j] <<" ";
cout <<endl;
}
}
/*
output:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
*/

New Edit: Continue to receive msg "Segmentation Fault: 11" when I try to run the program, any ideas?

Really unsure what to do, all it is is checking if a word matches in the matrix only from left to right. You out the word and the starting position in the command line. So the command "./a.out tcnj 1 1 < 0505matrix" will return true because the letters tcnj start at position 1 1 in the matrix Thanks so much, all help is appreciated!
So this is the matrix only the letters are in 5 rows of 5 (thats what the 5s are) similar to a word search
5 5
u r a q o
f t c n j
k r h p r
e a v o t
z h g a h
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char *argv[]){
for(int i = 0; i < argc; i++){
cout << argv[i] << " ";
}
std::string sWord = argv[1];
int wordLength = sWord.length();
int startRow = atoi(argv[2]);
int startCol = atoi(argv[3]);
int x, y;
cin >> x >> y;
cout << x << y << endl;
vector < vector < char > > matrix;
matrix.resize(x);
for(int i = 0; i < matrix.size(); i++){
matrix.resize(y);
for(int k = 0; k < matrix.size(); k++){
cin >> matrix[i][k];
}
}
for(int i = 0; i < wordLength; i++){
if(matrix[startRow][startCol + i] != sWord[i]){
return false;
}
else{
return true;
}
}
}
I think the problem in your code is, in the below line,
matrix.resize(y);
Since it is a two dimensional vector You should do like this.
matrix.resize(x);
for(int i = 0; i < matrix.size(); i++){
matrix[i].resize(y);
for(int k = 0; k < matrix.size(); k++){
cin >> matrix[i][k];
}
}

Nested "for" loops c++

On the way to understand working of nested for loop i wrote a program that takes a input and display a pyramid upto that input value like this:
1
22
333
4444
It's displaying just the height of pyramid but its not displaying written part in the second for loop.
Here is the code(after modification but required result not yet)
#include <iostream>
using namespace std;
int main(void)
{
int num;
cout << "Enter the number of pyramid" << endl ;
cin >> num ;
for (int i = 0; i < num ; i++)
{
int max;
for (int j = 0 ; j <= max ; j++)
{
cout << j ;
}
cout << endl ;
max++ ;
}
system("PAUSE");
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
int num ;
cout << "Enter the number of pyramid" << endl ;
cin >> num ;
for (int i = 0; i < num ; i++)
{
int max = i +1; //change 1
for (int j = 0 ; j < max ; j++)
{
cout << max; //change 2
}
cout << endl ;
//max++ ; //change 3
}
system("PAUSE") ;
return 0;
}
You should initialize max to 0.
int max = 0;
Additionally there are two more bugs.
int max ;
should be declared before the for loop for i. (Otherwise max is defined to be 0 always)
In the inner loop print i, not j.
First of all, please try to have a proper structure in your code:
#include <iostream>
using namespace std;
int main(void)
{
int num;
cout << "Enter the number of pyramid" << endl;
cin >> num;
for(int i = 0; i < num; i++)
{
int max;
for(int j = 0; j <= max; j++)
{
cout << j;
}
cout << endl;
max++;
}
system("PAUSE");
return 0;
}
And your mistake:
Change int max; to int max = 0;
You cannot add 1 to a non existing value.
As has been stated in other answers, your max counter isn't initialized. Additionally, you don't really need it, as you already have i doing the same task:
for (int i = 1; i <= num; i++)
{
for (int j = 0; j < i; j++)
{
cout << i;
}
cout << endl;
}
Unless you actually want to print something like 0 01 012 0123, this is the code you're looking for:
for (int i = 1; i <= num; i++)
{
for (int j = 0; j < i; j++)
cout << i;
cout << endl;
}
max is not set to an initial value.
Its declared insides 1st loop and then used in the 2nd loop.