Cannot convert character to integer in c++ in loop - c++

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;
}
}

Related

print all the prime numbers upto an upperlimit

#include<iostream>
using namespace std;
int main(){
int n=5;
int i = 2;
for (i; i <= n; i++)
// for all num to n
{
int j = 2;
bool divide = false;
for (j; j <= n - 1; j++)
// for checking each num
{
if (i % j == 0)
{
divide = true;
break;
}
}
if (divide == false)
{
cout << i << " ";
}
}
return 0;
}
my Q is that
//please tell me why it is not working
//it is expected to give ans 2,3,5 which it is not giving why???
maybe I found the issue.
I think that the problem here is:
for (j; j <= n - 1; j++)
Here you did j<=n-1;
So to fix this just do:
for(j; j < i; j++){
//this should fix
So everything should look like this:
#include<iostream>
using namespace std;
int main() {
int n = 5;
int i = 2;
//check prime numbers starting from i and max n using for loop
for (i = 2; i <= n; i++) {
bool divide = false;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
divide = true;
break;
}
}
if (!divide) {
//!divide is equal to divide=false
cout << i << " ";
}
}
}

Print all the substrings with equal number of zeros and ones, But am not able to

I want to print all the substrings to be printed and everysubstring should have equal number of zeros and ones.I have strated the loop where i points to the index and initially number of zeros and one is equal to 0 and loop runs till both of them becomes equal and i push that in the vector. But My code is not running good . I am given enough time to figure out what could be the error but am not able to find.
Output
0 1 0 0 1 1 0 1 0 1
Expected Output
01 0011 01 01
I am not able figure out the error , Please help me with this.
#include <iostream>
#include <vector>
using namespace std;
vector<string> maxSubStr(string s, int n)
{
vector<string> v;
for (int i = 0; i < n; i++)
{
int ones = 0;
int zeros = 0;
int j = 0;
for (j = i; zeros != ones && j<n; j++)
{
if (s[j] == '0')
{
zeros++;
}
else
{
ones++;
}
}
if (zeros == ones)
{
int size = j - i;
v.push_back(s.substr(i, size));
}
if(j==n){
break;
}
i = j ;
}
return v;
}
int main()
{
string str = "0100110101";
int n = str.length();
vector<string> v;
v = maxSubStr(str, n);
vector<string>::iterator it;
for (it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
return 0;
}
There are a number of little mistakes in your code. You should learn to use a debugger to step through it, the errors would become evident.
In the loop over i it is useless to set the value of j just before setting in again in the loop over j.Then, the loop should be limited by j<n to prevent an access past end of string. Next, you do not want to test s[i] but s[j]. Finally, the test zeros == ones should be in the j loop and should set i to the next position, that is j + 1. Code could become:
vector<string> maxSubStr(string s)
{
vector<string> v;
int n = s.size();
for (int i = 0; i < n; i++)
{
int ones = 0;
int zeros = 0;
int j = 0;
for (j = i; j < n; j++)
{
if (s[j] == '0')
{
zeros++;
}
else
{
ones++;
}
if (zeros == ones)
{
v.push_back(s.substr(i, j + 1 - i));
i = j;
break;
}
}
}
return v;
}

why my c++ code to find factorials is not working?

#include <iostream>
using namespace std;
int main() {
int n, t;
cin >> n;
int i;
for(i = 0; i < n; i++){
cin >> t;
int arr[200];
arr[0] = 1;
int j;
for(j = 1; j < 200; j++) arr[j] = 0;
int l = 1, k;
for(j = 1; j <= t; j++){
int rem = 0, flag = 0;
for(k = 0; k < l; k++){
int temp = (arr[k]*j) ;
arr[k] = (temp + rem) % 10;
rem = (temp+rem) / 10;
if(k == l-1 && rem != 0){
arr[l] = rem;
flag = 1;
}
}
if(flag) l++;
}
while(l--){
cout << arr[l];
}
if(i != n-1){
cout << "\n";
}
}
return 0;
}
Question statement:
You are asked to calculate the factorials of some small positive integers.
Input:
An integer n, 1<=n<=100, denoting the number of testcases, followed by n lines, each containing a single integer t, 1<=t<=100.
Output:
For each integer n given at input, display a line with the value of t!
This is working fine for t < 35 but starts giving error for t >= 35.
Also tell me how can I improve my coding style. I am new to coding.
CASE 1
sample input:
2
1
35
actual output:
1
-40427027-3-786144929666651337523200000000
expected output:
1
10333147966386144929666651337523200000000
CASE 2
sample input:
3
5
6
7
actual output:
120
720
5040
expected output:
120
720
5040
PS Sorry!, Initial question changed as I ignored floating point errors while calculating 17! from scientific calculator. Now, code is not working for values greater than 34
Error was in part that rem can be a 3 digit number so diving by 10 doesn't work. Need to take care for rem > 100
That part of your code looks wrong since it cycles only up to length of your number
so it may grow only by one digit when there is flag:
for(k = 0; k < l; k++){
int temp = (arr[k]*j) ;
arr[k] = (temp + rem) % 10;
rem = (temp+rem) / 10;
if(k == l-1 && rem != 0){
arr[l] = rem;
flag = 1;
}
}
if(flag) l++;
It should be something shorter like:
for(k = 0; k < l; k++) {
rem += arr[k] * j;
arr[k] = rem % 10;
rem /= 10;
if(k == l-1 && rem != 0) ++l;
}

How to remove zeros before the number?

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.

Output not correct for my C++ program

So, I have the following problem:
From the file tabl.in a number n will be read (n<=50).
After that a square array with n rows and n columns will be read; all the numbers in the array will be composed by a maximum of 2 digits each.
Shown in the file tabl.out, the modulo between the sum of numbers found on the second diagonal of the array and 10, if the sum is palindrome (true=1, false=0), and the arithmetic mean of elements situated below of the main diagonal.
Will be writing functions for:
reading the array
calculation of the operation sum of secondary diagonal%10
checking if the previous result it is palindrome
calculation of the arithmetic mean below main diagonal
Example:
tabl.in:
4
5 8 2 12
1 0 3 16
1 2 1 11
5 7 2 19
tabl.out:
2 1 3
where
(12+3+2+5)%10 = 22%10 = 2
22 is palindrome = 1
1+2+2+1+7+5 = 18, 18/6=3
My code so far is:
#include <fstream>
using namespace std;
ifstream fin("tabl.in");
ofstream fout("tabl.out");
void readn(int Arr[][51], int n) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
fin >> Arr[i][j];
}
int sumsec(int Arr[][51], int n) {
int s = 0;
float r;
for (int i = 1; i <= n; i++)
s = s + Arr[i][n - i + 1];
r = s % 10;
return r;
}
void pald(int Arr[][51], int n) {
int s = 0, pal = 0;
for (int i = 1; i < n; i++)
s = s + Arr[i][n - i + 1];
while (s != 0) {
pal = pal * 10 + s % 10;
s = s / 10;
}
if (pal == s)
fout << "1 ";
else
fout << "0 ";
}
int ambmd(int Arr[][51], int n) {
int s = 0, k;
float ame;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
s = s + Arr[i][j];
k++;
}
}
ame = s / k;
return ame;
}
int main() {
int Arr[51][51], n;
float r, ame;
fin >> n;
readn(Arr, n);
r = sumsec(Arr, n);
fout << r << " ";
pald(Arr, n);
ame = ambmd(Arr, n);
fout << ame;
}
But I have an issue with the palindrome() function: my output file will have 2 0 3 written to it for the given array from the example, instead of 2 1 3. What am I doing wrong?
Your pald function would work, if you compute s the same way as you do in sumsec and if s would still contain the sum, after you compute pal.
In your case, while (s != 0) {...}, followed by if (pal == s) {...} could be re-written as if (pal == 0), which is clearly not the intended solution. Just save your sum before computing pal, then compare with the saved sum.
Also, change your loop condition for computing s to for (int i = 1; i <= n; i++).
int s = 0, pal = 0, sum = 0;
for (int i = 1; i <= n; i++)
s = s + Arr[i][n - i + 1];
sum = s;
while (s != 0) {
pal = pal * 10 + s % 10;
s = s / 10;
}
if (pal == sum)
fout << "1 ";
else
fout << "0 ";
You should also consider the various comments for code improvements, like not re-computing the sum in the pald function.