I want the pyramid to look like this if the input was 6
0
12
345
6789
01234
567890
Here's my code
void HalfPyramid(int num)
{
for (int a=0; a<num; a++)
{
for (int b=0; b<num-a; b++)
{
cout << " ";
}
for (int c=0; c<a; c++)
{
cout << a;
}
cout << endl;
}
}
I'm getting
1
22
333
4444
55555
Not sure how to show the numbers as increasing throughout, I tried outputting a and a+1.
You need another variable. That variable needs to start at 0 and increment every time you print it. Then since you need to to wrap back to 0 once you print 9 we will use the modulus operator to constrain the output to the range of [0, 9]. With all that you get
void HalfPyramid(int num)
{
int output = 0;
for (int a=1; a<num+1; a++)
{
for (int b=0; b<num-a; b++)
{
cout << " ";
}
for (int c=0; c<a; c++)
{
cout << output++ % 10;
}
cout << endl;
}
}
void HalfPyramid(int num)
{
int cur = 0;
for (int a=0; a<num; a++)
{
for (int b = 1; b < num - a ; b++)
{
cout << " ";
}
for (int c=0; c < a + 1; c++)
{
cout << cur;
cur = (cur+1)%10;
}
cout << endl;
}
}
The other answers already provide working versions of HalfPyramid. This answer, hopefully, makes you think of the logic and functionality a bit differently. I like to create small functions that capture the essence what I am trying to do rather than using the language to just do it.
bool isSpace(int num, int a, int b)
{
return ((a + b) < (num - 1));
}
int getNextDigit(int in)
{
return (in+1)%10;
}
void HalfPyramid(int num)
{
int digit = 0;
for (int a = 0; a < num; ++a)
{
for (int b = 0; b < num; ++b)
{
if ( isSpace(num, a, b) )
{
cout << " ";
}
else
{
cout << digit;
digit = getNextDigit(digit);
}
}
cout << endl;
}
}
Related
I used a 'bubble-sort' for my C++ program, but it introduces random '0' values in array in a Fractional Greedy Program
int sorteaza()
{
int aux,schimb,i;
do
{
schimb=0;
for (i=0;i<=n;++i)
if (G[i][3]<G[i+1][3])
{
swap(G[i], G[i+1]);
}
}
while (schimb);
}
This is my entire code:
#include<iostream>
using namespace std;
int n; // Numarul de elemente
float G[100][3]; // Obiecte + detalii masa profit potenta
int masa = 0;
int read_data()
{
cout << "Greutatea Rucsac" << endl;
cin >> masa;
cout << "Obiecte: " << endl;
cin >> n;
for(int i = 1; i<=n;i++)
{
for(int j = 1; j<=2;j++)
{
cin >> G[i][j];
if(G[i][1] != 0 && G[i][2] != 0)
{
G[i][3] = G[i][2] / G[i][1];
}
}
}
}
// 2 500
// 4 500
int sorteaza()
{
int aux,schimb,i;
do
{
schimb=0;
for (i=0;i<=n;++i)
if (G[i][3]<G[i+1][3])
{
swap(G[i], G[i+1]);
}
}
while (schimb);
}
int verify()
{
for(int i = 1; i<=n;i++)
{
for(int j = 1; j<=3;j++)
{
cout << G[i][j];
cout << endl;
//G[i][3] = G[i][1] / G[i][2];
}
}
}
int greedy()
{
float profit = 0;
int i = 1;
int aux;
while(i<=n && masa>=0)
{
//cout << "G[i][1]: " << G[i][1] << endl;
if(masa>=G[i][1]) {
//cout << "Am ajuns aici";
profit=profit+G[i][2];
masa=masa-G[i][1];
}
else {
//cout << "Am ajuns dincolo";
aux= (masa*100)/G[i][1];
profit = profit + (aux * G[i][2])/100;
break;
}
i++;
}
cout << profit;
}
int main()
{
read_data();
sorteaza();
verify();
// greedy();
}
Learn to index all your arrays from zero.
float G[100][3];
Legal indexes are 0 to 99 and 0 to 2. So this code should be
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 2; j++)
{
cin >> G[i][j];
}
if (G[i][0] != 0 && G[i][1] != 0)
{
G[i][2] = G[i][1] / G[i][0];
}
}
and this code should be
if (G[i][2] < G[i+1][2])
{
swap(G[i], G[i+1]);
}
All your arrays start at zero. I'm sure you've been told this, but you have to start putting it into practise.
In general, write your for loops like this
for (int i = 0; i < N; ++i)
That's the correct loop for an array of size N.
You probably need <n instead of ≤n (that's where the uninitialized value i.e. 0 comes from). And you miss one loop in the bubble sort. Right now you're only bubbling the smallest element to the end of the list.
Also no idea what you're doing with that schimb and while condition.
Furthermore you're defining G as float[100][3] so you can't use G[i][3], only G[i][2].
int sorteaza()
{
int i,j;
for (i=0; i<n; i++)
{
for (j=i+1; j<n; j++)
{
if (G[i][2] < G[j][2])
{
swap(G[i], G[j]);
}
}
}
}
bool sal_sk(int sal) // If sal is a composite figure, then true, if its not then false.
{
for (int i = 2; i <= sal; i++) {
if (sal%i == 0)
return true;
else
return false;
}
}
int lkd(int a, int b) // Checks the gcd
{
int c;
while (b > 0)
{
c = b;
b = a % b;
a = c;
}
return a;
}
int main()
{
int ok;
do
{
int n;//Number of elements
int*a; //Given number array variable
cout << "Put in the number of elements" << endl;
std::cin >> n;
a = new int[n];
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
std::cin >> *a;
}
int rez = a[0];
for (int i = 1; i < n; i++) {
if (sal_sk(a[i] == true))
rez = lkd(rez, a[i]);
}
delete[] a;
cout << "Composite figure gcd is " << rez << endl;
cout << " Do you want to continue(1) or to end (0)?" << endl;
cin >> ok;// Asks the user to enter in if he wants to continue or to end
} while (ok == 1);
}
Hey guys, I have two functions that check the greatest common divisor of composite figures in an array but it is random and idk why.
For Example if I enter 3 elements 4 6 9 it sometimes prints out that the gcd is 3 but sometimes it prints out that it is 1 which is correct but it happens at random times,and if I enter 9 4 6 it says that the gcd is 2, i just dont understand.Thanks in advance
bool sal_sk(int sal) returns true if sal is even false if it is not because you return always on the first iteration of your loop as #FrançoisAndrieux mentioned.
bool sal_sk(int sal) // If sal is a composite figure, then true, if its not then false.
{
for (int i = 2; i <= sal; i++) {
if (sal%i == 0)
return true;
else
return false;
}
}
To return true on composite make the following changes.
bool sal_sk(int sal) // If sal is a composite figure, then true, if its not then false.
{
for (int i = 2; i < sal; i++) {
if (sal%i == 0)
return true; // Has some factor other than 1 and itself
}
return false; // Does not have a factor other than 1 and itself
}
Another bug is here:
for (int i = 1; i < n; i++) {
if (sal_sk(a[i] == true))
rez = lkd(rez, a[i]);
}
This code passes true or false to sal_sk() depending on if a[i] !=0
Instead you want:
for (int i = 1; i < n; i++) {
if (sal_sk(a[i]))
rez = lkd(rez, a[i]);
}
This is also a bug:
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
std::cin >> *a; // This puts the value in a[0] always!
}
The code should be:
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
std::cin >> a[i]; // Put the value in the array at index i
}
The code here:
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
std::cin >> *a;
}
does not fill in all the array elements, but instead just overwrites the first entry.
This is far from the only problem with your code. In particular, sal_sk is not doing whatever you think its doing.
I had an idea to make a program that would take user input and make a... I am not quite sure how to call it correctly, so my WIP is top down pyramid. So that we don't get confused it should look something like this.
If c is 5:
11111
10001
10101
10001
11111
If c is 7:
0000000
0111110
0100010
0101010
0100010
0111110
0000000
Here is an image to help visualize the problem
The only conditions are that there has to be a 1 in the middle and that cin is odd.
Now, I've been thinking about it in my spare time and it seemed quite easy in my head, but when I try to put my thoughts into my code it never works out.
Is there anyone who could help me? I am quite desperate .-.
PS: Here is my WIP code so far (Please excuse my Czech ints and texts)
#include <iostream>
using namespace std;
void FillArray(int **PyramidArray,int a,int b,int c);
void ExtractArray(int **PyramidArray,int a,int b,int c);
int main()
{
cout << "input array size.(only odd numbers)" << endl;
int c;
cin >> c;
if (c%2 == 0)
{
cout << "Only odd numbers!" << endl;
return 1;
}
int **PyramidArray;
PyramidArray = new int*[c];
for (int i =0;i<c;i++)
{
PyramidArray[i] = new int[i];
}
FillArray(PyramidArray,c,c,c);
ExtractArray(PyramidArray,c,c,c);
return 0;
}
void FillArray(int **PyramidArray, int a, int b, int c)
{
for(int i=0;i<a;i++)
{
for (int j=0;j<b;j++)
{
PyramidArray [i][j] = 1;
}
}
}
void ExtractArray(int **PyramidArray, int a,int b,int c)
{
for(int i=0;i<a;i++)
{
for (int j=0;j<b;j++)
{
cout << PyramidArray [i][j] << " ";
}
cout << endl;
}
cout << endl;
}
For solving this kind of problem, if you find you need a new, then something is wrong. Just keep tracing the current state of printing, everything can be done in one-pass.
#include <iostream>
int main() {
int n = 0, half_n = 0;
std::cin >> n;
if ( n % 2 == 0 ) return -1;
half_n = n / 2;
// the first symbol to print, 1 or 0
int start_symbol = 1 - half_n % 2;
// how many steps from beginning require alternating symbol ?
int alternate_range = 0;
// 0 for upperhalf, 1 for lowerhalf
int direct = 0;
for ( int i = 0; i < n; ++i ) {
int current_symbol = start_symbol;
for ( int j = 0; j < alternate_range; ++j ) {
std::cout << current_symbol;
current_symbol = 1 - current_symbol;
}
for ( int j = alternate_range; j < n - alternate_range; ++j ) {
std::cout << current_symbol;
}
for ( int j = n - alternate_range; j < n; ++j ) {
current_symbol = 1 - current_symbol;
std::cout << current_symbol;
}
std::cout << "\n";
if ( alternate_range == half_n ) {
direct = 1;
}
if ( direct == 0 ) {
++alternate_range;
} else {
--alternate_range;
}
}
}
Ok, so I was doing a tiny project for school and I can't find the answer anywhere to why this small change in code makes it finish in no time when number m gets higher. Look at the variable "k" I change it from int to long.
I'm trying to find the longest sequence in the Collatz sequence between 1 and 1000000
void lengstaRuna() {
cout << "Hæsta tala?:";
int m;
cin >> m;
int lengstaRuna = 0;
int talaLengstuRunu = 0;
int k;
for(int i = 2; i < m; i++) {
int lengd = 1;
k = i;
while(k != 1) {
if(k % 2 == 0) {
k = k/2;
} else {
k = k*3 +1;
}
lengd++;
}
if(lengd > lengstaRuna) {
lengstaRuna = lengd;
talaLengstuRunu = i;
}
}
cout << "Lengsta runa: " << lengstaRuna << endl;
cout << "Tala lengstu runu: " << talaLengstuRunu << endl;
}
void lengstaRuna() {
cout << "Hæsta tala?:";
int m;
cin >> m;
int lengstaRuna = 0;
int talaLengstuRunu = 0;
long k;
for(int i = 2; i < m; i++) {
int lengd = 1;
k = i;
while(k != 1) {
if(k % 2 == 0) {
k = k/2;
} else {
k = k*3 +1;
}
lengd++;
}
if(lengd > lengstaRuna) {
lengstaRuna = lengd;
talaLengstuRunu = i;
}
}
cout << "Lengsta runa: " << lengstaRuna << endl;
cout << "Tala lengstu runu: " << talaLengstuRunu << endl;
}
The question is simple: Why does it run so much faster when input m==1000000?
I see what's happening here. Basically, above certain value for your input, the int is overflowing since you are doing k*3.
I modified your code to check this (see below). Upto input value of around 113000, the max your 'k' has to hold is 1570824735 (close to INT_MAX 2147483647). Anything 114000 or above, 'k' overflows and the code goes into uncharted territory. That problem doesn't happen when you use long of course.
./a.out 113000
j: 1570824735
Lengsta runa: 354
Tala lengstu runu: 106239
#include <iostream>
#include <string>
using namespace std;
void lengstaRuna(int m) {
int lengstaRuna = 0;
int talaLengstuRunu = 0;
int k;
long j = 0;
for(int i = 2; i < m; i++) {
int lengd = 1;
k = i;
while(k != 1) {
if(k % 2 == 0) {
k = k/2;
} else {
if (k*3 > j)
j = k*3;
k = k*3 +1;
}
lengd++;
}
if(lengd > lengstaRuna) {
lengstaRuna = lengd;
talaLengstuRunu = i;
}
}
cout << "j: " << j << endl;
cout << "Lengsta runa: " << lengstaRuna << endl;
cout << "Tala lengstu runu: " << talaLengstuRunu << endl;
}
int main (int ac, char** av) {
std::string::size_type sz;
lengstaRuna(std::stoi(av[1]));
}
Can someone please help me. I am struggling to find in my code why the last value in column B always gets incremented by one. I have written some code since its an assignment due today. I also cant figure out why the last value in column B is not equal to 196 because in the reset function it sets all the values in the array to 196 . Any suggestion would be appreciated. Thank you in advance
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main() { //main starts
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats) {
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
if (i == 0) {
cout << " " << static_cast<char>(j + 65) << " ";
} else {
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++) {
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats) {
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS))) {
if (seats[(seatChoiceNumber)][(letter - 65)] == 82) {
} else {
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats) {
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats) {
printAllSeats(seats);
anyFreeSeats = false;
}
}
} else {
}
}
I kind of cleaned up the code a bit. It seems you found your answer in the comments, so I just did some indentation. Try and eliminate whitespaces in your code (mind you, the one I am putting here is not perfect either, but you get the point). Clean and easy to read code doesn't only make it better for you, but as you get higher up in the industry and other people begin reading and working on your code, having clean and easy to read code really helps :)
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main()
{
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats)
{
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++)
{
if (i == 0)
{
cout << " " << static_cast<char>(j + 65) << " ";
}
else
{
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++)
{
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats)
{
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS)))
{
if (seats[(seatChoiceNumber)][(letter - 65)] == 82)
{
}
else
{
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats)
{
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats)
{
printAllSeats(seats);
anyFreeSeats = false;
}
}
}
else {
}
}
Note: Some more whitespaces could even come out but I generally like to have spaces after certain statements (personal preference).