issue with while loops - c++

#include <iostream>
#include <math.h>
using namespace std;
int main() {
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
int i = 0;
int j = 0;
cout << "Enter arrow base height:" << endl;
cin >> arrowBaseHeight;
cout << "Enter arrow base width:" << endl;
cin >> arrowBaseWidth;
cout << "Enter arrow head width:" << endl;
cin >> arrowHeadWidth;
cout << endl;
// Draw arrow base
while (i <= arrowBaseHeight){
while (j <= arrowBaseWidth){
cout << "*";
j++;
}
cout << endl;
j = 0;
i++;
}
// Draw arrow head (width = 4)
return 0;
}
I am trying to write a simple program that takes 3 user entered integers and assigns them to arrowBaseHeight, arrowBaseWidth, and arrowHeadWidth. The output should be a series of asterisks (*) that print out like:
**
**
**
****
***
**
*
to create an image of an arrow.
I have been trying to figure out the best way to print out the base portion of the arrow using nested loops (I have been using while but if for is better, let me know). I have tried multiple different ways and I have yet to figure one out that doesn't throw back an error. I have yet to get to the arrow head portion but if anyone wants to point me in the right direction, it would be helpful!

You were close, but if you want for a loop to be executed exactly n times, starting your counter i at 0, the condition should be i < n, not i <= n.
About the head, you just have to decrement the number of characters printed in every line, starting from the inputted width.
#include <iostream>
int main()
{
using std::cout;
using std::cin;
int arrowBaseHeight = 0;
cout << "Enter arrow base height:\n";
cin >> arrowBaseHeight;
int arrowBaseWidth = 0;
cout << "Enter arrow base width:\n";
cin >> arrowBaseWidth;
int arrowHeadWidth = 0;
cout << "Enter arrow head width:\n";
cin >> arrowHeadWidth;
cout << '\n';
// Draw arrow base
for ( int i = 0; i < arrowBaseHeight; ++i )
{
for ( int j = 0; j < arrowBaseWidth; ++j )
{
cout << '*';
}
cout << '\n';
}
// Draw arrow head
for ( int i = 0, width = arrowHeadWidth; i < arrowHeadWidth; ++i, --width )
{
for ( int j = 0; j < width; ++j )
{
cout << '*';
}
cout << '\n';
}
return 0;
}
You can see a lot of repeated code, consider refactoring it using some custom functions, instead.

You should change the condition of the while loops to:
while (i < arrowBaseHeight) and
while (j < arrowBaseWidth).
And for the arrowHeadWidth you could try to get the middle of the arrowBaseHeight. Maybe like this
int r = 0;
if(i == arrowBaseHeight / 2)
{
while(r < arrowHeadWidth)
{
cout << "*";
r++;
}
}
I haven't tested it. I hope it helps.

All you need to do is to add a new variable which could indicate what are you need to print right now.
The rule is :
If: up to half of "arrowBaseHeight" iteration you need to print the base
Else: print the head and after that decrease in 1
In addition finger rule - if you are using "while" and you need to increase an iterator it always indicate that you need to use For
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
int newArrowBaseWidth=0;
cout << "Enter arrow base height:" << endl;
cin >> arrowBaseHeight;
cout << "Enter arrow base width:" << endl;
cin >> arrowBaseWidth;
cout << "Enter arrow head width:" << endl;
cin >> arrowHeadWidth;
cout << endl;
// Draw arrow base
for(int i=0; i < arrowBaseHeight; i++){
newArrowBaseWidth= i < arrowBaseHeight/2 ? arrowBaseWidth : arrowHeadWidth--;
for(int j=0; j < newArrowBaseWidth; j++){
cout << "*";
}
cout << endl;
}
// Draw arrow head (width = 4)
return 0;
}
Another thing is if you want to iterate n time you need to change the condition from =< that here mean - n+1 time, to <

Related

Reversing the order of a for loop output

