Parallelogram with 3 loops with c++ - c++

Its my first year in computer science and I'm having some trouble with this problem.
The instructor asks to write a code for a parallelogram:
Enter number of rows: 13
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
...with forced odd entry (like 4 changing to 5). rules are - I can't use stew - must draw shape using only 3 loops - plus one loop for forced entry (while r is between 3 and 23) - must use the total number of rows or current row for all calculations (can't use previous row and can't generate your own numbers)
int main() {
int control = 0;
int i = 0, j = 0, k = 0, l = 0;
int r = 0, c = 0, crntRow = 0, crntRow2 = 0,
cuur_8r = 0, space = 0, star = 0;
char a = '-', b = '+';
//cin >> r;
r = 11;
if (!(r % 2))
r++;
c = 0;
//cout << c << r;
for (i = 0; i < r; i++)
{
space = r / 2;
star = r / 2;
crntRow = i;
while (crntRow > space)
{
space++;
cout << a;
}
//cout << j;
for (int j = 0; j < c; j++)
{
if (star > j)
{
cout << b;
}
}
c++;
cout << '\n';
}
}
TLDR: this is the terrible code I came up with so far, I don't know how to shrink the number of stars after rows/2

The three loops that your instructor was referring to are:
An outer loop over the lines
A loop to prefix spaces to each line (0 spaces for the first half)
A loop to print stars on each line (this is always non-zero)
Here is a very stripped-down example:
int i, j, k, sp, st;
int r = 11;
// 1. An outer loop over the lines
for (i = 0; i < r; i++)
{
if(i <= r/2) {
sp = 0; // No spaces in the first half
st = i + 1; // Increasing stars in the first half
} else {
sp = i - r / 2; // Increasing spaces in the second half
st = r - i; // Decreasing stars in the second half
}
// 2. A loop to prefix spaces to each line (0 spaces for the first half)
for(j = 0; j < sp; j++) cout << ' ';
// 3. A loop to print stars on each line (this is always non-zero)
for(k = 0; k < st; k++) cout << '*';
cout << '\n';
}
As an excercise, you can do the same thing in two loops:
An outer loop over the lines
An inner loop over the character in each line
In this case, you would have to select which character to print during each iteration of the inner loop.

Related

print two diamonds side by side

I wanna print two diamonds side by side, but my code prints 1 diamond.
I spent lots of time on it and really do not know what else to do.
Any help would be appreciated.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;
middle = (n - 1) / 2;
for ( i = 0; i < n; i++)
{
spaceCount = abs(middle - i);
starCount = n - 2 * abs(middle - i);
for ( j = 0; j < spaceCount; j++)
cout << " ";
for (j = 0; j < starCount; j++)
cout << "*";
for (j = 0; j < spaceCount; j++)
cout << " ";
cout << endl;
}
}
input = odd numbers
desired output =
* *
*** ***
**********
*** ***
* *
You forgot to print you second diamond shape. Each iteration, you should print first a few spaces, then the stars, then the double of the spaces (to finish the first diamond and set spaces for the second diamond) and then you should draw you stars for your second diamond.
This is an example of code that prints two diamonds:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;
middle = (n - 1) / 2;
for ( i = 0; i < n; i++)
{
spaceCount = abs(middle - i);
starCount = n - 2 * abs(middle - i);
// print a row of the first diamonds
// spaces at the beginning
for ( j = 0; j < spaceCount; j++)
cout << " ";
// the stars itself
for (j = 0; j < starCount; j++)
cout << "*";
// finish the rectangle of the first diamond
for (j = 0; j < spaceCount; j++)
cout << " ";
// print a row of the second diamond
// spaces at the beginning
for (j = 0; j < spaceCount; j++)
cout << " ";
// the stars itself
for (j = 0; j < starCount; j++)
cout << "*";
// spaces at the end are not necessarily required for the last diamond
cout << endl;
}
}
It would be even better to create a function to print one diamond (row) and call this function two times (this prevents duplicate code).

Determine the number of columns that do not contain a single letter 'K' in a three-dimensional array of characters

I have a task I cannot figure out how to solve. The problem is:
Form an array of type char with a size of 2x3x4 elements.
Initialize it with random characters from A to Z.
Determine the number of columns that do not contain a single letter 'K' (total columns 2 * 4).
Use pointers.
I started, but I don’t know how to count the number of columns that do not contain the letter K.
const int N = 2, M = 3, K = 4;
char a[N][M][K];
char *pa = &a[0][0][0];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < K; k++) {
*pa = 'A' + rand() % 26;
cout << *(pa++) << ' ';
}
cout << endl;
}
cout << endl;
}
First of all, at least in my opinion, this is a bit too broad of a question, but I'm not a mod so I'll just stick to answering:
int count = 0;
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) { // checks every column
bool hasK = false; //--
for(char k: a[i][j]) {// |
if(k == 'k') { // |
hasK = true; // |_checks if the column
break; // | contains the char 'k'
} // |
} // |
if(!hasK) count++; //--
}
}

