Calculate the sum of the operation in a string - c

Problem Statement
You are given a string S consisting only +(Addition),*(Multiplication). The next line will contain two positive values.
Now, Calculate the sum of every operations. See the explanation for more clarification.
Input Format
First line contains a string S, consisting only +(Addition),*(Multiplication) operator.
The second line will contain two positive integers a and b
Constraints
1 <= |S| <= 20, where |S| means the length of S.
1<= a, b <= 50
Output Format
Print the summation which were perform based on String S.
Sample Input 0
type here
+*
5 10
Sample Output 0
65
Explanation 0
when S[i] = '+',Then a+b = 5 + 10 = 15 and sum = 15
when S[i] = '*',Then a*b = 5 * 10 = 50 and sum = 15 + 50 = 65
Sample Input 1
+***+
2 1
Sample Output 1
12
Following is my attempted code
#include<stdio.h>
int main(){
char S[20];
int a,b,i,sum=0;
scanf("%s %d %d",S, &a,&b);
for(i=0; i<=20; i++){
if (S[i]= "+"){
sum+=a+b;
}
else{
sum+=a*b;
}
}
printf("%d",sum);
return 0;
}

You have typos like
if (S[i]= "+"){
where there is used the assignment operator = instead of the equality operator == or using string literals like "+" instead of integer character constants. and incorrect expression in the for loop.
At least change the for loop the following way
for ( i = 0; S[i] != '\0'; ++i ){
if ( S[i] == '+' ){
sum += a + b;
}
else if ( S[i] == '*' ){
sum += a * b;
}
}
Pay attention to that according to the assignment you need to check entered values of a and b that they are in the range [1, 50]. And the call of scanf should be written like
scanf( "%19s %d %d", S, &a, &b );.

I did not understand how you should write the parameters, but looking at your code I saw a error:
if (S[i]= "+"){
here you want to compare the value of S with index i with the plus sign
for compare you need to use ==
S[i] is a char, you need to use '+'
the dobles "+" are for char* and not char.
I hope I was helpful.

Related

String not showing up in console for C

I'm trying to convert a number in to a binary string:
void num_to_binary(const int num, char *binary) {
int number = num;
for(int i = 0; number > 0; i++){
binary[i] = number % 2;
number = number/ 2;
}
int length = strlen(binary);
binary[length] = '\0';
printf("%s", binary);
}
While I'm sure that the binary string contains what I want (I tested the code by using
printf("printed %d end",binary[i]); in the for loop), I was unable to print it out by writing the code printf("%s", binary);
Any suggestions would be much appreciated! Thank you.
you have to do binary[i] = !!(number % 2) + '0';, otherwise strlen() will find the null character (value zero) somewhere it shouldn't, it is probably the first character in your case. See the code below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void num_to_binary(const int num, char *binary) {
int number = num;
for(int i = 0; number > 0; i++){
binary[i] = !!(number % 2) + '0';
number = number/ 2;
}
int length = strlen(binary);
binary[length] = '\0';
printf("%s", binary);
}
int main(void)
{
char buffer[100];
num_to_binary(25, buffer);
return 0;
}
The issue is that you're setting the values of a character string to either 1 or 0. When that string gets printed, those numbers are interpreted by the ASCII table. Both 1 and 0 represent unprintable characters. In fact, 0 is called a "null terminator" which will cause C to stop printing when it sees that value.
Instead, you want the ASCII representations of "1" and "0", which are 31 and 30, respectively. So, you could replace that line with
binary[i] = 30 + number % 2;
EDIT:
It should be 30 + !!(number % 2). That will take care of the situation when number is negative.
binary[i] = number % 2; in this line, you're just storing a integer value to a string. And every time the value is either 0 or 1. So, printf("%s", binary); shows the charactera corresponding to ASCII 0 or 1.
The ASCII value for character '0' is 48 and '1' is 49. You have two ways:
binary[i] = number % 2 + 48; or,
binary[i] = number % 2 + '0';

Can you reverse the total? (Looping C)

