I am a beginner to C++ Programming. I tried to make a simple program to learn the continue statement. But, when I use continue statement for skipping an iteration of the loop, the program runs but doesn't show any output. Any Help?
#include<bits/stdc++.h>
using namespace std;
int main() {
for (int j=1; j < 13; j++)
{if(j=8) continue;
for (int i=1; i<j+1; i++)
{cout << setw(4) << i*j;}
cout << endl;
}
}
We use == instead of = for comparision.
The = is an assignment operator we generally use it to initialize values.
int main() {
for (int j=1; j < 13; j++){
if(j==8) continue;
for (int i=1; i<j+1; i++){
cout << setw(4) << i*j;
}
cout << endl;
}
}
Related
I'm a newbie here and this my first question. I learn basic c++ on Visual Studio 2019. I study functions and loops.
I have a homework that wants print:
I solved it with my code of 30 lines. But I just wonder is a there a shorter and better way of this?
using namespace std;
int main()
{
for (int j = 0; j < 5; j++)
{
if (j == 1)
break;
cout << "*";
cout << endl;
if (j == 2)
break;
cout << "**";
cout << endl;
if (j == 3)
break;
cout << "***";
cout << endl;
if (j == 4)
break;
cout << "****";
cout << endl;
if (j == 5)
break;
cout << "*****";
cout << endl;
}
return 0;
}
You have made several mistakes in your code.
break statements should actually be continue statements. break terminates the entire loop altogether (ie. absolutely nothing will be printed in the code you provide).
i == 5 can never be true, as the loop terminates before thet happens (see the condition i < 5).
To answer your question, you can use a nested loop.
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i; j++) {
std::cout << '*';
}
std::cout << std::endl;
}
Or, if you don't need to be flexible, do it manually.
std::cout << "*\n**\n***\n****\n*****" << std::endl;
Why a loop? Here’s the same logic in seven lines of code:
int main() {
std::cout << "*\n";
std::cout << "**\n";
std::cout << "***\n";
std::cout << "****\n";
std::cout << "*****\n";
}
To be honest, this is probably as short as it gets (apart from cramming all the output into a single statement).
But the issue with your loop isn’t primarily its length, it’s that your loop isn’t variable: it fails to work as soon as you change the number of iterations, and that defeats the purpose. All the logic is hard-coded, which makes the loop useless.
But if you want to use loops, then make the logic of printing stars configurable:
void print_stars(int n) {
for (int i = 0; i < n; ++i) {
std::cout << "*";
}
std::cout << "\n";
}
int main() {
for (int i = 0; i < 5; ++i) {
print_stars(i + 1);
}
}
#include<iostream>
using namespace std;
int main(){
for(int i = 1;i <= 5;i++){
for(int j = 1;j <= i;j++){
cout<<"*";
}
cout<<"\n";
}
}
void printTrignleL(size_t n)
{
for (size_t i = 0; i < n; ++i) {
std::cout << std::string(i + 1, '*') << '\n';
}
}
I'll start by saying that I'm a begginer. I've started learning 2 weeks ago. Today I found some video on YT about this Conway's Game of Life. With things I've learned so far I tought I'll give it a go. So I've made this(code below). It doesn't work like it should be and I'm out of ideas why. Could someone check this code below and tell me what's wrong? I don't want a complete solution, just a tip what I should check.
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main()
{
bool positions[50][50], new_positions[50][50];
int neighbours=0;
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) positions[i][x]=false;
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) new_positions[i][x]=false;
}
//This is some patterns i've put manually into array just for testing,
//they should "be alive" at least for 3-4 cycles
positions[5][5]=true;
positions[5][6]=true;
positions[5][7]=true;
positions[6][5]=true;
positions[5][8]=true;
positions[5][9]=true;
positions[5][10]=true;
positions[5][11]=true;
positions[5][5]=true;
positions[6][5]=true;
positions[7][5]=true;
positions[5][6]=true;
positions[8][5]=true;
positions[5][7]=true;
positions[6][8]=true;
positions[7][9]=true;
positions[8][10]=true;
positions[9][5]=true;
positions[11][11]=true;
positions[11][12]=true;
positions[12][11]=true;
positions[12][12]=true;
while(!(kbhit())) {
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) {
for(int pozX=x-1, pozI=i-1; pozI<=i+1; pozX++) {
//if(pozX!=x && pozI!=i) { //this "if" doesn't work, don't know why
if(pozX>=0 && pozX<50 && pozI>=0 && pozI<50) {
if(positions[pozI][pozX]==true) neighbours++;
}
//}
if(pozX==x+1) {
pozX=x-1;
pozI++;
}
}
if(neighbours==1) neighbours=0;//had to use this instead of previously mentioned if
if(positions[i][x]==true) {
if((neighbours>3) || (neighbours<2)) new_positions[i][x]=false;
}
if(positions[i][x]==false && neighbours==3) new_positions[i][x]=true;
neighbours=0;
}
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) {
if(new_positions[i][x]==true) cout << "X";
else cout << " ";
}
cout << endl;
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) positions[i][x]=false; //clears all cells
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) positions[i][x]=new_positions[i][x]; //sets cell status from this cycle, I know I could do this in one loop, but it is more clear to me this way
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) new_positions[i][x]=false; //clears this cycle "buffor"
}
Sleep(3000);
system("cls");
}
}
How I debugged the code was by using https://www.onlinegdb.com/
Learning to use a debugger is really valuable...
What I have done includes:
I removed the kbhit() test and the Sleep() call and cls because I am not using a windows compiler. You will have to put those back if you use my code.
I changed the bit that displays the board to give it line numbers and to display something in the blank cells and I changed it to avoid calling endl to flush the buffer for every single line.
I changed the way neighbours were calculated. I think you were subtracting 1 to take care of the extra add that was done - I think it is better to just use an if statement to avoid adding it in the first place. Here is my version:
int neighbours=0;
for(int pozI=i-1; pozI<=i+1; pozI++)
for(int pozX=x-1; pozX<=x+1; pozX++)
if(pozX!=x || pozI!=i) // don't check position[i][x]
if(pozX>=0 && pozX<50 && pozI>=0 && pozI<50)
if(positions[pozI][pozX]==true)
neighbours++;
I changed the way the new board was calculated. You weren't ever setting the new position to true if it should stay alive. Here is my version:
// set the new board
new_positions[i][x]=false;
if(positions[i][x]==true)
if(neighbours==2 || neighbours==3)
new_positions[i][x]=true; // staying alive
if(positions[i][x]==false)
if(neighbours==3)
new_positions[i][x]=true; // being born
What I have not done includes:
There are a lot of stylistic issues and whether they are changed is debateable depending on what you consider best practice. Things like the variable names and having everything in main() and having a global using namespace std; and using a C style array instead of a std::array are all things that we could argue about but that belongs on codereview.stackexchange.com so I haven't changed any of that.
Here is the full program:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
bool positions[50][50], new_positions[50][50];
// clear the board
for(int i=0; i<50; i++)
for(int x=0; x<50; x++)
positions[i][x]=false;
//This is some patterns i've put manually into array just for testing,
//they should "be alive" at least for 3-4 cycles
positions[5][5]=true;
positions[5][6]=true;
positions[5][7]=true;
positions[6][5]=true;
positions[5][8]=true;
positions[5][9]=true;
positions[5][10]=true;
positions[5][11]=true;
positions[5][5]=true;
positions[6][5]=true;
positions[7][5]=true;
positions[5][6]=true;
positions[8][5]=true;
positions[5][7]=true;
positions[6][8]=true;
positions[7][9]=true;
positions[8][10]=true;
positions[9][5]=true;
positions[11][11]=true;
positions[11][12]=true;
positions[12][11]=true;
positions[12][12]=true;
// print the initial board
for(int i=0; i<50; i++) {
cout << setw(4) << i << setw(1) << " ";
for(int x=0; x<50; x++)
cout << (positions[i][x]==true?"X":".");
cout << "\n";
}
cout << endl;
for(int steps=0; steps<15; steps++) {
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) {
// count the neighbours
int neighbours=0;
for(int pozI=i-1; pozI<=i+1; pozI++)
for(int pozX=x-1; pozX<=x+1; pozX++)
if(pozX!=x || pozI!=i) // don't check position[i][x]
if(pozX>=0 && pozX<50 && pozI>=0 && pozI<50)
if(positions[pozI][pozX]==true)
neighbours++;
// set the new board
new_positions[i][x]=false;
if(positions[i][x]==true)
if(neighbours==2 || neighbours==3)
new_positions[i][x]=true; // staying alive
if(positions[i][x]==false)
if(neighbours==3)
new_positions[i][x]=true; // being born
}
}
// print the old board and the new one
for(int i=0; i<50; i++) {
cout << setw(4) << i << setw(1) << " ";
for(int x=0; x<50; x++)
cout << (positions[i][x]==true?"X":".");
cout << " ";
for(int x=0; x<50; x++)
cout << (new_positions[i][x]==true?"X":".");
cout << "\n";
}
cout << endl;
// save the generation
for(int i=0; i<50; i++)
for(int x=0; x<50; x++)
positions[i][x]=new_positions[i][x];
}
// print the final board
for(int i=0; i<50; i++) {
cout << setw(4) << i << setw(1) << " ";
for(int x=0; x<50; x++)
cout << (positions[i][x]==true?"X":".");
cout << "\n";
}
cout << endl;
return 0;
}
i wrote a simple sorting code on pass by reference. here i am passing an array to function and then performing the sorting operation. after passing the array i am printing the entire array as entered by user then performing the sorting operation(in descending order) but after sorting when i print the sorted array i get an output that the array index '0' contains the value '41'. if i enter the numbers less than '41' then the sorted array is displayed as '41',and then the other numbers in sorted manner. Please explain why i am getting such an output.
#include<iostream>
using namespace std;
int sort_array(int *p);
int main() {
int arr[10];
for (int i=0; i<10; i++) {
cout << "enter " << (i+1) << " value:";
cin >> arr[i];
cout << "\n";
}
sort_array(arr);
return 0;
}
int sort_array(int *p) {
int c=0;
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << "arr:"<<p[0];
cout<<"\n";
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
if (p[j] < p[j+1]) {
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
cout << "\n";
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << p[0];
}
It appears that you are trying to do a bubble sort on your array in sort_array(), but the logic is wrong. Try using this code instead:
int sort_array(int *p) {
int c=0;
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << "arr:" << p[0];
cout << "\n";
for (int i=0; i < 10; i++) {
for (int j=1; j < (10-i); j++) {
if (p[j-1] > p[j]) {
c = p[j-1];
p[j-1] = p[j];
p[j] = c;
}
}
}
cout << "\n";
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout<<p[0];
}
The problem is in your sorting. j is from 0 to 9, and than you access p[j+1] when j = 9, p[10] is outside your array boundaries.
So fix your following part to proper sorting.
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(p[j]<p[j+1])
{
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
}
Clarification: the code above is the problematic part of the original code posted. This is NOT the fixed sorting. That is the part to be fixed.
for(int i=0;i<10;i++)
{
for(int j=0;j<9;j++)
{
if(p[j]<p[j+1])
{
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
}
the sorting works fine once i changed the limit of inner loop. the problem was accessing the array index [10] but i had it declared till index [9].
#include <iostream>
using namespace std;
int main ()
{
for (int i = 0; i < 10;i++)
{
cout << '\t' << i; // \t represent a tab character, which will format our output nice;y
}
cout << '\n';
for( int i = 0; i < 10; ++i)
cout << i;
}
(int j = 0; j < 10; ++j)
cout <<'\t' << i * j;
}
cout << '\n';
}
}
'for' keyword is missing and there are lot of error in braces. I would suggest use of proper indentation for avoiding such errors
You forgot the for keyword in the loop using j.
I am trying to search a 2D vector for a char, namely a '?' and replace it with 'x'.
I have no issues doing this task with a single vector but keep having issues with the a 2D vector implementation, see below for code.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// An empty vector of vectors
vector<vector<char> > v2d;
// Create a vector with 5 elements
vector<char> v2(5, '?');
// Create a vector of 3 elements.
vector<vector<char> > v2d2(3, v2);
// Print out the elements
cout << "Before Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
cout << v2d2[i][j] << " ";
cout << endl;
}
cout << "" << endl;
/* Does not work as expected
cout << "Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
{
if (v2d[i] == '?');
(v2d[i] = 'x');
}
}
*/
cout << "" << endl;
cout << "After Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
cout << v2d2[i][j] << " ";
cout << endl;
}
system("pause > NULL");
return 0;
}
I receive the error message below when I try and compile the code.
IntelliSense: no operator "==" matches these operands operand types are: std::vector>, std::allocator>>> == char Project3\Source.cpp 77 16 Project3
I believe it is an issue with the container in updating the proper row and column. Any help would be much appreciated.
Thank you
There were a few bugs, please compare with your original code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// An empty vector of vectors
vector<vector<char> > v2d;
// Create a vector with 5 elements
vector<char> v2(5, '?');
// Create a vector of 3 elements.
vector<vector<char> > v2d2(3, v2);
// Print out the elements
cout << "Before Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
cout << v2d2[i][j] << " ";
cout << endl;
}
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
if (v2d2[i][j] == '?')
v2d2[i][j] = 'x';
}
cout << "After Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
std::cout << v2d2[i][j] << " ";
cout << endl;
}
return 0;
}
Take a look at your commented code:
cout << "Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
{
if (v2d[i] == '?'); // <------
(v2d[i] = 'x');
}
}
There is a semi-colon after the if statement, meaning that nothing will be executed if the statement is true. Rookie mistake.
Many consider it a bad practice not to always use parentheses with if-statements and other similar situations that require them, because of issues like this one
You should have :
if (v2d[i][j] == '?'){
v2d[i][j] = 'x';
}
cout << "Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
{
if (v2d[i][j] == '?')
v2d[i][j] = 'x';
}
}
You were not accessing the 2d vector correctly, should have used v2d[i][j] instead.