C++: How do I make this shape from this code? - c++

I am trying to make this shape from the code below. I'm confused as to how to make it print the 2nd row, second to last star without it skipping and printing the extra space before printing the star. Once that is figured out would the bottom half, when the stars expands back out, would the code be similar to the top half? I have tried a couple combinations of code between c and r but I have been stuck with what I currently.
---------------------- //row 0
* *| //row 1
* * * *| //row 2
* * * * * *|
* * * * * * * *|
* * * * * * * * * *|
* * * * * * * * * * *|
* * * * * * * * * *|
* * * * * * * *|
* * * * * *|
* * * *|
* *|
----------------------
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a)
{
cin >> num;
if (num < 38 && num > 0 && num % 2 == 1)
{
cout << "Thank you!" << endl << endl;
for (int r = 0; r < num; ++r) //outer loop/rows
{
for (int c = 0; c < num; ++c) //inner loop/columns
{
if (r == 0) cout << "--"; //top of square
else if (c >= r + r - c && c < num - 1)
cout << " ";
//else if (c == num - 1) cout << "*|";
else if (r == num - 1) cout << "--"; //bottom of square
else if (c == num - 1) cout << "*|"; //right side of square
else if (r > c) cout << "* ";
}
cout << endl;
}
break;
}
else cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}

I just took two variables left=0 & right=num-1 and increased left & decreased right till r<=num/2, after that i reversed the process,when the col <= left or col >=right I printed *.
I hope it will be easy to understand.
Here is the code:
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a)
{
cin >> num;
if (num < 38 && num > 0 && num % 2 == 1)
{
cout << "Thank you!" << endl << endl;
int left=0,right=num-1;
//for printing top line
for(int i = 0; i < num; i++) cout<<"- ";
cout<<"-"<<endl;
for (int r = 0; r < num; ++r) //outer loop/rows
{
//printing columns
for(int c = 0; c < num; c++)
{
if(c <= left || c >= right)
cout<<"* ";
else
cout<<" ";
}
if(r >= num/2) //checking for half of the rows
{
left--;right++;
}
else
{
left++;right--;
}
cout<<"|"<<endl;
}
//for printing last additional line
for(int i = 0; i < num; i++) cout<<"- ";
cout<<"-"<<endl;
break;
}
else cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}

This approach does it the math way.
Furthermore it draws a full frame with plus-chars at the edges.
Give it a try.
#include <iostream>
#include <cmath>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a) {
cin >> num;
if (num < 40 && num > 0 && num % 2 == 1) {
cout << "Thank you!" << endl << endl;
int center = ceil(num / 2.0);
for (int r = 0; r <= num+1; ++r) { //outer loop/rows
for (int c = 0; c <= num+1; ++c) { //inner loop/columns
if (r == 0 || r == num+1) {
if (c == 0 || c == num+1)
cout << "+"; // corner
else
//top or botton of square between corners
if (c == center)
cout << "-";
else
cout << "--";
}
else if (c == 0 || c == num+1) {
cout << "|"; // left or right frame
} else {
// inner part
if ((center-std::abs(center-r)) >= center-std::abs(center-c))
if (c < center)
cout << "* ";
else if (c > center)
cout << " *";
else
cout << "*";
else
if (c == center)
cout << " ";
else
cout << " ";
}
}
cout << endl;
}
} else
cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}

Just another way (with some more user input checking):
#include <iostream>
#include <string>
#include <limits>
#include <sstream>
using std::cout;
using std::cin;
using std::string;
const auto ssmax = std::numeric_limits<std::streamsize>::max();
const int max_dim = 40;
const int max_iter = 3;
int main() {
cout << "Enter a positive odd number less than " << max_dim << ": ";
int num = 0, counter = 0;
while ( counter < max_iter ) {
cin >> num;
if ( cin.eof() )
break;
if ( cin.fail() ) {
cout << "Please, enter a number!\n";
cin.clear();
cin.ignore(ssmax,'\n');
}
if ( num < max_dim && num > 0 && num % 2 ) {
cout << "Thank you!\n\n";
//top line
string line(num * 2, '-');
cout << line << '\n';
for ( int r = 0, border = num - 1; r < num; ++r ) {
cout << '*';
for ( int c = 1; c < num; ++c ) {
if ( (c > r && c < border) || (c < r && c > border) )
cout << " ";
else
cout << " *";
}
// right border
cout << "|" << '\n';
--border;
}
//bottom line
cout << line << '\n';
++counter;
} else {
cout << "Please, enter a positive odd number that is less than 40!\n";
}
}
cout << std::endl;
}
Or my favorite:
// top line
string line = string(num * 2, '-') + '\n';
cout << line;
// inside lines
int r = 0, border = ( num - 1 ) * 2;
string inside = string(border + 1, ' ') + "|\n";
// top
while ( r < border ) {
inside[r] = '*';
inside[border] = '*';
r += 2;
border -= 2;
cout << inside;
}
// center line
inside[r] = '*';
cout << inside;
// bottom
while ( border > 0 ) {
inside[r] = ' ';
inside[border] = ' ';
r += 2;
border -= 2;
cout << inside;
}
//bottom line
cout << line;