I am currently writing a task where the user will input a number and it'll output a number of "*" depending on the number. Eg if the user inputted a 5, the answer would be:
*
**
***
****
*****
This is my current code:
#include <iostream>
using namespace std;
int number;
char star = '*';
int main()
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
for (int i = 0; i < number; i++)
{
for (int j = i; j < number; j++)
{
cout << star;
}
cout << " " << endl;
}
return 0;
}
If the number 5 was inputted, this would output:
*****
****
***
**
*
How would I go about reversing the order so that it is ascending order rather than descending.
You would need to use the for loop in descending instead of ascending order.
Example:
cout << "Input a number between 1 and 10" << endl;
cin >> number;
for (int i = number -1; i >= 0; i--) {
for (int j = i; j < number; j++) {
cout << star;
}
cout << "*" << endl;
}
return 0;
}
You can do that by just reversing the order of the change of i:
#include <iostream>
using namespace std;
int number;
char star = '*';
int main()
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
//for (int i = 0; i < number; i++)
for (int i = number - 1; i >= 0; i--)
{
for (int j = i; j < number; j++)
{
cout << star;
}
cout << "*" << endl;
}
return 0;
}
(Just how to reverse the order of the change of i is shown. The output of this program is not as expected.)
My preference is using i as the number of stars to print and avoiding using gloval variables and using namespace std;:
#include <iostream>
int main()
{
int number;
char star = '*';
std::cout << "Input a number between 1 and 10" << std::endl;
std::cin >> number;
for (int i = 1; i <= number; i++)
{
for (int j = 0; j < i; j++)
{
std::cout << star;
}
std::cout << std::endl;
}
return 0;
}
Just go from number all the way down instead of up:
for (int i = number; i > 0; i--)
{
for (int j = i; j > 0; j--)
{
cout << star;
}
std::cout << '\n';
}
Instead of j<number use j<i and instead of j=i use j=0:
#include <iostream>
using namespace std;
int number;
char star = '*';
int main()
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
for (int i = 0; i < number; i++)
{
for (int j = 0; j < i; j++)
{
cout << star;
}
cout << "*" << endl;
}
return 0;
}
there are multiple ways to achieve what you want. You can insert everything inside and array and print the array backwards, insert everything inside an array (starting from last position) and then print it in natural order, you can do it only by using indexes (like i and a lot of other persons did here). I also added a little input check, and put the code in functions for clearness:
#include <iostream>
using namespace std;
int number;
char star = '*';
void version1();
void version2();
int main()
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
while(number < 1 || number > 10)
{
cout << "I SAID BETWEEN 1 AND 10, IS EASY, RIGHT?" << endl;
cin >> number;
}
version1();
version2();
return 0;
}
void version1()
{
cout <<"Version 1: "<<endl;
for (int i = 0; i < number; i++)
{
for (int j = i; j < number; j++)
{
cout << star;
}
cout << endl;
}
}
void version2()
{
cout << "Version 2: (only by using indexes)" << endl;
int cap_index = 0;
for (int i = 0; i < number; i++)
{
cap_index = i + 1;
for (int j = 0; j < cap_index; j++)
{
cout << star;
}
cout << endl;
}
}
btw this is a more serious (and simple) way to check input:
do
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
}while(number < 1 || number > 10);
In my case i put it this way:
starting from 0 print '*' up to i + 1 times, this way you have an ascending order print:
first time (from 0 to 1) print '*' -> print once
second time (from 0 to 2) print '*' -> prints twice
and so on up to the inserted number
Obviously this isn't not the best way to do this nor the most elegant, like in nearly every situation there are a lot of possibilities.
ps. i'm not sure, but you have an error in your code, as it prints a '*' more, each time, i don't know how is your output like that
By using a std::string, you can avoid the need of an explicit inner loop.
#include <iostream>
#include <string>
int main()
{
int number;
char star = '*';
std::cout << "Input a number between 1 and 10" << "\n";
std::cin >> number;
for (int i = 0; i < number; i++)
{
std::cout << std::string (i+1, star) << "\n";
}
return 0;
}
Changing the second 'for' loop condition, in your function --
From,
for (int j = i; j < number; j++)
Change it to ,
for (int j = 0; j <= i; j++)

Creating a header column via "for" loop in a multiplication table produces gibberish instead of desired result