Can't find path in Minimum Cost Path using Dynamic Programming

The algorithm is supposed to find the minimum cost path in NxN matrix given as an input. The starting cell is always left bottom and the destination is right top.
Each cell of the matrix represents a cost to traverse through that cell.
You can only move up and right.
I have managed to find the cost, however, I still struggle to backtrack the path.
I tried to start from top right cell and use the greedy algorithm to find my "way back", but the output was either completely wrong or skipping random columns/rows. I also tried to keep track of decisions I was making by creating an additional matrix, but I always end up stuck in the loop.
So how do I find the path?
Here's the code that works well (counts the cost and that's it):
#include <iostream>
using namespace std;
int main()
{
int tab[101][101], N, cost[101][101], backtrack[101][101];
cout << "N (size of NxN matrix) :" << endl;
cin >> N;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
cin >> tab[i][j];
cost[i][j] = 0;
backtrack[i][j] = 0;
}
}
cost[N-1][0] = tab[N-1][0];
int a = N-1;
for(int i = N-2; i >= 0; i--) // column 0 can be chosen only in 1 way
{
cost[i][0] = cost[i+1][0] + tab[i][0];
backtrack[i][0] = 4; // came from down
}
for(int j = 1; j < N; j++) // row N-1 can be chosen only in 1 way
{
cost[a][j] = cost[a][j-1] + tab[a][j];
backtrack[a][j] = 3; // came from right
}
for(int i = N-2; i >= 0; i--)
{
for(int j = 1; j < N; j++)
{
if(cost[i][j-1] <= cost[i+1][j])
{
cost[i][j] = tab[i][j] + cost[i][j-1];
backtrack[i][j] = 3;
}
else
{
cost[i][j] = tab[i][j]+cost[i+1][j];
backtrack[i][j] = 4;
}
}
}
cout << "Cost: " << cost[0][a] << endl;
return 0;
}
Now, here's the function with flawed additional matrix that's supposed to give me the path, but ends up in an infinite loop:
(matrix backtrack from previous code was given as track here)
void path(int track[101][101], int N)
{
int help[101][101];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
help[i][j] = 0;
}
int w = 0, k = N-1;
help[w][k] = 1; // top right argument is included in the output
while(w < N || k >= 0)
{
if(track[w][k] == 3)
{
help[w][k-1] = 1; // 3 means I came from the previous column k-1
k--;
}
else if(track[w][k] == 4)
{
help[w+1][k] = 1; //4 means I came from the previous row w+1
w++;
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(help[i][j] != 0)
cout << i << " " << j << endl;
}
}
}
Example input:
5
2 3 4 2 5
5 2 1 2 2
2 4 2 2 3
1 2 2 4 3
3 2 1 2 3
Expected output:
Cost: 20
4 0
4 1
4 2
3 2
2 2
1 2
1 3
0 3
0 4
Actual output
Cost: 20
And no path at all since it ends up in an infinite loop.
You have written the while loop in path() incorrectly:
while(w < N || k >= 0)
...
You intend this loop to continue until w = N-1 and k=0, which it does, but the loop doesn't terminate there, it just runs in place. (You could see this yourself by adding cout << w << " " << k << endl; to the loop.) The conditional I think you want is:
while(w < N-1 || k > 0)

How to print diamond shape w/ c++

