brute force technique for the following problem - c++

Given an integer A which denotes the number of people standing in the queue.
A selection process follows a rule where people standing on even positions are selected. Of the selected people a queue is formed and again out of these only people on even position are selected.
This continues until we are left with one person. Find and return the position of that person in the original queue.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int A=10,p=0,i;
vector<bool> mark(A+1,true);
mark[0]=false;
for(i=0;i<=A;i=i+2)
{
p++;
if(p==2)
{
mark[i]=false;
p=0;
}
}
for(int j=0;j<A;j++)
{
cout<<mark[j];
}
for(i=0;i<A;i++)
{
if(mark[i]==true)
{
cout<<i<<endl;
}
}
}
i tried this but it only works for the first set of even numbers
ps:i am new here so please forgive me if i asked in a wrong way

If you are interested in a simple algorithm similar to yours, then please see this example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int A = 10, currentSize = A;
vector<bool> mark(A, true);
while (currentSize > 1) {
for (int i = 0, j = 1; i < A; i++) {
if (mark[i]) {
if (j % 2 != 0) {
mark[i] = false;
currentSize--;
}
j++;
}
}
}
for (int i = 0; i < A; i++) {
if (mark[i]) {
cout << i + 1 << endl;
break;
}
}
return 0;
}
If you only need an answer to a problem with a faster algorithm, then I think that this will be correct:
#include <iostream>
using namespace std;
int main()
{
int A = 10, p = 0;
while (A / 2 != 0) {
A /= 2;
p++;
}
cout << pow(2, p);
return 0;
}
I used VS2017 to compile this code.

Related

Bad Access on Sieve

My block of code runs, but whenever I type in input, it returns Thread 1: EXC_BAD_ACCESS (code=1, address=0x4). I'm fairly new to coding, and was wondering what's wrong.
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int x, count = 1;
cin >> x;
vector<int> sieve;
fill(sieve.begin(), sieve.begin()+x-1, 1);
while (count <= x) {
for (int i = count+1; i <= x; i++) {
if (sieve[i-1] == 1) {
count = i;
break;
}
}
for (int i = count*count; i < x; i+=count) {
sieve[i-1] = 0;
}
}
for (int i = 0; i < x-1; i++) {
if (sieve[i] == 1) {
cout << i+1 << endl;
}
}
}
You need to allocate space for your sieve. So you might want vector<int> sieve(x). Or, you can even do vector<int> sieve(x, 1), which will allocate space for x ints and fill them all with 1s already, so you won't need the fill afterwards.

How do I make a simple program to output a pyramid of two symbols:one printed twice as the other using loops in C++?

I want:
*!!
**!!!!
***!!!!!!
// And so on.
My attempt is below:
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
cout<<"*";
for(ex =1; ex<= 2*a; ex++)
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
I get this as the output:
*!!
*!!!!
*!!!!!!
//and so on...
It does what I need it to do for the second symbol but I don't know how to arrange the loops so that first symbol is outputted the desired number of times and not cut off by the second loop.
there is a small logical mistake in your code, you are only printing '*' once every loop. use the code below
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
cout<<std::string((a),'*');
cout<<std::string((a*2),'!');
cout<<endl;
}
return 0;
}
You need to have the cout << '*' statement in a loop as well:
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++) // signifies the number of lines to print
{
auto i = 1;
while (i <= a) // prints * a times
{
cout<<"*";
++i;
}
for(ex =1; ex<= 2*a; ex++) // prints ! 2*a times
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
You need another loop to print a-counted * symbols inside the main loop.
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
for(int i = 0; i < a; ++i)
{
cout<<"*";
}
for(ex =1; ex<= 2*a; ex++)
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
Another solution is:
#include <iostream>
using namespace std;
int main(){
int times = 5;
char simbol1 = '*', simbol2 = '!';
for(int i=1 ; i<=times ; i++){
for(int k=0; k<i; k++) cout << simbol1;
for(int j=0; j<i*2; j++) cout << simbol2;
cout << endl;
}
return 0;
}

main.exe has stopped working in code blocks

This is a code I wrote for bubble sort. I gave a comment //this line due to which I'm unable to run this program. Every time the first element of the array needs to be stored in 'temp'.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[7]={7,8,5,2,4,6};
int temp;
for(int i=0;i<7;i++)
{
temp=arr[0]; //this line.
for(int j=0;j<7-i;j++)
{
if(temp<arr[j])
temp=arr[j];
else
swap(arr[j],arr[j-i]);
}
}
for(int k=0;k<7;k++)
{
cout<<arr[k]<<endl;
}
return 0;
}
There were some issue with your program:
Array size should be 6 instead of 7
The for loop condition was incorrect
swap(arr[j],arr[j-i]) will break when j-i is less than 0(for instance i=1, j=0).
Program
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[6]={7,8,5,2,4,6};
for(int i=0;i<5;i++)
{
for(int j=0;j<5-i;j++)
{
if(arr[j]>arr[j+1])
swap(arr[j],arr[j+1]);
}
}
for(int k=0;k<6;k++)
cout<<arr[k]<<endl;
return 0;
}
Ideone
You seem flipped for() loops over... what I got - not the most elegant solution, but I stick to the same tools you're using. Mostly. I could make it as template and it would work with any appropriate container. std::sort sometimes implemented like that.
#include <iostream>
#include <algorithm>
void bubbleSort(int arr[], int n)
{
bool swapped;
for (int i = 0; i < n-1; i++)
{
swapped = false;
for (int j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
std::swap(arr[j], arr[j+1]);
swapped = true;
}
}
// no elements were swapped, array already sorted.
if (!swapped) break;
}
}
int main()
{
int arr[] = {7,8,5,2,4,6};
bubbleSort(arr, std::size(arr));
for( auto v : arr )
std::cout << v << " ";
std::cout << std::endl;
}
In C++11 and later <algorithm> can be replaced by <utility>, it's just for swap/size.

