C++ pattern output - c++

I am working on a project that requires me to have a loop output a pattern into the console.
I have to use a for loop in my code. I've gotten to a point where I can only get half of the pattern onto the screen but the rest does not appear in the console.
My code:
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i=5; i>=1; i--)
{
for (j=1; j != i; j++)
{
cout << "5";
cout << "#";
cout << endl;
}
}
return 0;
}
So this code outputs:
####5
###5
##5
#5
5
But I need it to output:
####5
###5#
##5##
#5###
5####
How would I change my code to get it to show that output?
I hope that makes sense,
Thank you

your internal loop is the one that tracks moving 5 from right to left
for (i=5; i >= 1; i--) {
for (j=1; j <= 5; j++) {
cout << (( i == j ) ? "5" : "#");
}
cout << endl;
}
removing the fancy things
for (i=5; i >= 1; i--)
{
for (j=1; j <= 5; j++)
{
if( i == j )
cout << "5";
else
cout << "#";
}
cout << endl;
}

This is what you are looking for.
#include <iostream>
using namespace std;
int main()
{
int i,j;
for (i=4; i>=0; i--)
{
for(int z=0; z<5;z++){
if(z==i){
cout <<"5";
}
else{
cout <<"#";
}
}
cout <<"\n";
}
return 0;
}
####5
###5#
##5##
#5###
5####
Chears :-)

Let's be clear: the OP's code is completely wrong and I cannot understand how it works and produces that output, so I wrote it from scratch.
Before_edit:
I can't understand how your code work, so I rewrite that from scratch.
#include <iostream>
void output_sharp(int cnt)
{
while (cnt != 0)
std::cout << "#";
}
int main(int argc, char *argv[])
{
for (int i = 0, j = 4; i != 5; ++i, --j) {
output_sharp(j - i);
std::cout << "5";
output_sharp(4 - j);
}
return 0;
}

Related

How can I compare the elements in a vector?

I took a look online and none of the answers solves the problem I have comparing the elements from a vector.
I tried implementing a bool function but the problem is the same.
I am pretty new in c++ so please be patient!
PART2: First of all thank you.
So I changed my programm and created a bool function, the problem is now that it doesn get recognised before 5-6 tries.
#include <iostream>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <string>
using namespace std;
vector<int> input, compareMe, randomNumbers;
const unsigned int MAX_VEKTORSTELLEN = 5;
const unsigned int UPPER_GRENZE = 49;
const unsigned int LOWER_GRENZE = 1;
unsigned int i, j;
string output;
int random, anzahlRichtige, eingabe;
bool isEqual = false;
string lotto(vector<int>)
{
if (input[i] < LOWER_GRENZE || input[i] > UPPER_GRENZE)
{
output = "Die Zahlen muessen zwischen 1 und 49 liegen! \n";
input.pop_back();
}
else if (input.size() != MAX_VEKTORSTELLEN)
output = "Es muessen 6 Zahlen uebergeben werde! \n";
else if (isEqual == true)
output = "Es duerfen keine doppelten Zahlen vorkommen! \n";
else
for (i = 0; i <= MAX_VEKTORSTELLEN; i++)
srand((unsigned)time(NULL) <= UPPER_GRENZE && (unsigned)time(NULL) > 0);
random = rand();
randomNumbers.push_back(random);
return output;
}
bool compare()
{
compareMe = input;
for (i = 0; i < input.size(); i++)
for (j = 0; j < compareMe.size(); j++)
if (compareMe[j] == input[i])
isEqual = true;
return isEqual;
}
int main()
{
cout << "insert 6 numbers: ";
while (cin >> eingabe)
{
input.push_back(eingabe);
lotto(input);
compare();
cout << output;
for (i = 0; i < input.size(); i++) //Debug
cout << input[i] << ", ";
continue;
}
for (i = 0; i < input.size(); i++)
cout << input[i];
system("pause");
return 0;
}
From line 34 to line I didn´t finish to code but doesn´t really matter because I got stuck before.
All your loops in lotto are wrong. You go one past the end of your containers.
for (i = 0; i <= input.size(); i++)
// ^ !!!
It should be <.
You got this right in main.

How to remove blank from code

