Floyd pattern in c++ - c++

hi i am try to print the following pattern in c++
1
2 3
4 5 6
7 8 9 10
7 8 9 10
4 5 6
2 3
1
using the following loop a print half of it.
int i,j,k=1;
cout<<"Enter row";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<k<<"\t";
k++;
}
}
i got the output like
1
2 3
4 5 6
7 8 9 10
how can i print the balance output. but how can i print the mirror of this pattern.

First of all, the code above is wrong, it should be:
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<k<< " ";
k++;
}
cout << '\n';
}
Now, to draw the pattern, you could use a loop that would use the following logic:
for(;i > 0; i--)
{
k -= i-1;
for(j=1;j<i;j++)
{
cout <<k<< " ";
k++;
}
cout << '\n';
k -= i-1;
}

int main()
{
int n;
int i, j, k = 1;
cout << "Enter row";
cin >> n;
int elemcount = 0;
for (i = 1; i <= n; i++)
{
elemcount = 0;
for (j = 1; j <= i; j++)
{
cout << k << "\t";
k++;
}
cout <<endl;
}
k = k - n; //Reset Counter to the value of the first digit in current row.
i--;
j--;
for (; i > 0; i--)
{
j = i;
elemcount = 0; //Counter to keep track of elements printed.
for (; j> 0; j--)
{
cout << k << "\t";
k++;
elemcount++;
}
k = k - elemcount - (i-1); //Resetting K, substracting the number of elements printed and number of elements to be printed in next row.
cout << endl;
}
return 0;
}

Related

How to make my program to swap the columns when sorting a row, in a 2d array?

I made a program which sorts a bi-dimensional array last row, and it works, but what I want it to do is to swap the columns of the sorted digit, for example:
when I input:
5 8 7
9 5 4
6 2 1
to output:
7 8 5
4 5 9
1 2 6
It's mandatory to use the selection method.
Here is the code:
#include <iostream>
using namespace std;
int main()
{
int n,m,i,j,ni,nj;
int A[99][99];
int aux;
cout<< "Lines : "; cin>>n;
cout<< "Columns : "; cin>>m;
for(i=0;i<n;i++)
for(j=0; j<m;j++)
{
cout<<"A["<<i<<"]"<<"["<<j<<"]";
cin>>A[i][j];
}
for(i=n-1;i<n;i++){
for(j=0; j<m;j++){
for(ni = i;ni<n;ni++){
for(nj = j;nj<m;nj++){
{
if(A[i][j] > A[ni][nj])
{
aux = A[ni][nj];
A[ni][nj] = A[i][j];
A[i][j] = aux;
}
}
}
}
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
cout << A[i][j] << " ";
}
cout << endl;
}
}

Sorting an array— issue

I'm asking for help with a problem implying an array sorting in the following manner: all even numbers must be in front of the odd ones. I've partially made the problem, but I did the sorting in the opposite manner and I can not manage to fix it.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int v[100], n, i, aux = 0, inv;
cout << "Number of elements: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "v[" << i << "]=";
cin >> v[i];
}
do
{
inv = 0;
for (i = 0; i < n; i++)
{
if (v[i] % 2 == 1 && v[i + 1] % 2 == 0)
{
inv = 1;
aux = v[i];
v[i] = v[i + 1];
v[i + 1] = aux;
}
}
} while (inv != 0);
cout << endl;
for (i = 0; i < n; i++)
cout << v[i] << " ";
cout << endl;
system("pause");
return 0;
}
The output for this would be:
n = 8
1 3 2 4 7 8 4 2
Result: 2 4 8 4 2 -858993460 1 3
In the expression v[i + 1], you access v[n] when i = n - 1, this will result in an out-of-bounds memory access which results in undefined behaviour.
You should change the for loop to this:
for (i = 0; i < n - 1; i++)
The output for the given input is:
a.exe
Number of elements: 8
v[0]=1
v[1]=3
v[2]=2
v[3]=4
v[4]=7
v[5]=8
v[6]=4
v[7]=2
2 4 8 4 2 1 3 7

How to print the index of the first element of a sequence of the longest repeating number?