Related

Finding determinate and Multiplication two matrixes with different length in c++

Hey guys I want to make a calculator for matrixes but I have problems.
first, when I want to multiply them it will work when columns and rows are equal in the matrixes but when it's different like n1=5 m1=2 n2=2 m2=6 it goes wrong and have some bugs
and the bigger problem is for calculating the determinate I have no idea what should I do
thanks for answering.
this the code
#include <iostream>
using namespace std;
int main() {
int n1, m1, n2, m2;
int counter = 0;
int ask;
bool matrixCheck;
int determinant = 0;
cout << "pls enter N1" << endl;
cin >> n1;
while (n1 > 100) {
cout << "N most be lower than 100";
cin >> n1;
}
cout << "pls enter M1" << endl;
cin >> m1;
while (m1 > 100) {
cout << "M most be lower than 100";
cin >> m1;
}
cout << "pls enter N2" << endl;
cin >> n2;
while (n2 > 100) {
cout << "N most be lower than 100";
cin >> n2;
}
cout << "pls enter M2" << endl;
cin >> m2;
while (m2 > 100) {
cout << "M most be lower than 100";
cin >> m2;
}
int matrixA[n1][m1];
int matrixB[n2][m2];
cout << "ENTER MATRIX A" << endl;
for (int i = 0; i <= n1 - 1; i++) {
cout << " ";
cout << counter + 1 << "," << i + 1 << "= ";
cin >> matrixA[counter][i];
cout << " ";
if (i == n1 - 1) {
cout << endl;
counter++;
i = -1;
}
if (counter == m1) {
break;
}
}
counter = 0;
cout << "This is A" << endl;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixA[counter][j] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m1) {
break;
}
}
counter = 0;
cout << "ENTER MATRIX B" << endl;
for (int k = 0; k <= n2 - 1; k++) {
cout << " ";
cout << counter + 1 << "," << k + 1 << "= ";
cin >> matrixB[counter][k];
cout << " ";
if (k == n2 - 1) {
cout << endl;
counter++;
k = -1;
}
if (counter == m2) {
break;
}
}
counter = 0;
cout << "This is B" << endl;
for (int q = 0; 1 <= n2 - 1; q++) {
cout << matrixB[counter][q] << " ";;
if (q == n2 - 1) {
cout << endl;
counter++;
q = -1;
}
if (counter == m2) {
break;
}
}
cout << "Choose an operation" << endl
<< "1-(+) 2-(-) 3-(A * B) 4-(A / B) 5-(A --> B) 6-(B --> A) 7-(a * A) 8-(|A|) 9-(|B|)" << endl;
if (n1 != n2 || m1 != m2) {
cout
<< "We have to note that you can't use operaroin number 1, 2, 5 and 6 because the length of the matrixs are different"
<< endl;
matrixCheck = false;
} else if (n1 == n2 && m1 == m2) {
matrixCheck = true;
}
cin >> ask;
while (ask == 1 || ask == 2 || ask == 5 || ask == 6) {
if (matrixCheck) {
break;
}
cout
<< "We have to note that you can't use operaroin number 1, 2, 5 and 6 because the length of the matrixs are different"
<< endl;
cin >> ask;
}
if (ask == 1 && matrixCheck) {
counter = 0;
for (int n = 0; n <= n1 - 1; n++) {
matrixA[counter][n] = matrixA[counter][n] + matrixB[counter][n];
if (n + 1 == n1) {
n = -1;
if (counter + 1 == m1) {
break;
}
counter++;
}
}
counter = 0;
cout << "This is A" << endl;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixA[j][counter] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m1) {
break;
}
}
}
if (ask == 2 && matrixCheck) {
counter = 0;
for (int n = 0; n <= n1 - 1; n++) {
matrixA[counter][n] = matrixA[counter][n] - matrixB[counter][n];
if (n + 1 == n1) {
n = -1;
if (counter + 1 == m1) {
break;
}
counter++;
}
}
counter = 0;
cout << "This is A" << endl;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixA[j][counter] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m1) {
break;
}
}
}
if (ask == 3) {
int matrixResult[n1][m1];
int sum = 0;
// Multiplying two matrices...
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m2; j++) {
sum = 0;
for (int k = 0; k < m1; k++)
sum = sum + (matrixA[i][k] * matrixB[k][j]);
matrixResult[i][j] = sum;
}
}
cout << "\nMultiplication Result:\n";
counter = 0;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixResult[j][counter] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m2) {
break;
}
}
}
}
and this the result
pls enter N1
3
pls enter M1
2
pls enter N2
2
pls enter M2
3
ENTER MATRIX A
1,1=3
1,2=3
1,3=3
2,1=3
2,2=3
2,3=3
This is A
3 3 3
3 3 3
ENTER MATRIX B
1,1=3
1,2=3
2,1=3
2,2=3
3,1=3
3,2=3
This is B
3 3
3 3
3 3
Choose an operation
1-(+) 2-(-) 3-(A * B) 4-(A / B) 5-(A --> B) 6-(B --> A) 7-(a * A) 8-(|A|) 9-(|B|)
We have to note that you can't use operaroin number 1, 2, 5 and 6 because the length of the matrixs are different
3
Multiplication Result:
18 18 12600660
18 18 12600660
18 12600660 364487398
Process finished with exit code 0
Size of resulting matrix is n1 * m2, not n1*m1.

