Help with a for loops problem
Jan 10, 2016 at 11:20pm UTC
What am i doing wrong? I am trying to make a pattern like this where the user enters a height and a width for the design.
*******
*0*0*0*
*0*0*0*
*******
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main () {
int width;
int height;
cout << "Please enter a width for your box design" << endl;
cin >> width;
cout << "Please enter a height for your box design " << endl;
cin >> height;
for (int heightcounter = 1; heightcounter <= height; heightcounter++){
cout << endl;
for (int widthcounter = 1; widthcounter <= width; widthcounter++) {
if (heightcounter == 1){
cout << "*" ;
}
if (heightcounter % height == 0) {
cout << "*" ;
}
else if (widthcounter == 1, widthcounter % width ==0) {
cout << "*" ;
}
if (widthcounter == 1, widthcounter % width ==0) {
cout << "0" ;
}
}
}
}
Jan 10, 2016 at 11:44pm UTC
My solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <iostream>
void hLine(int width);
void inLine(int width);
int main() {
int width, height;
std::cout << "Enter width: " ;
std::cin >> width;
std::cout << "Enter height: " ;
std::cin >> height;
hLine(width);
for (int i = 0; i < height - 2; ++i) {
inLine(width);
}
hLine(width);
return 0;
}
void hLine(int width) {
for (int i = 0; i < width; ++i) {
std::cout << "*" ;
}
std::cout << std::endl;
}
void inLine(int width) {
for (int i = 0; i < width; ++i) {
if (i % 2 == 0) {
std::cout << "*" ;
}
else {
std::cout << "0" ;
}
}
std::cout << std::endl;
}
Jan 11, 2016 at 12:14am UTC
I don't know how to use void and in line h line stuff :(
Jan 11, 2016 at 12:40am UTC
if (widthcounter == 1, widthcounter % width == 0) {
is the same as
if (widthcounter % width == 0) {
A working set o' loops:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for (int heightcounter = 1; heightcounter <= height; heightcounter++)
{
for (int widthcounter = 1; widthcounter <= width; widthcounter++)
{
if (heightcounter == 1 || heightcounter == height)
cout << "*" ;
else
{
if (widthcounter == 1 || widthcounter == width || widthcounter % 2 == 1)
cout << "*" ;
else
cout << "0" ;
}
}
std::cout << '\n' ;
}
Jan 11, 2016 at 3:34am UTC
THANK YOU!!!!!! :)
Topic archived. No new replies allowed.