I have coded this program and it works fine. I get the result I want but because we are using an old system to submit it, my code is rejected because it saying that the 3 last lines generate a blank of my code. Can someone please tell me where is the problem and how I can fix it? Thank you!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(i = 1; i <= row_nr; i++)
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
You're outputting a space after every value, so there is going to be a space at the end of each line. You should add a check so that you don't output a space after the last value of each line. It seems like you might have intended to do this, but forgot to write the if statement.
#include <iostream>
//#include <iomanip> why?
using namespace std;
int main()
{
int row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(int i = 1; i <= row_nr; i++) //declare iterator variable in for loop statement
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
if(j < row_nr - 1) //you forgot this line
{
cout << " ";
}
}
cout << '\n'; //endl flushes the buffer, unnecessary here
}
return 0;
}

.exe file stopped working when i run a c++ program(no '/0')

When i run this program (i am using codeblock and its fully upgraded), it shows a box with:
''''.exe has stopped working
A problem caused the program to stop working correctly. Windows will close the program and notify if a solution is available.''''
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
int main()
{
int no, hlf, arr[no], arrno;
cout << "ENTER A NUMBER";
cin >> no;
hlf = ceil(no/2);
for(int i = 1;i <= no;i++)
{
for(int j = 2;j <= hlf;j++)
{
int ij = i/j;
if(j != i && ij == 0)
{
goto cont;
}
else
{
continue;
}
}
arr[arrno] = i;
arrno++;
cont: ;
}
for(int k = 0;k <= arrno;k++)
{
cout << arr[k] << " ";
}
getch();
return 0;
}
There are few mistakes in your code
no need of #include <conio.h> and getch();
Array arr[no] declaration is wrong. It should be int arr[50];
Here is the corrected code that runs fine.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int no, hlf, arrno;
int arr[50];
cout << "ENTER A NUMBER";
cin >> no;
hlf = ceil(no/2);
for(int i = 1;i <= no;i++)
{
for(int j = 2;j <= hlf;j++)
{
int ij = i/j;
if(j != i && ij == 0)
{
goto cont;
}
else
{
continue;
}
}
arr[arrno] = i;
arrno++;
cont: ;
}
for(int k = 0;k <= arrno;k++)
{
cout << arr[k] << " ";
}
return 0;
}
thanks guys, i got the answer. it was my bad, i didn't post that i need to print prime numbers. its my first question in a web forum. never used one.
ps -> thanks again
include
include
using namespace std;
int main()
{
int numb = 12, half;
int arra[50], arrno = 0;
half = ceil(numb/2);
for(int r = 2;r <= numb;r++)
{
for(int t = 2;t <= half;t++)
{
if(r%t != 0 || t == r) continue;
else goto rpp;
}
arra[arrno] = r;
arrno++;
continue;
rpp:
continue;
}
for (int v = 0;v < arrno;v++)
{
cout << arra[v] << " ";
}
return 0;
}

Problems with terminating loop at specific time

