I'm creating this program just for fun but i'm havin a problem
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int j = 1;
std::vector<int> n_bin(j);
int n=0;
int a;
cin>>n;
do
{
n_bin[j] = n%2;
n= n/2;
a++;
j++;
}while(n>0);
for(j=a; j>0; j--)
{
cout<<n_bin[j];
}
return 0;
}
the only problem is the output, infact before the real binary number i get a lot of more number for example:
if i assign 104 to n i get: 246978862916874543020108190880108137760108190884026797651687454302000108137760108191524026797651687454302183600828414839627280108137760146978862716874543020001321208800108136960-10108137760108364961101000 where only the last 7 are useful.
n_bin is initialized to have exactly one element, at n_bin[0]. n_bin[j] exhibits undefined behavior for any value of j other than 0, by way of accessing an index out of bounds.
void decToBinary(int n)
{
int binaryNum[1000];
int i = 0;
while(n > 0)
{
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for(int j = i - 1; j >= 0; j--)
{
cout << binaryNum[j];
}
}
Related
You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list.
You don't need to print or return anything, just change in the input array itself.
#include <iostream>;
using namespace std;
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i]<<i;
}
void UpdateArr(int arr[], int n)
{
int i = 0, j = n - 1;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j -= 2;
}
cout<<' printArr(arr[], n)';
}
int main()
{
int t;
cin>> t;
int n;
cin>> n;
int input[100];
for(int i=0; i<n; i++) {
cin >>input[i];
}
int arr[100] ;
n = sizeof(arr) / sizeof(arr[0]);
UpdateArr(arr, n);
return 0;
}
I'm not sure what are you exactly expecting the output to be (pls edit it and show the expected output) but I think this is what you need to do
#include <iostream>
#include <iomanip>
using namespace std;
void UpdateArray(int Arr[], size_t n) {
for (size_t i = 0; i < n / 2; i++) {
int Holder = Arr[i];
Arr[i] = Arr[~i + n];
Arr[~i + n] = Holder; } }
int main() {
int Arr[7] = { 1,2,3,4,5,6,7 };
UpdateArray(Arr, 7);
for (int i = 0; i < 7; i++) {
std::cout << Arr[i] << "\n"; }
return 0; }
size_t is like an int but it can't go into negative, but it can take bigger positive numbers, you can replace it with int, it shouldn't make a difference.
so we loop through half the array, replacing first items with last, the [~i + n] flips the value to the other side, so like index 4 in a array size of 20 will become 15
I have a program that finds and prints prime numbers from a predefined scope:
#include <iostream>
#include <vector>
int main()
{
int scope = 90;
std::vector<int> arr;
int n = 2;
for (int i = n; i < scope; i++)
{
arr.push_back(i);
}
int index = 0;
while ((n * n) <= scope)
{
int size = (int)arr.size();
for (int i = index + 1; i < size; i++)
{
if (arr[i] % n == 0)
{
arr.erase(arr.begin() + i);
}
}
index += 1;
n = arr[index];
}
int final_size = (int)arr.size();
for (int i = 0; i < final_size; i++)
{
std::cout << arr[i] << std::endl;
}
}
it works fine but only when the scope is <= 90. When it is larger than 90 I get an error:
zsh: segmentation fault
I am a beginner and can't figure out reason for this. What's wrong with my code?
I am trying to implement the Sieve of Eratosthenes algorithm but it giving a runtime error.
didn't get any output though. after providing the input,
#include<iostream>
using namespace std;
//Sieve Approach - Generate an array containing prime Numbers
void prime_sieve(int *p) {
//first mark all odd number's prime
for (int i = 3; i <= 10000; i += 2) {
p[i] = 1;
}
// Sieve
for (long long int i = 3; i <= 10000; i += 2) {
//if the current number is not marked (it is prime)
if (p[i] == 1) {
//mark all the multiples of i as not prime
for (long long int j = i * i; j <= 10000; j = j + i ) {
p[j] = 0;
}
}
}
//special case
p[2] = 1;
p[1] = p[0] = 0;
}
int main() {
int n;
cin >> n;
int p[10000] = {0};
prime_sieve(p);
//lets print primes upto range n
for (int i = 0; i <= n; i++) {
if (p[i] == 1) {
cout << i << " ";
}
}
return 0;
}
compiler didn't throwing any error also it is not providing the output also
program freezes for some seconds and then terminates
As mentioned in the comments, you are going out of bound.
There is also some confusion about the meaning of p[].
In addition, you are not using the value of n in the function, which leads to unnecessary calculations.
Here is a tested programme (up to n = 10000):
#include <iostream>
#include <vector>
#include <cmath>
//Sieve Approach - Generate an array containing prime Numbers less than n
void prime_sieve(std::vector<int> &p, long long int n) {
//first mark all odd number's prime
for (long long int i = 4; i <= n; i += 2) {
p[i] = 0;
}
// Sieve
for (long long int i = 3; i <= sqrt(n); i += 2) {
//if the current number is not marked (it is prime)
if (p[i] == 1) {
//mark all the multiples of i as not prime
for (long long int j = i * i; j <= n; j = j + i ) {
p[j] = 0;
}
}
}
//special cases
p[1] = p[0] = 0;
}
int main() {
long long int n;
std::cout << "Enter n: ";
std::cin >> n;
std::vector<int> p (n+1, 1);
prime_sieve(p, n);
//lets print primes upto range n
for (long long int i = 0; i <= n; i++) {
if (p[i] == 1) {
std::cout << i << " ";
}
}
return 0;
}
Sigabrt runtime error occurs of a fatal error, because of an assert statement not returning true? Or use of excessive memory, I'm not able to figure out what I'm doing wrong here, help me out?
( problem 1343 C on codeforces) link
so here's the code.
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
long int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while (i<n && check(i,a)== s) {
if (a[i] > max)max = a[i];
i++;
}
b.push_back(max);
}
int s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
cout << s << endl;
}
}
I have debugged your code and also the modified code has been accepted for the above question.
Mistakes you made:
1. In the below loop, value at i'th index of vector<int> b is being added to long int s. Instead, b[k] should be added to long int s because the variable being used in the loop is k not i.
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
2. In the question, range of variable n is given as (1 ≤ n ≤ 2.10^5). So, it is safe to use int n instead of long int n. Also, when I submitted my code on codeforces it gave me signed integer overflow error when I used long int n.
3. You need to use long long s instead of long int s because the value of each element of array A lies between (−10^9 ≤ a[i] ≤ 10^9 , ai ≠ 0) and when we add the elements it can easily surpass int and long int ranges.
4. Although, the answer got accepted when I used vector<int> a in the function
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
But as the user Scheff has said and is correct that it comes with a penalty in space and time, you should use call by reference i.e. vector<int> &a.
Modified Code:
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i, vector<int> &a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while ((i<n) && (check(i,a)== s)) {
if (a[i] > max)
max = a[i];
i++;
}
b.push_back(max);
}
long long s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[k];
}
cout << s << endl;
}
}
Screenshot of Accepted Answer:
I have a homework task to check if reversed array would be the same as the original one using functions. I have tried to write the code but it gives me the same "TAIP" answer everytime I run it. I read a tutorial how to reverse an array, so I have tried to have 2 arrays: original one and reversed one and then I compare it. However, it doesn't work in my code and I don't know why.
#include <iostream>
#include <fstream>
using namespace std;
void funk(int a[], int n, int b[], int &nes);
int main()
{
int a[10], n, b[10],nes = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];//the same elements
}
funk(a, n, b, nes);
if (nes != 0) {
cout << "NE";
}
else
cout << "TAIP";
}
void funk(int a[], int n, int b[], int &nes) {
int j, i=0, pap;
j = i - 1; //last element
while (i < j)
{
pap = b[i];
b[i] = b[j];
b[j] = pap;
i++;
j--;
}
for (int c = 0; c < n; c++) {
if (a[c] != b[c]) {
nes++;
break;
}
}
}
Input:
6
8 5 8 1 3 8
Output I need to get: NE
Your approach seems to be unnecessarily complicated and inefficient. For a reverse-invariant array a of n elements, the condition a[i]==a[n-i-1] must be true for all i between 0 and n/2.
An exemplary solution:
template <typename T>
bool is_reverse_invariant(T a[], size_t n)
{
for (size_t i = 0; i < n/2; i++)
if (a[i] != a[n-i-1]) return false;
return true;
}
A problem with your code
int j, i=0, pap;
j = i - 1; //last element
is that j should be initialized with
j = n - 1;
instead.