(C++) Problem with if statement ,simple == condition

I have something which outputs all the factors for an integer using a fixed loop.
in this case, int_end_int_ = 4
and middle_x_coefficient = 4
for (int i = 1; i <= int_end_int_; i++)
{
if (int_end_int_ % i == 0) // This gets the factors
{
//here
}
}
i have that inside the if loop that if i * 2 == 4, print a string. So i thought that when i = 2, it will output the string.
//inside if loop
int newi = i * 2;
//i = 2
if (newi == middle_x_coefficient) {
preroot1 = i; //ignore
cout << "prerooted";
preroot2 = i; //ignore
}
It does not output "prerooted", and i have no clue why.
Full Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Quadratic Equation Solver ( to roots )" << endl;
cout << "Enter quadratic equation, e.x (x^2 + 4x + 4) must be in this form" << endl;
string equation;
cout << ">> ";
getline(cin, equation);
if (equation.length() < 12)
{
cout << "Please enter valid string." << endl;
while (equation.length() < 12)
{
cout << ">> ";
getline(cin, equation);
}
}
char middle_x_coefficient = equation[6]; // getting x^2 + 4(this<-)x + 4
char end_int_ = equation[11]; // getting x^2 + 4x + 4 <-- this
int preroot1 = 0;
int preroot2 = 0;
int int_end_int_ = static_cast<int>(end_int_); //convert char to int using static cast for like no reason
//nvm <- https://stackoverflow.com/questions/103512/why-use-static-castintx-instead-of-intx this says it is better bc compiler bad or smthn
int_end_int_ -= 48; //This converts the ascii value (52 for 4) to 4 (-48)
int pasti = 0;
for (int i = 1; i <= int_end_int_; i++)
{
if (int_end_int_ % i == 0)
{
cout << i << "this<- i" << endl;
cout << middle_x_coefficient << "this<- x" << endl;
int newi = i * 2;
//i = 2
if (newi == middle_x_coefficient) {
preroot1 = i;
cout << "prerooted";
preroot2 = i;
}
else if (i + pasti == middle_x_coefficient) {
preroot1 = i;
preroot2 = pasti;
}
pasti = i;
}
}
cout << preroot1 << " " << preroot2 << endl;
return 0;
}
You converted the character end_int_ to the integer int_end_int_, but you didn't convert the character middle_x_coefficient to an integer. Convert and use converted integer just as you did for end_int_.
Instead of using magic number 48, using character literal '0' is better.

Binary search algorithm in c++

I am new to programming so please help me completing the task
the problem is:
After pressing y the while loop does not run again.
and secondly, how to print or get the array elements in descending order?
thank you!
#include <iostream>
using namespace std;
int main()
{
int item;
int flaging = 0;
int ind_low = 0;
int ind_high = 9;
int ind_mid = (ind_low + ind_high) / 2;
char conti;
//Array declaration and taking user input
int arr[10];
cout << "enter some values here : \n" << endl;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
// for sorthing the array
int temp;
for (int p = 1; p <= 9; p++)
for (int c = 0; c <= 8; c++)
if (arr[c] > arr[c + 1])
{
temp = arr[c];
arr[c] = arr[c + 1];
arr[c + 1] = temp;
}
do {
//asking for searching
cout << "Enter the value you want to search : " << endl;
cin >> item;
while (ind_low <= ind_high)
{
if (item == arr[ind_mid])
{
cout << "At " << ind_mid << " index the value " << item << " is found " << endl;
flaging++;
break;
}
if (item < arr[ind_mid])
{
ind_high = ind_mid - 1;
ind_mid = (ind_low + ind_high) / 2;
}
else
{
ind_low = ind_mid + 1;
ind_mid = (ind_low + ind_high) / 2;
}
}
if (flaging == 0)
{
cout << "Value not found" << endl;
}
cout << "To search again press 'y', to exit press any key" << endl;
cin >> conti;
} while ((conti == 'y') || (conti == 'Y'));
}
when I ran it on my pc after pressing y it did run again, can you provide the input that failed you?
for the second question what do you mean?
you can do a for loop that goes like this:
for(int index = ARR_SIZE -1 ; index >= 0 ; --index){
cout << array[index];
}
edit: I understand what you mean. after each run you should reset your indexes otherwise you will always run on the same once:
before you end the loop the values should be reseted.
ind_low = 0;
ind_high = 9;
ind_mid = (ind_low + ind_high) / 2;
that gonna print the array from end to start.

power function no scope in function

#include<iostream>//Pls note:Only header allowed...
As this is the C++ i dont think so any other header is needed for that math function.
using namespace std;
int comparator(int audience[][2], int index1, int index2) {
int b1, e1;
int b2, e2;
b1 = audience[index1][1];
e1 = audience[index1][2];
b2 = audience[index2][1];
e2 = audience[index2][2];
double re1;
re1 = pow(b1, e1);
cout << re1 << endl;
double re2 = pow(b2, e2);
cout << re2 << endl;
if (re1 == re2)
{
return 0;
}
if (re1 > re2)
{
return -1;
}
if (re1 < re2)
{
return 1;
}
}
//Nothing has to be done with the rest of the two functions.
void sorting(int audience[][2], int N, int &i_index, int &j_index)
{
int i, j, temp;
for (i = 0; i<N - 1; i++)
{
if (audience[i][2] < audience[i + 1][2])
continue;
else
i_index = i;
break;
}
for (i = N; i > 1; i++)
{
if (audience[i][2]>audience[i - 1][2])
continue;
else
j_index = i;
break;
}
for (i = i_index + 1; i < j_index - 1; i++)
{
min = audience[i_index + 1][2];
for (i = )
if (audience[i_index][1] > audience[i_index + 1][1])
{
temp = audience[i_index + 1][1];
audience[i_index + 1][1] = audience[i_index][1];
audience[i_index][1] = temp;
}
}
for (i = i_index + 1; i <= j_index - 1; i++)
{
min = audience[i][2];
for (j = i_index + 2; j <= j_index - 1; j++)
{
if (min > audience[j][2])
{
temp = audience[i_index + 2][2];
audience[i_index + 1][2] = audience[i_index][2];
audience[i_index][2] = temp;
}
}
}
}
void merge(int audience[][2], int mergedarray[][2], int N, int i_index, int j_index)
{
}
int main()
{
int audience[100][2], mergedmarks[100][2];
int i, N;
int index1 = 0, index2 = 0;
int comp_result;
cout << "Enter the value of N : ";
cin >> N; // Enter size of the table
cout << "Enter the base and exponent for " << N << "rows " << endl;
for (i = 0; i < N; i++)
cin >> audience[i][0] >> audience[i][1]; //Enter numbers in the table
cout << endl << "Checking Function 1: Compare ecodes for 5 index pairs" << endl;
for (i = 0; i < 5; i++)
{
cout << "\nEnter indices of row1 and row2 that you want to compare: ";
cin >> index1 >> index2;
if (index1 < 0 || index2 < 0 || index1 >= N || index2 >= N)
continue;
comp_result = comparator(audience, index1, index2);
if (comp_result == -1)
cout << "ecode of index 1 is greater than ecode of index2" << endl;
else if (comp_result == 1)
cout << "ecode of index 1 is less than ecode of index2" << endl;
else if (comp_result == 0)
cout << "ecode of index 1 is equal to ecode of index2" << endl;
}
cout << endl;
int index_i = 0, index_j = N;
sorting(audience, N, index_i, index_j);
cout << "Checking Function 2: Printing sorted array " << endl;
for (i = 0; i < N; i++)
cout << audience[i][0] << " " << audience[i][1] << endl;
cout << endl;
cout << "index i: " << index_i << "\nindex j: " << index_j << endl;
cout << endl << "Checking Function 3: Printing Merged Array " << endl;
merge(audience, mergedmarks, N, index_i, index_j);
int merge_array_size = index_i + (N - (index_j + 1));
for (i = 0; i < N; i++)
cout << mergedmarks[i][0] << " " << mergedmarks[i][1] << endl;
cout << endl;
return 0;
}
This is the whole problem. I have to still edit the merge function. That is the whole problem.This is all.
You need to include the pow header, which is math.h, in order to use it.
add at the beginning of your file:
#include <math.h>
#include <iostream>
using namespace std;
POW is declared in math.h header file so use
#include<math.h>

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