I have been working on this code for 3 days now and I can't figure out how to remove zeros before the output.
It is a program which calculates factorial of a number. Even if I use if statement as you can see which is commented it removes zeros after and between the numbers. I even tried to take size as the initial value for a but it just takes the global value, but not from the while loop, I even tried to store its value in another variable then also its not working.
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// Complete the extraLongFactorials function below.
void extraLongFactorials(int n) {
int arr[500] = {0};
int size = 1, i = 0, carry = 0, temp = 0;
arr[0] = 1;
for (int j = 1; j <= n; j++) {
for (int k = 0; k < 500; k++) {
temp = arr[k] * j + carry;
arr[k] = temp % 10;
carry = temp / 10;
}
while (carry) {
size++;
arr[size] = carry % 10;
carry %= 10;
}
}
for (int a = 499; a >= 0; a--) { // if(arr[a]!=0)
cout << arr[a];
}
}
int main() {
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
extraLongFactorials(n);
return 0;
}
Just skip the leading zeros by finding out the index of the first non-zero value:
i = 499;
// Skip leading zeros.
while (i > 0 && arr[i] == 0) {
--i;
}
while (i >= 0) {
cout << arr[i--];
}
Also, please don't #include <bits/stdc++.h>. This is a private, compiler specific header that you are not supposed to include.
Related
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;
}
This code is supposed to calculate the frequency of maximum number in an array I.E the number of times the highest number in the array has occured unfortunately this code does not display any output:-
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int birthdayCakeCandles(int n, int a[]){
int j=0,max,count=0;
max = a[j];
while(j<n){
if(a[j+1]> max){
max = a[j+1];
j++;
}
}
int seen[n];
for(int i = 0; i < n; i++)
seen[i] = 0;
for(int i = 0; i < n;i++) {
if(seen[i] == 0) {
int count = 0;
for(int j = i; j < n;j++)
if(a[j] == a[i] && a[i] == max)
count += 1;
seen[j] = 1;
}
}
return count;
}
int main() {
int i,n;
cin >> n;
int a[n];
for(i = 0; i < n; i++){
cin >> a[i];
}
int result = birthdayCakeCandles(n, a);
cout << result << endl;
return 0;
}
Your program never stops, because your maximum finding loop is for n > 0 endless. Your loop in birthdayCakeCandles should be changed to:
while (j < n)
{
if (a[j + 1] > max)
{
max = a[j + 1];
}
j++;
}
Also consider using more readable coding style and please read this.
In addition to the bug found by vasek, you made at least another mistake in the (overcomplicated) following loops, where you are trying to count the occurences of the maximum value.
// I've kept OP's indentation on purpose...
int seen[n]; // <-- Variable Length Arrays are not standard in C++
for(int i = 0; i < n; i++)
seen[i] = 0;
for(int i = 0; i < n;i++) {
if(seen[i] == 0) {
int count = 0;
for(int j = i; j < n;j++)
if(a[j] == a[i] && a[i] == max)
count += 1;
seen[j] = 1; // <-- misleading indentation, this is always executed
// no matter what the condition is
}
}
While all you need to do, once you have found the maximum value, is:
int count = 0;
for( int i = 0; i < n; ++i ) {
if( a[i] == max )
++count;
}
As a matter of fact (unless you want to create a function operating on an array for other reasons), you don't need any array (or std::vector) at all to complete your assignment. This code will perform the same task:
#include <iostream>
#include <limits>
int main()
{
int n;
std::cin >> n;
int x,
max = std::numeric_limits<int>::min();
int count = 0;
for ( int i = 0;
i < n && std::cin >> x;
++i )
{
if ( x >= max )
{
if ( x > max )
{
max = x;
count = 1;
}
else
{
++count;
}
}
}
std::cout << count << '\n';
}
Why my code shows nothing for the input of 10 5 3. It is work for everything till 1 to 9 but whenever it goes for 10 or greater then 10 then doesn't show any output. I also try to use atoi() for it but error in this line int x = str[j] - '0' . Please help me.
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
int n, sum = 1, num = 0;
string str;
cin >> n;
for(int i = 0; i <= n; i++) {
getline(cin, str);
for(int j = 0; j < str.length(); j++) {
if(str[j] != ' ') {
int x = str[j] - '0'; // Here is the problem even I use atoi() but error
sum *= x;
num = sum;
}
}
if(num != 0) {
cout << num << endl;
num = 0;
sum = 1;
}
}
}
It's not clear from your post why you have two variable num and sum. It seems redundant.
Assuming you just need num, replace the lines
sum *= x;
num = sum;
by
num = (10*num + x);
to get the numbers right.
Also, when you encounter a space, you'll need to reset num to 0. Otherwise, the input 10 8 will be treated as 108.
for(int j = 0; j < str.length(); j++) {
if(str[j] != ' ') {
num = (10*num + x);
} else {
// Use num and then reset it 0
// ...
num = 0;
}
}
I am facing SIGSEGV error on submitting solution for codechef small factorial problem code FCTRL2 though the code works fine on ideone
coding language C++ 4.3.2
Example
Sample input:
4
1
2
5
3
Sample output:
1
2
120
6
#include <iostream>
using namespace std;
void fact(int n) {
int m = 1, a[200];
for (int j = 0; j < 200; j++) {
a[j] = 0;
}
a[0] = 1;
for (int i = 1; i <= n; i++) {
int temp = 0;
for (int j = 0; j < m; j++) {
a[j] = (a[j] * i) + temp;
temp = a[j] / 10;
a[j] %= 10;
if (temp > 0) {
m++;
}
}
}
if (a[m - 1] == 0) {
m -= 1;
}
for (int l = m - 1; l >= 0; l--) {
cout << a[l];
}
}
int main() {
int i;
cin >> i;
while (i--) {
int n;
cin >> n;
fact(n);
cout << endl;
}
return 0;
}
Caveat I'm not going to just fix up your code for you straight up, but I will highlight where it's going wrong and why you get the seg fault.
Your problem is with your implementation of how you're trying to handle the digit by digit multiplication - specifically with what happens to your m value. Test it out by outputting m each time it's incremented - you'll find it's incrementing more often than you intend. You're right to realise you need to use an approach to get to 158 digits and your basic concept could be made to work.
The first clue is by testing with n = 6 when you get a leading 0 that you shouldn't even though you try to get rid of that problem with the if block that contains m-=1
Try with n = 25 and you will see a lot of leading zeros.
Any value greater than this will fail with a Segmentation error. The Seg fault is because, with this error, you try to set values of the array a beyond the max index (as m gets greater than 200)
N.B. Your assertion that the code works on Ideone.com is only true up to a point - it will fail with n > 25.
(Erased code computing a factorial using int)
The problem in your code is that you increment m each time temp is not 0 for each digit multiplication. You may then get a SIGSEGV when computing big factorials because m becomes too big. You probably saw it because 0 shows up in front of your result. I guess this is why you added the
if (a[m - 1] == 0) {
m -= 1;
}
You should only increment m when the inside loop is finished and term is not null. Once fixed you can get rid of the above code.
void fact(int n) {
int m = 1, a[200];
for (int j = 0; j < 200; j++) {
a[j] = 0;
}
a[0] = 1;
for (int i = 1; i <= n; i++) {
int temp = 0;
for (int j = 0; j < m; j++) {
a[j] = (a[j] * i) + temp;
temp = a[j] / 10;
a[j] %= 10;
}
// if (temp > 0) {
// a[m++] = temp;
// }
while (temp > 0)
{
a[m++] = temp%10;
temp /= 10;
}
}
for (int l = m - 1; l >= 0; l--) {
cout << a[l];
}
}
This code is supposed to check the input array for five consecutive '1's if found, it is supposed to add a '0' at the end as a parity bit for a simple parity bit checker.
This is the code.
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
int n, a[30], b[5] = {1, 1, 1, 1, 1}, temp = 0, count = 0;
cout << "Enter the size of input bits :";
cin >> n;
cout << endl;
cout << "Enter the input bits :";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n; i >= 0; i--) {
if (i >= 4) {
temp = i;
for (int j = 0; j < 5; j++) {
if (a[temp] == b[j]) {
temp++;
count++;
}
}
}
}
if (count == 4) {
n = n + 1;
a[n] = 0;
}
cout << endl << endl;
for (int i = 0; i < n; i++) {
cout << a[i];
}
getch();
return 0;
}
Here is a simple logic of what you want to achieve.. let's say input array is a, it's length is n:
int counter = 0;
for(int i=0; i<n; i++) {
if(a[i] == 1)
counter++;
else
counter = 0; //need to start looking for 1's again because consecutive stream is broken
if(counter == 5) {
a[i+1] = 0; //found 5 consecutive 1's so next bit will be 0
i++; //don't need to check the next bit which is already 0
counter = 0; //resetting counter
}
}
The above code will change the array [2,3,1,1,1,1,1,3,4,5] to -> [2,3,1,1,1,1,1,0,4,5]
If you want to insert 0 at the end of the array then simply change a[i+1] = 0 to a[n+1] = 0 and remove i++;
You also need to make sure that n is not greater than the size of array.
I'll go line by line from the beginning:
change for(int i=n; i>=0; i--) to for (int i = n-1; i >= 0; i--) (n-sized array in C++ has cells in the range of [0, n-1])
change if(i>=4) to if (n >= 5)
place temp++ after if(a[temp]==b[j]){}, not inside of it.
add
if (count == 5) break;
else count = 0;
just after for(int j = 0; j < 5; j++) loop
change
if(count==4)
{
n=n+1;
a[n]=0;
}
to
if(count == 5){
a[n] = 0;
n = n+1;
}
Once again! n-sized array holds n elements on positions 0 to n-1
Of course sequence makes a difference above!
You can also write it as if (count == 5) a[n++] = 0;
And that would be all.