There's a error changing values in array. <C++>

This is Project Euler question 26.
I would like to save the abundant values in an array, assuming there are no more than 200 abundant numbers smaller than 28123, which is the upper limit provided in the question.
This is not the complete code, but my program stops defining values at abundantarr[200]. Why am I seeing a limit to the values in this array?
#include <iostream>
#include <cmath>
using namespace std;
bool IsitAbundant(int a);
int main()
{
int abundantarr[200] = {0};
int counter = 0;
int totalsum = 0;
for (int u = 1; u < 28123; u++)
{
if (IsitAbundant(u))
{
abundantarr[counter] = u;
cout << abundantarr[counter] << endl;
counter++;
}
}
return 0;
}
bool IsitAbundant (int a)
{
int sum = 0;
for (int i = 1; i < a; i++)
{
if (a % i == 0)
{
sum+= i;
}
}
if (sum > a)
{
return true;
}
else
{
return false;
}
}

std::vector out of range for min-heap: C++

I've neglected to work on this code (or any other coding projects) for a while, so while I know what is basically wrong with the code, I've been having a hard time finding exactly where the vector is going out of range. I've been running gdb on it all morning to no avail. I'm trying to make a min-heap out of a vector "theData" in C++.
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
using std::cin;
using std::cout;
using std::swap;
using std::pair;
using std::make_pair;
class HeapBuilder {
private:
vector<int> data_;
vector< pair<int, int> > swaps_;
void WriteResponse() const {
cout << swaps_.size() << "\n";
for (int i = 0; i < swaps_.size(); ++i) {
cout << swaps_[i].first << " " << swaps_[i].second << "\n";
}
}
void ReadData() {
int n;
cin >> n;
data_.resize(n);
for(int i = 0; i < n; ++i)
cin >> data_[i];
}
void makeMinHeap(vector<int> &theData, int i, int n) {
int minIndex;
int left = 2*i;
int right = 2*i + 1;
if (left < n && theData.at(left) < theData.at(i)) {
minIndex = left;
}
else if (right < n && theData.at(right) < theData.at(i)) {
minIndex = right;
}
if (minIndex != i) {
swap(theData.at(i), theData.at(minIndex));
swaps_.push_back(make_pair(i, minIndex));
makeMinHeap(theData, minIndex, n);
}
}
void GenerateSwaps() {
swaps_.clear();
int size = data_.size();
for (int i = (size/2); i >= 0; i--) {
makeMinHeap(data_, i, size);
}
}
public:
void Solve() {
ReadData();
GenerateSwaps();
WriteResponse();
}
};
int main() {
std::ios_base::sync_with_stdio(false);
HeapBuilder heap_builder;
heap_builder.Solve();
return 0;
}
You are not putting in a check for minIndex.
Look what happens when your left<=n and right <=n both fails, most likely when the whole recursion is about to stop, since you just check
minIndex != i
// ^-- default each time is garbage which in case last>n && right>n leaves it garbage
// hence when it comes to
if(minIndex!=i){
// It's actually true where it was suppose to break out n thus throws out_of_range
}
Quick n easy solution would be to add a flagcheck
bool flagcheck = false;
if(){ flagcheck = true; }
else if(){ flagcheck = true; }
if(minIndex!=i && flagcheck){}