Floyd's triangle variation - c++

So I have to print Floyd's triangle but like this:
7
1
2 3
4 5 6
7 * * *
Here is my code, I just can't figure out how to print the * at the end of the last line if there is any space left.
#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
int n;
cin>>n;
int br=1;
for (int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
if(br<=n)
cout<<br<<" ";
br++;
}
if(br<=n)
cout<<endl;
}
}

Here is the modified code:
#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
int n, i, j;
cin>>n;
int br=1;
for (i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(br>n)
break;
cout<<br<<" ";
br++;
}
if(br>n)
break;
cout<<endl;
}
for(int k = j; k <= i; k++)
{
cout<<"* ";
}
}

Note that on nth line, there are nth numbers. So, for every line, you must count the numbers you wrote already. So, when you write the number that you need you have written k numbers. Now, you need to add n-k stars.

Related

Partition of Array by Element given X

I Am Trying To Find Partition Of Array ,On Condition By Checking Variable x ,when less then x they will be on one side or else on another. but my code need some correction.
HERE am not able to find the error , i will be thankful to you if you help me.
Code is:-
#include<iostream>
using namespace std;
int partition(int arr[],int n,int x){
for(int i=0;i<n;){
if(arr[i]<x){
i++;
}
else if(arr[i]==x){
int temp=arr[i];
arr[i]=arr[n];
arr[n]=temp;
i--;
}
else if(arr[i]>x){
int temp=arr[i];
for(int j=i;j<n;j++){
arr[j]=arr[j+1];
}
arr[n]=temp;
i--;
}
}
return 0;
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int x;
cin>>x;
partition(arr,n,x);
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Input >> array={2,10,15,1,3,15} ,x=10
Expected << {2,1,3,10,15,15}
Output I get << nothing .
The code isn't giving any output because, first, the "cin" and "cout" are in upper case which is syntactically incorrect, secondly, the variable j is in different case in loop statement and body inside the second else-if clause in the partition function, same goes for the "I" in the first for loop in the main() function. Sort this out and you should be good to go.
First in C++ the size of an array must be a compile-time constant. So for example, consider the following examples:
int n = 10;
int arr[n]; //INCORRECT
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, in your code,
int n;
cin>>n;
int arr[n]; //INCORRECT because n is not a constant expression
Second, in your code, when you wrote:
arr[n] = temp; Undefined behavior
you're going out of bounds and so you have undefined behavior.
Solution
You can use std::stable_partition and std::vector to solve your problem as shown below:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
int n;
std::cout <<"Enter n:"<<std::endl;
std::cin >> n;
std::vector<int> arr(n); //create a vector of size n instead of an array
std::cout<<"Enter elements: "<<std::endl;
//iterate and take input from user
for(int &elem: arr){
std::cin >> elem ;
}
int x;
std::cout << "Enter x: "<<std::endl;
std::cin>>x;
//use std::partition
std::stable_partition(arr.begin(), arr.end(), [x](int i){return (i < x);});
std::cout<<"This is the partitioned vector: "<<std::endl;
for(int i=0;i<n;i++)
{
std::cout<<arr[i]<<"\t";
}
return 0;
}
Output
The output of the above program is as follows:
Enter n:
6
Enter elements:
2
10
15
1
3
15
Enter x:
10
This is the partitioned vector:
2 1 3 10 15 15
which can be seen here.

What's wrong in my approach in a problem of game of death in a circle?

#include<bits/stdc++.h>
#include<vector>
#include<algorithm>
using namespace std;
void safe(int n,int k,int a[])
{
vector<int>s(a+0,a+n);
vector<int>b(a+0,a+n);
while(s.size()>1)
{
int r;
r=s.size();
int count=1;
while(count<=r)
{
b[count%r]=0;
count=count+k;
}
b.erase(remove(b.begin(),b.end(),0),b.end());
s=b;
}
for(int x:s)
{
cout<<x<<"\n";
}
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,k;
cin>>n>>k;
int *a=new int[n];
for(int j=1;j<=n;j++)
{
a[j-1]=j;
}
if(n>2)
{
safe(n,k,a);
}
else if(n==2)
{
cout<<a[1]<<"\n";
}
else
cout<<a[0]<<"\n";
}
}
Input:
4
4 2
5 2
2 1
50 10
Output:
1
3
2
19 --> it should be 36
If you don't know this problem
Description of question:
There are n people standing in a circle (numbered clockwise 1 to n) waiting to be executed. The counting begins at point 1 in the circle and proceeds around the circle in a fixed direction (clockwise). In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.
Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive.
Consider if n = 5 and k = 2, then the safe position is 3.
Firstly, the person at position 2 is killed, then person at position 4 is killed, then person at position 1 is killed. Finally, the person at position 5 is killed. So the person at position 3 survives.
Here is a possible solution, hopefully the comments make it clear. There's no need to allocate an int[] and copy it into the vector, you can init the vector directly with iota. Likewise you can remove elements as you go, rather than zeroing and removing as a later step.
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
void safe(int n, int k) {
vector<int> s(n);
// Init the vector 1..n
iota(s.begin(), s.end(), 1);
int i = 0;
// While more than one survive...
while (s.size() > 1) {
// Skip forward (k-1)
i = (i + k - 1) % s.size();
// Kill the next one
s.erase(s.begin() + i);
}
for (int x : s) {
cout << x << "\n";
}
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n, k;
cin >> n >> k;
if (n > 2) {
safe(n, k);
} else if (n == 2) {
cout << 2 << "\n";
} else
cout << 1 << "\n";
}
}