"can you reverse the number" is the number already reverse by using Mod(%). My question, can it be reversed to normal?
For example, if you enter the number "2552" it'll change to "2+5+5+2", which is correct, but, when you enter another number like "4125" it will change to "5+2+1+4" instead of "4+1+2+5"
Ok, I just entered the programming world, a newcomer
With the "if" can it add "+" without exceeding the number like "4+1+2+5+"
there are "+" after "5", how can I delete this extra "+"?
#include <stdio.h>
main(){
int a, b, h=0;
printf("Enter the number : ");
scanf("%d",&a);
printf("%d = ", a);
while(a != 0)
{
b=a%10;
a=a/10;
printf("%d",b);
if(b != a)
{
printf("+");
}
h=h+b;
}
printf(" = %d\n", h);
}
Instead of scanning for an actual number, you can scan a string from the user. Strings are easy to reverse:
char num[5]; // Has the input
char rev[5]; // Will have the reverse
int len = strlen(num);
rev[len] = 0; // End the string
for(int i = 0; i < len; i++){
rev[i] = num[len-i-1];
}
You can go backwards by taking the highest digits in your number first instead of the lowest, e.g. like so:
int currentLog10;
int powerOfTenForCurrentDigit;
...
while(a != 0)
{
currentLog10 = (int)log10(a);
powerOfTenForCurrentDigit = (int)pow(10, currentLog10);
b = a/powerOfTenForCurrentDigit;
a = a - b * powerOfTenForCurrentDigit;
...
}
How does it work:
log10 of 4125 will give us 3.
b is set to 4125 devided by 1000 (10^3) = 4.
a is set to 4125 - 4 * 1000 = 125
Next step log10 of 125 gives 2, we devide by 10^2 (100) and so on.
Last step 5 gives 0, 5 devided by 10^0 (1) gives 5, a becomes 5 - 5 * 1, which is 0, we are done.
You have to be careful in edge cases, where log10 returns something like (n-1).999999999 instead of n, but it shouldn't happen for small numbers as are entered in your program. Maybe add some sanity check for the input.

storing large integer consisting of zeroes and ones

I am stuck at a question where i have to read an integer D such that D follows the following rules:
-> D contains only zeros and ones
-> 1 ≤ Length of the number D ≤ 10^5.
-> D may begin with zero
And further, I have to store this number in array such that each digit of the no is stored in one cell of the array.
for example, if the number is 001011 it should be stored in the array like
arr[0]=0 arr[1]=0 arr[2]=1 arr[3]=0 arr[4]=1 arr[5]=1
How can this be done?
Your question is known as an XY problem (see https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem).
The real problem (as you linked in a comment) doesn't require storing digits of D. Instead just count the number of ones and zeros.
Something like:
#include <stdio.h>
int main(int argc, const char *argv[]) {
int rc;
char c;
int numZero = 0;
int numOne = 0;
while(((rc = scanf("%c", &c)) == 1) && (c == '0' || c == '1'))
{
if (c == '0')
++numZero ;
else
++numOne;
printf("%c", c);
}
printf("\n");
printf("Zeros: %d Ones: %d\n", numZero, numOne);
return 0;
}
Input:
111000111
Output:
111000111
Zeros: 3 Ones: 6
Now you can do some logic on the counters to find out if the actual output shall be "Yes" or "No".
Good luck.

c loop stops when array value is zero not null

