Inverted Half Triangle using Astrix Problem - c++

Hi I'm trying to get my C++ console to output the first image I've attached. I know you have to like count the spaces in like maybe a for loop and decrement it as you go down the rows..However, I'm not exactly sure on how to go about that, pretty new to c++. The solution I'm currently working on is the exact mirror image of it. Any help would be greatly appreciated.
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
cout << "" << endl;
for (int j = 1; j <= i; ++j) {
cout << "*";
}
}
}
Correct Solution:
My Current Output:

You need an extra for loop before the one that is sending the asterisks, like so that will send the spaces to push the asterisks to the right.
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
for (int j = rows - i; j > 0; j--) {
cout << " ";
}
for (int j = 1; j <= i; ++j) {
cout << "*";
}
cout << endl;
}
}

#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
cout << "" << endl; // Mistake here, you are putting a empty string
for (int j = 1; j <= i; ++j) {
cout << "*";
}
}
}
You're almost good. You have missed to put the spaces in order to create the right padding of the stars.

Related

How do i flip a number half triangle in c++?

I need to code this in c++ using nested for loops. click thisAnd this is what I have right now.
#include <iostream>
#include <map>
using namespace std;
int main() {
int i, j, k, n = 9;
for(i = n; i >= 0; i--){
for(j = i; j >= 0; j--)
cout << j<<" ";
cout <<endl;
}
}
how do I flip it.
Currently you are starting with the longest row and going down. Start with the shortest row and go up:
#include <iostream>
int main() {
int n = 9;
for(int i = 0; i <= n; i++){ // start with 0 and go up to n
for(int j = i; j >= 0; j--)
std::cout << j << ' ';
std::cout << '\n';
}
}
To flip in the other direction fill it with spaces:
#include <iostream>
int main() {
int n = 9;
for(int i = n; i >= 0; i--){
for(int j = 2*(n-i); j >= 0; j--) // fill with spaces
std::cout << ' ';
for(int j = 0; j <= i; j++) // start with 0 and go up to i
std::cout << j << ' ';
std::cout << '\n';
}
}
#include <iostream>
using namespace std;
int main()
{
for (int i = 9; i >= 0; i--) { // i is 9 and its decrementing over time
for (int j = 9; j > i; j--) { // j is also 9 and it must be greater than i, otherwise you will have unnecessary one blank space in first row
cout << " "; // This is my way of doing things, i would rather print double blank space
}
for (int k = 0; k <= i; k++) { // k is just incrementing from 0 to i and it has blank space
cout << k << " ";
}
cout << endl;
}
}

making a triangle diagram however iam unable to make make the final pattern

Trying to make the following pattern and have tried to make several changes; however, I'm unable to replicate the exact same pattern can someone help? I've also attached the picture of the pattern!
#include <iostream>
using namespace std;
void triangle(int n)
{
int k = 2 * n - 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++)
cout << " ";
k = k - 1;
for (int j = 0; j <= i; j++) {
cout << " "<<i;
}
cout << endl;
}
}
int main()
{
int n = 5;
triangle(n);
return 0;
}
There are minor issues in your code
#include <iostream>
using namespace std;
void triangle(int n)
{
int k = 2 * n - 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++)
cout << " ";
k = k - 1;
for (int j = 1; j <= i; j++) { //you started loop from j=0
cout << " "<<j; //you need to print 'j' and not 'i'
}
cout<<" 1"<<endl; // for the trailing '1' in every line
}
}
int main()
{
int n = 5;
triangle(n);
return 0;
}
I have commented the changes in the code.
There are few errors in your logic. In the 2nd nested for loop the loop should start as-
for(j=1; j<=i; j++) //you used j=0 here
cout<<" "<<j; //you used i here
and coming out of the loop you need to print an extra one here.
so after this loop use
cout<<" 1"<<endl // it is for the extra 1 at the end of each line;

How do you calculate the total number of -1s and the total numbers of 1s in the table? (c++)