getting error while trying to rotate array clock wise using stack

Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
Input
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements
Output
For each testcase, in a new line, output the rotated array
Example
Input
1 2 3 4 5
Output
3 4 5 1 2
#include <iostream>
#include <stack>
using namespace std;
void rotate(int *a,int s,int r) {
stack<int> st;
for(int i=0;i<r;i++) {
st.push(a[i]);
}
for(int j=r;j<s;j++) {
a[j-r] = a[j];
}
for(int k=s-1;k>r+1;k--) {
a[k] = st.top();
st.pop();
}
for(int l=0;l<s;l++) {
cout<<a[l]<<" ";
}
}
int main() {
//code
int T;
cin>>T;
while(T--) {
int N,r;
cin>>N>>r;
int A[N];
for(int i=0;i<N;i++) {
cin>>A[i];
}
rotate(A,N,r);
cout<<endl;
}
return 0;
}
I followed your logic, it seems like there is problem in your backfill part.
for(int k=s-1;k>=s-r;k--) { // change k>r+1 to k>=s-r
a[k] = st.top();
st.pop();
}
sorry my bad, int third for loop in rotate function there should be k>s-r-1

(C++) Generate first p*n perfect square numbers in an array (p and n inputted from the keyboard)

I input p and n (int type) numbers from my keyboard, I want to generate the first p*n square numbers into the array pp[99]. Here's my code:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i, j, n, p, pp[19];
cout<<"n="; cin>>n;
cout<<"p="; cin>>p;
i=n*p;
j=-1;
while(i!=0)
{
if(sqrt(i)==(float)sqrt(i))
{
j++;
pp[j]=i;
}
i--;
}
for(i=0; i<n*p; i++)
cout<<pp[i]<<" ";
return 0;
}
But I am encountering the following problem: If I for example I enter p=3 and n=3, it will only show me the first 3 square numbers instead of 9, the rest 6 being zeros. Now I know why this happens, just not sure how to fix it (it's checking the first n * p natural numbers and seeing which are squares, not the first n*p squares).
If I take the i-- and add it in the if{ } statement then the algorithm will never end, once it reaches a non-square number (which will be instant unless the first one it checks is a perfect square) the algorithm will stop succeeding in iteration and will be blocked checking the same number an infinite amount of times.
Any way to fix this?
Instead of searching for them, generate them.
int square(int x)
{
return x * x;
}
int main()
{
int n = 0;
int p = 0;
std::cin >> n >> p;
int limit = n * p;
int squares[99] = {};
for (int i = 0; i < limit; i++)
{
squares[i] = square(i+1);
}
for (int i = 0; i < limit; i++)
{
std::cout << squares[i] << ' ';
}
}

Find the biggest sum out of the all, it containing numbers and

I need to find the biggest sum and it containing numbers and whether the first or second number out of two was bigger. How to find it?
Let's say that n=10, the two put numbers are 6 and 2, followings: 7 and 1,
5 and 6, 1 and 8, 4 and 3. Then the answer should be that the biggest sum is 11, it containing numbers are 5 and 6, and the bigger numb was the second one.
I have a code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
for (i=1; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
for (int j=sum; j<=n/2; j++);
{
cout<<sum<<endl;
}
}
fd.close();
fr<<sum;
fr.close();
return 0;
}
I think your code should be like this :
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
fd>>p>>a;
int biggestSum=p+a;
int first = p;
int second = a;
for (i=2; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
if(sum > biggestSum)
{
biggestSum = sum;
first = p;
second = a;
}
}
cout <<"biggest sum is "<<biggestSum<<"\n";
cout <<"The first number is "<<first<<"\n";
cout<<"The second number is "<<second<<"\n";
fd.close();
fr<<sum;
fr.close();
return 0;
}
updated : you should be careful to the index i of the for loop it should start by 2 since you read the first two numbers before the for loop.