I am writing a program for an assignment, which is:
Write a program that creates an int array of 20 elements, would ask the user how many tosses are required(1-20), fills the array with the result of tossing a number cube as many times as requested by the user, prints the values of the tosses horizontally then displays the longest run found
in the sequence of tosses.
For the above example the output would be if the user requested 18 tosses:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 5 5 4 3 1 2 2 2 2 6 1 3 3 5 5 5 5
The longest run occurs at index 14.
I wrote the program for the first part, but I am stuck on trying to find the index of the longest run. This is my code so far:
const int SIZE = 20;
int main()
int arr[SIZE];
int numToss;
srand(time(0));
cout << "How many tosses are required? Enter a value from 1 to 20: " << endl;
cin >> numToss;
while (numToss > SIZE || numToss < 1)
{
cout << "Please enter a value for the number of tosses greater than 1 and less than 20: " << endl;
cin >> numToss;
}
for (int i = 0; i < numToss; i++)
{
arr[i] = 1 + rand() % 6;
cout << " " << arr[i];
}
cout << endl;
int max_count = 0;
int length = arr[0];
for (int i = 0; i < length; i++)
{
int count = 1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > max_count)
max_count = count;
}
int result = 0;
for (int i = 0; i < length; i++)
{
int count=1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count == max_count)
{
max_count = arr[i];
result = i;
cout << "The longest run occurs at " << result << endl;
}
}
return 0;
It does not print correctly, how ca i fix this? Any help is appreciated!

How do you make a rectangle and triangle shape in C++

How do i achieve this format when i keyed in int of ' 245 ':
(where if it is odd number it will be a rectangle, and even number will be triangle)
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
This is my code so far:
(I can't seemed to output triangle and rectangle at the same time)
int n;
int lastDigit;
do
{
cout << "Enter a positive integer: ";
cin >> n;
}while ( n <= 1 || n == '0');
cout << endl;
// If even digit - tri
do
{
lastDigit = n%10;
if (lastDigit / 2 ==0)
{
for (int i = 1; i <= lastDigit; ++i)
for (int tri = 1; tri <= i; ++tri)
cout << "\t" << tri;
cout << endl;
}
// if odd digit - rect
else if (lastDigit / 2 != 0)
{
for (int i = 1; i <= lastDigit; i++)
{
for (int rect = 1; rect <= i; rect++)
cout << "\t" << rect;
cout << endl;
}
n = n/10;
}
cout << endl;
}while (lastDigit != 0);
n = n/10;
cout << endl;
return 0;
And, how should i code when keyed in the int, the compiler will extract the first digit (From left to right) and output it accordingly?
Any help would be appreciated!
Following is the complete code.
Step1: Take user input and check whether it's odd or even.
Step2: If it is odd then perform triangle else rectangle.
#include<iostream>
int main () {
int n;
cout<<"Enter number: ";
cin>>n;
if (n % 2 != 0)
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= n; j++)
{
cout<<j<<" ";
}
}
}
else
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= i; j++)
{
cout<<j<<" ";
}
}
}
return 0;
}
Both of your print is similar. I suggest:
#include<iostream>
using namespace std;
int main () {
int n;
cout << "Enter number: ";
cin >> n;
int i = 1;
int& rowLim = ((n % 2) ? n : i);
for(i = 1; i <= n; i++)
{
cout << endl;
for(int j = 1; j <= rowLim; j++)
{
cout<<j<<" ";
}
}
return 0;
}
Simplest would be to use a string, iterate char by char and output accordingly. For instance
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
for (char c : s) {
int n = c - '0';
bool k = n % 2;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= (k ? n : i); ++j)
cout << " " << j;
cout << endl;
}
cout << endl;
}
return 0;
}
Output
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
See a DEMO

Forming Heap Using array

I am trying to form a heap using the following code ,But not sure why its not showing the correct output.
#include <iostream>
using namespace std;
int h[10], n;
void heapbottom()
{
int i, j;
for (i = n / 2; i >= 1; i--) {
int k = i;
int v = h[k];
bool heap = false;
while (!heap && 2 * k <= n) {
cout << "\n i value is :" << i;
j = 2 * k;
if (j < n) //there sre 2 children
{
if (h[j] < h[j + 1])
j++;
}
if (v >= h[j])
heap = true;
else {
h[k] = h[j];
k = j;
}
h[k] = v;
} //end of while
}
cout << "\n HEAP GENERATED \n";
for (int i = 0; i < n; i++)
cout << "\n ELEMENT IS:" << h[i];
}
int main()
{
cout << "\n Enter the maximum number of array elements \n";
cin >> n;
cout << "\n Enter the array to perform heap sort \n";
for (int i = 0; i < n; i++)
cin >> h[i];
heapbottom();
return 0;
}
If I change the outer loop to be
for (i = n / 2; i >= 0; i--)
I get 9 8 7 6 5 2 as a result, which I believe is a valid heap.