Question

In: Computer Science

Currently the code outputs 3. Is there a way to make it output 32 writing logic...

Currently the code outputs 3. Is there a way to make it output 32 writing logic in the parseint and not the main function?

#include <stdio.h>
#include <ctype.h>

int main()
{
printf("%d\n", parseint("32"));
return 0;
}

int parseint(char *str) {
int i=0;
while(str[i] != '\0'){
return (str[i] - '0');
i++;
}
return 1;   

Solutions

Expert Solution

The statement that is being updated is provided below:

return (str[i] - '0');

The above statement is updated as follows:

x = x*10 + str[i] - '0';

Explanation:

  • Declare a variable x, that store the value of string that is being passed by the main function.
  • And at last the value x is being returned to the main function which represents the whole string passed by the main function.

The complete correct code that display 32 is provided below:

Screenshot of the code:

//The modified lines are highlighted in yellow:

Sample Output:

Code To Copy:

#include <stdio.h>

#include <ctype.h>

int main()

{

printf("%d\n", parseint("32"));

return 0;

}

int parseint(char *str) {

int i=0 ,x = 0;

while(str[i] != '\0')

{

x = x*10 + str[i] - '0';

i++;

}

return x;  

}

//For any query please comment. Please provide the positive feedback. Thanks


Related Solutions

****Please make it sure that it explains with logic and/or writing. It is an essay but...
****Please make it sure that it explains with logic and/or writing. It is an essay but it has to be one based on scientific facts. Please be specific preferably with equations and/or calculations (such as short derivations) if necessary to make the argument. The suggested length is between 400-500 words. A) What are the Maxwell's Equations and their significance in physics? B) What is the Displacement Current and its meaning? Thank you for your help!
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
There are 3 SPSS outputs in this homework assignment. The questions for each output are listed...
There are 3 SPSS outputs in this homework assignment. The questions for each output are listed below. Please type your answers into this word document and submit it as an attachment in the assignment tab. Q1. Researchers were interested in determining whether background music helped or hindered students’ performance on a math test. Students were randomly assigned to 1 of 3 groups: 1) no music; 2) music only; and 3) music with lyrics. Students were then given a math exam,...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an input file. For example, with this input: Raspberry Grapefruit Straw berry raspBERRY Blue$$berry apple Pine_apple raspberry The output should be: raspberry grapefruit straw berry blue berry apple pine NOTE: Words with different capitlization, like raspberry and raspBERRY count as 1 unique word, so raspberry should only be seen once in the output. #include <stdio.h> #include <stdlib.h> int main() {     //Pointer to access the...
Could you modify the code make output arranged into 5x5, like this: 1 2 3 4...
Could you modify the code make output arranged into 5x5, like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 The code is: import java.util.*; public class RandomGen { public static void main(String[] args) { int[][] ArrayRand = new int [5][5]; Random rand = new Random(); int sum = 0; System.out.println("The generated random matris is: ");    for(int i = 0; i <...
gray code to excess 3 code converter circuit and its logic diagram and ic based diagram
gray code to excess 3 code converter circuit and its logic diagram and ic based diagram
3. Chemical reactions and equilibrium constants (a) An alternative way of writing the reaction for the...
3. Chemical reactions and equilibrium constants (a) An alternative way of writing the reaction for the dissolution of CdCO3(s) is to form the species HCO3- rather than CO32-. Write a balanced chemical reaction for CdCO3(s) dissolution with HCO3- rather than CO32- as a product species. Calculate an equilibrium constant and standard free energy of reaction for your new reaction. You will need to look up some equilibrium constants. (b) How would you decide which reaction to use to describe the...
Below is my code in C#, When I run it, the output shows System.32[], Can you...
Below is my code in C#, When I run it, the output shows System.32[], Can you please check and let me know what is the problem in the code. class Program { static void Main(string[] args) { int number=12; Console.WriteLine(FizzArray(number)); } public static int[] FizzArray(int number) { int[] array = new int[number]; for (int i = 1; i < number; i++) array[i] = i; return array; }
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs....
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs. Create a GUI Calculator with the following: Title : Calculator Label and Entry box for 1st Number Label and Entry box for 2nd Number Buttons for *, /, +, - Label for result and Displaying the result
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types. 2. You must use a switch statement to check the user's menu option choice. 3. You may create additional functions in addition to the required functions listed above if you would like. 4. If user provides option which is not a choice...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT