Need to scroll this. Dont know how to do it - c++

This is the code:
for (i = 0; i < max; i++) { //RIGHT-SHIFT
cout << " "; //blank space before padding
cout << "***"; //left padding
for (j = i; j >= 0; j--) { // this prints one more * than the last line (left side)
cout << "**";
}
for (k = 0; k < width; k++) { // print the white space
cout << " ";
}
for (j = max - i; j > 0; j--) { // this prints one less than the last line (right side)
cout << "**";
}
cout << "***\n";//right padding
}
for (i = max; i > 0; i--) { //LEFT-SHIFT
cout << " "; //blank space before padding
cout << "***"; //left padding
for (j = i; j >= 0; j--) { // this prints one more * than the last line (left side)
cout << "**";
}
for (k = 0; k < width; k++) { // print the white space
cout << " ";
}
for (j = max - i; j > 0; j--) { // this prints one less than the last line (right side)
cout << "**";
}
cout << "***\n";//right padding
}
It looks something like
I want the first row to have 4 - 19 instead of 5- 20. How do i change it?

Print out one fewer asterisk in your initial cout << "***" in each outer loop. ie. do cout << "**" instead for your "left padding". That will make all the rows narrower by one asterisk, but will preserve the overall pattern, which is what I presume you wanted.

Related

C++ print a reverse heart shape

enter image description here
Hello everyone, I am trying to print a reverse heart shape in c++. While user will input a integer, and the program will display the reverse heart according to the value.
in the photo, the input value are 1,3,4
Now my code can only show the triangle but still missing the two small triangle, so hoping can get some guide here.
int size,space;
cout << "Input size: ";
cin >> size;
int rows = (size * 2)+1;
for (int i = 1, k = 0; i <= rows; ++i, k = 0)
{
for (space = 1; space <= rows - i; ++space)
{
cout << " ";
}
while (k != 2 * i - 1)
{
cout << "* ";
++k;
}
cout << endl;
}
{
for (int i = size; i >= 1; --i)
{
for (int space = 0; space < size - i; ++space)
cout << " ";
for (int j = i; j <= 2 * i - 1; ++j)
cout << "* ";
for (int j = 0; j < i - 1; ++j)
cout << "* ";
cout << endl;
}
}
I try to print a Inverted first, but I fail to print two at the same time.

Difference of 2 sets retained in arrays - C++

Consider two sets retained in two arrays. Find the union, intersection and difference (relative complement) of the two sets.
I managed to solve the union and the intersection, but the difference is giving me a hard time. Any hints? And if possible, keep it as simple as possible, without functions or more complex aspects, because I'm a beginner and I still have a lot to learn.
Thank you in advance!
#include <iostream>
using namespace std;
int main()
{
int v1[100], v2[100], u[200], intersection[100], d[100];
unsigned int v1_length, v2_length, i, j, OK = 0, union_length;
cout << "Enter the number of elements of the first array:" << " ";
cin >> v1_length;
cout << "Enter the elements of the first array:" << '\n';
for (i = 0; i < v1_length; i++)
cin >> v1[i];
cout << "Enter the number of elements of the second array:" << " ";
cin >> v2_length;
cout << "Enter the elements of the second array:" << '\n';
for (i = 0; i < v2_length; i++)
cin >> v2[i];
//Union
union_length = v1_length;
for (i = 0; i < v1_length; i++)
u[i] = v1[i];
for (i = 0; i < v2_length; i++)
{
int ok = 0;
for (j = 0; !ok && j < v1_length; j++)
if (v1[j] == v2[i])
ok = 1;
if (!ok)
{
u[union_length] = v2[i];
union_length++;
}
}
cout << "The union of the two sets contained in the arrays is: ";
for (i = 0; i < union_length; i++)
cout << u[i] << " ";
cout << '\n';
//Intersection
unsigned int k = 0;
cout << "The intersection of the two sets contained in the arrays is: ";
for (i = 0; i < v1_length; i++)
for (j = 0; j < v2_length; j++)
if (v1[i] == v2[j])
{
intersection[k] = v1[i];
k++;
}
for (i = 0; i < k; i++)
cout << intersection[i] << " ";
cout << '\n';
//Difference
unsigned int l = 0, OK2 = 0;
cout << "The difference of the two sets contained in the arrays is: ";
for (i = 0; i < v1_length; i++)
{
for (j = 0; j < v2_length; j++)
{
if (v1[i] == v2[j])
OK2 = 1;
if (!OK2)
{
d[l] = v1[i];
l++;
}
}
}
for (i = 0; i < l; i++)
cout << d[i] << " ";
cout << '\n';
return 0;
}
It seems that the intersection is the best place to start. You want the items that only in appear in one of the two arrays, right?
So, for the inner loop, you need to compare all the elements. Then, if no match was found, you have the a unique element.
You need to add the curly braces {} to the for loop. I know that curly braces are distracting at times, but over time, you will probably find it safer to almost always include them to avoid confusion.
for (i = 0; i < v1_length; i++)
for (j = 0; j < v2_length; j++) {
if (v1[i] == v2[j]){
break; // this item is not unique
} else if(j == v2_length - 1){
d[l] = v1[i]; // This is the unique one, add it to the answer array
l++;
}
}
for (i = 0; i < l; i++)
cout << intersection[l] << " ";
cout << '\n';
You're on the right track!
You're doing a few things wrong. Here are some fixes you can try:
Only set OK2 to 0 once per inner-loop
Reset OK2 to 0 at the end of the inner-loop
Only do the insertion into d after the inner-loop has completed
As an optimization, consider breaking after you set OK2 to 1, as you know at that point it can never be set to 0 for the current value pointed to by the outer-loop.

Solving Knapsack using recursive algorithm

So, I am trying to implement this algorithm from our textbook.
I wrote this :
// Knapsack_memoryfunc.cpp : Defines the entry point for the console application.
//Solving Knapsack problem using dynamic programmig and Memory function
#include "stdafx.h"
#include "iostream"
#include "iomanip"
using namespace std;
int table[20][20] = { 0 };
int value, n, wt[20], val[20], max_wt;
// ---CONCERNED FUNCTION-----
int MNSack(int i, int j)
{
value = 0;
if (table[i][j] < 0)
if (j < wt[i])
value = MNSack(i - 1, j);
else
value = fmax(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));
table[i][j] = value;
return table[i][j];
}
// --------------------------
void items_picked(int n, int max_wt)
{
cout << "\n Items picked : " << endl;
while (n > 0)
{
if (table[n][max_wt] == table[n - 1][max_wt]) // if value doesnot change in table column-wise, item isn't selected
n--; // n-- goes to next item
else // if it changes, it is selected
{
cout << " Item " << n << endl;
max_wt -= wt[n]; // removing weight from total available (max_wt)
n--; // next item
}
}
}
int main()
{
cout << " Enter the number of items : ";
cin >> n;
cout << " Enter the Maximum weight : ";
cin >> max_wt;
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << " Enter weight and value of item " << i << " : ";
cin >> wt[i] >> val[i];
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= max_wt; j++)
table[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= max_wt; j++)
table[i][j] = -1;
cout << " Optimum value : " << MNSack(n, max_wt);
cout << " \n Table : \n";
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= max_wt; j++)
if (table[i][j] == -1)
cout << setw(5) << "-";
else
cout << setw(5) << table[i][j];
cout << endl;
}
items_picked(n, max_wt);
return 0;
}
Here is the question and output :
It seems like its correct on some places like optimum value, yet isn't fully acceptable.
I've tried to debug it, but its quite hard with recursive functions. Can someone please help?
int MNSack(int i, int j)
{
value = 0;
if (table[i][j] < 0)
{
if (j < wt[i])
value = MNSack(i - 1, j);
else
value = max(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));
table[i][j] = value;
}
return table[i][j];
}
The problem comes in here. When your table item is greater or equal to 0, you will skip the recursion but still set the table item to 0, which won't be right if your table item is greater than 0.
You only need to update the table item when it needs to be change, so put it in the braces will correct this.
The bottom up solution.
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
int table[20][20] = { 0 };
int value, n, wt[20], val[20], max_wt;
cout << " Enter the number of items : ";
cin >> n;
cout << " Enter the Maximum weight : ";
cin >> max_wt;
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << " Enter weight and value of item " << i << " : ";
cin >> wt[i] >> val[i];
}
// Initialization
for (int i = 0; i <= n; i++)
for (int j = 0; j <= max_wt; j++)
table[i][j] = 0;
// In practice, this can be skipped in a bottom up solution
for (int i = 1; i <= n; i++)
for (int j = 1; j <= max_wt; j++)
table[i][j] = -1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= max_wt; j++)
{
if (j < wt[i])
table[i][j] = table[i - 1][j];
else
table[i][j] = max(table[i - 1][j], val[i] + table[i - 1][j - wt[i]]);
}
}
cout << " Optimum value : " << table[n][max_wt] << endl;
cout << " \n Table : \n";
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= max_wt; j++)
if (table[i][j] == -1)
cout << setw(5) << "-";
else
cout << setw(5) << table[i][j];
cout << endl;
}
return 0;
}
You can see that this changes the recursion to a loop, and therefore avoids the global variables. It also makes the code simpler, so that you can avoid checking if the table item is valid (equal to -1 in your example).
The drawback of this solution is, it always traverses all the possible nodes. But it gains better coefficient per item because the recursion and double checking the table item costs more. Both top-down and bottom-up have the same order of complexity O(n^2), and it's hard to tell which one is faster.

Printin a X with *

i wanna print a X with * , i have done the left side of the X but i don't know how to print the other side (flip/mirror) .
if you run this codes it will print just the left side of (X) and now i wanna print the right side of (X) ? so what should i do to complete the (X) using stars(*)? thank you guys.
i was wondering is it possible to do this?(i'm a newbie to programming)
#include <iostream>
// Expected output pattern:
//
// * *
// * *
// * *
// *
// * *
// * *
// * *
using namespace std;
int main() {
cout << "Printing X with star(*)" << endl;
cout << endl;
int i;
int p;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
return 0;
}
You're on the right track, to do the right hand side you have to print more **** on each line in addition to what you already have done. It might help to think of printing each line of the X as printing some **** then some spaces then more **** and reduce the number of spaces each time you get closer to the cross-over point. Does that make sense? This might help get you further (. = space):
*......*
.*....*
..*..*
...**
and so on
This is one of many ways you could get there:
int main()
{
int size = 8;
int spacesBefore;
int spacesBetween = size;
int numStars = 1;
// Top half:
int i, j;
for ( i = 0; i < size/2; i++ ) {
spacesBetween = size - ( 2 * ( i + 1 ) );
spacesBefore = i;
for ( j = 0; j < spacesBefore; j++ ) // before
cout << " ";
for ( j = 0; j < numStars; j++ ) // * left
cout << "*";
for ( j = 0; j < spacesBetween; j++ ) // between
cout << " ";
for ( j = 0; j < numStars; j++ ) // * right
cout << "*";
cout << endl;
}
// bottom half, do the same kind of thing but changing the spacings
// ...
}
ok thank you every one that helped me , i found the answer i wanted after almost 6 hours and here is the answer:
#include <iostream>
using namespace std;
int main() {
cout << "Printing X with stars" << endl;
cout << endl;
int i;
int p;
int k;
int s;
int count = 72;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++){
cout << " ";
}
count-=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
}
count = 0;
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++) {
cout << " ";
}
count +=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
if (count == 80) break;
}
return 0;
}

Draw A Rectangle With Asterisks

I am trying to write a C++ Program to display a rectangle drawn in asterisks. I have the program running properly except for the fact that only one side of the heights of my rectangles print. Here is the code I have currently written for the display rectangle method.
void Rectangle::displayRectangle()
{
int i=0, j=0;
for (int i = 0; i < width; i++)
{
cout << "*";
}
cout << endl;
for (int i = 0; i < height - 2; i++)
{
cout << "*";
for (int j = 0; j < width; j++)
{
cout << " ";
}
cout << endl;
}
for (int i = 0; i < width; i++)
{
cout << "*";
}
cout << endl;
}
Specify a width and height at the start then you only need 3 loops. The first will print the top line of the rectangle. The second will print both sides of the rectangle (minus the very top and very bottom of the sides). The third will print the bottom line of the rectangle.
Like so
// Width and height must both be at least 2
unsigned int width = 7; // Example value
unsigned int height = 5; // Example value
// Print top row
for(unsigned int i = 0; i < width; i++);
{
std::cout << "*";
}
std::cout << std::endl;
// Print sides
for(unsigned int i = 0; i < height - 2; i++)
{
std::cout << std::setw(width - 1) << std::left << "*";
std::cout << "*" << std::endl;
}
// Print bottom row
for(unsigned int i = 0; i < width; i++)
{
std::cout << "*";
}
std::endl;
You will need to include both iostream and iomanip for this to work (setw is part of iomanip).
The top and bottom rows could also be done using the method to fill spaces with a given character, but I cannot recall that method right now.
This can be done much easier and clearer.
The logic here is to draw from line to line, so you only need one loop
(I chose to use the auto specifier in this example because I think it looks neater and used often in modern c++, if your compiler doesn't support c++11, use char, int etc.):
int main()
{
using namespace std;
auto star = '*';
auto space = ' ';
auto width = 20;
auto height = 5;
auto space_cnt = width-2;
for (int i{0}; i != height+1; ++i) {
// if 'i' is the first line or the last line, print stars all the way.
if (i == 0 || i == height)
cout << string(width, star) << endl;
else // print [star, space, star]
cout << star << string(space_cnt, space) << star << endl;
}
}
Well, you don't see the second vertical line, because you don't draw it in your line loop.
void DrawRect(int w, int h, char c)
{
cout << string(w, c) << '\n';
for (int y = 1; y < h - 1; ++y)
cout << c << string(w - 2, ' ') << c << '\n';
cout << string(w, c) << '\n';
}
Try to prompt the user for the number of rows and columns. Then, using nested loops, display a rectangle of stars based on the user input.