I need to create a multiplication table of a number between 1 and 20, I've did just that using 2 nested for loops. What I'm having problems with is the formatting of the table - the output in the console needs to look exactly like your average multiplication table in a schoolbook, horizontal and vertical row of numbers and when you find the cross between the horizontal and the vertical numbers you find your multiplication. I've done the horizontal row using a "first pass" check as you can see in the code below:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int br;
int firstpass = 1;
cin >> br;
if (br > 1 && br < 21)
{
if (firstpass = 1)
{
for (int h = 0; h <= br; h++)
cout << setw(5) << h << " ";
cout << endl;
firstpass = 0;
}
for (int a = 1; a <= br; a++)
{
for (int b = 1; b <= br; b++)
cout << setw(5) << a * b << " ";
cout << endl;
}
}
return 0;
}
but the problem with the code above is that it doesn't produce a vertical column (in front of each output in the nested loop should be the number of a), so I added a "second pass" check. The goal was to check if it's a start of a new row via "secondpass", if it is, output the current value of "a" then lie dormant until the loop ends and secondpass is reset, but instead of just adding the value of "a" at the start of the row and then outputting the rest of the row like the first example does, it's showing pure gibberish that I can't untangle.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int br;
int firstpass = 1;
int secondpass = 1;
cin >> br;
if (br > 1 && br < 21)
{
if (firstpass = 1)
{
for (int h = 0; h <= br; h++)
cout << setw(5) << h;
cout << endl;
firstpass = 0;
}
for (int a = 1; a <= br; a++)
{
for (int b = 1; b <= br; b++)
{
if (secondpass = 1)
{
cout << setw(5) << a;
secondpass = 0;
}
cout << setw(5) << a << a * b;
}
secondpass = 1;
cout << endl;
}
}
return 0;
}
What am I doing wrong? I'm sure this fever I'm under isn't exactly helping but I'm feeling really dumb right now. Thanks in advance!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int br;
cin >> br;
if (br > 1 && br < 21)
{
for (int a = 0; a <= br; a++)
{
for (int b = 0; b <= br; b++)
{
if (b == 0)
cout << setw(5) << a;
else if (a == 0)
cout << setw(5) << b;
else
cout << setw(5) << a * b;
}
cout << endl;
}
}
return 0;
}
Here's the answer for someone down the line, finally cracked it. Still no idea why the nested for loops were acting spooky when trying to add another character the original way I did but I guess that's a question for another time.

Building a Triangle with c++ loops