int findMax(int*sums){
int t = 0;
int max = sums[0];
while (sums[t] != '\0'){
printf("current max: %d %d\n", max, sums[t]);
if (sums[t] > max ){
max = sums[t];
}
t ++;
}
return max;
}
This outputs:
current max: 7 7
current max: 7 4
current max: 7 2
And its ignoring the rest of the list, sums. I think this is because the next element in sums is 0. But I can't see why it would treat 0 as '\0' (null).
sums is an array of integers (technically a pointer to integer). '\0' (the null byte) and 0 are the same value, so your loop will stop when it encounters a 0. There is no such thing as a null value as far as integers are concerned. The term "null" is used to refer to the value NULL, which is a pointer usually with the value 0 (i.e., a pointer that doesn't point to anything), and also the null (0) byte, such as the one that occurs at the end of a null-terminated string.
I do remember the time when I first encountered the same problem ( while I was trying to build a big number library using int arrays ), and eventually I figured out pretty much the same as what other answers say that technically '\0' and 0 have the same value.
Now here are 2 ways that I used to overcome this problem and these are only applicable under certain conditions
Case 1 :
Condition : When all your input elements are positive
Now since all your input elements are positive, you can mark the end of the array by inserting a negative number
Typically, I use -1, this way :
int a[] = {1, 2, 3, 4, -1}
for(int index = 0; a[index] != -1; index++)
{
//use the array element a[index] for desired purpose!
}
Instead you can enter any negative number and do it this way
for(int index = 0; a[index] >= 0; index++)
{
//use the array element a[index] for desired purpose!
}
Case 2 :
Condition : When all your elements are bound within a certain range
You might have got the idea by now :), lets say that all your elements belong to the range [-100,100]
you can insert any number above or below the bounds of the range to mark the end... so in the above case I can mark the end by entering a number < -100 and >100.
And you can iterate the loop this way :
for(int index = 0; (a[index] > -100) && (a[index] < 100); index++)
{
//use the array element a[index] for desired purpose!
}
Generalizing both the cases, just place a value at the end of array which you know for sure is not equal to an array element
for(int index = 0; a[index] != value_not_in_array; index++)
{
//use the array element a[index] for desired purpose!
}
So, now under Case 1, your while loop condition can be either of the following :
while(sums[t] != -1) //typically ended with `-1`
//(or)
while (sums[t] >= 0) //ended with any negative number
And under Case 2 :
while ((sums[t] >min_range) && (sums[t] < max_range)) // when elements are bound within a range
Or more generally :
while( sums[t] != value_not_in_array )
The underlying fact of both the cases is that I'm finding out a
potential replacement for terminating '\0' character.
Hope this helps, happy coding ;)
'\0' is a representation of a non-printable ASCII character. Specifically, it is the character 0 (as in, the zeroeth character, not the character '0', whichis 48. Look it up on an ASCII table).
'\0' is the same as 0 the same way 'A' is == 65. There is no difference as far as the compiler is concerned. '\0' == 0 will always evaluate as true.
Note that only strings are terminated with a '\0', unlike all other arrays.
In C, the character literal '\0' has the value (int)0, that's what the escape sequence translates to.
#include <stdio.h>
int main() {
int i = 0;
char c = '\0';
printf("%s\n", (i == c) ? "same" : "different");
}
http://ideone.com/sYRbYZ
I think you're confusing a pointer check for NULL vs a value check for zero.
Here are two slightly different variants of your function to illustrate the point:
#include <stdio.h>
int
findPtr(int **sums)
{
int t = 0;
int max = *sums[0];
int val;
while (sums[t] != NULL) {
val = *sums[t];
printf("current max: %d %d\n", max, val);
if (val > max) {
max = val;
}
t++;
}
return max;
}
int
findArr(int *sums,int count)
{
int t = 0;
int max = sums[0];
while (t < count) {
printf("current max: %d %d\n", max, sums[t]);
if (sums[t] > max) {
max = sums[t];
}
t++;
}
return max;
}
Since zero (either 0 or \0--which are equivalent) is a valid value in sums, it can't be used as a sentinel for end of array as your check was doing. You'll need to pass down the array count as in the latter example.
In your code, you are taking a pointer to an array of integers as input in the findMax function. '\0' is a character. You are comparing integers to a character, causing the compiler to cast the character '\0' and use its integer equivalent NULL (or simply 0). Therefore the program stops when it comes to a 0 in the array.
You might want to try :
int findMax(int*sums,int arraysize)
{
int t=0;
int max = sums[t];
while(t<arraysize)
{
printf("current max: %d %d\n", max, sums[t]);
if (sums[t] > max )
{max = sums[t];}
t++;
}
return max;
}

Splitting a double into an array in C

I'd like the following code to work:
double num = 31415; /* num will never have a decimal part */
/* ... code here so that we can now say */
printf("%d", num_array[0] == 3 && num_array[1] == 1 && num_array[4] == 5); //1
I realize it's trivial to do this with ints (just int i=0; while(num>0) numarr[size-++i] = num%10, num/=10; where size is predetermined to be the number of digits in the number), but that obviously won't work for floats/doubles because you can't mod one floats.
And yes, I need to use floats/doubles, despite not using the floating point section (it's an exercise).
And I have figured out how to determine the number of digits in a double, using floor().
/* n > 0, n=floor(n) */
int numdigits(double n){
int i = 0;
while (n >0)
n = floor(n/10), i++;
return i;
}
Look up fmod. The number of digits will probably be easier to compute using log10.
It's probably easiest to just convert it to a string:
char* str = malloc(num_digits+1);
snprintf(str, num_digits+1, "%f", num);
Indexing into str will give you each digit as an ASCII value, so you'll need to subtract '0' to get the actual numeric value:
str[0] - '0' == 3;
str[1] - '0' == 1;
str[2] - '0' == 4;
...
Apart from fmod(), you could also cast to a int64_t or long long int, and use % as before. If the range is less than 10 digits, you could use a int32_t.

Resources