C++ TopDown Pyramid - c++

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;
}
}
}

Related

My bubble sort is introducing values of '0' in array

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]);
}
}
}
}

Printing half pyramid

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;
}
}

C++ generating a pascal triangle, wrong output

I have a problem with generating a pascal triangle in c++, same algorithm works good in java and in c++ it only works for the first two numbers of every line of the triangle in any other it generates way to big numbers. For example in java it generates:
1 5 10 10 5 1 and in C++: 1 5 1233124 1241241585 32523523500 etc
Here is code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Pascal {
private:
int* tab;
int prev1;
int prev2;
public:
Pascal(int n) {
tab = new int[n+1];
prev1=0;
prev2=0;
for(int i = 0; i <= n; i++) {
for(int k = 0; k <= i; k++) {
if (k == 0) {
tab[k] = 1;
prev2 = 1;
} else {
prev1 = tab[k-1] + tab[k];
tab[k-1] = prev2;
prev2 = prev1;
}
}
}
}
int wspolczynnik(int m) {
return tab[m];
}
};
int main (int argc, char* argv[]) {
int n = 0, m = 0;
n = atoi(argv[1]); // konwersja string na int
if (n >= 0)
for (int i = 2; i < argc; i++) {
Pascal *wiersz = new Pascal(n);
m = atoi(argv[i]);
int result = wiersz->wspolczynnik(m);
if (m < 0 || m > n)
cout << m << " - element poza zakresem" << endl;
else
cout << m << " : " << result << endl;
delete[] wiersz;
}
return 0;
}
See if initializing the tab array helps:
tab = new int[n+1]();

Add two large number using array

sorry for my english.
I wrote a programm that add two large numbers.
Number 1 is read from file data1.in, the same with the second, data2.in.
The problem was when I tryed to add 68925579999999999990+79925579999999999990 I am getting the wrong result:48951159999999999980.
using python to add these numbers I have got 148851159999999999980.
Where I got wrong ??
#include <iostream.h>
#include <fstream.h>
#include<math.h>
int n = 0;
int m = 0;
const int zerou = 9000;
//using namespace std;
void zero(int*a)
{
for(int i = 0;i<zerou;i++)
a[i]=0;
}
void zero(int*a,int*b)
{
for(int i = 0;i<zerou;i++)
a[i]=b[i]=0;
}
void rebuild(int* a)
{
int temp[9000];
zero(temp);
int i;
int delta = abs(m-n)+1;//k1 -dim a
for(i = delta;i<n+delta;i++)
{
temp[i] = a[i-delta];
a[i-delta] = 0;
}
n += delta;
for(i =0;i<n;i++)
a[i] = temp[i];
}
void rebuildS(int* a)
{
int temp[9000];
zero(temp);
int i;
int delta = abs(m-n)+1;//k1 -dim a
for(i = delta;i<m+delta;i++)
{
temp[i] = a[i-delta];
}
m += delta;
for(i =0;i<m;i++)
a[i] = temp[i];
}
void citirea(int* ar){
ifstream f;
f.open("data1.in");
int data;
while (f>>data){
ar[n++] = data;
}
f.close();
}
void citirea_(int* ar){
ifstream f("data2.in");
int data;
while (f>>data){
ar[m++] = data;
}
f.close();
}
/*
void perDig(int*a, int*b,int *t,int i)
{
*t += (a[i]+b[i])/10;
a[i] = (a[i]+b[i]+*t)%10;
}*/
void adunarea(int*a, int* b)
{
int transport = 0;
int sum;
for(int i = n;i>=0;i--)
{
sum = a[i]+b[i]+transport;
//sum = perDig(a,b,transport,i);
if(sum >9)
{
transport = (sum)/10;
sum %=10;
}
a[i] = sum;
}
}
int main()
{
int a[9000],b[9000];
zero(a,b);
citirea(a);
citirea_(b);
if(n > m)
rebuildS(b);
else if(m > n)
rebuild(a);
adunarea(a,b);
ofstream rez;
rez.open("data.out");
for(int i = 0;i<m;i++)
{
rez<<a[i]<<" ";
}
rez.close();
cin.get();
return 0;
}
There are two errors in your answer.
if (!sum > 9) you don't zero the transport, so numbers carry forever.
You are going to be one digit short because you add N digits and don't account for an Nth+1 digit in the answer if transport != 0.
You have a bigger problem, though. You don't know how to debug the code yourself. If in doubt, make the program tell you each step it performs, and read what it says, and see where it goes wrong. Knowing how to do this is worth more than 10 correct versions of your code.
void adunarea(int*a, int* b)
{
int transport = 0;
int sum;
for(int i = n;i>=0;i--)
{
cout << a[i] << " + " << b[i] << " + " << transport << endl;
sum = a[i]+b[i]+transport;
if(sum >9)
{
transport = (sum)/10;
sum %=10;
}
a[i] = sum;
cout << " = " << sum << " ( " << transport << " )" << endl;
}
}
All I've done is add two print statements, and with that and the correct answer you can look for the first incorrect digit and see absolutely everything about how the wrong digit was made, and then fix it.

Coin Change C++

I've tried to solve a coin change problem in such a way that it'll compute the minimum numbers of coins that can be used. I've used the algorithm post on http://www.algorithmist.com. Here's the algorithm:
C(N,m) = min(C(N,m - 1),C(N - Sm,m) + 1)
with the base cases:
C(N,m) = 1,N = 0
C(N,m) = 0,N < 0
C(N, m) = 0, N >= 1, m <= 0
But when I write the code it run to infinity.
Here's the code:
#include <iostream>
#include <algorithm>
using namespace std;
int Types[101];
int Coins(int N, int m)
{
if(N==0)
{
return 1;
}
else if(N<0)
{
return 0;
}
else if(N>0 && m<=0)
{
return 0;
}
else
{
int a = Coins(N,m-1);
int b = Coins(N-Types[m],m) + 1;
int c = min(a,b);
return c;
}
}
int main()
{
int noOfCoins, Target;
cin >> noOfCoins >> Target;
for(int i = 0; i<noOfCoins; i++)
{
cin >> Types[i];
}
cout << Coins(Target, noOfCoins);
return 0;
}
What can be wrong?
It should be cout << Coins(Target, noOfCoins - 1);
instead of cout << Coins(Target, noOfCoins);
Otherwise you are accessing a 0 element, and go to the same state again and again here:
int b = Coins(N-Types[m],m) + 1;