I am trying to build a triangle, with a user entered base and height.
When these entered values are different (base!=height), the program goes haywire and gets stuck in the triangle draw loop.
I've tried altering the code a couple of times, but please treat me as a programming novice.
//BUILD TRIANGLE//
#include <string>
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "\nEnter base and height:\n";
int height{0}; int base{0};
std::cin >> base >> height;
std::string bottom(base, '*');
std::string top = "*";
int middlerows = height - 1;
int middlespacechars;
std::cout << top << std::endl;
for (middlespacechars = 0;
middlerows != 1 || middlespacechars != base - 2;
++middlespacechars, --middlerows) {
std::string middlespace(middlespacechars, ' ');
std::cout << "*" << middlespace << "*\n";
}
std::cout << bottom << "\n" << std::endl;
std::cout << "^TRIANGLE\n";
std::cout << "BASE = " << base << std::endl;
std::cout << "HEIGHT = " << height << std::endl;
std::cout << "goodbye" << "\n" << std::endl;
}
The output is totally haywire, with asterisks across the screen in no discernible shape.
When I put in values where base=height, though, a pretty little right angle triangle pops up.
With your code, you can only draw well triangles which have base equal to height.
If you change stop condition in your for loop, you can get what you probably want to get:
for (middlespacechars = 0; middlerows != 1 || middlespacechars != base - 2; ++middlespacechars, --middlerows) {
... into ...
for (middlespacechars = 0; middlerows > 1 || middlespacechars < base - 2; ++middlespacechars, --middlerows) {
It was huge probability that if base and height are different then stop condition will not be achieved. For loop in your code will stop if middlerows will be 1 and middlespacechars will be base - 2 at the same moment.
Test it here.
//C++ program to display hollow star pyramid
#include<iostream>
using namespace std;
int main()
{
int rows, i, j, space;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
//for loop to put space in pyramid
for (space = i; space < rows; space++)
cout << " ";
//for loop to print star
for(j = 1; j <= (2 * rows - 1); j++)
{
if(i == rows || j == 1 || j == 2*i - 1)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}

Equivalence Relation

I am stuck on what to do next... The program is suppose to check to see if entered Zero-One Matrix is an Equivalence relation (transitive, symmetric, and reflexive) or not. I am still new to C++ (started this semester). I know how to create the matrix using vector but not on how to check if it is equivalence relation or not..
I assume I need to use boolean function but I'm stuck on what I need to put in as an argument or if this is correct. My original thought was... so for symmetric it will look like (which I know this goes after #include and beofre int main(). Any help would be awesome.
bool isSymmetric(vector<int> &vect, int Value)
{
for (int i = 0; i < Value; i++)
for (int j = 0; j < Value; j++)
if (vect[i][j] != vect[j][i])
return false;
return true;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< vector<int> > vec;
cout << "NxN matrix N: ";
int Value;
cin >> Value;
cout << Value << "x" << Value << " matrix\n";
for (int i = 0; i < Value; i++) {
vector<int> row;
for (int j = 0; j < Value; j++) {
cout << "Enter a number (0 or 1): ";
int User_num;
cin >> User_num;
while (User_num != 0 && User_num != 1) {
cout << "Invalid Entry! Enter 0 or 1!\n";
cout << "Enter a number (0 or 1): ";
cin >> User_num;
}
row.push_back(User_num);
}
vec.push_back(row);
}
cout << endl;
for (int i = 0; i < Value; i++) {
for (int j = 0; j < Value; j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}

How do you get cin to only accept numbers from user input? [duplicate]

This question already has answers here:
How to make cin take only numbers
(2 answers)
Closed 6 years ago.
So the requirements for this program is to be able to increment arrays of the same size (size from 5 to 15 indexes) and increment each element in the array by one using for and while loops. The last task is to take values from the first array and put them in reverse order and assign them to the second array.
So everything works as normal, and the program rejects invalid inputs and does not go into an infinite loop. However, the program accepts some inputs that are not wanted.
For example, I would input something like '12 a' or '7 asdfkla;j lasnfg jasklgn asfg' and it would go through. It is interesting too because the code registers only 12 or 7 and completely ignores the rest. I think it is because once it hits a non-integer character, it would stop ignore the rest.
Why is it ignoring the rest of the input? And is there a way to catch this error from going through?
Also, if you see anything that catches your eye, feel free to critique c: I am always looking to improving.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
int x;
int j = 0;
bool not_valid = true;
system("color f");
cout << "Program will ask for an input for the size of an array.\n"
<< "With the array size defined, program will generate semi-\n"
<< "true random integers from 0 to 8. First array will then\n"
<< "be assigned to the second in reverse (descending) order.\n\n";
do {
cout << "Enter array size (0 - 15): ";
cin >> x;
if (x >= 5 && x <= 15) {
not_valid = false;
cout << "\nArray size: " << x << endl;
}
else {
cout << "Invalid input.\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
} while (not_valid);
int *arr0;
int *arr1;
arr0 = new int[x];
arr1 = new int[x];
for (int i = 0; i < x; i++) {
arr0[i] = rand() % 9;
}
for (int i = 0; i < x; i++) {
arr1[i] = rand() % 9;
}
cout << "\nARRAY 0 (unmodified, for):\n";
for (int i = 0; i < x; i++) {
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 0 (modified, for):\n";
for (int i = 0; i < x; i++) {
arr0[i]++;
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 1 (unmodified, while):\n";
for (int i = 0; i < x; i++) {
cout << arr1[i] << "\t";
}
cout << "\n\nARRAY 1 (modified, while):\n";
while (j < x) {
arr1[j]++;
cout << arr1[j] << "\t";
j++;
}
int second = x - 1;
for (int i = 0; i < x; i++) {
arr1[second] = arr0[i];
second--;
}
j = 0;
cout << "\n\nARRAY 1 (array 0, descending):\n";
while (j < x) {
cout << arr1[j] << "\t";
j++;
}
cout << endl << endl;
system("pause");
return 0;
}
Take input in string and then check if it's a number or not.
Example:
#include<iostream>
#include<sstream>
#include <string>
using namespace std;
int main()
{
string line;
int n;
bool flag=true;
do
{
cout << "Input: ";
getline(cin, line);
stringstream ss(line);
if (ss >> n)
{
if (ss.eof())
{
flag = false;
}
else
{
cout << "Invalid Input." << endl;
}
}
}while (flag);
cout << "Yo did it !";
}