Here is the coding below :) I have also commented on some parts so that it is easier to understand the output of the code.
I have a slight idea that I need to use an "if statement" with "rand()%" in order to make sure that the program knows we want to calculate the sum of 1s and -1s only. for e.g using "rand()%2-1" can help with getting the total sum of 1s outputted in the table. Again, I'm not sure if this idea will work or not.
So the program should output something like "The amount of 1s in the table is 5 and the amount of -1s in the table is 3" for the first time its ran. Then when it is ran the second time, it could output something like "The amount of 1s in the table is 2 and the amount is -1s in the table is 5"
Sorry for any confusions and All your help will be highly appreciated :) :)
#include<iostream>
#include<iomanip>
#include<ctime>
using namespace std;
int main() {
srand(time(0));
const int ROWS=3;
const int COLS=4;
int table[ROWS][COLS];
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
table[i][j] = rand()%3-1;
}
}
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++)
cout << setw(3) << table[i][j];
cout << endl;
}
bool checkTable [ROWS][COLS];
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
if (table[i][j] !=0) {
checkTable[i][j] = true;
}
else{
checkTable[i][j] = false;
}
//The output in the line below is the final line outputted by the
console. This prints out "1" if there is a value in the index within
the table provided above (the value is represented by 1 or -1), and
prints out "0" if there is no value in the index (represented by 0)
cout << " " << checkTable[i][j];
}
}
return 0;
}
[...] for e.g using "rand()%2-1" can help with getting the total sum of 1s
outputted in the table.
I dont really understand what you mean by that. Counting and randomness dont go well together. I mean of course you can fill a matrix with random numbers and then do some counting, but rand() wont help anything for the counting.
You need something as simple as that:
int main() {
srand(time(0));
const int ROWS=3;
const int COLS=4;
int table[ROWS][COLS];
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
table[i][j] = rand()%3-1;
}
}
unsigned ones_counter = 0;
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) { // dont forget the bracket
cout << setw(3) << table[i][j];
if (table[i][j] == 1) { ones_counter++;} // <- this is counting
}
cout << endl;
}
std::cout << "number of 1s in the table : " << ones_counter << "\n";
....

Compilator won't stop printing the same, wrong answer

So I'm supposed to do a one line matrix from a document but the compilator keeps printing wrong answer. Not to mention when replacing one element with the other, the program won't react to the command.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
const char FV[] = "Masyvas.txt";
using namespace std;
int main()
{
int A[10];
int i,j, m, n;
ifstream Mas(FV);
cout << "Parasykite masyvo elemento indekso numeri, pries kuri iterpsite nauja elementa - ";
cin >> n;
cout << "irasykite iterpiamo elemento reiksme - ";
cin >> m;
for (i = 0; i < 5; i++)
{
Mas >> A[i];
}
for ( j = n; j < 5; j++)
{
A[j + 1] = A[j];
A[n] = m;
}
for (i = 0; i < 5; i)
{
cout << " " << A[i] << endl;
}
Mas.close();
return 0;
}
The problem is in this line:
for (i = 0; i < 5; i)
Here you have infinite loop, because condition i < 5 is always true.
It was easy to find it on your side! Use any debugger next time to analyze so simple problems.
So, just replace that line by this line to fix it:
for (i = 0; i < 5; i++)

print stars as much as the values in the array

I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this
5*****
Input
1
2
3
4
5
6
output
1*
2**
3***
4****
and so on
.. help :(
#include <iostream>
using namespace std;
void main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; x <= arr[i]; j++)
{
cout<< "*";
}
}
}
And another help please can you give me some useful link to practice on programming to be professional
Your code is wrong. Use the following code:
#include <iostream>
using namespace std;
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; j < x; j++){ // your condition was wrong
cout<< "*";
}
cout<<endl; // for better formatting
}
return 0;
}
For edited question
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 10; i++)
{
int x = arr[i];
cout << x;
for (int j = 0; j < x; j++){ // your condition was wrong
cout << "*";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void main()
{
int nbValues = 10;
int arr[nbValues];
// First recover the values
for (int i = 0; i < nbValues; i++)
{
cin >> arr[i];
}
// Then print the output
for (int i = 0; i < nbValues; i++)
{
int x = arr[i];
cout << x;// Print the number
for (int j = 0; j < x; j++)
{
cout<< "*";// Then print the stars
}
cout << endl;// Then new line
}
}