Having trouble with my loop. The program is supposed to resemble a print out of a
lotto ticket. The user enters in how many sets of lotto numbers he/she wants. Each line is labeled alphabetically, yet if someone wants more then ten lines (letter J) of lotto numbers the program is supposed to start back over at A again. My problem is that if anyone enters 10 (or any interval of ten) "mega" gets printed like so:
"Mega" should only be printed again if there is another line of lotto numbers.
In "int main()" inside the "for()" is my attempt to remedy this problem.
#include <iostream> //I/O
#include <iomanip> //setw
#include <ctime> //seeding srand
#include <string> //size
#define RAND(a,b) (a+rand()% (b-a+1))
#define die(errmsg) {cerr << errmsg << endl; exit(1);}
using namespace std;
/*
Author: Zachary Stow
Date: July/20/15
Homework #5
Objective: To design a program that imitates the print out
of a lottery ticket.
*/
//********************************fillup()********************************
void fillup(int lotto[], int n, int from, int to)
{
void bubble_sort(int x[], int n);
for(int i = 0; i < n; i++)
{
lotto[i] = RAND(from,to);
}
bubble_sort(lotto,5);
}
//*****************************bubble_sort()******************************
void bubble_sort(int x[], int n)
{
for(int i = 0; i < n-1; i++)
{
int temp;
for(int j=i+1; j<n ; j++)
{
if(x[i] > x[j])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
}
//********************************print()*********************************
void print(int x[], int n)
{
for(int i = 0; i < n; i++)
{
cout << setfill('0') << setw(2) << x[i] <<" ";
}
cout <<" ";
cout << setfill('0') << setw(2) << RAND(1,46);
cout << endl;
}
//********************************isNumber********************************
bool isNumber(string str)
{
for(int i = 0; i < str.size(); i++)
{
if(!isdigit(str[i]))return(false);
}
return(true);
}
//**********************************check*********************************
void check(int argc, char **argv)
{
bool isNumber(string);
if(argc != 2)die("usage: megaMillion number_tickets");
string num_tickets = argv[1];
if(!isNumber(num_tickets))die("Not a digit."); //removed num_tickets for now
int num;
num = atoi(num_tickets.c_str());
if(num <= 0)die("Zero or negative number."); //doesnt work
}
//*********************************printmega()****************************
void printmega(int letter)
{
if(letter == 65)
{
cout << endl;
cout <<" Mega" << endl; //10 you get a mega
}
}
//*********************************main()*********************************
int main(int argc, char **argv)
{
void fillup(int x[], int n, int from, int to);
void print(int x[], int n);
void check(int argc, char **argv);
void printmega(int letter);
check(argc, argv);
srand(time(NULL));
cout <<" Mega" << endl;
int letter = 65;
for(int i = 0; i < atoi(argv[1]); i++)
{
if(i == atoi(argc[1]))cout << "Hi"; //my attempt to stop the loop from printing
//only mega after J
cout <<(char)letter++; //when theres no more lines
cout <<" ";
if(letter == 75)letter = 65;
int lotto[5];
fillup(lotto,5,1,56);
print(lotto,5);
printmega(letter);
}
return(0);
}
For this problem I would choose to use two nested loops, here is a minimal example:
#include <iostream>
int main(int argc, char** argv)
{
double aValues[] = {1, 2, 3, 4, 5, 6, 7, 8, 6, 2};
int nTotalLines = sizeof(aValues)/sizeof(double);
int nLinesPerBlock = 5;
int nLineNumber = 0;
for (int i = 0; i <= nTotalLines/nLinesPerBlock
&& nLineNumber < nTotalLines; ++i)
{
std::cout << "Start of block..." << std::endl;
for (int j = 0; j < nLinesPerBlock && nLineNumber < nTotalLines; ++j)
{
std::cout << " Number: " << aValues[nLineNumber++] << std::endl;
}
}
return 0;
}
You will need to adapt this code to fit your problem. It is not necessary to use nested loops but to me it seems more logical (you cycle one outer loop for each block and one inner loop for each line).
Another tip: Make use of variables so you don't need to keep typing things like atoi(argv[1]), instead create a variable int name = atoi(argv[1]); and use name wherever you need it.

Treating an array of bools as though incrementing in binary [duplicate]

This question already has answers here:
Bitset in C++, about continuously add
(2 answers)
Closed 8 years ago.
I'm trying to create a loop that changes the values in a boolean array so that it looks like the array is incrementing in binary values.
For example
1st iteration [0|0|0]
2nd iteration [0|0|1]
3rd iteration [0|1|0]
4th iteration [0|1|1]
etc.
This array is dynamic, however, and can be different sizes. So whatever loop I write would need to also work on an array with five elements instead of three.
Apologies for not having any starting code, but I've been frustrating myself with this for hours and still can't even come up with how to begin.
Try this. This may not be complete but you could do something similar
#include <iostream>
using namespace std;
void increment(bool* array, int len)
{
for (int i = len - 1; i >= 0; --i)
{
if ( ! array[i])
{
array[i] = true;
return;
}
array[i] = false;
}
}
int main()
{
bool* array = new bool[10];
for (int i = 0; i < 5; ++i)
{
increment(array, 10);
for (int i = 0; i < 10; ++i)
{
cout << (array[i] ? 1 : 0) << "|";
}
cout << endl;
}
return 0;
}
#include <iostream>
#include <cmath>
#include <memory>
using namespace std;
void ArrayIterate(int);
void printArray(bool*,int);
void ArrayIterate(int arraySize)
{
int decimal_value = 0;
int decimal_place_value = 0;
bool* boolArray = new bool(arraySize);
long double max_itr = pow(2,arraySize);
for (int i = 0; i < max_itr ; ++i)
{
decimal_value = i;
// set array values
for ( int k = arraySize - 1; k >= 0; --k)
{
decimal_place_value = pow(2,k);
if( decimal_value != 0 && decimal_value / decimal_place_value >= 1 )
{
boolArray[k] = true;
decimal_value -= decimal_place_value;
}
else
boolArray[k] = false;
}
printArray(boolArray,arraySize);
cout << " = " << i << endl; ;
}
delete boolArray;
return;
}
void printArray(bool* boolArray, int arraySize)
{
cout << "\t";
for(int i = arraySize - 1; i >= 0; --i)
cout << ((boolArray[i] == true)? 1 : 0) << " ";
return;
}
int main()
{
cout << "\n\n";
ArrayIterate(4);
cout << "\n\n" << endl;
return 0;
}