I need help, I created a short little program a while ago where it would print a simple pyramid with "*" like this:
*
***
*****
but I decided to challenge myself and see if I could create a simple diamond shape like this:
*
***
*****
***
*
Here is my code so far.
I should also add that the value you input, for example 5, determines how big the diamond is.
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value; i++) {
int number = 0;
number+= 2;
//print spaces v v v
for (int x = 0; x < (value - value + i + 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (/*ATTENTION: What do I do here? Plz help*/); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
What I've been trying to do is figure out what to put inside the parenthesis where it says (//ATTENTION). I've been working for at least an hour trying to do random things, and one time it worked when I input 4, but not for 5, and it's just been very hard. This is key to building the diamond, try putting in just value and compile to see what happens. I need it to be symmetrical.
I need to know what to put inside the parenthesis please. I'm sorry this is very long but the help would be appreciated thanks.
I also apologize if my code is messy and hard to read.
int number = 0; and number+= 2;
value - value inside for (int x = 0; x < (value - value + i + 1); x++) {
are not required.
Inside the parenthesis, you can use
2*(value-i-1)-1
However, I would suggest you to first analyze the problem and then try to solve it instead of trying random things. For instance, let's consider the cases of even and odd inputs i.e., 2 and 3.
Even Case (2)
*
***
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 1 1
1 0 3
2 0 3
3 1 1
For row index < value
Number of Spaces = value - row index - 1
Number of Stars = 2 * row index + 1
For row index >=value
The number of spaces and stars are simply reversed. In the odd cases, the situation is similar too with a small exception.
Odd Case (3)
*
***
*****
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 2 1
1 1 3
2 0 5
3 1 3
4 2 1
The small exception is that while reversing, we have to ignore the row index = value.
Now, if we put the above analysis in code we get the solution
//Define the Print Function
void PrintDiamond(int rowIndex, int value)
{
//print spaces v v v
for (int x = 0; x < value - rowIndex -1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < 2 * rowIndex + 1; y++) {
cout << "*";
}
cout << endl;
}
And then inside main
//Row index < value
for (int i = 0; i < value; i++) {
PrintDiamond(i,value);
}
//For row index >= value reversing the above case
//value-(value%2)-1 subtracts 1 for even and 2 for odd cases
//ignore the row index = value in odd cases
for (int i = value-(value%2)-1; i >=0; i--) {
PrintDiamond(i,value);
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value-1; i++) {
// int number = 0;
// number+= 2;
// //print spaces v v v
for (int x = 0; x < i+1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2*(value-1-i)-1); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
I hope that you will get this .Also in the second for loop you were iterating it one extra time by iterating the loop upto value. But since the pyramid is symmetric so the no of rows in the pyramid will be 2*value-1.So I in the second loop i should vary upto value -1.
This code should resolve the problem:
#include <sstream>
using namespace std;
void printSpaces(int howMany) {
for(int i = 0; i < howMany; i++) cout << " ";
}
void figure(int size) {
bool oddSize = size % 2 == 1;
int center = size / 2;
int spaces = size / 2;
// If figure is of an odd size adjust center
if (oddSize) {
center++;
} else { // Else if figure is of even size adjust spaces
spaces--;
}
for (int i = 1; i <= center; i++) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++) cout << "*";
cout << endl;
spaces--;
}
spaces = oddSize ? 1 : 0; // If the figure's size is odd number adjust spaces to 1
center -= oddSize ? 1 : 0; // Adjust center if it's an odd size figure
for(int i = center; i >= 1; i--) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++)
cout << "*";
cout << endl;
spaces++;
}
}
int main() {
int value = 0;
while(value < 3) {
cout << "Please enter in a value (>= 3): ";
cin >> value;
cout << endl;
}
figure(value);
return 0;
}

previous row element of array also getting updated

in this program i am separating integers from a character array which consists of a space between them
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
int main()
{
int i = 0, t, l = 0, j, c, k, q = 0, num = 0;
char ch[10][10];
int ach[10][1];
cout << "enter the number of test cases";
cin >> t;
for (i = 0; i < t; i++)
{
fflush(stdin);
cin.getline(ch[i], 9);
}
for (i = 0; i < t; i++)
{
num = 0;
for (j = 0; ch[i][j] != '\0'; j++) //calculating length
{
l = j;
}
l = l + 1;
for (j = 0; j < l; j++)
{
if (ch[i][j] == ' ') //finding the space
c = j;
}
for (k = 0; k < c; k++) //taking first integer out of char array
{
q = ch[i][k] - 48; //parsing char to int
num = (num * 10) + q;
}
cout << "\n previous row element " << ach[0][1] << "\n"; //checking the value
ach[i][0] = num; // this statement is updating the previous row's last element of the array
cout << "\n previous row element " << ach[0][1] << "\n"; //checking the value
cout << ach[i][0];
num = 0;
q = 0;
for (k = c + 1; k < l; k++) //taking second element out of char array
{
q = ch[i][k] - 48; //parsing char to int
num = (num * 10) + q;
}
ach[i][1] = num;
cout << ach[i][1];
}
for (i = 0; i < t; i++)
{
cout << "\n" << ach[i][0] << "\t" << ach[i][1] << "\n"; //displaying the values
}
getch();
return 0;
}
I have marked the code that is malfunctioning , it is updating the previous row's last element. please help.
Oups your code is not really optimized and is mainly C with the exception of cin.getline. But your real problem is that with int ach[10][1], ach is a 2D array of size 10x1, so ach[i][1] may not be what you expect because you should define int ach[10][2] to safely use it. The rules for array indexes computing give &(ach[i][1]) == &ach[0][0] + i*1 + 1 so you are actually accessing ach[i+1][0] with a possible past end array access if i is 9.
Moreover, at first access, ach[0][1] is used without being first initialized.
So your ach definition should be:
int ach[10][2] = {0};